ia64/xen-unstable

view tools/python/xen/xend/XendOptions.py @ 16295:76f30b65854f

xend: Fix missing boolean xend config options

If a boolean option was missing in the SCF database, we weren't
processing the default value correctly.

Signed-off-by: John Levon <john.levon@sun.com>
author Keir Fraser <keir@xensource.com>
date Thu Nov 01 09:36:46 2007 +0000 (2007-11-01)
parents dc3fcd5dd4eb
children 54482c56e435
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 servers and handles configuration.
22 Other classes get config variables by importing this module,
23 using instance() to get a XendOptions 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 from xen.xend import sxp, osdep, XendLogging
34 from xen.xend.XendError import XendError
36 if os.uname()[0] == 'SunOS':
37 from xen.lowlevel import scf
39 class XendOptions:
40 """Configuration options."""
42 """Where network control scripts live."""
43 network_script_dir = osdep.scripts_dir
45 """Where block control scripts live."""
46 block_script_dir = osdep.scripts_dir
48 """Default path to the log file. """
49 logfile_default = "/var/log/xen/xend.log"
51 """Default level of information to be logged."""
52 loglevel_default = 'DEBUG'
54 """Default Xen-API server configuration. """
55 xen_api_server_default = [['unix']]
57 """Default for the flag indicating whether xend should run an http server
58 (deprecated)."""
59 xend_http_server_default = 'no'
61 xend_tcp_xmlrpc_server_default = 'no'
63 xend_tcp_xmlrpc_server_address_default = 'localhost'
65 xend_tcp_xmlrpc_server_port_default = 8006
67 xend_unix_xmlrpc_server_default = 'yes'
69 """Default interface address xend listens at. """
70 xend_address_default = ''
72 """Default for the flag indicating whether xend should run a relocation server."""
73 xend_relocation_server_default = 'no'
75 """Default interface address the xend relocation server listens at. """
76 xend_relocation_address_default = ''
78 """Default port xend serves HTTP at. """
79 xend_port_default = 8000
81 """Default port xend serves relocation at. """
82 xend_relocation_port_default = 8002
84 xend_relocation_hosts_allow_default = ''
86 """Default for the flag indicating whether xend should run a unix-domain
87 server (deprecated)."""
88 xend_unix_server_default = 'no'
90 """Default external migration tool """
91 external_migration_tool_default = ''
93 """Default path the unix-domain server listens at."""
94 xend_unix_path_default = '/var/lib/xend/xend-socket'
96 dom0_min_mem_default = 0
98 dom0_vcpus_default = 0
100 vncpasswd_default = None
102 """Default interface to listen for VNC connections on"""
103 xend_vnc_listen_default = '127.0.0.1'
105 """Use of TLS mode in QEMU VNC server"""
106 xend_vnc_tls = 0
108 """x509 certificate directory for QEMU VNC server"""
109 xend_vnc_x509_cert_dir = "/etc/xen/vnc"
111 """Verify incoming client x509 certs"""
112 xend_vnc_x509_verify = 0
114 """Default session storage path."""
115 xend_domains_path_default = '/var/lib/xend/domains'
117 """Default xend management state storage."""
118 xend_state_path_default = '/var/lib/xend/state'
120 """Default xend QCoW storage repository location."""
121 xend_storage_path_default = '/var/lib/xend/storage'
123 """Default script to configure a backend network interface"""
124 vif_script = osdep.vif_script
126 def __init__(self):
127 self.configure()
129 def _logError(self, fmt, *args):
130 """Logging function to log to stderr. We use this for XendOptions log
131 messages because they may be logged before the logger has been
132 configured. Other components can safely use the logger.
133 """
134 print >>sys.stderr, "xend [ERROR]", fmt % args
137 def configure(self):
138 self.set_config()
139 XendLogging.init(self.get_config_string("logfile",
140 self.logfile_default),
141 self.get_config_string("loglevel",
142 self.loglevel_default))
144 def set_config(self):
145 raise NotImplementedError()
147 def get_config_bool(self, name, val=None):
148 raise NotImplementedError()
150 def get_config_int(self, name, val=None):
151 raise NotImplementedError()
153 def get_config_string(self, name, val=None):
154 raise NotImplementedError()
156 def get_xen_api_server(self):
157 raise NotImplementedError()
159 def get_xend_http_server(self):
160 """Get the flag indicating whether xend should run an http server.
161 """
162 return self.get_config_bool("xend-http-server", self.xend_http_server_default)
164 def get_xend_tcp_xmlrpc_server(self):
165 return self.get_config_bool("xend-tcp-xmlrpc-server",
166 self.xend_tcp_xmlrpc_server_default)
168 def get_xend_tcp_xmlrpc_server_port(self):
169 return self.get_config_int("xend-tcp-xmlrpc-server-port",
170 self.xend_tcp_xmlrpc_server_port_default)
172 def get_xend_tcp_xmlrpc_server_address(self):
173 return self.get_config_string("xend-tcp-xmlrpc-server-address",
174 self.xend_tcp_xmlrpc_server_address_default)
176 def get_xend_tcp_xmlrpc_server_ssl_key_file(self):
177 return self.get_config_string("xend-tcp-xmlrpc-server-ssl-key-file")
179 def get_xend_tcp_xmlrpc_server_ssl_cert_file(self):
180 return self.get_config_string("xend-tcp-xmlrpc-server-ssl-cert-file")
182 def get_xend_unix_xmlrpc_server(self):
183 return self.get_config_bool("xend-unix-xmlrpc-server",
184 self.xend_unix_xmlrpc_server_default)
186 def get_xend_relocation_server(self):
187 """Get the flag indicating whether xend should run a relocation server.
188 """
189 return self.get_config_bool("xend-relocation-server",
190 self.xend_relocation_server_default)
192 def get_xend_port(self):
193 """Get the port xend listens at for its HTTP interface.
194 """
195 return self.get_config_int('xend-port', self.xend_port_default)
197 def get_xend_relocation_port(self):
198 """Get the port xend listens at for connection to its relocation server.
199 """
200 return self.get_config_int('xend-relocation-port',
201 self.xend_relocation_port_default)
203 def get_xend_relocation_hosts_allow(self):
204 return self.get_config_string("xend-relocation-hosts-allow",
205 self.xend_relocation_hosts_allow_default)
207 def get_xend_address(self):
208 """Get the address xend listens at for its HTTP port.
209 This defaults to the empty string which allows all hosts to connect.
210 If this is set to 'localhost' only the localhost will be able to connect
211 to the HTTP port.
212 """
213 return self.get_config_string('xend-address', self.xend_address_default)
215 def get_xend_relocation_address(self):
216 """Get the address xend listens at for its relocation server port.
217 This defaults to the empty string which allows all hosts to connect.
218 If this is set to 'localhost' only the localhost will be able to connect
219 to the relocation port.
220 """
221 return self.get_config_string('xend-relocation-address', self.xend_relocation_address_default)
223 def get_xend_unix_server(self):
224 """Get the flag indicating whether xend should run a unix-domain server.
225 """
226 return self.get_config_bool("xend-unix-server", self.xend_unix_server_default)
228 def get_xend_unix_path(self):
229 """Get the path the xend unix-domain server listens at.
230 """
231 return self.get_config_string("xend-unix-path", self.xend_unix_path_default)
233 def get_xend_domains_path(self):
234 """ Get the path for persistent domain configuration storage
235 """
236 return self.get_config_string("xend-domains-path", self.xend_domains_path_default)
238 def get_xend_state_path(self):
239 """ Get the path for persistent domain configuration storage
240 """
241 return self.get_config_string("xend-state-path", self.xend_state_path_default)
243 def get_xend_storage_path(self):
244 """ Get the path for persistent domain configuration storage
245 """
246 return self.get_config_string("xend-storage-path", self.xend_storage_path_default)
248 def get_network_script(self):
249 """@return the script used to alter the network configuration when
250 Xend starts and stops, or None if no such script is specified."""
252 s = self.get_config_string('network-script')
254 if s:
255 result = s.split(" ")
256 result[0] = os.path.join(self.network_script_dir, result[0])
257 return result
258 else:
259 return None
261 def get_external_migration_tool(self):
262 """@return the name of the tool to handle virtual TPM migration."""
263 return self.get_config_string('external-migration-tool', self.external_migration_tool_default)
265 def get_enable_dump(self):
266 return self.get_config_bool('enable-dump', 'no')
268 def get_vif_script(self):
269 return self.get_config_string('vif-script', self.vif_script)
271 def get_dom0_min_mem(self):
272 return self.get_config_int('dom0-min-mem', self.dom0_min_mem_default)
274 def get_dom0_vcpus(self):
275 return self.get_config_int('dom0-cpus', self.dom0_vcpus_default)
277 def get_console_limit(self):
278 return self.get_config_int('console-limit', 1024)
280 def get_vnclisten_address(self):
281 return self.get_config_string('vnc-listen', self.xend_vnc_listen_default)
283 def get_vncpasswd_default(self):
284 return self.get_config_string('vncpasswd',
285 self.vncpasswd_default)
287 def get_keymap(self):
288 return self.get_config_value('keymap', None)
290 def get_resource_label_change_script(self):
291 s = self.get_config_value('resource-label-change-script')
292 if s:
293 result = s.split(" ")
294 result[0] = os.path.join(osdep.scripts_dir, result[0])
295 return result
296 else:
297 return None
300 def get_vnc_tls(self):
301 return self.get_config_string('vnc-tls', self.xend_vnc_tls)
303 def get_vnc_x509_cert_dir(self):
304 return self.get_config_string('vnc-x509-cert-dir', self.xend_vnc_x509_cert_dir)
306 def get_vnc_x509_verify(self):
307 return self.get_config_string('vnc-x509-verify', self.xend_vnc_x509_verify)
310 class XendOptionsFile(XendOptions):
312 """Default path to the config file."""
313 config_default = "/etc/xen/xend-config.sxp"
315 """Environment variable used to override config_default."""
316 config_var = "XEND_CONFIG"
318 def set_config(self):
319 """If the config file exists, read it. If not, ignore it.
321 The config file is a sequence of sxp forms.
322 """
323 self.config_path = os.getenv(self.config_var, self.config_default)
324 if os.path.exists(self.config_path):
325 try:
326 fin = file(self.config_path, 'rb')
327 try:
328 config = sxp.parse(fin)
329 finally:
330 fin.close()
331 if config is None:
332 config = ['xend-config']
333 else:
334 config.insert(0, 'xend-config')
335 self.config = config
336 except Exception, ex:
337 self._logError('Reading config file %s: %s',
338 self.config_path, str(ex))
339 raise
340 else:
341 self._logError('Config file does not exist: %s',
342 self.config_path)
343 self.config = ['xend-config']
345 def get_config_value(self, name, val=None):
346 """Get the value of an atomic configuration element.
348 @param name: element name
349 @param val: default value (optional, defaults to None)
350 @return: value
351 """
352 return sxp.child_value(self.config, name, val=val)
354 def get_config_bool(self, name, val=None):
355 v = string.lower(str(self.get_config_value(name, val)))
356 if v in ['yes', 'y', '1', 'on', 'true', 't']:
357 return True
358 if v in ['no', 'n', '0', 'off', 'false', 'f']:
359 return False
360 raise XendError("invalid xend config %s: expected bool: %s" % (name, v))
362 def get_config_int(self, name, val=None):
363 v = self.get_config_value(name, val)
364 try:
365 return int(v)
366 except Exception:
367 raise XendError("invalid xend config %s: expected int: %s" % (name, v))
369 def get_config_string(self, name, val=None):
370 return self.get_config_value(name, val)
372 def get_xen_api_server(self):
373 """Get the Xen-API server configuration.
374 """
375 return self.get_config_value('xen-api-server',
376 self.xen_api_server_default)
378 if os.uname()[0] == 'SunOS':
379 class XendOptionsSMF(XendOptions):
381 def set_config(self):
382 pass
384 def get_config_bool(self, name, val=None):
385 try:
386 return scf.get_bool(name)
387 except scf.error, e:
388 if e[0] == scf.SCF_ERROR_NOT_FOUND:
389 if val in ['yes', 'y', '1', 'on', 'true', 't']:
390 return True
391 if val in ['no', 'n', '0', 'off', 'false', 'f']:
392 return False
393 return val
394 else:
395 raise XendError("option %s: %s:%s" % (name, e[1], e[2]))
397 def get_config_int(self, name, val=None):
398 try:
399 return scf.get_int(name)
400 except scf.error, e:
401 if e[0] == scf.SCF_ERROR_NOT_FOUND:
402 return val
403 else:
404 raise XendError("option %s: %s:%s" % (name, e[1], e[2]))
406 def get_config_string(self, name, val=None):
407 try:
408 return scf.get_string(name)
409 except scf.error, e:
410 if e[0] == scf.SCF_ERROR_NOT_FOUND:
411 return val
412 else:
413 raise XendError("option %s: %s:%s" % (name, e[1], e[2]))
415 def get_xen_api_server(self):
416 # When the new server is a supported configuration, we should
417 # expand this.
418 return [["unix"]]
420 def instance():
421 """Get an instance of XendOptions.
422 Use this instead of the constructor.
423 """
424 global inst
425 try:
426 inst
427 except:
428 if os.uname()[0] == 'SunOS':
429 inst = XendOptionsSMF()
430 else:
431 inst = XendOptionsFile()
432 return inst