]> git.proxmox.com Git - ceph.git/blob - ceph/src/ceph-volume/ceph_volume/drive_group/main.py
import 15.2.5
[ceph.git] / ceph / src / ceph-volume / ceph_volume / drive_group / main.py
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):
32 logger.error(f'argv: {argv}')
33 self.argv = argv
34
35 def main(self):
36 parser = argparse.ArgumentParser(
37 prog='ceph-volume drive-group',
38 formatter_class=argparse.RawDescriptionHelpFormatter,
39 description=self.help,
40 )
41 parser.add_argument(
42 'path',
43 nargs='?',
44 default=None,
45 help=('Path to file containing drive group spec or "-" to read from stdin'),
46 )
47 parser.add_argument(
48 '--spec',
49 default='',
50 nargs='?',
51 help=('drive-group json string')
52 )
53 parser.add_argument(
54 '--dry-run',
55 default=False,
56 action='store_true',
57 help=('dry run, only print the batch command that would be run'),
58 )
59 self.args = parser.parse_args(self.argv)
60 if self.args.path:
61 if self.args.path == "-":
62 commands = self.from_json(sys.stdin)
63 else:
64 with open(self.args.path, 'r') as f:
65 commands = self.from_json(f)
66 elif self.args.spec:
67 dg = json.loads(self.args.spec)
68 commands = self.get_dg_spec(dg)
69 else:
70 # either --spec or path arg must be specified
71 parser.print_help(sys.stderr)
72 sys.exit(0)
73 cmd = commands.run()
74 if not cmd:
75 logger.error('DriveGroup didn\'t produce any commands')
76 return
77 if self.args.dry_run:
78 logger.info('Returning ceph-volume command (--dry-run was passed): {}'.format(cmd))
79 print(cmd)
80 else:
81 logger.info('Running ceph-volume command: {}'.format(cmd))
82 batch_args = cmd.split(' ')[2:]
83 b = Batch(batch_args)
84 b.main()
85
86 def from_json(self, file_):
87 dg = {}
88 dg = json.load(file_)
89 return self.get_dg_spec(dg)
90
91 def get_dg_spec(self, dg):
92 dg_spec = DriveGroupSpec._from_json_impl(dg)
93 dg_spec.validate()
94 i = Inventory([])
95 i.main()
96 inventory = i.get_report()
97 devices = [Device.from_json(i) for i in inventory]
98 selection = DriveSelection(dg_spec, devices)
99 return to_ceph_volume(selection)