diff options
author | Scott Moser <smoser@ubuntu.com> | 2016-07-19 14:46:14 -0400 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2016-07-19 14:46:14 -0400 |
commit | 4264783ae982e48d184e5991a67deab6f63a9054 (patch) | |
tree | 91a1a56a27c47a72902749efc5ed3875604785c7 /cloudinit | |
parent | 676445299dae888b357a821eae2ae6a9db49b90d (diff) | |
download | vyos-cloud-init-4264783ae982e48d184e5991a67deab6f63a9054.tar.gz vyos-cloud-init-4264783ae982e48d184e5991a67deab6f63a9054.zip |
mcollective: add tests, cleanups and bug fix when no config in /etc.
Things here:
- restart rather than 'start' the service, to pick up a config change
that we would have written.
- update the config and write cert files whether or not the file
existed on the system. Previously it would only write the cert
files if /etc/mcollective/server.cfg already existed.
- improve test coverage
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/config/cc_mcollective.py | 72 |
1 files changed, 38 insertions, 34 deletions
diff --git a/cloudinit/config/cc_mcollective.py b/cloudinit/config/cc_mcollective.py index 0c84d600..ada535f8 100644 --- a/cloudinit/config/cc_mcollective.py +++ b/cloudinit/config/cc_mcollective.py @@ -36,49 +36,53 @@ SERVER_CFG = '/etc/mcollective/server.cfg' LOG = logging.getLogger(__name__) -def configure(config): +def configure(config, server_cfg=SERVER_CFG, + pubcert_file=PUBCERT_FILE, pricert_file=PRICERT_FILE): # Read server.cfg values from the # original file in order to be able to mix the rest up try: - mcollective_config = ConfigObj(SERVER_CFG, file_error=True) + mcollective_config = ConfigObj(server_cfg, file_error=True) + existed = True except IOError: - LOG.warn("Did not find file %s", SERVER_CFG) - mcollective_config = ConfigObj(config) - else: - for (cfg_name, cfg) in config.items(): - if cfg_name == 'public-cert': - util.write_file(PUBCERT_FILE, cfg, mode=0o644) - mcollective_config[ - 'plugin.ssl_server_public'] = PUBCERT_FILE - mcollective_config['securityprovider'] = 'ssl' - elif cfg_name == 'private-cert': - util.write_file(PRICERT_FILE, cfg, mode=0o600) - mcollective_config[ - 'plugin.ssl_server_private'] = PRICERT_FILE - mcollective_config['securityprovider'] = 'ssl' + LOG.debug("Did not find file %s", server_cfg) + mcollective_config = ConfigObj() + existed = False + + for (cfg_name, cfg) in config.items(): + if cfg_name == 'public-cert': + util.write_file(pubcert_file, cfg, mode=0o644) + mcollective_config[ + 'plugin.ssl_server_public'] = pubcert_file + mcollective_config['securityprovider'] = 'ssl' + elif cfg_name == 'private-cert': + util.write_file(pricert_file, cfg, mode=0o600) + mcollective_config[ + 'plugin.ssl_server_private'] = pricert_file + mcollective_config['securityprovider'] = 'ssl' + else: + if isinstance(cfg, six.string_types): + # Just set it in the 'main' section + mcollective_config[cfg_name] = cfg + elif isinstance(cfg, (dict)): + # Iterate through the config items, create a section if + # it is needed and then add/or create items as needed + if cfg_name not in mcollective_config.sections: + mcollective_config[cfg_name] = {} + for (o, v) in cfg.items(): + mcollective_config[cfg_name][o] = v else: - if isinstance(cfg, six.string_types): - # Just set it in the 'main' section - mcollective_config[cfg_name] = cfg - elif isinstance(cfg, (dict)): - # Iterate through the config items, create a section if - # it is needed and then add/or create items as needed - if cfg_name not in mcollective_config.sections: - mcollective_config[cfg_name] = {} - for (o, v) in cfg.items(): - mcollective_config[cfg_name][o] = v - else: - # Otherwise just try to convert it to a string - mcollective_config[cfg_name] = str(cfg) + # Otherwise just try to convert it to a string + mcollective_config[cfg_name] = str(cfg) + + if existed: # We got all our config as wanted we'll rename # the previous server.cfg and create our new one - util.rename(SERVER_CFG, "%s.old" % (SERVER_CFG)) + util.rename(server_cfg, "%s.old" % (server_cfg)) # Now we got the whole file, write to disk... contents = BytesIO() mcollective_config.write(contents) - contents = contents.getvalue() - util.write_file(SERVER_CFG, contents, mode=0o644) + util.write_file(server_cfg, contents.getvalue(), mode=0o644) def handle(name, cfg, cloud, log, _args): @@ -98,5 +102,5 @@ def handle(name, cfg, cloud, log, _args): if 'conf' in mcollective_cfg: configure(config=mcollective_cfg['conf']) - # Start mcollective - util.subp(['service', 'mcollective', 'start'], capture=False) + # restart mcollective to handle updated config + util.subp(['service', 'mcollective', 'restart'], capture=False) |