Added ReCreate. Also added try catch to api eval of rpc method.
[nodemanager.git] / conf_files.py
index d76ec37..4903b3f 100644 (file)
@@ -7,16 +7,16 @@ import sha
 import string
 import threading
 
-import config
 import curlwrapper
 import logger
 import tools
 
 
 class conf_files:
-    def __init__(self):
+    def __init__(self, config, noscripts=False):
+        self.config = config
+        self.noscripts = noscripts
         self.cond = threading.Condition()
-        self.config = config.Config()
         self.data = None
 
     def checksum(self, path):
@@ -27,31 +27,41 @@ class conf_files:
         except IOError: return None
 
     def system(self, cmd):
-        if cmd:
+        if not self.noscripts and cmd:
             logger.log('conf_files: running command %s' % cmd)
-            return os.system(cmd)
+            return tools.fork_as(None, os.system, cmd)
         else: return 0
 
     def update_conf_file(self, cf_rec):
         if not cf_rec['enabled']: return
         dest = cf_rec['dest']
-        logger.log('conf_files: considering file %s' % dest)
+        # XXX Remove once old Node Manager is out of service
+        if dest == '/etc/proper/propd.conf': return
         err_cmd = cf_rec['error_cmd']
         mode = string.atoi(cf_rec['file_permissions'], base=8)
         uid = pwd.getpwnam(cf_rec['file_owner'])[2]
         gid = grp.getgrnam(cf_rec['file_group'])[2]
         url = 'https://%s/%s' % (self.config.PLC_BOOT_HOST, cf_rec['source'])
-        contents = curlwrapper.retrieve(url)
-        logger.log('conf_files: retrieving url %s' % url)
+        try:
+            contents = curlwrapper.retrieve(url, self.config.cacert)
+        except curlwrapper.CurlException:
+            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):
-            logger.log('conf_files: skipping file %s, always_update is false and checksums are identical' % dest)
             return
         if self.system(cf_rec['preinstall_cmd']):
             self.system(err_cmd)
             if not cf_rec['ignore_cmd_errors']: return
-        logger.log('conf_files: installing file %s' % dest)
+        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))
-        if self.system(cf_rec['postinstall_cmd']): system(err_cmd)
+        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:
@@ -60,10 +70,7 @@ class conf_files:
             data = self.data
             self.data = None
             self.cond.release()
-            for d in data:
-                for f in d['conf_files']:
-                    try: self.update_conf_file(f)
-                    except: logger.log_exc()
+            self.run_once(data)
 
     def callback(self, data):
         if data != None:
@@ -72,8 +79,40 @@ class conf_files:
             self.cond.notify()
             self.cond.release()
 
-main = conf_files()
+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 __name__ == '__main__':
+    import optparse
+    parser = optparse.OptionParser()
+    parser.add_option('-f', '--config', action='store', dest='config', default='/etc/planetlab/plc_config', help='PLC configuration file')
+    parser.add_option('-k', '--session', action='store', dest='session', default='/etc/planetlab/session', help='API session key (or file)')
+    parser.add_option('--noscripts', action='store_true', dest='noscripts', default=False, help='Do not run pre- or post-install scripts')
+    (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()
+    else:
+        session = options.session
 
-def GetSlivers_callback(data): main.callback(data)
+    # Initialize XML-RPC client
+    from plcapi import PLCAPI
+    plc = PLCAPI(config.plc_api_uri, config.cacert, auth = session)
 
-def start(options): tools.as_daemon_thread(main.run)
+    main = conf_files(config, options.noscripts)
+    data = plc.GetSlivers()
+    main.run_once(data)