]>
Commit | Line | Data |
---|---|---|
f6b5b4d7 TL |
1 | # -*- coding: utf-8 -*- |
2 | ||
3 | import argparse | |
4 | import json | |
5 | import logging | |
6 | import sys | |
7 | ||
8 | from ceph.deployment.drive_group import DriveGroupSpec | |
9 | from ceph.deployment.drive_selection.selector import DriveSelection | |
10 | from ceph.deployment.translate import to_ceph_volume | |
11 | from ceph.deployment.inventory import Device | |
12 | from ceph_volume.inventory import Inventory | |
13 | from ceph_volume.devices.lvm.batch import Batch | |
14 | ||
15 | logger = logging.getLogger(__name__) | |
16 | ||
17 | class Deploy(object): | |
18 | ||
19 | help = ''' | |
20 | Deploy OSDs according to a drive groups specification. | |
21 | ||
22 | The DriveGroup specification must be passed in json. | |
23 | It can either be (preference in this order) | |
24 | - in a file, path passed as a positional argument | |
25 | - read from stdin, pass "-" as a positional argument | |
26 | - a json string passed via the --spec argument | |
27 | ||
28 | Either the path postional argument or --spec must be specifed. | |
29 | ''' | |
30 | ||
31 | def __init__(self, argv): | |
f6b5b4d7 TL |
32 | self.argv = argv |
33 | ||
34 | def main(self): | |
35 | parser = argparse.ArgumentParser( | |
36 | prog='ceph-volume drive-group', | |
37 | formatter_class=argparse.RawDescriptionHelpFormatter, | |
38 | description=self.help, | |
39 | ) | |
40 | parser.add_argument( | |
41 | 'path', | |
42 | nargs='?', | |
43 | default=None, | |
44 | help=('Path to file containing drive group spec or "-" to read from stdin'), | |
45 | ) | |
46 | parser.add_argument( | |
47 | '--spec', | |
48 | default='', | |
49 | nargs='?', | |
50 | help=('drive-group json string') | |
51 | ) | |
52 | parser.add_argument( | |
53 | '--dry-run', | |
54 | default=False, | |
55 | action='store_true', | |
56 | help=('dry run, only print the batch command that would be run'), | |
57 | ) | |
58 | self.args = parser.parse_args(self.argv) | |
59 | if self.args.path: | |
60 | if self.args.path == "-": | |
61 | commands = self.from_json(sys.stdin) | |
62 | else: | |
63 | with open(self.args.path, 'r') as f: | |
64 | commands = self.from_json(f) | |
65 | elif self.args.spec: | |
66 | dg = json.loads(self.args.spec) | |
67 | commands = self.get_dg_spec(dg) | |
68 | else: | |
69 | # either --spec or path arg must be specified | |
70 | parser.print_help(sys.stderr) | |
71 | sys.exit(0) | |
72 | cmd = commands.run() | |
73 | if not cmd: | |
74 | logger.error('DriveGroup didn\'t produce any commands') | |
75 | return | |
76 | if self.args.dry_run: | |
77 | logger.info('Returning ceph-volume command (--dry-run was passed): {}'.format(cmd)) | |
78 | print(cmd) | |
79 | else: | |
80 | logger.info('Running ceph-volume command: {}'.format(cmd)) | |
81 | batch_args = cmd.split(' ')[2:] | |
82 | b = Batch(batch_args) | |
83 | b.main() | |
84 | ||
85 | def from_json(self, file_): | |
86 | dg = {} | |
87 | dg = json.load(file_) | |
88 | return self.get_dg_spec(dg) | |
89 | ||
90 | def get_dg_spec(self, dg): | |
91 | dg_spec = DriveGroupSpec._from_json_impl(dg) | |
92 | dg_spec.validate() | |
cd265ab1 | 93 | i = Inventory(['--filter-for-batch']) |
f6b5b4d7 TL |
94 | i.main() |
95 | inventory = i.get_report() | |
96 | devices = [Device.from_json(i) for i in inventory] | |
97 | selection = DriveSelection(dg_spec, devices) | |
98 | return to_ceph_volume(selection) |