ae92e07d28afb979698c1dce95dad0a7971db7dd
[nodemanager.git] / nm.py
1 #!/usr/bin/python
2
3 """Node Manager"""
4
5 import optparse
6 import time
7 import xmlrpclib
8 import socket
9 import os
10 import sys
11 import resource
12
13 import logger
14 import tools
15
16 from config import Config
17 from plcapi import PLCAPI
18
19
20 savedargv = sys.argv[:]
21
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()
29
30 modules = []
31
32 def GetSlivers(plc):
33     data = plc.GetSlivers()
34     for module in modules:
35         callback = getattr(module, 'GetSlivers')
36         callback(data)
37
38 def run():
39     try:
40         if options.daemon: tools.daemon()
41
42         # Load /etc/planetlab/plc_config
43         config = Config(options.config)
44
45         try:
46             other_pid = tools.pid_file()
47             if other_pid != None:
48                 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)
49                 return
50         except OSError, err:
51             print "Warning while writing PID file:", err
52
53         # Load and start modules
54         for module in ['net', 'proper', 'conf_files', 'sm', 'bwmon']:
55             try:
56                 m = __import__(module)
57                 m.start(options, config)
58                 modules.append(m)
59             except ImportError, err:
60                 print "Warning while loading module %s:" % module, err
61
62         # Load /etc/planetlab/session
63         if os.path.exists(options.session):
64             session = file(options.session).read().strip()
65         else:
66             session = options.session
67
68         # Initialize XML-RPC client
69         plc = PLCAPI(config.plc_api_uri, config.cacert, session, timeout=options.period/2)
70
71         while True:
72             try: GetSlivers(plc)
73             except: logger.log_exc()
74             time.sleep(options.period)
75     except: logger.log_exc()
76
77
78 if __name__ == '__main__':
79     stacklim = 512*1024  # 0.5 MiB
80     curlim = resource.getrlimit(resource.RLIMIT_STACK)[0]  # soft limit
81     if curlim > stacklim:
82         resource.setrlimit(resource.RLIMIT_STACK, (stacklim, stacklim))
83         # for some reason, doesn't take effect properly without the exec()
84         python = '/usr/bin/python'
85         os.execv(python, [python] + savedargv)
86     run()
87 else:
88     # This is for debugging purposes.  Open a copy of Python and import nm
89     tools.as_daemon_thread(run)