- RPM spec file for new NodeManager package
[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
11 import logger
12 import tools
13
14 from config import Config
15 from plcapi import PLCAPI
16
17 parser = optparse.OptionParser()
18 parser.add_option('-d', '--daemon', action='store_true', dest='daemon', default=False, help='run daemonized')
19 parser.add_option('-s', '--startup', action='store_true', dest='startup', default=False, help='run all sliver startup scripts')
20 parser.add_option('-f', '--config', action='store', dest='config', default='/etc/planetlab/plc_config', help='PLC configuration file')
21 parser.add_option('-k', '--session', action='store', dest='session', default='/etc/planetlab/session', help='API session key (or file)')
22 (options, args) = parser.parse_args()
23
24 # XXX - awaiting a real implementation
25 data = []
26 modules = []
27
28 def GetSlivers(plc):
29     # XXX Set a socket timeout, maybe in plcapi.PLCAPI
30     # socket.setdefaulttimeout(timeout)
31     data = plc.GetSlivers()
32
33     for mod in modules: mod.GetSlivers_callback(data)
34
35 def start_and_register_callback(mod):
36     mod.start(options)
37     modules.append(mod)
38
39
40 def run():
41     try:
42         if options.daemon: tools.daemon()
43
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         try:
54             import sm
55             start_and_register_callback(sm)
56             import conf_files
57             start_and_register_callback(conf_files)
58         except ImportError, err:
59             print "Warning while registering callbacks:", err
60
61         # Load /etc/planetlab/plc_config
62         config = Config(options.config)
63
64         # Load /etc/planetlab/session
65         if os.path.exists(options.session):
66             session = file(options.session).read().strip()
67         else:
68             session = options.session
69
70         # Initialize XML-RPC client
71         plc = PLCAPI(config.plc_api_uri, session)
72
73         while True:
74             try: GetSlivers(plc)
75             except: logger.log_exc()
76             time.sleep(600)
77     except: logger.log_exc()
78
79
80 if __name__ == '__main__': run()
81 else:
82     # This is for debugging purposes.  Open a copy of Python and import nm
83     tools.as_daemon_thread(run)