1 """configuration files"""
16 def __init__(self, config, noscripts=False):
18 self.noscripts = noscripts
19 self.cond = threading.Condition()
22 def checksum(self, path):
25 try: return sha.new(f.read()).digest()
27 except IOError: return None
29 def system(self, cmd):
30 if not self.noscripts and cmd:
31 logger.log('conf_files: running command %s' % cmd)
32 return tools.fork_as(None, os.system, cmd)
35 def update_conf_file(self, cf_rec):
36 if not cf_rec['enabled']: return
38 # XXX Remove once old Node Manager is out of service
39 if dest == '/etc/proper/propd.conf': return
40 err_cmd = cf_rec['error_cmd']
41 mode = string.atoi(cf_rec['file_permissions'], base=8)
42 uid = pwd.getpwnam(cf_rec['file_owner'])[2]
43 gid = grp.getgrnam(cf_rec['file_group'])[2]
44 url = 'https://%s/%s' % (self.config.PLC_BOOT_HOST, cf_rec['source'])
46 contents = curlwrapper.retrieve(url, self.config.cacert)
47 except curlwrapper.CurlException:
48 logger.log('conf_files: failed to retrieve %s from %s, skipping' % (dest, url))
50 if not cf_rec['always_update'] and sha.new(contents).digest() == self.checksum(dest):
52 if self.system(cf_rec['preinstall_cmd']):
54 if not cf_rec['ignore_cmd_errors']: return
55 logger.log('conf_files: installing file %s from %s' % (dest, url))
56 try: os.makedirs(os.path.dirname(dest))
58 tools.write_file(dest, lambda f: f.write(contents), mode=mode, uidgid=(uid,gid))
59 if self.system(cf_rec['postinstall_cmd']): self.system(err_cmd)
61 def run_once(self, data):
62 for f in data['conf_files']:
63 try: self.update_conf_file(f)
64 except: logger.log_exc()
69 while self.data == None: self.cond.wait()
75 def callback(self, data):
84 def start(options, config):
86 main = conf_files(config)
87 tools.as_daemon_thread(main.run)
91 assert main is not None
92 return main.callback(data)
94 if __name__ == '__main__':
96 parser = optparse.OptionParser()
97 parser.add_option('-f', '--config', action='store', dest='config', default='/etc/planetlab/plc_config', help='PLC configuration file')
98 parser.add_option('-k', '--session', action='store', dest='session', default='/etc/planetlab/session', help='API session key (or file)')
99 parser.add_option('--noscripts', action='store_true', dest='noscripts', default=False, help='Do not run pre- or post-install scripts')
100 (options, args) = parser.parse_args()
102 # Load /etc/planetlab/plc_config
103 from config import Config
104 config = Config(options.config)
106 # Load /etc/planetlab/session
107 if os.path.exists(options.session):
108 session = file(options.session).read().strip()
110 session = options.session
112 # Initialize XML-RPC client
113 from plcapi import PLCAPI
114 plc = PLCAPI(config.plc_api_uri, config.cacert, auth = session)
116 main = conf_files(config, options.noscripts)
117 data = plc.GetSlivers()