X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=conf_files.py;h=a5b2cb2cb1414caa2ce07c56639a29351d39d832;hb=570d234b7d7b40416b296de71225f28c826ee991;hp=fd926a5c4da4960f283f322e6d1289fc12704096;hpb=602e6d4985351cd9db55b1fb2c52388e4555e344;p=nodemanager.git diff --git a/conf_files.py b/conf_files.py index fd926a5..a5b2cb2 100644 --- a/conf_files.py +++ b/conf_files.py @@ -3,26 +3,31 @@ import grp import os import pwd -import sha +try: + from hashlib import sha1 as sha +except ImportError: + from sha import sha import string -import threading import curlwrapper import logger import tools import xmlrpclib +from config import Config + +# right after net +priority = 2 class conf_files: - def __init__(self, config, noscripts=False): - self.config = config + def __init__(self, noscripts=False): + self.config = Config() self.noscripts = noscripts - self.cond = threading.Condition() self.data = None def checksum(self, path): try: f = open(path) - try: return sha.new(f.read()).digest() + try: return sha(f.read()).digest() finally: f.close() except IOError: return None @@ -40,12 +45,12 @@ class conf_files: try: uid = pwd.getpwnam(cf_rec['file_owner'])[2] except: - logger.log('conf_files: cannot find user %s -- %s not updated'%(cf_rec['file_owner'],dest)) + logger.log('conf_files: cannot find user %s -- %s not updated'%(cf_rec['file_owner'], dest)) return try: gid = grp.getgrnam(cf_rec['file_group'])[2] except: - logger.log('conf_files: cannot find group %s -- %s not updated'%(cf_rec['file_group'],dest)) + logger.log('conf_files: cannot find group %s -- %s not updated'%(cf_rec['file_group'], dest)) return url = 'https://%s/%s' % (self.config.PLC_BOOT_HOST, cf_rec['source']) # set node_id at the end of the request - hacky @@ -54,19 +59,14 @@ class conf_files: else: url += '?' url += "node_id=%d"%tools.node_id() else: - logger.log('%s -- WARNING, cannot add node_id to request'%dest) - # pass slicefamily as well, as stored in /etc/planetlab/slicefamily ont the node - if tools.slicefamily(): - if url.find('?') >0: url += '&' - else: url += '?' - url += "slicefamily=%s"%tools.slicefamily() + logger.log('conf_files: %s -- WARNING, cannot add node_id to request'%dest) try: - logger.verbose("retrieving URL=%s"%url) + logger.verbose("conf_files: retrieving URL=%s"%url) contents = curlwrapper.retrieve(url, self.config.cacert) - except xmlrpclib.ProtocolError,e: + except xmlrpclib.ProtocolError as e: logger.log('conf_files: failed to retrieve %s from %s, skipping' % (dest, url)) return - if not cf_rec['always_update'] and sha.new(contents).digest() == self.checksum(dest): + if not cf_rec['always_update'] and sha(contents).digest() == self.checksum(dest): return if self.system(cf_rec['preinstall_cmd']): self.system(err_cmd) @@ -74,41 +74,25 @@ class conf_files: logger.log('conf_files: installing file %s from %s' % (dest, url)) try: os.makedirs(os.path.dirname(dest)) except OSError: pass - tools.write_file(dest, lambda f: f.write(contents), mode=mode, uidgid=(uid,gid)) + tools.write_file(dest, lambda f: f.write(contents), mode=mode, uidgid=(uid, gid)) if self.system(cf_rec['postinstall_cmd']): self.system(err_cmd) def run_once(self, data): - for f in data['conf_files']: - try: self.update_conf_file(f) - except: logger.log_exc() - - def run(self): - while True: - self.cond.acquire() - while self.data == None: self.cond.wait() - data = self.data - self.data = None - self.cond.release() - self.run_once(data) - - def callback(self, data): - if data != None: - self.cond.acquire() - self.data = data - self.cond.notify() - self.cond.release() - -main = None - -def start(options, config): - global main - main = conf_files(config) - tools.as_daemon_thread(main.run) - -def GetSlivers(data): - global main - assert main is not None - return main.callback(data) + if data.has_key("conf_files"): + for f in data['conf_files']: + try: self.update_conf_file(f) + except: logger.log_exc("conf_files: failed to update conf_file") + else: + logger.log_missing_data("conf_files.run_once", 'conf_files') + + +def start(): pass + +def GetSlivers(data, config = None, plc = None): + logger.log("conf_files: Running.") + cf = conf_files() + cf.run_once(data) + logger.log("conf_files: Done.") if __name__ == '__main__': import optparse @@ -119,12 +103,12 @@ if __name__ == '__main__': (options, args) = parser.parse_args() # Load /etc/planetlab/plc_config - from config import Config config = Config(options.config) # Load /etc/planetlab/session if os.path.exists(options.session): - session = file(options.session).read().strip() + with open(options.session) as f: + session = f.read().strip() else: session = options.session @@ -132,6 +116,6 @@ if __name__ == '__main__': from plcapi import PLCAPI plc = PLCAPI(config.plc_api_uri, config.cacert, auth = session) - main = conf_files(config, options.noscripts) + main = conf_files(options.noscripts) data = plc.GetSlivers() main.run_once(data)