ia64/xen-unstable

view tools/python/xen/xend/XendRoot.py @ 12725:36fe7ca48e54

Tidy up the creation of directories that Xend needs. This avoids potential
races in this creation.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Fri Dec 01 11:32:32 2006 +0000 (2006-12-01)
parents 6206685650f5
children ae3f3dd40df4
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 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 from xen.xend import sxp, osdep, XendLogging
34 from xen.xend.XendError import XendError
36 class XendRoot:
37 """Root of the management classes."""
39 """Default path to the config file."""
40 config_default = "/etc/xen/xend-config.sxp"
42 """Environment variable used to override config_default."""
43 config_var = "XEND_CONFIG"
45 """Where network control scripts live."""
46 network_script_dir = osdep.scripts_dir
48 """Where block control scripts live."""
49 block_script_dir = osdep.scripts_dir
51 """Default path to the log file. """
52 logfile_default = "/var/log/xen/xend.log"
54 """Default level of information to be logged."""
55 loglevel_default = 'DEBUG'
57 """Default Xen-API server configuration. """
58 xen_api_server_default = [['unix']]
60 """Default for the flag indicating whether xend should run an http server
61 (deprecated)."""
62 xend_http_server_default = 'no'
64 xend_tcp_xmlrpc_server_default = 'no'
66 xend_unix_xmlrpc_server_default = 'yes'
68 """Default interface address xend listens at. """
69 xend_address_default = ''
71 """Default for the flag indicating whether xend should run a relocation server."""
72 xend_relocation_server_default = 'no'
74 """Default interface address the xend relocation server listens at. """
75 xend_relocation_address_default = ''
77 """Default port xend serves HTTP at. """
78 xend_port_default = '8000'
80 """Default port xend serves relocation at. """
81 xend_relocation_port_default = '8002'
83 xend_relocation_hosts_allow_default = ''
85 """Default for the flag indicating whether xend should run a unix-domain
86 server (deprecated)."""
87 xend_unix_server_default = 'no'
89 """Default external migration tool """
90 external_migration_tool_default = ''
92 """Default path the unix-domain server listens at."""
93 xend_unix_path_default = '/var/lib/xend/xend-socket'
95 dom0_min_mem_default = '0'
97 dom0_vcpus_default = '0'
99 vncpasswd_default = None
101 """Default interface to listen for VNC connections on"""
102 xend_vnc_listen_default = '127.0.0.1'
104 """Default session storage path."""
105 xend_domains_path_default = '/var/lib/xend/domains'
107 components = {}
109 def __init__(self):
110 self.config_path = None
111 self.config = None
112 self.configure()
115 def _logError(self, fmt, *args):
116 """Logging function to log to stderr. We use this for XendRoot log
117 messages because they may be logged before the logger has been
118 configured. Other components can safely use the logger.
119 """
120 print >>sys.stderr, "xend [ERROR]", fmt % args
123 def configure(self):
124 self.set_config()
125 XendLogging.init(self.get_config_value("logfile",
126 self.logfile_default),
127 self.get_config_value("loglevel",
128 self.loglevel_default))
131 def set_config(self):
132 """If the config file exists, read it. If not, ignore it.
134 The config file is a sequence of sxp forms.
135 """
136 self.config_path = os.getenv(self.config_var, self.config_default)
137 if os.path.exists(self.config_path):
138 try:
139 fin = file(self.config_path, 'rb')
140 try:
141 config = sxp.parse(fin)
142 finally:
143 fin.close()
144 if config is None:
145 config = ['xend-config']
146 else:
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:
193 raise XendError("invalid xend config %s: expected int: %s" % (name, v))
195 def get_xen_api_server(self):
196 """Get the Xen-API server configuration.
197 """
198 return self.get_config_value('xen-api-server',
199 self.xen_api_server_default)
201 def get_xend_http_server(self):
202 """Get the flag indicating whether xend should run an http server.
203 """
204 return self.get_config_bool("xend-http-server", self.xend_http_server_default)
206 def get_xend_tcp_xmlrpc_server(self):
207 return self.get_config_bool("xend-tcp-xmlrpc-server",
208 self.xend_tcp_xmlrpc_server_default)
210 def get_xend_unix_xmlrpc_server(self):
211 return self.get_config_bool("xend-unix-xmlrpc-server",
212 self.xend_unix_xmlrpc_server_default)
214 def get_xend_relocation_server(self):
215 """Get the flag indicating whether xend should run a relocation server.
216 """
217 return self.get_config_bool("xend-relocation-server",
218 self.xend_relocation_server_default)
220 def get_xend_port(self):
221 """Get the port xend listens at for its HTTP interface.
222 """
223 return self.get_config_int('xend-port', self.xend_port_default)
225 def get_xend_relocation_port(self):
226 """Get the port xend listens at for connection to its relocation server.
227 """
228 return self.get_config_int('xend-relocation-port',
229 self.xend_relocation_port_default)
231 def get_xend_relocation_hosts_allow(self):
232 return self.get_config_value("xend-relocation-hosts-allow",
233 self.xend_relocation_hosts_allow_default)
235 def get_xend_address(self):
236 """Get the address xend listens at for its HTTP port.
237 This defaults to the empty string which allows all hosts to connect.
238 If this is set to 'localhost' only the localhost will be able to connect
239 to the HTTP port.
240 """
241 return self.get_config_value('xend-address', self.xend_address_default)
243 def get_xend_relocation_address(self):
244 """Get the address xend listens at for its relocation server port.
245 This defaults to the empty string which allows all hosts to connect.
246 If this is set to 'localhost' only the localhost will be able to connect
247 to the relocation port.
248 """
249 return self.get_config_value('xend-relocation-address', self.xend_relocation_address_default)
251 def get_xend_unix_server(self):
252 """Get the flag indicating whether xend should run a unix-domain server.
253 """
254 return self.get_config_bool("xend-unix-server", self.xend_unix_server_default)
256 def get_xend_unix_path(self):
257 """Get the path the xend unix-domain server listens at.
258 """
259 return self.get_config_value("xend-unix-path", self.xend_unix_path_default)
261 def get_xend_domains_path(self):
262 """ Get the path for persistent domain configuration storage
263 """
264 return self.get_config_value("xend-domains-path", self.xend_domains_path_default)
266 def get_network_script(self):
267 """@return the script used to alter the network configuration when
268 Xend starts and stops, or None if no such script is specified."""
270 s = self.get_config_value('network-script')
272 if s:
273 result = s.split(" ")
274 result[0] = os.path.join(self.network_script_dir, result[0])
275 return result
276 else:
277 return None
279 def get_external_migration_tool(self):
280 """@return the name of the tool to handle virtual TPM migration."""
281 return self.get_config_value('external-migration-tool', self.external_migration_tool_default)
283 def get_enable_dump(self):
284 return self.get_config_bool('enable-dump', 'no')
286 def get_vif_script(self):
287 return self.get_config_value('vif-script', 'vif-bridge')
289 def get_dom0_min_mem(self):
290 return self.get_config_int('dom0-min-mem', self.dom0_min_mem_default)
292 def get_dom0_vcpus(self):
293 return self.get_config_int('dom0-cpus', self.dom0_vcpus_default)
295 def get_console_limit(self):
296 return self.get_config_int('console-limit', 1024)
298 def get_vnclisten_address(self):
299 return self.get_config_value('vnc-listen', self.xend_vnc_listen_default)
301 def get_vncpasswd_default(self):
302 return self.get_config_value('vncpasswd',
303 self.vncpasswd_default)
305 def instance():
306 """Get an instance of XendRoot.
307 Use this instead of the constructor.
308 """
309 global inst
310 try:
311 inst
312 except:
313 inst = XendRoot()
314 return inst