Take the new doc out of the branch and into trunk
[nodemanager.git] / nm.py
diff --git a/nm.py b/nm.py
index cb76340..8ad5e65 100644 (file)
--- a/nm.py
+++ b/nm.py
@@ -1,52 +1,99 @@
+#!/usr/bin/python
+
+# $Id: nm.py,v 1.15.2.8 2007/09/04 20:50:30 faiyaza Exp $
+
 """Node Manager"""
 
 import optparse
 import time
 import xmlrpclib
+import socket
+import os
+import sys
+import resource
 
-import cf
 import logger
-import sm
 import tools
 
+from config import Config
+from plcapi import PLCAPI 
+import random
+import net
+
+savedargv = sys.argv[:]
 
 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)')
+parser.add_option('-p', '--period', action='store', dest='period', default=600, help='Polling interval (sec)')
 (options, args) = parser.parse_args()
 
-# XXX - awaiting a real implementation
-data = []
 modules = []
 
-def GetSlivers():
-    for mod in modules: mod.GetSlivers_callback(data)
-
-def start_and_register_callback(mod):
-    mod.start(options)
-    modules.append(mod)
-
+def GetSlivers(plc):
+    try: data = plc.GetSlivers()
+    except: logger.log_exc()
+    # Set i2 ip list for nodes in I2 nodegroup.
+    try: net.GetSlivers(plc, data)
+    except: logger.log_exc()
+    #  All other callback modules
+    for module in modules:
+        try:        
+            callback = getattr(module, 'GetSlivers')
+            callback(data)
+        except: logger.log_exc()
 
 def run():
     try:
         if options.daemon: tools.daemon()
 
+        # Load /etc/planetlab/plc_config
+        config = Config(options.config)
+
+        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
+
+        # Load and start modules
+        for module in ['proper', 'conf_files', 'sm', 'bwmon']:
+            try:
+                m = __import__(module)
+                m.start(options, config)
+                modules.append(m)
+            except ImportError, err:
+                print "Warning while loading module %s:" % module, err
+
+        # Load /etc/planetlab/session
+        if os.path.exists(options.session):
+            session = file(options.session).read().strip()
+        else:
+            session = options.session
 
-        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
+        # Initialize XML-RPC client
+        plc = PLCAPI(config.plc_api_uri, config.cacert, session, timeout=options.period/2)
 
-        start_and_register_callback(sm)
-        start_and_register_callback(cf)
         while True:
-            try: GetSlivers()
-            except: logger.log_exc()
-            time.sleep(10)
+        # Main NM Loop
+            GetSlivers(plc)
+            time.sleep(options.period + random.randrange(0,301))
     except: logger.log_exc()
 
 
-if __name__ == '__main__': run()
+if __name__ == '__main__':
+    stacklim = 512*1024  # 0.5 MiB
+    curlim = resource.getrlimit(resource.RLIMIT_STACK)[0]  # soft limit
+    if curlim > stacklim:
+        resource.setrlimit(resource.RLIMIT_STACK, (stacklim, stacklim))
+        # for some reason, doesn't take effect properly without the exec()
+        python = '/usr/bin/python'
+        os.execv(python, [python] + savedargv)
+    run()
 else:
     # This is for debugging purposes.  Open a copy of Python and import nm
     tools.as_daemon_thread(run)