tweak privatebridge to remove exception message when ovs is not installed - prints...
[nodemanager.git] / conf_files.py
index d349de0..252c795 100644 (file)
@@ -3,46 +3,70 @@
 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):
-        self.config = config
-        self.cond = threading.Condition()
+    def __init__(self, noscripts=False):
+        self.config = Config()
+        self.noscripts = noscripts
         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
 
     def system(self, cmd):
-        if cmd:
-            logger.log('conf_files: running command %s' % cmd)
-            return os.system(cmd)
+        if not self.noscripts and cmd:
+            logger.verbose('conf_files: running command %s' % 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']
-        # 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]
+        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))
+            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))
+            return
         url = 'https://%s/%s' % (self.config.PLC_BOOT_HOST, cf_rec['source'])
-        contents = curlwrapper.retrieve(url, self.config.cacert)
-        if not cf_rec['always_update'] and sha.new(contents).digest() == self.checksum(dest):
+        # set node_id at the end of the request - hacky
+        if tools.node_id():
+            if url.find('?') >0: url += '&'
+            else:                url += '?'
+            url += "node_id=%d"%tools.node_id()
+        else:
+            logger.log('conf_files: %s -- WARNING, cannot add node_id to request'%dest)
+        try:
+            logger.verbose("conf_files: retrieving URL=%s"%url)
+            contents = curlwrapper.retrieve(url, self.config.cacert)
+        except xmlrpclib.ProtocolError,e:
+            logger.log('conf_files: failed to retrieve %s from %s, skipping' % (dest, url))
+            return
+        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)
@@ -54,47 +78,31 @@ class conf_files:
         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
     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
@@ -107,6 +115,6 @@ if __name__ == '__main__':
     from plcapi import PLCAPI
     plc = PLCAPI(config.plc_api_uri, config.cacert, auth = session)
 
-    main = conf_files(config)
+    main = conf_files(options.noscripts)
     data = plc.GetSlivers()
     main.run_once(data)