- warn if pid file generation fails
authorMark Huang <mlhuang@cs.princeton.edu>
Tue, 31 Oct 2006 23:14:41 +0000 (23:14 +0000)
committerMark Huang <mlhuang@cs.princeton.edu>
Tue, 31 Oct 2006 23:14:41 +0000 (23:14 +0000)
- warn if callback registration fails
- initialize XML-RPC interface, call GetSlivers() for real

nm.py

diff --git a/nm.py b/nm.py
index 23a0d34..c185fd8 100644 (file)
--- a/nm.py
+++ b/nm.py
@@ -1,25 +1,35 @@
+#!/usr/bin/python
+
 """Node Manager"""
 
 import optparse
 import time
 import xmlrpclib
+import socket
+import os
 
-import conf_files
 import logger
-import sm
 import tools
 
+from config import Config
+from plcapi import PLCAPI
 
 parser = optparse.OptionParser()
 parser.add_option('-d', '--daemon', action='store_true', dest='daemon', default=False, help='run daemonized')
 parser.add_option('-s', '--startup', action='store_true', dest='startup', default=False, help='run all sliver startup scripts')
+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)')
 (options, args) = parser.parse_args()
 
 # XXX - awaiting a real implementation
 data = []
 modules = []
 
-def GetSlivers():
+def GetSlivers(plc):
+    # XXX Set a socket timeout, maybe in plcapi.PLCAPI
+    # socket.setdefaulttimeout(timeout)
+    data = plc.GetSlivers()
+
     for mod in modules: mod.GetSlivers_callback(data)
 
 def start_and_register_callback(mod):
@@ -32,15 +42,36 @@ def run():
         if options.daemon: tools.daemon()
 
 
-        other_pid = tools.pid_file()
-        if other_pid != None:
-            print """There might be another instance of the node manager running as pid %d.  If this is not the case, please remove the pid file %s""" % (other_pid, tools.PID_FILE)
-            return
+        try:
+            other_pid = tools.pid_file()
+            if other_pid != None:
+                print """There might be another instance of the node manager running as pid %d.  If this is not the case, please remove the pid file %s""" % (other_pid, tools.PID_FILE)
+                return
+        except OSError, err:
+            print "Warning while writing PID file:", err
+
+        try:
+            import sm
+            start_and_register_callback(sm)
+            import conf_files
+            start_and_register_callback(conf_files)
+        except ImportError, err:
+            print "Warning while registering callbacks:", err
+
+        # Load /etc/planetlab/plc_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
+
+        # Initialize XML-RPC client
+        plc = PLCAPI(config.plc_api_uri, session)
 
-        start_and_register_callback(sm)
-        start_and_register_callback(conf_files)
         while True:
-            try: GetSlivers()
+            try: GetSlivers(plc)
             except: logger.log_exc()
             time.sleep(10)
     except: logger.log_exc()