ia64/xen-unstable

view tools/python/xen/xend/XendRoot.py @ 5230:d086633d58bf

bitkeeper revision 1.1610 (429c6844gwqwv-oCcjfTguJRDbgCXQ)

XendRoot.py:
Fix get_xend_relocation_port comment.
Signed-off-by: Nguyen Anh Quynh <aquynh@gmail.com>
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Tue May 31 13:36:04 2005 +0000 (2005-05-31)
parents ea32d909807d
children 6946d70596e8 6d3e8f90c2df
line source
1 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
3 """Xend root class.
4 Creates the event server and handles configuration.
6 Other classes get config variables by importing this module,
7 using instance() to get a XendRoot instance, and then
8 the config functions (e.g. get_xend_port()) to get
9 configured values.
10 """
12 import os
13 import os.path
14 import sys
16 import EventServer
17 from XendLogging import XendLogging
18 from XendError import XendError
20 # Initial create of the event server.
21 eserver = EventServer.instance()
23 import sxp
25 class XendRoot:
26 """Root of the management classes."""
28 """Default path to the root of the database."""
29 dbroot_default = "/var/lib/xen/xend-db"
31 """Default path to the config file."""
32 config_default = "/etc/xen/xend-config.sxp"
34 """Environment variable used to override config_default."""
35 config_var = "XEND_CONFIG"
37 """Where network control scripts live."""
38 network_script_dir = "/etc/xen/scripts"
40 """Where block control scripts live."""
41 block_script_dir = "/etc/xen/scripts"
43 """Default path to the log file. """
44 logfile_default = "/var/log/xend.log"
46 """Default level of information to be logged."""
47 loglevel_default = 'DEBUG'
49 """Default for the flag indicating whether xend should run an http server."""
50 xend_http_server_default = 'no'
52 """Default interface address xend listens at. """
53 xend_address_default = ''
55 """Default for the flag indicating whether xend should run a relocation server."""
56 xend_relocation_server_default = 'yes'
58 """Default interface address the xend relocation server listens at. """
59 xend_relocation_address_default = ''
61 """Default port xend serves HTTP at. """
62 xend_port_default = '8000'
64 """Default port xend serves events at. """
65 xend_event_port_default = '8001'
67 """Default port xend serves relocation at. """
68 xend_relocation_port_default = '8002'
70 """Default for the flag indicating whether xend should run a unix-domain server."""
71 xend_unix_server_default = 'yes'
73 """Default path the unix-domain server listens at."""
74 xend_unix_path_default = '/var/lib/xend/xend-socket'
76 """Default interface address xend listens at for consoles."""
77 console_address_default = 'localhost'
79 """Default port xend serves consoles at. """
80 console_port_base_default = '9600'
82 components = {}
84 def __init__(self):
85 self.dbroot = None
86 self.config_path = None
87 self.config = None
88 self.logging = None
89 self.configure()
90 eserver.subscribe('xend.*', self.event_handler)
91 #eserver.subscribe('xend.domain.created', self.event_handler)
92 #eserver.subscribe('xend.domain.died', self.event_handler)
94 def add_component(self, name, val):
95 """Add a xend component.
97 @param name: component name
98 @param val: component object
99 """
100 self.components[name] = val
102 def get_component(self, name):
103 """Get a xend component from its name.
104 This is used as a work-round for problems caused by mutually
105 recursive imports.
107 @param name: component name
108 @return: component object (or None)
109 """
110 return self.components.get(name)
112 def start(self):
113 eserver.inject('xend.start', 0)
115 def _format(self, msg, args):
116 if args:
117 return str(msg) % args
118 else:
119 return str(msg)
121 def _log(self, mode, fmt, args):
122 """Logging function that uses the logger if it exists, otherwise
123 logs to stderr. We use this for XendRoot log messages because
124 they may be logged before the logger has been configured.
125 Other components can safely use the logger.
126 """
127 log = self.get_logger()
128 if mode not in ['warning', 'info', 'debug', 'error']:
129 mode = 'info'
130 level = mode.upper()
131 if log:
132 getattr(log, mode)(fmt, *args)
133 else:
134 print >>sys.stderr, "xend", "[%s]" % level, self._format(fmt, args)
136 def logDebug(self, fmt, *args):
137 """Log a debug message.
139 @param fmt: message format
140 @param args: arguments
141 """
142 self._log('debug', fmt, args)
144 def logInfo(self, fmt, *args):
145 """Log an info message.
147 @param fmt: message format
148 @param args: arguments
149 """
150 self._log('info', fmt, args)
152 def logWarning(self, fmt, *args):
153 """Log a warning message.
155 @param fmt: message format
156 @param args: arguments
157 """
158 self._log('warning', fmt, args)
160 def logError(self, fmt, *args):
161 """Log an error message.
163 @param fmt: message format
164 @param args: arguments
165 """
166 self._log('error', fmt, args)
168 def event_handler(self, event, val):
169 self.logInfo("EVENT> %s %s", str(event), str(val))
171 def configure(self):
172 self.set_config()
173 self.configure_logger()
174 self.dbroot = self.get_config_value("dbroot", self.dbroot_default)
176 def configure_logger(self):
177 logfile = self.get_config_value("logfile", self.logfile_default)
178 loglevel = self.get_config_value("loglevel", self.loglevel_default)
179 self.logging = XendLogging(logfile, level=loglevel)
180 #self.logging.addLogStderr()
182 def get_logging(self):
183 """Get the XendLogging instance.
184 """
185 return self.logging
187 def get_logger(self):
188 """Get the logger.
189 """
190 return self.logging and self.logging.getLogger()
192 def get_dbroot(self):
193 """Get the path to the database root.
194 """
195 return self.dbroot
197 def set_config(self):
198 """If the config file exists, read it. If not, ignore it.
200 The config file is a sequence of sxp forms.
201 """
202 self.config_path = os.getenv(self.config_var, self.config_default)
203 if os.path.exists(self.config_path):
204 #self.logInfo('Reading config file %s', self.config_path)
205 try:
206 fin = file(self.config_path, 'rb')
207 try:
208 config = sxp.parse(fin)
209 finally:
210 fin.close()
211 config.insert(0, 'xend-config')
212 self.config = config
213 except Exception, ex:
214 self.logError('Reading config file %s: %s', self.config_path, str(ex))
215 raise
216 else:
217 self.logError('Config file does not exist: %s', self.config_path)
218 self.config = ['xend-config']
220 def get_config(self, name=None):
221 """Get the configuration element with the given name, or
222 the whole configuration if no name is given.
224 @param name: element name (optional)
225 @return: config or none
226 """
227 if name is None:
228 val = self.config
229 else:
230 val = sxp.child(self.config, name)
231 return val
233 def get_config_value(self, name, val=None):
234 """Get the value of an atomic configuration element.
236 @param name: element name
237 @param val: default value (optional, defaults to None)
238 @return: value
239 """
240 return sxp.child_value(self.config, name, val=val)
242 def get_config_bool(self, name, val=None):
243 v = self.get_config_value(name, val)
244 if v in ['yes', '1', 'on', 1, True]:
245 return True
246 if v in ['no', '0', 'off', 0, False]:
247 return False
248 raise XendError("invalid xend config %s: expected bool: %s" % (name, v))
250 def get_config_int(self, name, val=None):
251 v = self.get_config_value(name, val)
252 try:
253 return int(v)
254 except Exception, ex:
255 raise XendError("invalid xend config %s: expected int: %s" % (name, v))
257 def get_xend_http_server(self):
258 """Get the flag indicating whether xend should run an http server.
259 """
260 return self.get_config_bool("xend-http-server", self.xend_http_server_default)
262 def get_xend_relocation_server(self):
263 """Get the flag indicating whether xend should run a relocation server.
264 """
265 return self.get_config_bool("xend-relocation-server", self.xend_relocation_server_default)
267 def get_xend_port(self):
268 """Get the port xend listens at for its HTTP interface.
269 """
270 return self.get_config_int('xend-port', self.xend_port_default)
272 def get_xend_event_port(self):
273 """Get the port xend listens at for connection to its event server.
274 """
275 return self.get_config_int('xend-event-port', self.xend_event_port_default)
277 def get_xend_relocation_port(self):
278 """Get the port xend listens at for connection to its relocation server.
279 """
280 return self.get_config_int('xend-relocation-port', self.xend_relocation_port_default)
282 def get_xend_address(self):
283 """Get the address xend listens at for its HTTP and event ports.
284 This defaults to the empty string which allows all hosts to connect.
285 If this is set to 'localhost' only the localhost will be able to connect
286 to the HTTP and event ports.
287 """
288 return self.get_config_value('xend-address', self.xend_address_default)
290 def get_xend_relocation_address(self):
291 """Get the address xend listens at for its relocation server port.
292 This defaults to the empty string which allows all hosts to connect.
293 If this is set to 'localhost' only the localhost will be able to connect
294 to the HTTP and event ports.
295 """
296 return self.get_config_value('xend-relocation-address', self.xend_relocation_address_default)
298 def get_xend_unix_server(self):
299 """Get the flag indicating whether xend should run a unix-domain server.
300 """
301 return self.get_config_bool("xend-unix-server", self.xend_unix_server_default)
303 def get_xend_unix_path(self):
304 """Get the path the xend unix-domain server listens at.
305 """
306 return self.get_config_value("xend-unix-path", self.xend_unix_path_default)
308 def get_console_address(self):
309 """Get the address xend listens at for its console ports.
310 This defaults to 'localhost', allowing only the localhost to connect
311 to the console ports. Setting this to the empty string, allows all
312 hosts to connect.
313 """
314 return self.get_config_value('console-address', self.console_address_default)
316 def get_console_port_base(self):
317 """Get the base port number used to generate console ports for domains.
318 """
319 return self.get_config_int('console-port-base', self.console_port_base_default)
321 def get_block_script(self, type):
322 return self.get_config_value('block-%s' % type, '')
324 def get_network_script(self):
325 return self.get_config_value('network-script', 'network')
327 def get_enable_dump(self):
328 return self.get_config_value('enable-dump', 'false')
330 def get_vif_bridge(self):
331 return self.get_config_value('vif-bridge', 'xen-br0')
333 def get_vif_script(self):
334 return self.get_config_value('vif-script', 'vif-bridge')
336 def get_vif_antispoof(self):
337 return self.get_config_bool('vif-antispoof', 'yes')
339 def instance():
340 """Get an instance of XendRoot.
341 Use this instead of the constructor.
342 """
343 global inst
344 try:
345 inst
346 except:
347 inst = XendRoot()
348 return inst
350 def logger():
351 """Get the logger.
352 """
353 return instance().get_logger()
355 def add_component(name, val):
356 """Register a component with XendRoot.
357 This is used to work-round import cycles.
359 @param name: component name
360 @param val: component value (often a module)
361 """
362 return instance().add_component(name, val)
364 def get_component(name):
365 """Get a component.
366 This is used to work-round import cycles.
368 @param name: component name
369 @return component or None
370 """
371 return instance().get_component(name)