ia64/xen-unstable

view tools/python/xen/xend/XendRoot.py @ 5217:eef4852dd799

bitkeeper revision 1.1159.258.157 (429c276eHQwfuqCUJJBnQeDTzUz3lg)

XendRoot.py:
XendRoot::logDebug wrongly set the message format to 'info', instead
of 'debug'.
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 08:59:26 2005 +0000 (2005-05-31)
parents c7837f5059f1
children 412ada0d1ed6 49476fed0296
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
19 # Initial create of the event server.
20 eserver = EventServer.instance()
22 import sxp
24 class XendRoot:
25 """Root of the management classes."""
27 """Default path to the root of the database."""
28 dbroot_default = "/var/lib/xen/xend-db"
30 """Default path to the config file."""
31 config_default = "/etc/xen/xend-config.sxp"
33 """Environment variable used to override config_default."""
34 config_var = "XEND_CONFIG"
36 """Where network control scripts live."""
37 network_script_dir = "/etc/xen/scripts"
39 """Where block control scripts live."""
40 block_script_dir = "/etc/xen/scripts"
42 """Default path to the log file. """
43 logfile_default = "/var/log/xend.log"
45 loglevel_default = 'DEBUG'
47 """Default interface address xend listens at. """
48 xend_address_default = ''
50 """Default port xend serves HTTP at. """
51 xend_port_default = '8000'
53 """Default port xend serves events at. """
54 xend_event_port_default = '8001'
56 """Default inteface address xend listens at for consoles."""
57 console_address_default = ''
59 """Default port xend serves consoles at. """
60 console_port_base_default = '9600'
62 components = {}
64 def __init__(self):
65 self.dbroot = None
66 self.config_path = None
67 self.config = None
68 self.logging = None
69 self.configure()
70 eserver.subscribe('xend.*', self.event_handler)
71 #eserver.subscribe('xend.domain.created', self.event_handler)
72 #eserver.subscribe('xend.domain.died', self.event_handler)
74 def add_component(self, name, val):
75 """Add a xend component.
77 @param name: component name
78 @param val: component object
79 """
80 self.components[name] = val
82 def get_component(self, name):
83 """Get a xend component from its name.
84 This is used as a work-round for problems caused by mutually
85 recursive imports.
87 @param name: component name
88 @return: component object (or None)
89 """
90 return self.components.get(name)
92 def start(self):
93 eserver.inject('xend.start', 0)
95 def _format(self, msg, args):
96 if args:
97 return str(msg) % args
98 else:
99 return str(msg)
101 def _log(self, mode, fmt, args):
102 """Logging function that uses the logger if it exists, otherwise
103 logs to stderr. We use this for XendRoot log messages because
104 they may be logged before the logger has been configured.
105 Other components can safely use the logger.
106 """
107 log = self.get_logger()
108 if mode not in ['warning', 'info', 'debug', 'error']:
109 mode = 'info'
110 level = mode.upper()
111 if log:
112 getattr(log, mode)(fmt, *args)
113 else:
114 print >>sys.stderr, "xend", "[%s]" % level, self._format(fmt, args)
116 def logDebug(self, fmt, *args):
117 """Log a debug message.
119 @param fmt: message format
120 @param args: arguments
121 """
122 self._log('debug', fmt, args)
124 def logInfo(self, fmt, *args):
125 """Log an info message.
127 @param fmt: message format
128 @param args: arguments
129 """
130 self._log('info', fmt, args)
132 def logWarning(self, fmt, *args):
133 """Log a warning message.
135 @param fmt: message format
136 @param args: arguments
137 """
138 self._log('warning', fmt, args)
140 def logError(self, fmt, *args):
141 """Log an error message.
143 @param fmt: message format
144 @param args: arguments
145 """
146 self._log('error', fmt, args)
148 def event_handler(self, event, val):
149 self.logInfo("EVENT> %s %s", str(event), str(val))
151 def configure(self):
152 self.set_config()
153 self.configure_logger()
154 self.dbroot = self.get_config_value("dbroot", self.dbroot_default)
156 def configure_logger(self):
157 logfile = self.get_config_value("logfile", self.logfile_default)
158 loglevel = self.get_config_value("loglevel", self.loglevel_default)
159 self.logging = XendLogging(logfile, level=loglevel)
161 def get_logging(self):
162 """Get the XendLogging instance.
163 """
164 return self.logging
166 def get_logger(self):
167 """Get the logger.
168 """
169 return self.logging and self.logging.getLogger()
171 def get_dbroot(self):
172 """Get the path to the database root.
173 """
174 return self.dbroot
176 def set_config(self):
177 """If the config file exists, read it. If not, ignore it.
179 The config file is a sequence of sxp forms.
180 """
181 self.config_path = os.getenv(self.config_var, self.config_default)
182 if os.path.exists(self.config_path):
183 #self.logInfo('Reading config file %s', self.config_path)
184 try:
185 fin = file(self.config_path, 'rb')
186 try:
187 config = sxp.parse(fin)
188 finally:
189 fin.close()
190 config.insert(0, 'xend-config')
191 self.config = config
192 except Exception, ex:
193 self.logError('Reading config file %s: %s', self.config_path, str(ex))
194 raise
195 else:
196 self.logError('Config file does not exist: %s', self.config_path)
197 self.config = ['xend-config']
199 def get_config(self, name=None):
200 """Get the configuration element with the given name, or
201 the whole configuration if no name is given.
203 @param name: element name (optional)
204 @return: config or none
205 """
206 if name is None:
207 val = self.config
208 else:
209 val = sxp.child(self.config, name)
210 return val
212 def get_config_value(self, name, val=None):
213 """Get the value of an atomic configuration element.
215 @param name: element name
216 @param val: default value (optional, defaults to None)
217 @return: value
218 """
219 return sxp.child_value(self.config, name, val=val)
221 def get_xend_port(self):
222 """Get the port xend listens at for its HTTP interface.
223 """
224 return int(self.get_config_value('xend-port', self.xend_port_default))
226 def get_xend_event_port(self):
227 """Get the port xend listens at for connection to its event server.
228 """
229 return int(self.get_config_value('xend-event-port', self.xend_event_port_default))
231 def get_xend_address(self):
232 """Get the address xend listens at for its HTTP and event ports.
233 This defaults to the empty string which allows all hosts to connect.
234 If this is set to 'localhost' only the localhost will be able to connect
235 to the HTTP and event ports.
236 """
237 return self.get_config_value('xend-address', self.xend_address_default)
239 def get_console_address(self):
240 """Get the address xend listens at for its console ports.
241 This defaults to the empty string which allows all hosts to connect.
242 If this is set to 'localhost' only the localhost will be able to connect
243 to the console ports.
244 """
245 return self.get_config_value('console-address', self.console_address_default)
247 def get_console_port_base(self):
248 """Get the base port number used to generate console ports for domains.
249 """
250 return int(self.get_config_value('console-port-base', self.console_port_base_default))
252 def get_block_script(self, type):
253 return self.get_config_value('block-%s' % type, '')
255 def get_network_script(self):
256 return self.get_config_value('network-script', 'network')
258 def get_vif_bridge(self):
259 return self.get_config_value('vif-bridge', 'xen-br0')
261 def get_vif_script(self):
262 return self.get_config_value('vif-script', 'vif-bridge')
264 def get_vif_antispoof(self):
265 v = self.get_config_value('vif-antispoof', 'yes')
266 return v in ['yes', '1', 'on']
268 def instance():
269 """Get an instance of XendRoot.
270 Use this instead of the constructor.
271 """
272 global inst
273 try:
274 inst
275 except:
276 inst = XendRoot()
277 return inst
279 def logger():
280 """Get the logger.
281 """
282 return instance().get_logger()
284 def add_component(name, val):
285 """Register a component with XendRoot.
286 This is used to work-round import cycles.
288 @param name: component name
289 @param val: component value (often a module)
290 """
291 return instance().add_component(name, val)
293 def get_component(name):
294 """Get a component.
295 This is used to work-round import cycles.
297 @param name: component name
298 @return component or None
299 """
300 return instance().get_component(name)