Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / ceph-volume / ceph_volume / devices / simple / trigger.py
1 from __future__ import print_function
2 import argparse
3 from textwrap import dedent
4 from ceph_volume.exceptions import SuffixParsingError
5 from ceph_volume import decorators
6 from .activate import Activate
7
8
9 def parse_osd_id(string):
10     osd_id = string.split('-', 1)[0]
11     if not osd_id:
12         raise SuffixParsingError('OSD id', string)
13     if osd_id.isdigit():
14         return osd_id
15     raise SuffixParsingError('OSD id', string)
16
17
18 def parse_osd_uuid(string):
19     osd_id = '%s-' % parse_osd_id(string)
20     # remove the id first
21     osd_uuid = string.split(osd_id, 1)[-1]
22     if not osd_uuid:
23         raise SuffixParsingError('OSD uuid', string)
24     return osd_uuid
25
26
27 class Trigger(object):
28
29     help = 'systemd helper to activate an OSD'
30
31     def __init__(self, argv):
32         self.argv = argv
33
34     @decorators.needs_root
35     def main(self):
36         sub_command_help = dedent("""
37         ** DO NOT USE DIRECTLY **
38         This tool is meant to help the systemd unit that knows about OSDs.
39
40         Proxy OSD activation to ``ceph-volume simple activate`` by parsing the
41         input from systemd, detecting the UUID and ID associated with an OSD::
42
43             ceph-volume simple trigger {SYSTEMD-DATA}
44
45         The systemd "data" is expected to be in the format of::
46
47             {OSD ID}-{OSD UUID}
48
49         The devices associated with the OSD need to have been scanned previously,
50         so that all needed metadata can be used for starting the OSD process.
51         """)
52         parser = argparse.ArgumentParser(
53             prog='ceph-volume simple trigger',
54             formatter_class=argparse.RawDescriptionHelpFormatter,
55             description=sub_command_help,
56         )
57
58         parser.add_argument(
59             'systemd_data',
60             metavar='SYSTEMD_DATA',
61             nargs='?',
62             help='Data from a systemd unit containing ID and UUID of the OSD, like 0-asdf-lkjh'
63         )
64         if len(self.argv) == 0:
65             print(sub_command_help)
66             return
67         args = parser.parse_args(self.argv)
68         osd_id = parse_osd_id(args.systemd_data)
69         osd_uuid = parse_osd_uuid(args.systemd_data)
70         Activate([osd_id, osd_uuid], systemd=True).main()