~milner/cloud-init/lint-cleanups

« back to all changes in this revision

Viewing changes to cloudinit/CloudConfig/cc_puppet.py

  • Committer: Mike Milner
  • Date: 2012-01-18 01:23:09 UTC
  • mfrom: (502.2.5 trunk)
  • Revision ID: mike.milner@canonical.com-20120118012309-aqzzfh04frni7q0y
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import cloudinit.CloudConfig as cc
26
26
import cloudinit.util as util
27
27
 
28
 
def handle(_name,cfg,cloud,log,_args):
 
28
 
 
29
def handle(_name, cfg, cloud, log, _args):
29
30
    # If there isn't a puppet key in the configuration don't do anything
30
 
    if not cfg.has_key('puppet'): return
 
31
    if 'puppet' not in cfg:
 
32
        return
31
33
    puppet_cfg = cfg['puppet']
32
34
    # Start by installing the puppet package ...
33
35
    cc.install_packages(("puppet",))
34
36
 
35
37
    # ... and then update the puppet configuration
36
 
    if puppet_cfg.has_key('conf'):
 
38
    if 'conf' in puppet_cfg:
37
39
        # Add all sections from the conf object to puppet.conf
38
40
        puppet_conf_fh = open('/etc/puppet/puppet.conf', 'r')
39
41
        # Create object for reading puppet.conf values
40
42
        puppet_config = ConfigParser.ConfigParser()
41
 
        # Read puppet.conf values from original file in order to be able to mix the rest up
42
 
        puppet_config.readfp(StringIO.StringIO(''.join(i.lstrip() for i in puppet_conf_fh.readlines())))
 
43
        # Read puppet.conf values from original file in order to be able to
 
44
        # mix the rest up
 
45
        puppet_config.readfp(StringIO.StringIO(''.join(i.lstrip() for i in
 
46
                                               puppet_conf_fh.readlines())))
43
47
        # Close original file, no longer needed
44
48
        puppet_conf_fh.close()
45
49
        for cfg_name, cfg in puppet_cfg['conf'].iteritems():
63
67
                util.restorecon_if_possible('/var/lib/puppet', recursive=True)
64
68
            else:
65
69
                #puppet_conf_fh.write("\n[%s]\n" % (cfg_name))
66
 
                # If puppet.conf already has this section we don't want to write it again
 
70
                # If puppet.conf already has this section we don't want to
 
71
                # write it again
67
72
                if puppet_config.has_section(cfg_name) == False:
68
73
                    puppet_config.add_section(cfg_name)
69
74
                # Iterate throug the config items, we'll use ConfigParser.set
77
82
                              cloud.datasource.get_instance_id())
78
83
                        # certname needs to be downcase
79
84
                        v = v.lower()
80
 
                    puppet_config.set(cfg_name,o,v)
 
85
                    puppet_config.set(cfg_name, o, v)
81
86
                    #puppet_conf_fh.write("%s=%s\n" % (o, v))
82
87
            # We got all our config as wanted we'll rename
83
88
            # the previous puppet.conf and create our new one
84
 
            os.rename('/etc/puppet/puppet.conf','/etc/puppet/puppet.conf.old')
 
89
            os.rename('/etc/puppet/puppet.conf', '/etc/puppet/puppet.conf.old')
85
90
            with open('/etc/puppet/puppet.conf', 'wb') as configfile:
86
91
                puppet_config.write(configfile)
87
92
            util.restorecon_if_possible('/etc/puppet/puppet.conf')
98
103
        log.warn("Do not know how to enable puppet service on this system")
99
104
    # Start puppetd
100
105
    subprocess.check_call(['service', 'puppet', 'start'])
101