direct-io.hg

view tools/misc/xend @ 4651:3b385d58d823

bitkeeper revision 1.1378 (426ca26eUpP7OInuOBvTyETb6y9VRQ)

Add /usr/lib64/python to the path for python tools so that they can be
found on 64-bit platforms.
Signed-off-by: Jerone Young <jyoung@us.ibm.com>
author kaf24@firebug.cl.cam.ac.uk
date Mon Apr 25 07:55:26 2005 +0000 (2005-04-25)
parents 189085197f6d
children fc5081a78657
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
28 XCS_PATH = "/var/lib/xen/xcs_socket"
29 XCS_EXEC = "/usr/sbin/xcs"
30 XCS_PIDFILE = "/var/run/xcs.pid"
31 XCS_ARGS = (XCS_EXEC, "-p", XCS_PIDFILE)
33 # add fallback path for non-native python path installs if needed
34 sys.path.append('/usr/lib/python')
35 sys.path.append('/usr/lib64/python')
36 from xen.xend.server import SrvDaemon
38 class CheckError(ValueError):
39 pass
41 def hline():
42 print >>sys.stderr, "*" * 70
44 def msg(message):
45 print >>sys.stderr, "*" * 3, message
47 def check_logging():
48 """Check python logging is installed and raise an error if not.
49 Logging is standard from Python 2.3 on.
50 """
51 try:
52 import logging
53 except ImportError:
54 hline()
55 msg("Python logging is not installed.")
56 msg("Use 'make install-logging' at the xen root to install.")
57 msg("")
58 msg("Alternatively download and install from")
59 msg("http://www.red-dove.com/python_logging.html")
60 hline()
61 raise CheckError("logging is not installed")
63 def check_twisted_version():
64 """Check twisted is installed with a supported version and print a warning if not.
65 Raises an error if twisted is not installed.
66 """
67 # Supported twisted release and major version.
68 RELEASE = 1
69 MAJOR = 3
70 try:
71 from twisted.copyright import version
72 except ImportError:
73 hline()
74 msg("The Twisted framework is not installed.")
75 msg("Use 'make install-twisted' at the xen root to install.")
76 msg("")
77 msg("Alternatively download and install version %d.%d or higher" % (RELEASE, MAJOR))
78 msg("from http://www.twistedmatrix.com/products")
79 hline()
80 raise CheckError("twisted is not installed")
83 (release, major, minor) = version.split('.')
84 release = int(release)
85 major = int(major)
86 if release > RELEASE: return
87 if release == RELEASE and major >= MAJOR: return
88 hline()
89 msg("Warning: Twisted version not supported: %s" % version)
90 msg("Use Twisted version %d.%d.0 or higher" % (RELEASE, MAJOR))
91 hline()
93 def check_user():
94 """Check that the effective user id is 0 (root).
95 """
96 if os.geteuid() != 0:
97 hline()
98 msg("Xend must be run as root.")
99 hline()
100 raise CheckError("invalid user")
102 def xcs_running():
103 """ See if the control switch is running.
104 """
105 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
106 try:
107 s.connect( (XCS_PATH) )
108 s.close()
109 except:
110 try:
111 os.remove(XCS_PIDFILE)
112 except:
113 pass
114 return 0
115 return 1
117 def start_xcs():
118 if (not xcs_running()):
119 if os.fork():
120 time.sleep(0.1) # let xcs start
121 else:
122 if not os.path.isdir(os.path.dirname(XCS_PATH)):
123 os.makedirs(os.path.dirname(XCS_PATH))
124 try:
125 os.execvp(XCS_EXEC, XCS_ARGS)
126 except:
127 hline()
128 msg("Tried to start xcs, but failed. Is it installed?")
129 hline()
130 raise CheckError("couldn't start xcs")
131 if (not xcs_running()):
132 hline()
133 msg("Failed to start the control interface switch.")
134 hline()
135 raise CheckError("xcs not running")
137 def stop_xcs():
138 try:
139 xcs_pidfile = open(XCS_PIDFILE)
140 xcs_pid = int(xcs_pidfile.read().strip())
141 os.kill(xcs_pid, signal.SIGTERM)
142 xcs_pidfile.close()
143 except:
144 return
146 def main():
147 try:
148 check_logging()
149 check_twisted_version()
150 check_user()
151 except CheckError:
152 sys.exit(1)
154 daemon = SrvDaemon.instance()
155 if not sys.argv[1:]:
156 print 'usage: %s {start|stop|restart}' % sys.argv[0]
157 elif os.fork():
158 pid, status = os.wait()
159 return status >> 8
160 elif sys.argv[1] == 'start':
161 start_xcs()
162 return daemon.start()
163 elif sys.argv[1] == 'trace_start':
164 start_xcs()
165 return daemon.start(trace=1)
166 elif sys.argv[1] == 'stop':
167 stop_xcs()
168 return daemon.stop()
169 elif sys.argv[1] == 'restart':
170 stop_xcs()
171 start_xcs()
172 return daemon.stop() or daemon.start()
173 elif sys.argv[1] == 'status':
174 return daemon.status()
175 else:
176 print 'not an option:', sys.argv[1]
177 return 1
179 if __name__ == '__main__':
180 sys.exit(main())