ia64/xen-unstable

view tools/misc/xend @ 3953:4202d86eff9f

bitkeeper revision 1.1236.1.39 (422385e3JUTcpAnifvTDP-tLQGjpug)

trivial fix to remove time.usleep
Signed-off-by: ian@xensource.com
author iap10@freefall.cl.cam.ac.uk
date Mon Feb 28 20:58:11 2005 +0000 (2005-02-28)
parents d8cf61e99a52
children a6914c2c15cf 2101e98387dd
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 time
27 XCS_PATH = "/var/xen/xcs_socket"
28 XCS_EXEC = "/usr/sbin/xcs"
29 XCS_LOGFILE = "/var/log/xcs.log"
31 # Default install path for Xen binary packages.
32 sys.path = [ '/usr/lib/python' ] + sys.path
33 from xen.xend.server import SrvDaemon
35 class CheckError(ValueError):
36 pass
38 def hline():
39 print >>sys.stderr, "*" * 70
41 def msg(message):
42 print >>sys.stderr, "*" * 3, message
44 def check_logging():
45 """Check python logging is installed and raise an error if not.
46 Logging is standard from Python 2.3 on.
47 """
48 try:
49 import logging
50 except ImportError:
51 hline()
52 msg("Python logging is not installed.")
53 msg("Use 'make install-logging' at the xen root to install.")
54 msg("")
55 msg("Alternatively download and install from")
56 msg("http://www.red-dove.com/python_logging.html")
57 hline()
58 raise CheckError("logging is not installed")
60 def check_twisted_version():
61 """Check twisted is installed with a supported version and print a warning if not.
62 Raises an error if twisted is not installed.
63 """
64 # Supported twisted release and major version.
65 RELEASE = 1
66 MAJOR = 3
67 try:
68 from twisted.copyright import version
69 except ImportError:
70 hline()
71 msg("The Twisted framework is not installed.")
72 msg("Use 'make install-twisted' at the xen root to install.")
73 msg("")
74 msg("Alternatively download and install version %d.%d or higher" % (RELEASE, MAJOR))
75 msg("from http://www.twistedmatrix.com/products")
76 hline()
77 raise CheckError("twisted is not installed")
80 (release, major, minor) = version.split('.')
81 release = int(release)
82 major = int(major)
83 if release > RELEASE: return
84 if release == RELEASE and major >= MAJOR: return
85 hline()
86 msg("Warning: Twisted version not supported: %s" % version)
87 msg("Use Twisted version %d.%d.0 or higher" % (RELEASE, MAJOR))
88 hline()
90 def check_user():
91 """Check that the effective user id is 0 (root).
92 """
93 if os.geteuid() != 0:
94 hline()
95 msg("Xend must be run as root.")
96 hline()
97 raise CheckError("invalid user")
99 def xcs_running():
100 """ See if the control switch is running.
101 """
102 ret = 1
103 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
104 try:
105 s.connect( (XCS_PATH) )
106 except:
107 ret = 0
108 s.close()
109 return (ret)
111 def main():
112 try:
113 check_logging()
114 check_twisted_version()
115 check_user()
116 except CheckError:
117 sys.exit(1)
119 if (not xcs_running()):
120 if os.fork():
121 time.sleep(0.5) # let xcs start
122 else:
123 try:
124 logfile = os.open(XCS_LOGFILE,
125 os.O_WRONLY|os.O_APPEND|os.O_CREAT)
126 os.close(1)
127 os.dup(logfile)
128 os.close(2)
129 os.dup(logfile)
130 os.close(logfile)
131 os.execlp(XCS_EXEC, XCS_EXEC)
132 except:
133 hline()
134 msg("Tried to start xcs, but failed. Is it installed?")
135 hline()
136 raise CheckError("couldn't start xcs")
137 if (not xcs_running()):
138 hline()
139 msg("Failed to start the control interface switch.")
140 hline()
141 raise CheckError("xcs not running")
143 daemon = SrvDaemon.instance()
144 if not sys.argv[1:]:
145 print 'usage: %s {start|stop|restart}' % sys.argv[0]
146 elif os.fork():
147 pid, status = os.wait()
148 return status >> 8
149 elif sys.argv[1] == 'start':
150 return daemon.start()
151 elif sys.argv[1] == 'trace_start':
152 return daemon.start(trace=1)
153 elif sys.argv[1] == 'stop':
154 return daemon.stop()
155 elif sys.argv[1] == 'restart':
156 return daemon.stop() or daemon.start()
157 elif sys.argv[1] == 'status':
158 return daemon.status()
159 else:
160 print 'not an option:', sys.argv[1]
161 return 1
163 if __name__ == '__main__':
164 sys.exit(main())