25
25
import cloudinit.CloudConfig as cc
26
26
import cloudinit.util as util
28
def handle(_name,cfg,cloud,log,_args):
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:
31
33
puppet_cfg = cfg['puppet']
32
34
# Start by installing the puppet package ...
33
35
cc.install_packages(("puppet",))
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
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)
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
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
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')