Merge lp:~lynxman/cloud-init/puppet-mcollective into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Marc Cluet on 2011-02-15
Status: Merged
Merged at revision: 368
Proposed branch: lp:~lynxman/cloud-init/puppet-mcollective
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 125 lines (+85/-4)
2 files modified
cloudinit/CloudConfig/cc_mcollective.py (+63/-0)
cloudinit/CloudConfig/cc_puppet.py (+22/-4)
To merge this branch: bzr merge lp:~lynxman/cloud-init/puppet-mcollective
Reviewer Review Type Date Requested Status
Scott Moser 2011-02-15 Needs Fixing on 2011-02-16
Review via email: mp+49819@code.launchpad.net

Description of the change

Modified puppet module to try to replace values on the existing file instead of adding new values to the end

Also added initial mcollective module

To post a comment you must log in.
Scott Moser (smoser) wrote :

It looks good except, I'd like a doc/examples/cloud-config-mcollective.txt
or a section in doc/examples/cloud-config.txt that would explain the mcollective configurations.

review: Needs Fixing

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'cloudinit/CloudConfig/cc_mcollective.py'
2--- cloudinit/CloudConfig/cc_mcollective.py 1970-01-01 00:00:00 +0000
3+++ cloudinit/CloudConfig/cc_mcollective.py 2011-02-15 14:53:56 +0000
4@@ -0,0 +1,63 @@
5+# vi: ts=4 expandtab
6+#
7+# Copyright (C) 2009-2011 Canonical Ltd.
8+#
9+# Author: Marc Cluet <marc.cluet@canonical.com>
10+# Based on code by Scott Moser <scott.moser@canonical.com>
11+#
12+# This program is free software: you can redistribute it and/or modify
13+# it under the terms of the GNU General Public License version 3, as
14+# published by the Free Software Foundation.
15+#
16+# This program is distributed in the hope that it will be useful,
17+# but WITHOUT ANY WARRANTY; without even the implied warranty of
18+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+# GNU General Public License for more details.
20+#
21+# You should have received a copy of the GNU General Public License
22+# along with this program. If not, see <http://www.gnu.org/licenses/>.
23+import os
24+import pwd
25+import socket
26+import subprocess
27+import StringIO
28+import ConfigParser
29+import cloudinit.CloudConfig as cc
30+
31+# Our fake header section
32+class FakeSecHead(object):
33+ def __init__(self, fp):
34+ self.fp = fp
35+ self.sechead = '[nullsection]\n'
36+ def readline(self):
37+ if self.sechead:
38+ try: return self.sechead
39+ finally: self.sechead = None
40+ else: return self.fp.readline()
41+
42+def handle(name,cfg,cloud,log,args):
43+ # If there isn't a mcollective key in the configuration don't do anything
44+ if not cfg.has_key('mcollective'): return
45+ mcollective_cfg = cfg['mcollective']
46+ # Start by installing the mcollective package ...
47+ cc.install_packages(("mcollective",))
48+
49+ # ... and then update the mcollective configuration
50+ if mcollective_cfg.has_key('conf'):
51+ # Create object for reading server.cfg values
52+ mcollective_config = ConfigParser.ConfigParser()
53+ # Read server.cfg values from original file in order to be able to mix the rest up
54+ mcollective_config.readfp(FakeSecHead(open('/etc/mcollective/server.cfg')))
55+ for cfg_name, cfg in mcollective_cfg['conf'].iteritems():
56+ # Iterate throug the config items, we'll use ConfigParser.set
57+ # to overwrite or create new items as needed
58+ for o, v in cfg.iteritems():
59+ mcollective_config.set(cfg_name,o,v)
60+ # We got all our config as wanted we'll rename
61+ # the previous server.cfg and create our new one
62+ os.rename('/etc/mcollective/server.cfg','/etc/mcollective/server.cfg.old')
63+ with open('/etc/mcollective/server.cfg', 'wb') as configfile:
64+ mcollective_config.write(configfile)
65+ # Start mcollective
66+ subprocess.check_call(['service', 'mcollective', 'start'])
67+
68
69=== modified file 'cloudinit/CloudConfig/cc_puppet.py'
70--- cloudinit/CloudConfig/cc_puppet.py 2011-02-07 19:43:40 +0000
71+++ cloudinit/CloudConfig/cc_puppet.py 2011-02-15 14:53:56 +0000
72@@ -19,6 +19,8 @@
73 import pwd
74 import socket
75 import subprocess
76+import StringIO
77+import ConfigParser
78 import cloudinit.CloudConfig as cc
79
80 def handle(name,cfg,cloud,log,args):
81@@ -31,7 +33,13 @@
82 # ... and then update the puppet configuration
83 if puppet_cfg.has_key('conf'):
84 # Add all sections from the conf object to puppet.conf
85- puppet_conf_fh = open('/etc/puppet/puppet.conf', 'a')
86+ puppet_conf_fh = open('/etc/puppet/puppet.conf', 'r')
87+ # Create object for reading puppet.conf values
88+ puppet_config = ConfigParser.ConfigParser()
89+ # Read puppet.conf values from original file in order to be able to mix the rest up
90+ puppet_config.readfp(StringIO.StringIO(''.join(i.lstrip() for i in puppet_conf_fh.readlines())))
91+ # Close original file, no longer needed
92+ puppet_conf_fh.close()
93 for cfg_name, cfg in puppet_cfg['conf'].iteritems():
94 # ca_cert configuration is a special case
95 # Dump the puppetmaster ca certificate in the correct place
96@@ -51,7 +59,12 @@
97 os.chown('/var/lib/puppet/ssl/certs/ca.pem',
98 pwd.getpwnam('puppet').pw_uid, 0)
99 else:
100- puppet_conf_fh.write("\n[%s]\n" % (cfg_name))
101+ #puppet_conf_fh.write("\n[%s]\n" % (cfg_name))
102+ # If puppet.conf already has this section we don't want to write it again
103+ if puppet_config.has_section(cfg_name) == False
104+ puppet_config.add_section(cfg_name)
105+ # Iterate throug the config items, we'll use ConfigParser.set
106+ # to overwrite or create new items as needed
107 for o, v in cfg.iteritems():
108 if o == 'certname':
109 # Expand %f as the fqdn
110@@ -61,8 +74,13 @@
111 cloud.datasource.get_instance_id())
112 # certname needs to be downcase
113 v = v.lower()
114- puppet_conf_fh.write("%s=%s\n" % (o, v))
115- puppet_conf_fh.close()
116+ puppet_config.set(cfg_name,o,v)
117+ #puppet_conf_fh.write("%s=%s\n" % (o, v))
118+ # We got all our config as wanted we'll rename
119+ # the previous puppet.conf and create our new one
120+ os.rename('/etc/puppet/puppet.conf','/etc/puppet/puppet.conf.old')
121+ with open('/etc/puppet/puppet.conf', 'wb') as configfile:
122+ puppet_config.write(configfile)
123 # Set puppet default file to automatically start
124 subprocess.check_call(['sed', '-i',
125 '-e', 's/^START=.*/START=yes/',