| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
""" |
| 22 |
CLI configuration classes |
| 23 |
""" |
| 24 |
|
| 25 |
try: |
| 26 |
from configparser import ConfigParser, NoOptionError, NoSectionError |
| 27 |
except ImportError: |
| 28 |
|
| 29 |
from ConfigParser import ConfigParser, NoOptionError, NoSectionError |
| 30 |
|
| 31 |
import glob |
| 32 |
import os |
| 33 |
import shlex |
| 34 |
from string import Template |
| 35 |
|
| 36 |
from ClusterShell.Defaults import config_paths, DEFAULTS |
| 37 |
from ClusterShell.CLI.Display import VERB_QUIET, VERB_STD, \ |
| 38 |
VERB_VERB, VERB_DEBUG, THREE_CHOICES |
| 39 |
|
| 40 |
|
| 41 |
class ClushConfigError(Exception): |
| 42 |
"""Exception used by ClushConfig to report an error.""" |
| 43 |
def __init__(self, section=None, option=None, msg=None): |
| 44 |
Exception.__init__(self) |
| 45 |
self.section = section |
| 46 |
self.option = option |
| 47 |
self.msg = msg |
| 48 |
|
| 49 |
def __str__(self): |
| 50 |
serr = "" |
| 51 |
if self.section and self.option: |
| 52 |
serr += "(Config %s.%s): " % (self.section, self.option) |
| 53 |
if self.msg: |
| 54 |
serr += str(self.msg) |
| 55 |
return serr |
| 56 |
|
| 57 |
class ClushConfig(ConfigParser, object): |
| 58 |
"""Config class for clush (specialized ConfigParser)""" |
| 59 |
|
| 60 |
MAIN_SECTION = 'Main' |
| 61 |
MAIN_DEFAULTS = {"fanout": "%d" % DEFAULTS.fanout, |
| 62 |
"connect_timeout": "%f" % DEFAULTS.connect_timeout, |
| 63 |
"command_timeout": "%f" % DEFAULTS.command_timeout, |
| 64 |
"history_size": "100", |
| 65 |
"color": THREE_CHOICES[0], |
| 66 |
"verbosity": "%d" % VERB_STD, |
| 67 |
"node_count": "yes", |
| 68 |
"maxrc": "no", |
| 69 |
"fd_max": "8192", |
| 70 |
"command_prefix": "", |
| 71 |
"password_prompt": "no"} |
| 72 |
|
| 73 |
def __init__(self, options, filename=None): |
| 74 |
"""Initialize ClushConfig object from corresponding |
| 75 |
OptionParser options.""" |
| 76 |
ConfigParser.__init__(self) |
| 77 |
self.mode = None |
| 78 |
|
| 79 |
self.add_section(self.MAIN_SECTION) |
| 80 |
for key, value in self.MAIN_DEFAULTS.items(): |
| 81 |
self.set(self.MAIN_SECTION, key, value) |
| 82 |
|
| 83 |
if filename: |
| 84 |
files = [filename] |
| 85 |
else: |
| 86 |
files = config_paths('clush.conf') |
| 87 |
|
| 88 |
self.parsed = self.read(files) |
| 89 |
|
| 90 |
if self.parsed: |
| 91 |
|
| 92 |
cfg_dirname = os.path.dirname(self.parsed[-1]) |
| 93 |
|
| 94 |
|
| 95 |
try: |
| 96 |
|
| 97 |
loaded_confdirs = set() |
| 98 |
|
| 99 |
confdirstr = self.get(self.MAIN_SECTION, "confdir") |
| 100 |
for confdir in shlex.split(confdirstr): |
| 101 |
|
| 102 |
|
| 103 |
confdir = Template(confdir).safe_substitute( |
| 104 |
CFGDIR=cfg_dirname) |
| 105 |
confdir = os.path.normpath(confdir) |
| 106 |
if confdir in loaded_confdirs: |
| 107 |
continue |
| 108 |
loaded_confdirs.add(confdir) |
| 109 |
if not os.path.isdir(confdir): |
| 110 |
if not os.path.exists(confdir): |
| 111 |
continue |
| 112 |
msg = "Defined confdir %s is not a directory" % confdir |
| 113 |
raise ClushConfigError(msg=msg) |
| 114 |
|
| 115 |
for cfgfn in sorted(glob.glob('%s/*.conf' % confdir)): |
| 116 |
|
| 117 |
self.parsed += self.read(cfgfn) |
| 118 |
except (NoSectionError, NoOptionError): |
| 119 |
pass |
| 120 |
|
| 121 |
|
| 122 |
if options.quiet: |
| 123 |
self._set_main("verbosity", VERB_QUIET) |
| 124 |
if options.verbose: |
| 125 |
self._set_main("verbosity", VERB_VERB) |
| 126 |
if options.debug: |
| 127 |
self._set_main("verbosity", VERB_DEBUG) |
| 128 |
if options.fanout: |
| 129 |
self._set_main("fanout", options.fanout) |
| 130 |
if options.user: |
| 131 |
self._set_main("ssh_user", options.user) |
| 132 |
if options.options: |
| 133 |
self._set_main("ssh_options", options.options) |
| 134 |
if options.connect_timeout: |
| 135 |
self._set_main("connect_timeout", options.connect_timeout) |
| 136 |
if options.command_timeout: |
| 137 |
self._set_main("command_timeout", options.command_timeout) |
| 138 |
if options.whencolor: |
| 139 |
self._set_main("color", options.whencolor) |
| 140 |
if options.maxrc: |
| 141 |
self._set_main("maxrc", options.maxrc) |
| 142 |
|
| 143 |
try: |
| 144 |
|
| 145 |
for cfgopt in options.option: |
| 146 |
optkey, optvalue = cfgopt.split('=', 1) |
| 147 |
self._set_main(optkey, optvalue) |
| 148 |
except ValueError as exc: |
| 149 |
raise ClushConfigError(self.MAIN_SECTION, cfgopt, |
| 150 |
"invalid -O/--option value") |
| 151 |
|
| 152 |
def _set_main(self, option, value): |
| 153 |
"""Set given option/value pair in the Main section.""" |
| 154 |
self.set(self.MAIN_SECTION, option, str(value)) |
| 155 |
|
| 156 |
def _getx(self, xtype, section, option): |
| 157 |
"""Return a value of specified type for the named option.""" |
| 158 |
try: |
| 159 |
return getattr(ConfigParser, 'get%s' % xtype)(self, \ |
| 160 |
section, option) |
| 161 |
except (NoOptionError, NoSectionError, TypeError, ValueError) as exc: |
| 162 |
raise ClushConfigError(section, option, exc) |
| 163 |
|
| 164 |
def getboolean(self, section, option): |
| 165 |
"""Return a boolean value for the named option.""" |
| 166 |
return self._getx('boolean', section, option) |
| 167 |
|
| 168 |
def getfloat(self, section, option): |
| 169 |
"""Return a float value for the named option.""" |
| 170 |
return self._getx('float', section, option) |
| 171 |
|
| 172 |
def getint(self, section, option): |
| 173 |
"""Return an integer value for the named option.""" |
| 174 |
return self._getx('int', section, option) |
| 175 |
|
| 176 |
def _get_optional(self, section, option): |
| 177 |
"""Utility method to get a value for the named option, but do |
| 178 |
not raise an exception if the option doesn't exist.""" |
| 179 |
try: |
| 180 |
return self.get(section, option) |
| 181 |
except (NoOptionError, NoSectionError): |
| 182 |
pass |
| 183 |
|
| 184 |
def _getboolean_mode_optional(self, option): |
| 185 |
"""Return a boolean value for the named option in the current |
| 186 |
mode (optionally defined).""" |
| 187 |
if self.mode: |
| 188 |
try: |
| 189 |
return getattr(ConfigParser, |
| 190 |
'getboolean')(self, "mode:%s" % self.mode, |
| 191 |
option) |
| 192 |
except (NoOptionError, NoSectionError): |
| 193 |
pass |
| 194 |
return self.getboolean(self.MAIN_SECTION, option) |
| 195 |
|
| 196 |
def _getint_mode_optional(self, option): |
| 197 |
"""Return an integer value for the named option in the current |
| 198 |
mode (optionally defined).""" |
| 199 |
if self.mode: |
| 200 |
try: |
| 201 |
return getattr(ConfigParser, 'getint')(self, |
| 202 |
"mode:%s" % self.mode, |
| 203 |
option) |
| 204 |
except (NoOptionError, NoSectionError): |
| 205 |
pass |
| 206 |
return self.getint(self.MAIN_SECTION, option) |
| 207 |
|
| 208 |
def _getfloat_mode_optional(self, option): |
| 209 |
"""Return a float value for the named option in the current |
| 210 |
mode (optionally defined).""" |
| 211 |
if self.mode: |
| 212 |
try: |
| 213 |
return getattr(ConfigParser, 'getfloat')(self, |
| 214 |
"mode:%s" % self.mode, |
| 215 |
option) |
| 216 |
except (NoOptionError, NoSectionError): |
| 217 |
pass |
| 218 |
return self.getfloat(self.MAIN_SECTION, option) |
| 219 |
|
| 220 |
def _get_mode_optional(self, option): |
| 221 |
"""Utility method to get a value for the named option in the |
| 222 |
current mode, but do not raise an exception if the option |
| 223 |
doesn't exist.""" |
| 224 |
if self.mode: |
| 225 |
try: |
| 226 |
return self.get("mode:%s" % self.mode, option) |
| 227 |
except (NoOptionError, NoSectionError): |
| 228 |
pass |
| 229 |
return self._get_optional(self.MAIN_SECTION, option) |
| 230 |
|
| 231 |
@property |
| 232 |
def verbosity(self): |
| 233 |
"""verbosity value as an integer""" |
| 234 |
try: |
| 235 |
return self.getint(self.MAIN_SECTION, "verbosity") |
| 236 |
except ClushConfigError: |
| 237 |
return 0 |
| 238 |
|
| 239 |
@property |
| 240 |
def fanout(self): |
| 241 |
"""fanout value as an integer""" |
| 242 |
return self._getint_mode_optional("fanout") |
| 243 |
|
| 244 |
@property |
| 245 |
def connect_timeout(self): |
| 246 |
"""connect_timeout value as a float""" |
| 247 |
return self._getfloat_mode_optional("connect_timeout") |
| 248 |
|
| 249 |
@property |
| 250 |
def command_timeout(self): |
| 251 |
"""command_timeout value as a float""" |
| 252 |
return self._getfloat_mode_optional("command_timeout") |
| 253 |
|
| 254 |
@property |
| 255 |
def ssh_user(self): |
| 256 |
"""ssh_user value as a string (optional)""" |
| 257 |
return self._get_mode_optional("ssh_user") |
| 258 |
|
| 259 |
@property |
| 260 |
def ssh_path(self): |
| 261 |
"""ssh_path value as a string (optional)""" |
| 262 |
return self._get_mode_optional("ssh_path") |
| 263 |
|
| 264 |
@property |
| 265 |
def ssh_options(self): |
| 266 |
"""ssh_options value as a string (optional)""" |
| 267 |
return self._get_mode_optional("ssh_options") |
| 268 |
|
| 269 |
@property |
| 270 |
def scp_path(self): |
| 271 |
"""scp_path value as a string (optional)""" |
| 272 |
return self._get_mode_optional("scp_path") |
| 273 |
|
| 274 |
@property |
| 275 |
def scp_options(self): |
| 276 |
"""scp_options value as a string (optional)""" |
| 277 |
return self._get_mode_optional("scp_options") |
| 278 |
|
| 279 |
@property |
| 280 |
def rsh_path(self): |
| 281 |
"""rsh_path value as a string (optional)""" |
| 282 |
return self._get_mode_optional("rsh_path") |
| 283 |
|
| 284 |
@property |
| 285 |
def rcp_path(self): |
| 286 |
"""rcp_path value as a string (optional)""" |
| 287 |
return self._get_mode_optional("rcp_path") |
| 288 |
|
| 289 |
@property |
| 290 |
def rsh_options(self): |
| 291 |
"""rsh_options value as a string (optional)""" |
| 292 |
return self._get_mode_optional("rsh_options") |
| 293 |
|
| 294 |
@property |
| 295 |
def color(self): |
| 296 |
"""color value as a string in (never, always, auto)""" |
| 297 |
whencolor = self._get_mode_optional("color") |
| 298 |
if whencolor not in THREE_CHOICES: |
| 299 |
raise ClushConfigError(self.mode or self.MAIN_SECTION, "color", |
| 300 |
"choose from %s" % THREE_CHOICES) |
| 301 |
return whencolor |
| 302 |
|
| 303 |
@property |
| 304 |
def node_count(self): |
| 305 |
"""node_count value as a boolean""" |
| 306 |
return self._getboolean_mode_optional("node_count") |
| 307 |
|
| 308 |
@property |
| 309 |
def maxrc(self): |
| 310 |
"""maxrc value as a boolean""" |
| 311 |
return self._getboolean_mode_optional("maxrc") |
| 312 |
|
| 313 |
@property |
| 314 |
def fd_max(self): |
| 315 |
"""max number of open files (soft rlimit)""" |
| 316 |
return self.getint(self.MAIN_SECTION, "fd_max") |
| 317 |
|
| 318 |
def modes(self): |
| 319 |
"""return available run modes""" |
| 320 |
for section in self.sections(): |
| 321 |
if section.startswith("mode:"): |
| 322 |
yield section[5:] |
| 323 |
|
| 324 |
def set_mode(self, mode): |
| 325 |
"""set run mode; properties will use it by default""" |
| 326 |
if mode not in self.modes(): |
| 327 |
raise ClushConfigError(msg='invalid mode "%s" (available: %s)' |
| 328 |
% (mode, ' '.join(self.modes()))) |
| 329 |
self.mode = mode |
| 330 |
|
| 331 |
@property |
| 332 |
def command_prefix(self): |
| 333 |
"""command_prefix value as a string (optional)""" |
| 334 |
return self._get_mode_optional("command_prefix") |
| 335 |
|
| 336 |
@property |
| 337 |
def password_prompt(self): |
| 338 |
"""password_prompt value as a boolean (optional)""" |
| 339 |
return self._getboolean_mode_optional("password_prompt") |