ia64/xen-unstable

view tools/python/xen/xend/XendRoot.py @ 7441:857b79d27993

Set the console limit for DomUs based on a value set in xend-config.sxp.

Signed-off-by: Dan Smith <danms@us.ibm.com>
Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Wed Oct 19 13:37:16 2005 +0100 (2005-10-19)
parents d6d1c3cbc151
children 18eb059ae471
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 # Copyright (C) 2005 XenSource Ltd
17 #============================================================================
19 """Xend root class.
20 Creates the event server and handles configuration.
22 Other classes get config variables by importing this module,
23 using instance() to get a XendRoot instance, and then
24 the config functions (e.g. get_xend_port()) to get
25 configured values.
26 """
28 import os
29 import os.path
30 import string
31 import sys
33 import XendLogging
34 from XendError import XendError
36 import sxp
39 class XendRoot:
40 """Root of the management classes."""
42 """Default path to the config file."""
43 config_default = "/etc/xen/xend-config.sxp"
45 """Environment variable used to override config_default."""
46 config_var = "XEND_CONFIG"
48 """Where network control scripts live."""
49 network_script_dir = "/etc/xen/scripts"
51 """Where block control scripts live."""
52 block_script_dir = "/etc/xen/scripts"
54 """Default path to the log file. """
55 logfile_default = "/var/log/xend.log"
57 """Default level of information to be logged."""
58 loglevel_default = 'DEBUG'
60 """Default for the flag indicating whether xend should run an http server."""
61 xend_http_server_default = 'no'
63 """Default interface address xend listens at. """
64 xend_address_default = ''
66 """Default for the flag indicating whether xend should run a relocation server."""
67 xend_relocation_server_default = 'yes'
69 """Default interface address the xend relocation server listens at. """
70 xend_relocation_address_default = ''
72 """Default port xend serves HTTP at. """
73 xend_port_default = '8000'
75 """Default port xend serves events at. """
76 xend_event_port_default = '8001'
78 """Default port xend serves relocation at. """
79 xend_relocation_port_default = '8002'
81 """Default for the flag indicating whether xend should run a unix-domain server."""
82 xend_unix_server_default = 'yes'
84 """Default path the unix-domain server listens at."""
85 xend_unix_path_default = '/var/lib/xend/xend-socket'
87 dom0_min_mem_default = '0'
89 dom0_vcpus_default = '0'
91 components = {}
93 def __init__(self):
94 self.config_path = None
95 self.config = None
96 self.configure()
99 def add_component(self, name, val):
100 """Add a xend component.
102 @param name: component name
103 @param val: component object
104 """
105 self.components[name] = val
107 def get_component(self, name):
108 """Get a xend component from its name.
109 This is used as a work-round for problems caused by mutually
110 recursive imports.
112 @param name: component name
113 @return: component object (or None)
114 """
115 return self.components.get(name)
117 def _logError(self, fmt, args):
118 """Logging function to log to stderr. We use this for XendRoot log
119 messages because they may be logged before the logger has been
120 configured. Other components can safely use the logger.
121 """
122 print >>sys.stderr, "xend [ERROR]", fmt % args
124 def configure(self):
125 self.set_config()
126 logfile = self.get_config_value("logfile", self.logfile_default)
127 loglevel = self.get_config_value("loglevel", self.loglevel_default)
128 XendLogging.init(logfile, level = loglevel)
130 from xen.xend.server import params
131 if params.XEND_DEBUG:
132 XendLogging.addLogStderr()
134 def set_config(self):
135 """If the config file exists, read it. If not, ignore it.
137 The config file is a sequence of sxp forms.
138 """
139 self.config_path = os.getenv(self.config_var, self.config_default)
140 if os.path.exists(self.config_path):
141 try:
142 fin = file(self.config_path, 'rb')
143 try:
144 config = sxp.parse(fin)
145 finally:
146 fin.close()
147 config.insert(0, 'xend-config')
148 self.config = config
149 except Exception, ex:
150 self._logError('Reading config file %s: %s',
151 self.config_path, str(ex))
152 raise
153 else:
154 self._logError('Config file does not exist: %s',
155 self.config_path)
156 self.config = ['xend-config']
158 def get_config(self, name=None):
159 """Get the configuration element with the given name, or
160 the whole configuration if no name is given.
162 @param name: element name (optional)
163 @return: config or none
164 """
165 if name is None:
166 val = self.config
167 else:
168 val = sxp.child(self.config, name)
169 return val
171 def get_config_value(self, name, val=None):
172 """Get the value of an atomic configuration element.
174 @param name: element name
175 @param val: default value (optional, defaults to None)
176 @return: value
177 """
178 return sxp.child_value(self.config, name, val=val)
180 def get_config_bool(self, name, val=None):
181 v = string.lower(str(self.get_config_value(name, val)))
182 if v in ['yes', 'y', '1', 'on', 'true', 't']:
183 return True
184 if v in ['no', 'n', '0', 'off', 'false', 'f']:
185 return False
186 raise XendError("invalid xend config %s: expected bool: %s" % (name, v))
188 def get_config_int(self, name, val=None):
189 v = self.get_config_value(name, val)
190 try:
191 return int(v)
192 except Exception, ex:
193 raise XendError("invalid xend config %s: expected int: %s" % (name, v))
195 def get_xend_http_server(self):
196 """Get the flag indicating whether xend should run an http server.
197 """
198 return self.get_config_bool("xend-http-server", self.xend_http_server_default)
200 def get_xend_relocation_server(self):
201 """Get the flag indicating whether xend should run a relocation server.
202 """
203 return self.get_config_bool("xend-relocation-server", self.xend_relocation_server_default)
205 def get_xend_port(self):
206 """Get the port xend listens at for its HTTP interface.
207 """
208 return self.get_config_int('xend-port', self.xend_port_default)
210 def get_xend_event_port(self):
211 """Get the port xend listens at for connection to its event server.
212 """
213 return self.get_config_int('xend-event-port', self.xend_event_port_default)
215 def get_xend_relocation_port(self):
216 """Get the port xend listens at for connection to its relocation server.
217 """
218 return self.get_config_int('xend-relocation-port', self.xend_relocation_port_default)
220 def get_xend_address(self):
221 """Get the address xend listens at for its HTTP and event ports.
222 This defaults to the empty string which allows all hosts to connect.
223 If this is set to 'localhost' only the localhost will be able to connect
224 to the HTTP and event ports.
225 """
226 return self.get_config_value('xend-address', self.xend_address_default)
228 def get_xend_relocation_address(self):
229 """Get the address xend listens at for its relocation server port.
230 This defaults to the empty string which allows all hosts to connect.
231 If this is set to 'localhost' only the localhost will be able to connect
232 to the HTTP and event ports.
233 """
234 return self.get_config_value('xend-relocation-address', self.xend_relocation_address_default)
236 def get_xend_unix_server(self):
237 """Get the flag indicating whether xend should run a unix-domain server.
238 """
239 return self.get_config_bool("xend-unix-server", self.xend_unix_server_default)
241 def get_xend_unix_path(self):
242 """Get the path the xend unix-domain server listens at.
243 """
244 return self.get_config_value("xend-unix-path", self.xend_unix_path_default)
246 def get_block_script(self, type):
247 return self.get_config_value('block-%s' % type, '')
249 def get_network_script(self):
250 return self.get_config_value('network-script', '')
252 def get_enable_dump(self):
253 return self.get_config_bool('enable-dump', 'no')
255 def get_vif_bridge(self):
256 return self.get_config_value('vif-bridge', 'xen-br0')
258 def get_vif_script(self):
259 return self.get_config_value('vif-script', 'vif-bridge')
261 def get_vif_antispoof(self):
262 return self.get_config_bool('vif-antispoof', 'yes')
264 def get_dom0_min_mem(self):
265 return self.get_config_int('dom0-min-mem', self.dom0_min_mem_default)
267 def get_dom0_vcpus(self):
268 return self.get_config_int('dom0-cpus', self.dom0_vcpus_default)
270 def get_console_limit(self):
271 return self.get_config_int('console-limit', 1024)
273 def instance():
274 """Get an instance of XendRoot.
275 Use this instead of the constructor.
276 """
277 global inst
278 try:
279 inst
280 except:
281 inst = XendRoot()
282 return inst
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)