3
# Copyright (C) 2009-2011 Canonical Ltd.
4
# Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
6
# Author: Marc Cluet <marc.cluet@canonical.com>
7
# Based on code by Scott Moser <scott.moser@canonical.com>
8
# Author: Juerg Haefliger <juerg.haefliger@hp.com>
10
# This program is free software: you can redistribute it and/or modify
11
# it under the terms of the GNU General Public License version 3, as
12
# published by the Free Software Foundation.
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU General Public License for more details.
19
# You should have received a copy of the GNU General Public License
20
# along with this program. If not, see <http://www.gnu.org/licenses/>.
23
from six import BytesIO
25
# Used since this can maintain comments
26
# and doesn't need a top level section
27
from configobj import ConfigObj
29
from cloudinit import log as logging
30
from cloudinit import util
32
PUBCERT_FILE = "/etc/mcollective/ssl/server-public.pem"
33
PRICERT_FILE = "/etc/mcollective/ssl/server-private.pem"
34
SERVER_CFG = '/etc/mcollective/server.cfg'
36
LOG = logging.getLogger(__name__)
39
def configure(config, server_cfg=SERVER_CFG,
40
pubcert_file=PUBCERT_FILE, pricert_file=PRICERT_FILE):
41
# Read server.cfg values from the
42
# original file in order to be able to mix the rest up
44
mcollective_config = ConfigObj(server_cfg, file_error=True)
47
LOG.debug("Did not find file %s", server_cfg)
48
mcollective_config = ConfigObj()
51
for (cfg_name, cfg) in config.items():
52
if cfg_name == 'public-cert':
53
util.write_file(pubcert_file, cfg, mode=0o644)
55
'plugin.ssl_server_public'] = pubcert_file
56
mcollective_config['securityprovider'] = 'ssl'
57
elif cfg_name == 'private-cert':
58
util.write_file(pricert_file, cfg, mode=0o600)
60
'plugin.ssl_server_private'] = pricert_file
61
mcollective_config['securityprovider'] = 'ssl'
63
if isinstance(cfg, six.string_types):
64
# Just set it in the 'main' section
65
mcollective_config[cfg_name] = cfg
66
elif isinstance(cfg, (dict)):
67
# Iterate through the config items, create a section if
68
# it is needed and then add/or create items as needed
69
if cfg_name not in mcollective_config.sections:
70
mcollective_config[cfg_name] = {}
71
for (o, v) in cfg.items():
72
mcollective_config[cfg_name][o] = v
74
# Otherwise just try to convert it to a string
75
mcollective_config[cfg_name] = str(cfg)
78
# We got all our config as wanted we'll rename
79
# the previous server.cfg and create our new one
80
util.rename(server_cfg, "%s.old" % (server_cfg))
82
# Now we got the whole file, write to disk...
84
mcollective_config.write(contents)
85
util.write_file(server_cfg, contents.getvalue(), mode=0o644)
88
def handle(name, cfg, cloud, log, _args):
90
# If there isn't a mcollective key in the configuration don't do anything
91
if 'mcollective' not in cfg:
92
log.debug(("Skipping module named %s, "
93
"no 'mcollective' key in configuration"), name)
96
mcollective_cfg = cfg['mcollective']
98
# Start by installing the mcollective package ...
99
cloud.distro.install_packages(("mcollective",))
101
# ... and then update the mcollective configuration
102
if 'conf' in mcollective_cfg:
103
configure(config=mcollective_cfg['conf'])
105
# restart mcollective to handle updated config
106
util.subp(['service', 'mcollective', 'restart'], capture=False)