direct-io.hg

view tools/python/xen/xend/XendRoot.py @ 3496:5d3cf5e4272d

bitkeeper revision 1.1159.223.23 (41f2c9e33H5dJn6K7RN-o2UASMwI3Q)

Description: Use FHS-compatible paths

From: Adam Heath <doogie@brainfood.com>
Signed-off-by: ian.pratt@cl.cam.ac.uk
author iap10@labyrinth.cl.cam.ac.uk
date Sat Jan 22 21:47:15 2005 +0000 (2005-01-22)
parents 76c5af12d4aa
children 074d7606fd61 2f9077bf40da 552e3748f0e7 c7837f5059f1
line source
1 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
3 """Xend root class.
4 Creates the event server and handles configuration.
5 """
7 import os
8 import os.path
9 import sys
11 import EventServer
12 from XendLogging import XendLogging
14 # Initial create of the event server.
15 eserver = EventServer.instance()
17 import sxp
19 class XendRoot:
20 """Root of the management classes."""
22 """Default path to the root of the database."""
23 dbroot_default = "/var/lib/xen/xend-db"
25 """Default path to the config file."""
26 config_default = "/etc/xen/xend-config.sxp"
28 """Environment variable used to override config_default."""
29 config_var = "XEND_CONFIG"
31 """Where network control scripts live."""
32 network_script_dir = "/etc/xen/scripts"
34 """Where block control scripts live."""
35 block_script_dir = "/etc/xen/scripts"
37 logfile_default = "/var/log/xend.log"
39 loglevel_default = 'DEBUG'
41 components = {}
43 def __init__(self):
44 self.dbroot = None
45 self.config_path = None
46 self.config = None
47 self.logger = None
48 self.configure()
49 eserver.subscribe('xend.*', self.event_handler)
50 #eserver.subscribe('xend.domain.created', self.event_handler)
51 #eserver.subscribe('xend.domain.died', self.event_handler)
53 def add_component(self, name, val):
54 """Add a xend component.
56 @param name: component name
57 @param val: component object
58 """
59 self.components[name] = val
61 def get_component(self, name):
62 """Get a xend component from its name.
63 This is used as a work-round for problems caused by mutually
64 recursive imports.
66 @param name: component name
67 @return: component object (or None)
68 """
69 return self.components.get(name)
71 def start(self):
72 eserver.inject('xend.start', 0)
74 def _format(self, msg, args):
75 if args:
76 return str(msg)
77 else:
78 return str(msg) % args
80 def _log(self, mode, fmt, args):
81 """Logging function that uses the logger if it exists, otherwise
82 logs to stderr. We use this for XendRoot log messages because
83 they may be logged before the logger has been configured.
84 Other components can safely use the logger.
85 """
86 log = self.get_logger()
87 if mode not in ['warning', 'info', 'debug', 'error']:
88 mode = 'info'
89 level = mode.upper()
90 if log:
91 getattr(log, mode)(fmt, *args)
92 else:
93 print >>stderr, "xend", "[%s]" % level, self._format(msg, args)
95 def logDebug(self, fmt, *args):
96 """Log a debug message.
98 @param fmt: message format
99 @param args: arguments
100 """
101 self._log('info', fmt, args)
103 def logInfo(self, fmt, *args):
104 """Log an info message.
106 @param fmt: message format
107 @param args: arguments
108 """
109 self._log('info', fmt, args)
111 def logWarning(self, fmt, *args):
112 """Log a warning message.
114 @param fmt: message format
115 @param args: arguments
116 """
117 self._log('warning', fmt, args)
119 def logError(self, fmt, *args):
120 """Log an error message.
122 @param fmt: message format
123 @param args: arguments
124 """
125 self._log('error', fmt, args)
127 def event_handler(self, event, val):
128 self.logInfo("EVENT> %s %s", str(event), str(val))
130 def configure(self):
131 self.set_config()
132 self.configure_logger()
133 self.dbroot = self.get_config_value("dbroot", self.dbroot_default)
136 def configure_logger(self):
137 logfile = self.get_config_value("logfile", self.logfile_default)
138 loglevel = self.get_config_value("loglevel", self.loglevel_default)
139 self.logging = XendLogging(logfile, level=loglevel)
141 def get_logging(self):
142 """Get the XendLogging instance.
143 """
144 return self.logging
146 def get_logger(self):
147 """Get the logger.
148 """
149 return self.logging.getLogger()
151 def get_dbroot(self):
152 """Get the path to the database root.
153 """
154 return self.dbroot
156 def set_config(self):
157 """If the config file exists, read it. If not, ignore it.
159 The config file is a sequence of sxp forms.
160 """
161 self.config_path = os.getenv(self.config_var, self.config_default)
162 if os.path.exists(self.config_path):
163 fin = file(self.config_path, 'rb')
164 try:
165 config = sxp.parse(fin)
166 config.insert(0, 'xend-config')
167 self.config = config
168 finally:
169 fin.close()
170 else:
171 self.config = ['xend-config']
173 def get_config(self, name=None):
174 """Get the configuration element with the given name, or
175 the whole configuration if no name is given.
177 @param name: element name (optional)
178 @return: config or none
179 """
180 if name is None:
181 val = self.config
182 else:
183 val = sxp.child(self.config, name)
184 return val
186 def get_config_value(self, name, val=None):
187 """Get the value of an atomic configuration element.
189 @param name: element name
190 @param val: default value (optional, defaults to None)
191 @return: value
192 """
193 return sxp.child_value(self.config, name, val=val)
195 def get_xend_port(self):
196 return int(self.get_config_value('xend-port', '8000'))
198 def get_xend_address(self):
199 return self.get_config_value('xend-address', '')
201 def get_block_script(self, type):
202 return self.get_config_value('block-%s' % type, '')
204 def get_network_script(self):
205 return self.get_config_value('network-script', 'network')
207 def get_vif_bridge(self):
208 return self.get_config_value('vif-bridge', 'xen-br0')
210 def get_vif_script(self):
211 return self.get_config_value('vif-script', 'vif-bridge')
213 def get_vif_antispoof(self):
214 v = self.get_config_value('vif-antispoof', 'yes')
215 return v in ['yes', '1', 'on']
217 def instance():
218 """Get an instance of XendRoot.
219 Use this instead of the constructor.
220 """
221 global inst
222 try:
223 inst
224 except:
225 inst = XendRoot()
226 return inst
228 def logger():
229 """Get the logger.
230 """
231 return instance().get_logger()
233 def add_component(name, val):
234 """Register a component with XendRoot.
235 This is used to work-round import cycles.
237 @param name: component name
238 @param val: component value (often a module)
239 """
240 return instance().add_component(name, val)
242 def get_component(name):
243 """Get a component.
244 This is used to work-round import cycles.
246 @param name: component name
247 @return component or None
248 """
249 return instance().get_component(name)