]> git.proxmox.com Git - ceph.git/blame - ceph/src/ceph-volume/ceph_volume/drive_group/main.py
Import ceph 15.2.8
[ceph.git] / ceph / src / ceph-volume / ceph_volume / drive_group / main.py
CommitLineData
f6b5b4d7
TL
1# -*- coding: utf-8 -*-
2
3import argparse
4import json
5import logging
6import sys
7
8from ceph.deployment.drive_group import DriveGroupSpec
9from ceph.deployment.drive_selection.selector import DriveSelection
10from ceph.deployment.translate import to_ceph_volume
11from ceph.deployment.inventory import Device
12from ceph_volume.inventory import Inventory
13from ceph_volume.devices.lvm.batch import Batch
14
15logger = logging.getLogger(__name__)
16
17class 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()
93 i = Inventory([])
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)