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