1 """configuration files"""
16 def __init__(self, config):
18 self.cond = threading.Condition()
21 def checksum(self, path):
24 try: return sha.new(f.read()).digest()
26 except IOError: return None
28 def system(self, cmd):
30 logger.log('conf_files: running command %s' % cmd)
34 def update_conf_file(self, cf_rec):
35 if not cf_rec['enabled']: return
37 logger.log('conf_files: considering file %s' % dest)
38 err_cmd = cf_rec['error_cmd']
39 mode = string.atoi(cf_rec['file_permissions'], base=8)
40 uid = pwd.getpwnam(cf_rec['file_owner'])[2]
41 gid = grp.getgrnam(cf_rec['file_group'])[2]
42 url = 'https://%s/%s' % (self.config.PLC_BOOT_HOST, cf_rec['source'])
43 contents = curlwrapper.retrieve(url, self.config.cacert)
44 logger.log('conf_files: retrieving url %s' % url)
45 if not cf_rec['always_update'] and sha.new(contents).digest() == self.checksum(dest):
46 logger.log('conf_files: skipping file %s, always_update is false and checksums are identical' % dest)
48 if self.system(cf_rec['preinstall_cmd']):
50 if not cf_rec['ignore_cmd_errors']: return
51 logger.log('conf_files: installing file %s' % dest)
52 try: os.makedirs(os.path.dirname(dest))
54 tools.write_file(dest, lambda f: f.write(contents), mode=mode, uidgid=(uid,gid))
55 if self.system(cf_rec['postinstall_cmd']): self.system(err_cmd)
57 def run_once(self, data):
59 for f in d['conf_files']:
60 try: self.update_conf_file(f)
61 except: logger.log_exc()
66 while self.data == None: self.cond.wait()
72 def callback(self, data):
81 def GetSlivers_callback(data): main.callback(data)
83 def start(options, config):
84 main = conf_files(config)
85 tools.as_daemon_thread(main.run)
87 if __name__ == '__main__':
89 parser = optparse.OptionParser()
90 parser.add_option('-f', '--config', action='store', dest='config', default='/etc/planetlab/plc_config', help='PLC configuration file')
91 parser.add_option('-k', '--session', action='store', dest='session', default='/etc/planetlab/session', help='API session key (or file)')
92 (options, args) = parser.parse_args()
94 # Load /etc/planetlab/plc_config
95 from config import Config
96 config = Config(options.config)
98 # Load /etc/planetlab/session
99 if os.path.exists(options.session):
100 session = file(options.session).read().strip()
102 session = options.session
104 # Initialize XML-RPC client
105 from plcapi import PLCAPI
106 plc = PLCAPI(config.plc_api_uri, config.cacert, auth = session)
108 main = conf_files(config)
109 data = plc.GetSlivers()