debuggers.hg

view tools/python/xen/xend/XendOptions.py @ 13640:ee1febe317f7

[XEND] Allow TCP XMLRPC address:port to be configured, if enabled.

Signed-off-by: Alastair Tse <atse@xensource.com>
author Alastair Tse <atse@xensource.com>
date Thu Jan 25 11:58:00 2007 +0000 (2007-01-25)
parents 665be23d7fe9
children 966c65f0ddba
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 """Default session storage path."""
106 xend_domains_path_default = '/var/lib/xend/domains'
108 """Default xend management state storage."""
109 xend_state_path_default = '/var/lib/xend/state'
111 """Default xend QCoW storage repository location."""
112 xend_storage_path_default = '/var/lib/xend/storage'
114 """Default type of backend network interfaces"""
115 netback_type = osdep.netback_type
117 """Default script to configure a backend network interface"""
118 vif_script = osdep.vif_script
120 def __init__(self):
121 self.configure()
123 def _logError(self, fmt, *args):
124 """Logging function to log to stderr. We use this for XendOptions log
125 messages because they may be logged before the logger has been
126 configured. Other components can safely use the logger.
127 """
128 print >>sys.stderr, "xend [ERROR]", fmt % args
131 def configure(self):
132 self.set_config()
133 XendLogging.init(self.get_config_string("logfile",
134 self.logfile_default),
135 self.get_config_string("loglevel",
136 self.loglevel_default))
138 def set_config(self):
139 raise NotImplementedError()
141 def get_config_bool(self, name, val=None):
142 raise NotImplementedError()
144 def get_config_int(self, name, val=None):
145 raise NotImplementedError()
147 def get_config_string(self, name, val=None):
148 raise NotImplementedError()
150 def get_xen_api_server(self):
151 raise NotImplementedError()
153 def get_xend_http_server(self):
154 """Get the flag indicating whether xend should run an http server.
155 """
156 return self.get_config_bool("xend-http-server", self.xend_http_server_default)
158 def get_xend_tcp_xmlrpc_server(self):
159 return self.get_config_bool("xend-tcp-xmlrpc-server",
160 self.xend_tcp_xmlrpc_server_default)
162 def get_xend_tcp_xmlrpc_server_port(self):
163 return self.get_config_int("xend-tcp-xmlrpc-server-port",
164 self.xend_tcp_xmlrpc_server_port_default)
166 def get_xend_tcp_xmlrpc_server_address(self):
167 return self.get_config_string("xend-tcp-xmlrpc-server-address",
168 self.xend_tcp_xmlrpc_server_address_default)
170 def get_xend_unix_xmlrpc_server(self):
171 return self.get_config_bool("xend-unix-xmlrpc-server",
172 self.xend_unix_xmlrpc_server_default)
174 def get_xend_relocation_server(self):
175 """Get the flag indicating whether xend should run a relocation server.
176 """
177 return self.get_config_bool("xend-relocation-server",
178 self.xend_relocation_server_default)
180 def get_xend_port(self):
181 """Get the port xend listens at for its HTTP interface.
182 """
183 return self.get_config_int('xend-port', self.xend_port_default)
185 def get_xend_relocation_port(self):
186 """Get the port xend listens at for connection to its relocation server.
187 """
188 return self.get_config_int('xend-relocation-port',
189 self.xend_relocation_port_default)
191 def get_xend_relocation_hosts_allow(self):
192 return self.get_config_string("xend-relocation-hosts-allow",
193 self.xend_relocation_hosts_allow_default)
195 def get_xend_address(self):
196 """Get the address xend listens at for its HTTP port.
197 This defaults to the empty string which allows all hosts to connect.
198 If this is set to 'localhost' only the localhost will be able to connect
199 to the HTTP port.
200 """
201 return self.get_config_string('xend-address', self.xend_address_default)
203 def get_xend_relocation_address(self):
204 """Get the address xend listens at for its relocation server port.
205 This defaults to the empty string which allows all hosts to connect.
206 If this is set to 'localhost' only the localhost will be able to connect
207 to the relocation port.
208 """
209 return self.get_config_string('xend-relocation-address', self.xend_relocation_address_default)
211 def get_xend_unix_server(self):
212 """Get the flag indicating whether xend should run a unix-domain server.
213 """
214 return self.get_config_bool("xend-unix-server", self.xend_unix_server_default)
216 def get_xend_unix_path(self):
217 """Get the path the xend unix-domain server listens at.
218 """
219 return self.get_config_string("xend-unix-path", self.xend_unix_path_default)
221 def get_xend_domains_path(self):
222 """ Get the path for persistent domain configuration storage
223 """
224 return self.get_config_string("xend-domains-path", self.xend_domains_path_default)
226 def get_xend_state_path(self):
227 """ Get the path for persistent domain configuration storage
228 """
229 return self.get_config_string("xend-state-path", self.xend_state_path_default)
231 def get_xend_storage_path(self):
232 """ Get the path for persistent domain configuration storage
233 """
234 return self.get_config_string("xend-storage-path", self.xend_storage_path_default)
236 def get_network_script(self):
237 """@return the script used to alter the network configuration when
238 Xend starts and stops, or None if no such script is specified."""
240 s = self.get_config_string('network-script')
242 if s:
243 result = s.split(" ")
244 result[0] = os.path.join(self.network_script_dir, result[0])
245 return result
246 else:
247 return None
249 def get_external_migration_tool(self):
250 """@return the name of the tool to handle virtual TPM migration."""
251 return self.get_config_string('external-migration-tool', self.external_migration_tool_default)
253 def get_enable_dump(self):
254 return self.get_config_bool('enable-dump', 'no')
256 def get_vif_script(self):
257 return self.get_config_string('vif-script', self.vif_script)
259 def get_dom0_min_mem(self):
260 return self.get_config_int('dom0-min-mem', self.dom0_min_mem_default)
262 def get_dom0_vcpus(self):
263 return self.get_config_int('dom0-cpus', self.dom0_vcpus_default)
265 def get_console_limit(self):
266 return self.get_config_int('console-limit', 1024)
268 def get_vnclisten_address(self):
269 return self.get_config_string('vnc-listen', self.xend_vnc_listen_default)
271 def get_vncpasswd_default(self):
272 return self.get_config_string('vncpasswd',
273 self.vncpasswd_default)
275 class XendOptionsFile(XendOptions):
277 """Default path to the config file."""
278 config_default = "/etc/xen/xend-config.sxp"
280 """Environment variable used to override config_default."""
281 config_var = "XEND_CONFIG"
283 def set_config(self):
284 """If the config file exists, read it. If not, ignore it.
286 The config file is a sequence of sxp forms.
287 """
288 self.config_path = os.getenv(self.config_var, self.config_default)
289 if os.path.exists(self.config_path):
290 try:
291 fin = file(self.config_path, 'rb')
292 try:
293 config = sxp.parse(fin)
294 finally:
295 fin.close()
296 if config is None:
297 config = ['xend-config']
298 else:
299 config.insert(0, 'xend-config')
300 self.config = config
301 except Exception, ex:
302 self._logError('Reading config file %s: %s',
303 self.config_path, str(ex))
304 raise
305 else:
306 self._logError('Config file does not exist: %s',
307 self.config_path)
308 self.config = ['xend-config']
310 def get_config_value(self, name, val=None):
311 """Get the value of an atomic configuration element.
313 @param name: element name
314 @param val: default value (optional, defaults to None)
315 @return: value
316 """
317 return sxp.child_value(self.config, name, val=val)
319 def get_config_bool(self, name, val=None):
320 v = string.lower(str(self.get_config_value(name, val)))
321 if v in ['yes', 'y', '1', 'on', 'true', 't']:
322 return True
323 if v in ['no', 'n', '0', 'off', 'false', 'f']:
324 return False
325 raise XendError("invalid xend config %s: expected bool: %s" % (name, v))
327 def get_config_int(self, name, val=None):
328 v = self.get_config_value(name, val)
329 try:
330 return int(v)
331 except Exception:
332 raise XendError("invalid xend config %s: expected int: %s" % (name, v))
334 def get_config_string(self, name, val=None):
335 return self.get_config_value(name, val)
337 def get_xen_api_server(self):
338 """Get the Xen-API server configuration.
339 """
340 return self.get_config_value('xen-api-server',
341 self.xen_api_server_default)
343 if os.uname()[0] == 'SunOS':
344 class XendOptionsSMF(XendOptions):
346 def set_config(self):
347 pass
349 def get_config_bool(self, name, val=None):
350 try:
351 return scf.get_bool(name)
352 except scf.error, e:
353 if e[0] == scf.SCF_ERROR_NOT_FOUND:
354 return val
355 else:
356 raise XendError("option %s: %s:%s" % (name, e[1], e[2]))
358 def get_config_int(self, name, val=None):
359 try:
360 return scf.get_int(name)
361 except scf.error, e:
362 if e[0] == scf.SCF_ERROR_NOT_FOUND:
363 return val
364 else:
365 raise XendError("option %s: %s:%s" % (name, e[1], e[2]))
367 def get_config_string(self, name, val=None):
368 try:
369 return scf.get_string(name)
370 except scf.error, e:
371 if e[0] == scf.SCF_ERROR_NOT_FOUND:
372 return val
373 else:
374 raise XendError("option %s: %s:%s" % (name, e[1], e[2]))
376 def get_xen_api_server(self):
377 # When the new server is a supported configuration, we should
378 # expand this.
379 return [["unix"]]
381 def instance():
382 """Get an instance of XendOptions.
383 Use this instead of the constructor.
384 """
385 global inst
386 try:
387 inst
388 except:
389 if os.uname()[0] == 'SunOS':
390 inst = XendOptionsSMF()
391 else:
392 inst = XendOptionsFile()
393 return inst