~cloud-init-dev/cloud-init/trunk

« back to all changes in this revision

Viewing changes to cloudinit/config/cc_mcollective.py

  • Committer: Scott Moser
  • Date: 2016-07-14 15:30:14 UTC
  • mfrom: (1247.3.3 fix_mcollective)
  • Revision ID: smoser@ubuntu.com-20160714153014-12rtoev6f0ew73oa
Fix mcollective module with python3

fixes mcollective when used with python3 and also adds a unit test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
21
 
22
22
import six
23
 
from six import StringIO
 
23
from six import BytesIO
24
24
 
25
25
# Used since this can maintain comments
26
26
# and doesn't need a top level section
27
27
from configobj import ConfigObj
28
28
 
 
29
from cloudinit import log as logging
29
30
from cloudinit import util
30
31
 
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'
34
35
 
35
 
 
36
 
def handle(name, cfg, cloud, log, _args):
37
 
 
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)
42
 
        return
43
 
 
44
 
    mcollective_cfg = cfg['mcollective']
45
 
 
46
 
    # Start by installing the mcollective package ...
47
 
    cloud.distro.install_packages(("mcollective",))
48
 
 
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__)
 
37
 
 
38
 
 
39
def configure(config):
 
40
    # Read server.cfg values from the
 
41
    # original file in order to be able to mix the rest up
 
42
    try:
 
43
        mcollective_config = ConfigObj(SERVER_CFG, file_error=True)
 
44
    except IOError:
 
45
        LOG.warn("Did not find file %s", SERVER_CFG)
 
46
        mcollective_config = ConfigObj(config)
 
47
    else:
 
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
 
51
                mcollective_config[
 
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
 
56
                mcollective_config[
 
57
                    'plugin.ssl_server_private'] = PRICERT_FILE
63
58
                mcollective_config['securityprovider'] = 'ssl'
64
59
            else:
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...
82
 
        contents = StringIO()
83
 
        mcollective_config.write(contents)
84
 
        contents = contents.getvalue()
85
 
        util.write_file(SERVER_CFG, contents, mode=0o644)
 
76
 
 
77
    # Now we got the whole file, write to disk...
 
78
    contents = BytesIO()
 
79
    mcollective_config.write(contents)
 
80
    contents = contents.getvalue()
 
81
    util.write_file(SERVER_CFG, contents, mode=0o644)
 
82
 
 
83
 
 
84
def handle(name, cfg, cloud, log, _args):
 
85
 
 
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)
 
90
        return
 
91
 
 
92
    mcollective_cfg = cfg['mcollective']
 
93
 
 
94
    # Start by installing the mcollective package ...
 
95
    cloud.distro.install_packages(("mcollective",))
 
96
 
 
97
    # ... and then update the mcollective configuration
 
98
    if 'conf' in mcollective_cfg:
 
99
        configure(config=mcollective_cfg['conf'])
86
100
 
87
101
    # Start mcollective
88
102
    util.subp(['service', 'mcollective', 'start'], capture=False)