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