summaryrefslogtreecommitdiffstats
path: root/src/ceph-volume/ceph_volume/inventory/main.py
blob: da0ff6c882ee5c639444fbc72636939134784cbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-

import argparse
import json

from ceph_volume.util.device import Devices, Device


class Inventory(object):

    help = "Get this nodes available disk inventory"

    def __init__(self, argv):
        self.argv = argv

    def main(self):
        parser = argparse.ArgumentParser(
            prog='ceph-volume inventory',
            formatter_class=argparse.RawDescriptionHelpFormatter,
            description=self.help,
        )
        parser.add_argument(
            'path',
            nargs='?',
            default=None,
            help=('Report on specific disk'),
        )
        parser.add_argument(
            '--format',
            choices=['plain', 'json', 'json-pretty'],
            default='plain',
            help='Output format',
        )
        parser.add_argument(
            '--filter-for-batch',
            action='store_true',
            help=('Filter devices unsuitable to pass to an OSD service spec, '
                  'no effect when <path> is passed'),
            default=False,
        )
        parser.add_argument(
            '--with-lsm',
            action='store_true',
            help=('Attempt to retrieve additional health and metadata through '
                  'libstoragemgmt'),
            default=False,
        )
        parser.add_argument(
            '--list-all',
            action='store_true',
            help=('Whether ceph-volume should list lvm devices'),
            default=False
        )
        self.args = parser.parse_args(self.argv)
        if self.args.path:
            self.format_report(Device(self.args.path, with_lsm=self.args.with_lsm))
        else:
            self.format_report(Devices(filter_for_batch=self.args.filter_for_batch,
                                       with_lsm=self.args.with_lsm,
                                       list_all=self.args.list_all))

    def get_report(self):
        if self.args.path:
            return Device(self.args.path, with_lsm=self.args.with_lsm).json_report()
        else:
            return Devices(filter_for_batch=self.args.filter_for_batch,
                           with_lsm=self.args.with_lsm,
                           list_all=self.args.list_all).json_report()

    def format_report(self, inventory):
        if self.args.format == 'json':
            print(json.dumps(inventory.json_report()))
        elif self.args.format == 'json-pretty':
            print(json.dumps(inventory.json_report(), indent=4, sort_keys=True))
        else:
            print(inventory.pretty_report())