20
20
# along with this program. If not, see <http://www.gnu.org/licenses/>.
23
from six import StringIO
23
from six import BytesIO
25
25
# Used since this can maintain comments
26
26
# and doesn't need a top level section
27
27
from configobj import ConfigObj
29
from cloudinit import log as logging
29
30
from cloudinit import util
31
32
PUBCERT_FILE = "/etc/mcollective/ssl/server-public.pem"
32
33
PRICERT_FILE = "/etc/mcollective/ssl/server-private.pem"
33
34
SERVER_CFG = '/etc/mcollective/server.cfg'
36
def handle(name, cfg, cloud, log, _args):
38
# If there isn't a mcollective key in the configuration don't do anything
39
if 'mcollective' not in cfg:
40
log.debug(("Skipping module named %s, "
41
"no 'mcollective' key in configuration"), name)
44
mcollective_cfg = cfg['mcollective']
46
# Start by installing the mcollective package ...
47
cloud.distro.install_packages(("mcollective",))
49
# ... and then update the mcollective configuration
50
if 'conf' in mcollective_cfg:
51
# Read server.cfg values from the
52
# original file in order to be able to mix the rest up
53
mcollective_config = ConfigObj(SERVER_CFG)
54
# See: http://tiny.cc/jh9agw
55
for (cfg_name, cfg) in mcollective_cfg['conf'].items():
36
LOG = logging.getLogger(__name__)
39
def configure(config):
40
# Read server.cfg values from the
41
# original file in order to be able to mix the rest up
43
mcollective_config = ConfigObj(SERVER_CFG, file_error=True)
45
LOG.warn("Did not find file %s", SERVER_CFG)
46
mcollective_config = ConfigObj(config)
48
for (cfg_name, cfg) in config.items():
56
49
if cfg_name == 'public-cert':
57
50
util.write_file(PUBCERT_FILE, cfg, mode=0o644)
58
mcollective_config['plugin.ssl_server_public'] = PUBCERT_FILE
52
'plugin.ssl_server_public'] = PUBCERT_FILE
59
53
mcollective_config['securityprovider'] = 'ssl'
60
54
elif cfg_name == 'private-cert':
61
55
util.write_file(PRICERT_FILE, cfg, mode=0o600)
62
mcollective_config['plugin.ssl_server_private'] = PRICERT_FILE
57
'plugin.ssl_server_private'] = PRICERT_FILE
63
58
mcollective_config['securityprovider'] = 'ssl'
65
60
if isinstance(cfg, six.string_types):
66
61
# Just set it in the 'main' section
67
62
mcollective_config[cfg_name] = cfg
68
63
elif isinstance(cfg, (dict)):
69
# Iterate through the config items, create a section
70
# if it is needed and then add/or create items as needed
64
# Iterate through the config items, create a section if
65
# it is needed and then add/or create items as needed
71
66
if cfg_name not in mcollective_config.sections:
72
67
mcollective_config[cfg_name] = {}
73
68
for (o, v) in cfg.items():
78
73
# We got all our config as wanted we'll rename
79
74
# the previous server.cfg and create our new one
80
75
util.rename(SERVER_CFG, "%s.old" % (SERVER_CFG))
81
# Now we got the whole file, write to disk...
83
mcollective_config.write(contents)
84
contents = contents.getvalue()
85
util.write_file(SERVER_CFG, contents, mode=0o644)
77
# Now we got the whole file, write to disk...
79
mcollective_config.write(contents)
80
contents = contents.getvalue()
81
util.write_file(SERVER_CFG, contents, mode=0o644)
84
def handle(name, cfg, cloud, log, _args):
86
# If there isn't a mcollective key in the configuration don't do anything
87
if 'mcollective' not in cfg:
88
log.debug(("Skipping module named %s, "
89
"no 'mcollective' key in configuration"), name)
92
mcollective_cfg = cfg['mcollective']
94
# Start by installing the mcollective package ...
95
cloud.distro.install_packages(("mcollective",))
97
# ... and then update the mcollective configuration
98
if 'conf' in mcollective_cfg:
99
configure(config=mcollective_cfg['conf'])
87
101
# Start mcollective
88
102
util.subp(['service', 'mcollective', 'start'], capture=False)