ia64/xen-unstable

view tools/misc/xend @ 6689:7d0fb56b4a91

merge?
author cl349@firebug.cl.cam.ac.uk
date Wed Sep 07 19:01:31 2005 +0000 (2005-09-07)
parents 38c5199155fc 6d7b05e1c1e5
children b2f4823b6ff0 b35215021b32 9af349b055e5 7985a4d8bae9
line source
1 #!/usr/bin/env python
2 # -*- mode: python; -*-
3 #============================================================================
4 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
5 #============================================================================
7 """Xen management daemon. Lives in /usr/sbin.
8 Provides console server and HTTP management api.
10 Run:
11 xend start
13 Restart:
14 xend restart
16 The daemon is stopped with:
17 xend stop
19 The daemon should reconnect to device control interfaces
20 and recover its state when restarted.
21 """
22 import os
23 import sys
24 import socket
25 import signal
26 import time
27 import commands
29 # add fallback path for non-native python path installs if needed
30 sys.path.append('/usr/lib/python')
31 sys.path.append('/usr/lib64/python')
32 from xen.xend.server import SrvDaemon
34 class CheckError(ValueError):
35 pass
37 def hline():
38 print >>sys.stderr, "*" * 70
40 def msg(message):
41 print >>sys.stderr, "*" * 3, message
43 def check_logging():
44 """Check python logging is installed and raise an error if not.
45 Logging is standard from Python 2.3 on.
46 """
47 try:
48 import logging
49 except ImportError:
50 hline()
51 msg("Python logging is not installed.")
52 msg("Use 'make install-logging' at the xen root to install.")
53 msg("")
54 msg("Alternatively download and install from")
55 msg("http://www.red-dove.com/python_logging.html")
56 hline()
57 raise CheckError("logging is not installed")
59 def check_user():
60 """Check that the effective user id is 0 (root).
61 """
62 if os.geteuid() != 0:
63 hline()
64 msg("Xend must be run as root.")
65 hline()
66 raise CheckError("invalid user")
68 def start_xenstored():
69 XENSTORED_TRACE = os.getenv("XENSTORED_TRACE")
70 cmd = "/usr/sbin/xenstored --pid-file=/var/run/xenstore.pid"
71 if XENSTORED_TRACE:
72 cmd += " -T /var/log/xenstored-trace.log"
73 s,o = commands.getstatusoutput(cmd)
75 def start_consoled():
76 if os.fork() == 0:
77 os.execvp('/usr/sbin/xenconsoled', ['/usr/sbin/xenconsoled'])
79 def main():
80 try:
81 check_logging()
82 check_user()
83 except CheckError:
84 sys.exit(1)
86 daemon = SrvDaemon.instance()
87 if not sys.argv[1:]:
88 print 'usage: %s {start|stop|restart}' % sys.argv[0]
89 elif os.fork():
90 pid, status = os.wait()
91 return status >> 8
92 elif sys.argv[1] == 'start':
93 start_xenstored()
94 start_consoled()
95 return daemon.start()
96 elif sys.argv[1] == 'trace_start':
97 start_xenstored()
98 start_consoled()
99 return daemon.start(trace=1)
100 elif sys.argv[1] == 'stop':
101 return daemon.stop()
102 elif sys.argv[1] == 'restart':
103 start_xenstored()
104 start_consoled()
105 return daemon.stop() or daemon.start()
106 elif sys.argv[1] == 'status':
107 return daemon.status()
108 else:
109 print 'not an option:', sys.argv[1]
110 return 1
112 if __name__ == '__main__':
113 sys.exit(main())