Remove call to Boot API to update ssh keys.
[nodemanager.git] / nm.py
1 #!/usr/bin/python
2
3 # $Id: $
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     data = plc.GetSlivers()
37     # Set i2 ip list for nodes in I2 nodegroup.
38     try: net.GetSlivers(plc, data)
39     except: logger.log_exc()
40     #  All other callback modules
41     for module in modules:
42         try:        
43             callback = getattr(module, 'GetSlivers')
44             callback(data)
45         except: logger.log_exc()
46
47 def run():
48     try:
49         if options.daemon: tools.daemon()
50
51         # Load /etc/planetlab/plc_config
52         config = Config(options.config)
53
54         try:
55             other_pid = tools.pid_file()
56             if other_pid != None:
57                 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)
58                 return
59         except OSError, err:
60             print "Warning while writing PID file:", err
61
62         # Load and start modules
63         for module in ['proper', 'conf_files', 'sm', 'bwmon']:
64             try:
65                 m = __import__(module)
66                 m.start(options, config)
67                 modules.append(m)
68             except ImportError, err:
69                 print "Warning while loading module %s:" % module, err
70
71         # Load /etc/planetlab/session
72         if os.path.exists(options.session):
73             session = file(options.session).read().strip()
74         else:
75             session = options.session
76
77         # Initialize XML-RPC client
78         plc = PLCAPI(config.plc_api_uri, config.cacert, session, timeout=options.period/2)
79
80         while True:
81         # Main NM Loop
82             GetSlivers(plc)
83             time.sleep(options.period + random.randrange(0,301))
84     except: logger.log_exc()
85
86
87 if __name__ == '__main__':
88     stacklim = 512*1024  # 0.5 MiB
89     curlim = resource.getrlimit(resource.RLIMIT_STACK)[0]  # soft limit
90     if curlim > stacklim:
91         resource.setrlimit(resource.RLIMIT_STACK, (stacklim, stacklim))
92         # for some reason, doesn't take effect properly without the exec()
93         python = '/usr/bin/python'
94         os.execv(python, [python] + savedargv)
95     run()
96 else:
97     # This is for debugging purposes.  Open a copy of Python and import nm
98     tools.as_daemon_thread(run)