merge changes from head
[nodemanager.git] / nm.py
diff --git a/nm.py b/nm.py
index 47eb575..6e56501 100644 (file)
--- a/nm.py
+++ b/nm.py
@@ -7,6 +7,8 @@ import time
 import xmlrpclib
 import socket
 import os
+import sys
+import resource
 
 import logger
 import tools
@@ -14,6 +16,9 @@ import tools
 from config import Config
 from plcapi import PLCAPI
 
+
+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')
@@ -22,19 +27,13 @@ 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)
-
+    for module in modules:
+        callback = getattr(module, 'GetSlivers')
+        callback(data)
 
 def run():
     try:
@@ -51,13 +50,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 ['net', 'proper', 'conf_files', 'sm']:
+            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):
@@ -75,7 +75,15 @@ def run():
     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)