ia64/xen-unstable

view tools/misc/xend @ 5145:d61ceec31dfd

bitkeeper revision 1.1547 (42945455UNorq1ha3va8g7OVydNVmw)

XendDomain.py:
Improve error handling.
XendCheckpoint.py:
Switch restore to use os functions to read from the state file.
Simplify code.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Wed May 25 10:32:53 2005 +0000 (2005-05-25)
parents fc5081a78657
children fbf3bae97a88
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_user():
64 """Check that the effective user id is 0 (root).
65 """
66 if os.geteuid() != 0:
67 hline()
68 msg("Xend must be run as root.")
69 hline()
70 raise CheckError("invalid user")
72 def xcs_running():
73 """ See if the control switch is running.
74 """
75 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
76 try:
77 s.connect( (XCS_PATH) )
78 s.close()
79 except:
80 try:
81 os.remove(XCS_PIDFILE)
82 except:
83 pass
84 return 0
85 return 1
87 def start_xcs():
88 if (not xcs_running()):
89 if os.fork():
90 time.sleep(0.1) # let xcs start
91 else:
92 if not os.path.isdir(os.path.dirname(XCS_PATH)):
93 os.makedirs(os.path.dirname(XCS_PATH))
94 try:
95 os.execvp(XCS_EXEC, XCS_ARGS)
96 except:
97 hline()
98 msg("Tried to start xcs, but failed. Is it installed?")
99 hline()
100 raise CheckError("couldn't start xcs")
101 if (not xcs_running()):
102 hline()
103 msg("Failed to start the control interface switch.")
104 hline()
105 raise CheckError("xcs not running")
107 def stop_xcs():
108 try:
109 xcs_pidfile = open(XCS_PIDFILE)
110 xcs_pid = int(xcs_pidfile.read().strip())
111 os.kill(xcs_pid, signal.SIGTERM)
112 xcs_pidfile.close()
113 except:
114 return
116 def main():
117 try:
118 check_logging()
119 check_user()
120 except CheckError:
121 sys.exit(1)
123 daemon = SrvDaemon.instance()
124 if not sys.argv[1:]:
125 print 'usage: %s {start|stop|restart}' % sys.argv[0]
126 elif os.fork():
127 pid, status = os.wait()
128 return status >> 8
129 elif sys.argv[1] == 'start':
130 start_xcs()
131 return daemon.start()
132 elif sys.argv[1] == 'trace_start':
133 start_xcs()
134 return daemon.start(trace=1)
135 elif sys.argv[1] == 'stop':
136 stop_xcs()
137 return daemon.stop()
138 elif sys.argv[1] == 'restart':
139 stop_xcs()
140 start_xcs()
141 return daemon.stop() or daemon.start()
142 elif sys.argv[1] == 'status':
143 return daemon.status()
144 else:
145 print 'not an option:', sys.argv[1]
146 return 1
148 if __name__ == '__main__':
149 sys.exit(main())