ia64/xen-unstable

view tools/python/xen/xend/XendRoot.py @ 9425:b55ca95fdc84

Added example config entries for xend-tcp-xmlrpc-server and
xend-unix-xmlrpc-server. Deprecate the SXP-based servers, and disable them
by default.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Thu Mar 23 11:55:56 2006 +0100 (2006-03-23)
parents 5c9c44fc1c39
children 1fe63743a147
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 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 (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 path the unix-domain server listens at."""
90 xend_unix_path_default = '/var/lib/xend/xend-socket'
92 dom0_min_mem_default = '0'
94 dom0_vcpus_default = '0'
96 components = {}
98 def __init__(self):
99 self.config_path = None
100 self.config = None
101 self.configure()
104 def _logError(self, fmt, *args):
105 """Logging function to log to stderr. We use this for XendRoot log
106 messages because they may be logged before the logger has been
107 configured. Other components can safely use the logger.
108 """
109 print >>sys.stderr, "xend [ERROR]", fmt % args
112 def configure(self):
113 self.set_config()
114 XendLogging.init(self.get_config_value("logfile",
115 self.logfile_default),
116 self.get_config_value("loglevel",
117 self.loglevel_default))
120 def set_config(self):
121 """If the config file exists, read it. If not, ignore it.
123 The config file is a sequence of sxp forms.
124 """
125 self.config_path = os.getenv(self.config_var, self.config_default)
126 if os.path.exists(self.config_path):
127 try:
128 fin = file(self.config_path, 'rb')
129 try:
130 config = sxp.parse(fin)
131 finally:
132 fin.close()
133 if config is None:
134 config = ['xend-config']
135 else:
136 config.insert(0, 'xend-config')
137 self.config = config
138 except Exception, ex:
139 self._logError('Reading config file %s: %s',
140 self.config_path, str(ex))
141 raise
142 else:
143 self._logError('Config file does not exist: %s',
144 self.config_path)
145 self.config = ['xend-config']
147 def get_config(self, name=None):
148 """Get the configuration element with the given name, or
149 the whole configuration if no name is given.
151 @param name: element name (optional)
152 @return: config or none
153 """
154 if name is None:
155 val = self.config
156 else:
157 val = sxp.child(self.config, name)
158 return val
160 def get_config_value(self, name, val=None):
161 """Get the value of an atomic configuration element.
163 @param name: element name
164 @param val: default value (optional, defaults to None)
165 @return: value
166 """
167 return sxp.child_value(self.config, name, val=val)
169 def get_config_bool(self, name, val=None):
170 v = string.lower(str(self.get_config_value(name, val)))
171 if v in ['yes', 'y', '1', 'on', 'true', 't']:
172 return True
173 if v in ['no', 'n', '0', 'off', 'false', 'f']:
174 return False
175 raise XendError("invalid xend config %s: expected bool: %s" % (name, v))
177 def get_config_int(self, name, val=None):
178 v = self.get_config_value(name, val)
179 try:
180 return int(v)
181 except Exception:
182 raise XendError("invalid xend config %s: expected int: %s" % (name, v))
184 def get_xend_http_server(self):
185 """Get the flag indicating whether xend should run an http server.
186 """
187 return self.get_config_bool("xend-http-server", self.xend_http_server_default)
189 def get_xend_tcp_xmlrpc_server(self):
190 return self.get_config_bool("xend-tcp-xmlrpc-server", self.xend_tcp_xmlrpc_server_default)
192 def get_xend_unix_xmlrpc_server(self):
193 return self.get_config_bool("xend-unix-xmlrpc-server", self.xend_unix_xmlrpc_server_default)
195 def get_xend_relocation_server(self):
196 """Get the flag indicating whether xend should run a relocation server.
197 """
198 return self.get_config_bool("xend-relocation-server", self.xend_relocation_server_default)
200 def get_xend_port(self):
201 """Get the port xend listens at for its HTTP interface.
202 """
203 return self.get_config_int('xend-port', self.xend_port_default)
205 def get_xend_relocation_port(self):
206 """Get the port xend listens at for connection to its relocation server.
207 """
208 return self.get_config_int('xend-relocation-port', self.xend_relocation_port_default)
210 def get_xend_relocation_hosts_allow(self):
211 return self.get_config_value("xend-relocation-hosts-allow",
212 self.xend_relocation_hosts_allow_default)
214 def get_xend_address(self):
215 """Get the address xend listens at for its HTTP port.
216 This defaults to the empty string which allows all hosts to connect.
217 If this is set to 'localhost' only the localhost will be able to connect
218 to the HTTP port.
219 """
220 return self.get_config_value('xend-address', self.xend_address_default)
222 def get_xend_relocation_address(self):
223 """Get the address xend listens at for its relocation server port.
224 This defaults to the empty string which allows all hosts to connect.
225 If this is set to 'localhost' only the localhost will be able to connect
226 to the relocation port.
227 """
228 return self.get_config_value('xend-relocation-address', self.xend_relocation_address_default)
230 def get_xend_unix_server(self):
231 """Get the flag indicating whether xend should run a unix-domain server.
232 """
233 return self.get_config_bool("xend-unix-server", self.xend_unix_server_default)
235 def get_xend_unix_path(self):
236 """Get the path the xend unix-domain server listens at.
237 """
238 return self.get_config_value("xend-unix-path", self.xend_unix_path_default)
240 def get_network_script(self):
241 """@return the script used to alter the network configuration when
242 Xend starts and stops, or None if no such script is specified."""
244 s = self.get_config_value('network-script')
246 if s:
247 result = s.split(" ")
248 result[0] = os.path.join(self.network_script_dir, result[0])
249 return result
250 else:
251 return None
254 def get_enable_dump(self):
255 return self.get_config_bool('enable-dump', 'no')
257 def get_vif_script(self):
258 return self.get_config_value('vif-script', 'vif-bridge')
260 def get_dom0_min_mem(self):
261 return self.get_config_int('dom0-min-mem', self.dom0_min_mem_default)
263 def get_dom0_vcpus(self):
264 return self.get_config_int('dom0-cpus', self.dom0_vcpus_default)
266 def get_console_limit(self):
267 return self.get_config_int('console-limit', 1024)
269 def instance():
270 """Get an instance of XendRoot.
271 Use this instead of the constructor.
272 """
273 global inst
274 try:
275 inst
276 except:
277 inst = XendRoot()
278 return inst