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/>.
22
from StringIO import StringIO
24
# Used since this can maintain comments
25
# and doesn't need a top level section
26
from configobj import ConfigObj
28
from cloudinit import util
30
PUBCERT_FILE = "/etc/mcollective/ssl/server-public.pem"
31
PRICERT_FILE = "/etc/mcollective/ssl/server-private.pem"
34
def handle(name, cfg, cloud, log, _args):
36
# If there isn't a mcollective key in the configuration don't do anything
37
if 'mcollective' not in cfg:
38
log.debug(("Skipping module named %s, "
39
"no 'mcollective' key in configuration"), name)
42
mcollective_cfg = cfg['mcollective']
44
# Start by installing the mcollective package ...
45
cloud.distro.install_packages(("mcollective",))
47
# ... and then update the mcollective configuration
48
if 'conf' in mcollective_cfg:
49
# Read server.cfg values from the
50
# original file in order to be able to mix the rest up
51
server_cfg_fn = cloud.paths.join(True, '/etc/mcollective/server.cfg')
52
mcollective_config = ConfigObj(server_cfg_fn)
53
# See: http://tiny.cc/jh9agw
54
for (cfg_name, cfg) in mcollective_cfg['conf'].iteritems():
55
if cfg_name == 'public-cert':
56
pubcert_fn = cloud.paths.join(True, PUBCERT_FILE)
57
util.write_file(pubcert_fn, cfg, mode=0644)
58
mcollective_config['plugin.ssl_server_public'] = pubcert_fn
59
mcollective_config['securityprovider'] = 'ssl'
60
elif cfg_name == 'private-cert':
61
pricert_fn = cloud.paths.join(True, PRICERT_FILE)
62
util.write_file(pricert_fn, cfg, mode=0600)
63
mcollective_config['plugin.ssl_server_private'] = pricert_fn
64
mcollective_config['securityprovider'] = 'ssl'
66
if isinstance(cfg, (basestring, str)):
67
# Just set it in the 'main' section
68
mcollective_config[cfg_name] = cfg
69
elif isinstance(cfg, (dict)):
70
# Iterate throug the config items, create a section
71
# if it is needed and then add/or create items as needed
72
if cfg_name not in mcollective_config.sections:
73
mcollective_config[cfg_name] = {}
74
for (o, v) in cfg.iteritems():
75
mcollective_config[cfg_name][o] = v
77
# Otherwise just try to convert it to a string
78
mcollective_config[cfg_name] = str(cfg)
79
# We got all our config as wanted we'll rename
80
# the previous server.cfg and create our new one
81
old_fn = cloud.paths.join(False, '/etc/mcollective/server.cfg.old')
82
util.rename(server_cfg_fn, old_fn)
83
# Now we got the whole file, write to disk...
85
mcollective_config.write(contents)
86
contents = contents.getvalue()
87
server_cfg_rw = cloud.paths.join(False, '/etc/mcollective/server.cfg')
88
util.write_file(server_cfg_rw, contents, mode=0644)
91
util.subp(['service', 'mcollective', 'start'], capture=False)