Take the new doc out of the branch and into trunk
[nodemanager.git] / nm.py
diff --git a/nm.py b/nm.py
index 47eb575..8ad5e65 100644 (file)
--- a/nm.py
+++ b/nm.py
@@ -1,5 +1,7 @@
 #!/usr/bin/python
 
+# $Id: nm.py,v 1.15.2.8 2007/09/04 20:50:30 faiyaza Exp $
+
 """Node Manager"""
 
 import optparse
@@ -7,12 +9,18 @@ import time
 import xmlrpclib
 import socket
 import os
+import sys
+import resource
 
 import logger
 import tools
 
 from config import Config
-from plcapi import PLCAPI
+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')
@@ -22,19 +30,20 @@ parser.add_option('-k', '--session', action='store', dest='session', default='/e
 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(plc):
-    data = plc.GetSlivers()
-
-    for mod in modules: mod.GetSlivers_callback(data)
-
-def start_and_register_callback(mod, config):
-    mod.start(options, config)
-    modules.append(mod)
-
+    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:
@@ -51,13 +60,14 @@ def run():
         except OSError, err:
             print "Warning while writing PID file:", err
 
-        try:
-            import sm
-            start_and_register_callback(sm, config)
-            import conf_files
-            start_and_register_callback(conf_files, config)
-        except ImportError, err:
-            print "Warning while registering callbacks:", 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):
@@ -69,13 +79,21 @@ def run():
         plc = PLCAPI(config.plc_api_uri, config.cacert, session, timeout=options.period/2)
 
         while True:
-            try: GetSlivers(plc)
-            except: logger.log_exc()
-            time.sleep(options.period)
+        # 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)