ia64/xen-unstable

view tools/python/xen/xend/XendOptions.py @ 16522:54482c56e435

Implement legacy XML-RPC interface for ACM commands.

This patch moves the directory of files where xend is writing policies
and resource labels into to /var/lib/xend/security/policies.

Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Dec 05 09:45:13 2007 +0000 (2007-12-05)
parents 76f30b65854f
children 6143f5bd32a7
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 xend security state storage path."""
124 xend_security_path_default = '/var/lib/xend/security'
126 """Default script to configure a backend network interface"""
127 vif_script = osdep.vif_script
129 def __init__(self):
130 self.configure()
132 def _logError(self, fmt, *args):
133 """Logging function to log to stderr. We use this for XendOptions log
134 messages because they may be logged before the logger has been
135 configured. Other components can safely use the logger.
136 """
137 print >>sys.stderr, "xend [ERROR]", fmt % args
140 def configure(self):
141 self.set_config()
142 XendLogging.init(self.get_config_string("logfile",
143 self.logfile_default),
144 self.get_config_string("loglevel",
145 self.loglevel_default))
147 def set_config(self):
148 raise NotImplementedError()
150 def get_config_bool(self, name, val=None):
151 raise NotImplementedError()
153 def get_config_int(self, name, val=None):
154 raise NotImplementedError()
156 def get_config_string(self, name, val=None):
157 raise NotImplementedError()
159 def get_xen_api_server(self):
160 raise NotImplementedError()
162 def get_xend_http_server(self):
163 """Get the flag indicating whether xend should run an http server.
164 """
165 return self.get_config_bool("xend-http-server", self.xend_http_server_default)
167 def get_xend_tcp_xmlrpc_server(self):
168 return self.get_config_bool("xend-tcp-xmlrpc-server",
169 self.xend_tcp_xmlrpc_server_default)
171 def get_xend_tcp_xmlrpc_server_port(self):
172 return self.get_config_int("xend-tcp-xmlrpc-server-port",
173 self.xend_tcp_xmlrpc_server_port_default)
175 def get_xend_tcp_xmlrpc_server_address(self):
176 return self.get_config_string("xend-tcp-xmlrpc-server-address",
177 self.xend_tcp_xmlrpc_server_address_default)
179 def get_xend_tcp_xmlrpc_server_ssl_key_file(self):
180 return self.get_config_string("xend-tcp-xmlrpc-server-ssl-key-file")
182 def get_xend_tcp_xmlrpc_server_ssl_cert_file(self):
183 return self.get_config_string("xend-tcp-xmlrpc-server-ssl-cert-file")
185 def get_xend_unix_xmlrpc_server(self):
186 return self.get_config_bool("xend-unix-xmlrpc-server",
187 self.xend_unix_xmlrpc_server_default)
189 def get_xend_relocation_server(self):
190 """Get the flag indicating whether xend should run a relocation server.
191 """
192 return self.get_config_bool("xend-relocation-server",
193 self.xend_relocation_server_default)
195 def get_xend_port(self):
196 """Get the port xend listens at for its HTTP interface.
197 """
198 return self.get_config_int('xend-port', self.xend_port_default)
200 def get_xend_relocation_port(self):
201 """Get the port xend listens at for connection to its relocation server.
202 """
203 return self.get_config_int('xend-relocation-port',
204 self.xend_relocation_port_default)
206 def get_xend_relocation_hosts_allow(self):
207 return self.get_config_string("xend-relocation-hosts-allow",
208 self.xend_relocation_hosts_allow_default)
210 def get_xend_address(self):
211 """Get the address xend listens at for its HTTP port.
212 This defaults to the empty string which allows all hosts to connect.
213 If this is set to 'localhost' only the localhost will be able to connect
214 to the HTTP port.
215 """
216 return self.get_config_string('xend-address', self.xend_address_default)
218 def get_xend_relocation_address(self):
219 """Get the address xend listens at for its relocation server port.
220 This defaults to the empty string which allows all hosts to connect.
221 If this is set to 'localhost' only the localhost will be able to connect
222 to the relocation port.
223 """
224 return self.get_config_string('xend-relocation-address', self.xend_relocation_address_default)
226 def get_xend_unix_server(self):
227 """Get the flag indicating whether xend should run a unix-domain server.
228 """
229 return self.get_config_bool("xend-unix-server", self.xend_unix_server_default)
231 def get_xend_unix_path(self):
232 """Get the path the xend unix-domain server listens at.
233 """
234 return self.get_config_string("xend-unix-path", self.xend_unix_path_default)
236 def get_xend_domains_path(self):
237 """ Get the path for persistent domain configuration storage
238 """
239 return self.get_config_string("xend-domains-path", self.xend_domains_path_default)
241 def get_xend_state_path(self):
242 """ Get the path for persistent domain configuration storage
243 """
244 return self.get_config_string("xend-state-path", self.xend_state_path_default)
246 def get_xend_storage_path(self):
247 """ Get the path for persistent domain configuration storage
248 """
249 return self.get_config_string("xend-storage-path", self.xend_storage_path_default)
251 def get_xend_security_path(self):
252 """ Get the path for security state
253 """
254 return self.get_config_string("xend-security-path", self.xend_security_path_default)
256 def get_network_script(self):
257 """@return the script used to alter the network configuration when
258 Xend starts and stops, or None if no such script is specified."""
260 s = self.get_config_string('network-script')
262 if s:
263 result = s.split(" ")
264 result[0] = os.path.join(self.network_script_dir, result[0])
265 return result
266 else:
267 return None
269 def get_external_migration_tool(self):
270 """@return the name of the tool to handle virtual TPM migration."""
271 return self.get_config_string('external-migration-tool', self.external_migration_tool_default)
273 def get_enable_dump(self):
274 return self.get_config_bool('enable-dump', 'no')
276 def get_vif_script(self):
277 return self.get_config_string('vif-script', self.vif_script)
279 def get_dom0_min_mem(self):
280 return self.get_config_int('dom0-min-mem', self.dom0_min_mem_default)
282 def get_dom0_vcpus(self):
283 return self.get_config_int('dom0-cpus', self.dom0_vcpus_default)
285 def get_console_limit(self):
286 return self.get_config_int('console-limit', 1024)
288 def get_vnclisten_address(self):
289 return self.get_config_string('vnc-listen', self.xend_vnc_listen_default)
291 def get_vncpasswd_default(self):
292 return self.get_config_string('vncpasswd',
293 self.vncpasswd_default)
295 def get_keymap(self):
296 return self.get_config_value('keymap', None)
298 def get_resource_label_change_script(self):
299 s = self.get_config_value('resource-label-change-script')
300 if s:
301 result = s.split(" ")
302 result[0] = os.path.join(osdep.scripts_dir, result[0])
303 return result
304 else:
305 return None
308 def get_vnc_tls(self):
309 return self.get_config_string('vnc-tls', self.xend_vnc_tls)
311 def get_vnc_x509_cert_dir(self):
312 return self.get_config_string('vnc-x509-cert-dir', self.xend_vnc_x509_cert_dir)
314 def get_vnc_x509_verify(self):
315 return self.get_config_string('vnc-x509-verify', self.xend_vnc_x509_verify)
318 class XendOptionsFile(XendOptions):
320 """Default path to the config file."""
321 config_default = "/etc/xen/xend-config.sxp"
323 """Environment variable used to override config_default."""
324 config_var = "XEND_CONFIG"
326 def set_config(self):
327 """If the config file exists, read it. If not, ignore it.
329 The config file is a sequence of sxp forms.
330 """
331 self.config_path = os.getenv(self.config_var, self.config_default)
332 if os.path.exists(self.config_path):
333 try:
334 fin = file(self.config_path, 'rb')
335 try:
336 config = sxp.parse(fin)
337 finally:
338 fin.close()
339 if config is None:
340 config = ['xend-config']
341 else:
342 config.insert(0, 'xend-config')
343 self.config = config
344 except Exception, ex:
345 self._logError('Reading config file %s: %s',
346 self.config_path, str(ex))
347 raise
348 else:
349 self._logError('Config file does not exist: %s',
350 self.config_path)
351 self.config = ['xend-config']
353 def get_config_value(self, name, val=None):
354 """Get the value of an atomic configuration element.
356 @param name: element name
357 @param val: default value (optional, defaults to None)
358 @return: value
359 """
360 return sxp.child_value(self.config, name, val=val)
362 def get_config_bool(self, name, val=None):
363 v = string.lower(str(self.get_config_value(name, val)))
364 if v in ['yes', 'y', '1', 'on', 'true', 't']:
365 return True
366 if v in ['no', 'n', '0', 'off', 'false', 'f']:
367 return False
368 raise XendError("invalid xend config %s: expected bool: %s" % (name, v))
370 def get_config_int(self, name, val=None):
371 v = self.get_config_value(name, val)
372 try:
373 return int(v)
374 except Exception:
375 raise XendError("invalid xend config %s: expected int: %s" % (name, v))
377 def get_config_string(self, name, val=None):
378 return self.get_config_value(name, val)
380 def get_xen_api_server(self):
381 """Get the Xen-API server configuration.
382 """
383 return self.get_config_value('xen-api-server',
384 self.xen_api_server_default)
386 if os.uname()[0] == 'SunOS':
387 class XendOptionsSMF(XendOptions):
389 def set_config(self):
390 pass
392 def get_config_bool(self, name, val=None):
393 try:
394 return scf.get_bool(name)
395 except scf.error, e:
396 if e[0] == scf.SCF_ERROR_NOT_FOUND:
397 if val in ['yes', 'y', '1', 'on', 'true', 't']:
398 return True
399 if val in ['no', 'n', '0', 'off', 'false', 'f']:
400 return False
401 return val
402 else:
403 raise XendError("option %s: %s:%s" % (name, e[1], e[2]))
405 def get_config_int(self, name, val=None):
406 try:
407 return scf.get_int(name)
408 except scf.error, e:
409 if e[0] == scf.SCF_ERROR_NOT_FOUND:
410 return val
411 else:
412 raise XendError("option %s: %s:%s" % (name, e[1], e[2]))
414 def get_config_string(self, name, val=None):
415 try:
416 return scf.get_string(name)
417 except scf.error, e:
418 if e[0] == scf.SCF_ERROR_NOT_FOUND:
419 return val
420 else:
421 raise XendError("option %s: %s:%s" % (name, e[1], e[2]))
423 def get_xen_api_server(self):
424 # When the new server is a supported configuration, we should
425 # expand this.
426 return [["unix"]]
428 def instance():
429 """Get an instance of XendOptions.
430 Use this instead of the constructor.
431 """
432 global inst
433 try:
434 inst
435 except:
436 if os.uname()[0] == 'SunOS':
437 inst = XendOptionsSMF()
438 else:
439 inst = XendOptionsFile()
440 return inst