370c8df885e7cc83705c9c529c988385412e7c36
[nodemanager.git] / nm.py
1 #!/usr/bin/python
2 # Something relevant
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 import random
19 import net
20
21 savedargv = sys.argv[:]
22
23 parser = optparse.OptionParser()
24 parser.add_option('-d', '--daemon', action='store_true', dest='daemon', default=False, help='run daemonized')
25 parser.add_option('-s', '--startup', action='store_true', dest='startup', default=False, help='run all sliver startup scripts')
26 parser.add_option('-f', '--config', action='store', dest='config', default='/etc/planetlab/plc_config', help='PLC configuration file')
27 parser.add_option('-k', '--session', action='store', dest='session', default='/etc/planetlab/session', help='API session key (or file)')
28 parser.add_option('-p', '--period', action='store', dest='period', default=600, help='Polling interval (sec)')
29 (options, args) = parser.parse_args()
30
31 modules = []
32
33 def GetSlivers(plc):
34     try: data = plc.GetSlivers()
35     except: logger.log_exc()
36     # Set i2 ip list for nodes in I2 nodegroup.
37     try: net.GetSlivers(plc, data)
38     except: logger.log_exc()
39     #  All other callback modules
40     for module in modules:
41         try:        
42             callback = getattr(module, 'GetSlivers')
43             callback(data)
44         except: logger.log_exc()
45
46 def run():
47     try:
48         if options.daemon: tools.daemon()
49
50         # Load /etc/planetlab/plc_config
51         config = Config(options.config)
52
53         try:
54             other_pid = tools.pid_file()
55             if other_pid != None:
56                 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)
57                 return
58         except OSError, err:
59             print "Warning while writing PID file:", err
60
61         # Load and start modules
62         for module in ['proper', 'conf_files', 'sm', 'bwmon']:
63             try:
64                 m = __import__(module)
65                 m.start(options, config)
66                 modules.append(m)
67             except ImportError, err:
68                 print "Warning while loading module %s:" % module, err
69
70         # Load /etc/planetlab/session
71         if os.path.exists(options.session):
72             session = file(options.session).read().strip()
73         else:
74             session = options.session
75
76         # Initialize XML-RPC client
77         plc = PLCAPI(config.plc_api_uri, config.cacert, session, timeout=options.period/2)
78
79         while True:
80         # Main NM Loop
81             GetSlivers(plc)
82             time.sleep(options.period + random.randrange(0,301))
83     except: logger.log_exc()
84
85
86 if __name__ == '__main__':
87     stacklim = 512*1024  # 0.5 MiB
88     curlim = resource.getrlimit(resource.RLIMIT_STACK)[0]  # soft limit
89     if curlim > stacklim:
90         resource.setrlimit(resource.RLIMIT_STACK, (stacklim, stacklim))
91         # for some reason, doesn't take effect properly without the exec()
92         python = '/usr/bin/python'
93         os.execv(python, [python] + savedargv)
94     run()
95 else:
96     # This is for debugging purposes.  Open a copy of Python and import nm
97     tools.as_daemon_thread(run)