X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=nm.py;h=482f610f389a34de2d0ededf84391d43e3094b2b;hb=a526d7fa0e3359e63b70f6a0a5bc35e210f6a4f6;hp=47eb575a30d49b3ba176b275612ccfc104e1ff69;hpb=f6dec2d1f4295e66a7ada41a1ba27effaa006817;p=nodemanager.git diff --git a/nm.py b/nm.py index 47eb575..482f610 100644 --- a/nm.py +++ b/nm.py @@ -7,12 +7,17 @@ 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 + +savedargv = sys.argv[:] parser = optparse.OptionParser() parser.add_option('-d', '--daemon', action='store_true', dest='daemon', default=False, help='run daemonized') @@ -22,19 +27,23 @@ 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() + # net needs access to API for i2 nodes. + for module in modules: + if module.__name__ == 'net': + module.GetSlivers(plc, data) + else: + callback = getattr(module, 'GetSlivers') + callback(data) - for mod in modules: mod.GetSlivers_callback(data) - -def start_and_register_callback(mod, config): - mod.start(options, config) - modules.append(mod) - +def UpdateHostKey(plc): + logger.log('Trying to update ssh host key at PLC...') + ssh_host_key = open('/etc/ssh/ssh_host_rsa_key.pub').read().strip() + plc.BootUpdateNode(dict(ssh_host_key=ssh_host_key)) + logger.log('Host key update succeeded') 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 ['net', '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,23 @@ def run(): plc = PLCAPI(config.plc_api_uri, config.cacert, session, timeout=options.period/2) while True: + try: UpdateHostKey(plc) + except: logger.log_exc() try: GetSlivers(plc) except: logger.log_exc() - time.sleep(options.period) + 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)