]> git.proxmox.com Git - ceph.git/blob - ceph/src/ceph-volume/ceph_volume/main.py
a66b7bb7df96164f9803d43ccf29de840eed4dec
[ceph.git] / ceph / src / ceph-volume / ceph_volume / main.py
1 from __future__ import print_function
2 import argparse
3 import os
4 import pkg_resources
5 import sys
6 import logging
7
8 from ceph_volume.decorators import catches
9 from ceph_volume import log, devices, configuration, conf, exceptions, terminal, inventory, drive_group
10
11
12 class Volume(object):
13 _help = """
14 ceph-volume: Deploy Ceph OSDs using different device technologies like lvm or
15 physical disks.
16
17 Log Path: {log_path}
18 Ceph Conf: {ceph_path}
19
20 {sub_help}
21 {plugins}
22 {environ_vars}
23 {warning}
24 """
25
26 def __init__(self, argv=None, parse=True):
27 self.mapper = {
28 'lvm': devices.lvm.LVM,
29 'simple': devices.simple.Simple,
30 'raw': devices.raw.Raw,
31 'inventory': inventory.Inventory,
32 'drive-group': drive_group.Deploy,
33 }
34 self.plugin_help = "No plugins found/loaded"
35 if argv is None:
36 self.argv = sys.argv
37 else:
38 self.argv = argv
39 if parse:
40 self.main(self.argv)
41
42 def help(self, warning=False):
43 warning = 'See "ceph-volume --help" for full list of options.' if warning else ''
44 return self._help.format(
45 warning=warning,
46 log_path=conf.log_path,
47 ceph_path=self.stat_ceph_conf(),
48 plugins=self.plugin_help,
49 sub_help=terminal.subhelp(self.mapper),
50 environ_vars=self.get_environ_vars()
51 )
52
53 def get_environ_vars(self):
54 environ_vars = []
55 for key, value in os.environ.items():
56 if key.startswith('CEPH_'):
57 environ_vars.append("%s=%s" % (key, value))
58 if not environ_vars:
59 return ''
60 else:
61 environ_vars.insert(0, '\nEnviron Variables:')
62 return '\n'.join(environ_vars)
63
64 def enable_plugins(self):
65 """
66 Load all plugins available, add them to the mapper and extend the help
67 string with the information from each one
68 """
69 plugins = _load_library_extensions()
70 for plugin in plugins:
71 self.mapper[plugin._ceph_volume_name_] = plugin
72 self.plugin_help = '\n'.join(['%-19s %s\n' % (
73 plugin.name, getattr(plugin, 'help_menu', ''))
74 for plugin in plugins])
75 if self.plugin_help:
76 self.plugin_help = '\nPlugins:\n' + self.plugin_help
77
78 def load_log_path(self):
79 conf.log_path = os.getenv('CEPH_VOLUME_LOG_PATH', '/var/log/ceph')
80
81 def stat_ceph_conf(self):
82 try:
83 configuration.load(conf.path)
84 return terminal.green(conf.path)
85 except exceptions.ConfigurationError as error:
86 return terminal.red(error)
87
88 def _get_split_args(self):
89 subcommands = self.mapper.keys()
90 slice_on_index = len(self.argv) + 1
91 pruned_args = self.argv[1:]
92 for count, arg in enumerate(pruned_args):
93 if arg in subcommands:
94 slice_on_index = count
95 break
96 return pruned_args[:slice_on_index], pruned_args[slice_on_index:]
97
98 @catches()
99 def main(self, argv):
100 # these need to be available for the help, which gets parsed super
101 # early
102 configuration.load_ceph_conf_path()
103 self.load_log_path()
104 self.enable_plugins()
105 main_args, subcommand_args = self._get_split_args()
106 # no flags where passed in, return the help menu instead of waiting for
107 # argparse which will end up complaning that there are no args
108 if len(argv) <= 1:
109 print(self.help(warning=True))
110 raise SystemExit(0)
111 parser = argparse.ArgumentParser(
112 prog='ceph-volume',
113 formatter_class=argparse.RawDescriptionHelpFormatter,
114 description=self.help(),
115 )
116 parser.add_argument(
117 '--cluster',
118 default='ceph',
119 help='Cluster name (defaults to "ceph")',
120 )
121 parser.add_argument(
122 '--log-level',
123 default='debug',
124 choices=['debug', 'info', 'warning', 'error', 'critical'],
125 help='Change the file log level (defaults to debug)',
126 )
127 parser.add_argument(
128 '--log-path',
129 default='/var/log/ceph/',
130 help='Change the log path (defaults to /var/log/ceph)',
131 )
132 args = parser.parse_args(main_args)
133 conf.log_path = args.log_path
134 if os.path.isdir(conf.log_path):
135 conf.log_path = os.path.join(args.log_path, 'ceph-volume.log')
136 log.setup(log_level=args.log_level)
137 log.setup_console()
138 logger = logging.getLogger(__name__)
139 logger.info("Running command: ceph-volume %s %s", " ".join(main_args), " ".join(subcommand_args))
140 # set all variables from args and load everything needed according to
141 # them
142 configuration.load_ceph_conf_path(cluster_name=args.cluster)
143 try:
144 conf.ceph = configuration.load(conf.path)
145 except exceptions.ConfigurationError as error:
146 # we warn only here, because it is possible that the configuration
147 # file is not needed, or that it will be loaded by some other means
148 # (like reading from lvm tags)
149 logger.exception('ignoring inability to load ceph.conf')
150 terminal.red(error)
151 # dispatch to sub-commands
152 terminal.dispatch(self.mapper, subcommand_args)
153
154
155 def _load_library_extensions():
156 """
157 Locate all setuptools entry points by the name 'ceph_volume_handlers'
158 and initialize them.
159 Any third-party library may register an entry point by adding the
160 following to their setup.py::
161
162 entry_points = {
163 'ceph_volume_handlers': [
164 'plugin_name = mylib.mymodule:Handler_Class',
165 ],
166 },
167
168 `plugin_name` will be used to load it as a sub command.
169 """
170 logger = logging.getLogger('ceph_volume.plugins')
171 group = 'ceph_volume_handlers'
172 entry_points = pkg_resources.iter_entry_points(group=group)
173 plugins = []
174 for ep in entry_points:
175 try:
176 logger.debug('loading %s' % ep.name)
177 plugin = ep.load()
178 plugin._ceph_volume_name_ = ep.name
179 plugins.append(plugin)
180 except Exception as error:
181 logger.exception("Error initializing plugin %s: %s" % (ep, error))
182 return plugins