16 from config import Config
17 from plcapi import PLCAPI
20 savedargv = sys.argv[:]
22 parser = optparse.OptionParser()
23 parser.add_option('-d', '--daemon', action='store_true', dest='daemon', default=False, help='run daemonized')
24 parser.add_option('-s', '--startup', action='store_true', dest='startup', default=False, help='run all sliver startup scripts')
25 parser.add_option('-f', '--config', action='store', dest='config', default='/etc/planetlab/plc_config', help='PLC configuration file')
26 parser.add_option('-k', '--session', action='store', dest='session', default='/etc/planetlab/session', help='API session key (or file)')
27 parser.add_option('-p', '--period', action='store', dest='period', default=600, help='Polling interval (sec)')
28 (options, args) = parser.parse_args()
33 data = plc.GetSlivers()
34 # net needs access to API for i2 nodes.
35 for module in modules:
36 if module.__name__ == 'net':
37 module.GetSlivers(plc, data)
39 callback = getattr(module, 'GetSlivers')
42 def UpdateHostKey(plc):
43 logger.log('Trying to update ssh host key at PLC...')
44 ssh_host_key = open('/etc/ssh/ssh_host_rsa_key.pub').read().strip()
45 plc.BootUpdateNode(dict(ssh_host_key=ssh_host_key))
46 logger.log('Host key update succeeded')
50 if options.daemon: tools.daemon()
52 # Load /etc/planetlab/plc_config
53 config = Config(options.config)
56 other_pid = tools.pid_file()
58 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)
61 print "Warning while writing PID file:", err
63 # Load and start modules
64 for module in ['net', 'proper', 'conf_files', 'sm', 'bwmon']:
66 m = __import__(module)
67 m.start(options, config)
69 except ImportError, err:
70 print "Warning while loading module %s:" % module, err
72 # Load /etc/planetlab/session
73 if os.path.exists(options.session):
74 session = file(options.session).read().strip()
76 session = options.session
78 # Initialize XML-RPC client
79 plc = PLCAPI(config.plc_api_uri, config.cacert, session, timeout=options.period/2)
82 try: UpdateHostKey(plc)
83 except: logger.log_exc()
85 except: logger.log_exc()
86 time.sleep(options.period + random.randrange(0,301))
87 except: logger.log_exc()
90 if __name__ == '__main__':
91 stacklim = 512*1024 # 0.5 MiB
92 curlim = resource.getrlimit(resource.RLIMIT_STACK)[0] # soft limit
94 resource.setrlimit(resource.RLIMIT_STACK, (stacklim, stacklim))
95 # for some reason, doesn't take effect properly without the exec()
96 python = '/usr/bin/python'
97 os.execv(python, [python] + savedargv)
100 # This is for debugging purposes. Open a copy of Python and import nm
101 tools.as_daemon_thread(run)