ia64/xen-unstable

view tools/python/xen/xend/XendNode.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 1c51c580dc05
children 765ada5f74cc
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) 2006 Xensource Inc.
17 #============================================================================
19 import os
20 import socket
21 import xen.lowlevel.xc
22 from xen.xend import uuid
23 from xen.xend.XendError import XendError
24 from xen.xend.XendStorageRepository import XendStorageRepository
26 class XendNode:
27 """XendNode - Represents a Domain 0 Host."""
29 def __init__(self):
30 self.xc = xen.lowlevel.xc.xc()
31 self.uuid = uuid.createString()
32 self.cpus = {}
33 self.name = socket.gethostname()
34 self.desc = ""
35 self.sr = XendStorageRepository()
37 physinfo = self.physinfo_dict()
38 cpu_count = physinfo['nr_cpus']
39 cpu_features = physinfo['hw_caps']
41 for i in range(cpu_count):
42 # construct uuid by appending extra bit on the host.
43 # since CPUs belong to a host.
44 cpu_uuid = self.uuid + '-%04d' % i
45 cpu_info = {'uuid': cpu_uuid,
46 'host': self.uuid,
47 'number': i,
48 'features': cpu_features}
49 self.cpus[cpu_uuid] = cpu_info
51 def shutdown(self):
52 return 0
54 def reboot(self):
55 return 0
57 def notify(self, _):
58 return 0
60 #
61 # Ref validation
62 #
64 def is_valid_host(self, host_ref):
65 return (host_ref == self.uuid)
67 def is_valid_cpu(self, cpu_ref):
68 return (cpu_ref in self.cpus)
70 #
71 # Storage Repo
72 #
74 def get_sr(self):
75 return self.sr
77 #
78 # Host Functions
79 #
81 def xen_version(self):
82 info = self.xc.xeninfo()
83 try:
84 from xen import VERSION
85 return {'Xen': '%(xen_major)d.%(xen_minor)d' % info,
86 'Xend': VERSION}
87 except (ImportError, AttributeError):
88 return {'Xen': '%(xen_major)d.%(xen_minor)d' % info,
89 'Xend': '3.0.3'}
91 def get_name(self):
92 return self.name
94 def set_name(self, new_name):
95 self.name = new_name
97 def get_description(self):
98 return self.desc
100 def set_description(self, new_desc):
101 self.desc = new_desc
103 #
104 # Host CPU Functions
105 #
107 def get_host_cpu_by_uuid(self, host_cpu_uuid):
108 if host_cpu_uuid in self.cpus:
109 return host_cpu_uuid
110 raise XendError('Invalid CPU UUID')
112 def get_host_cpu_refs(self):
113 return self.cpus.keys()
115 def get_host_cpu_uuid(self, host_cpu_ref):
116 if host_cpu_ref in self.cpus:
117 return host_cpu_ref
118 else:
119 raise XendError('Invalid CPU Reference')
121 def get_host_cpu_features(self, host_cpu_ref):
122 try:
123 return self.cpus[host_cpu_ref]['features']
124 except KeyError:
125 raise XendError('Invalid CPU Reference')
127 def get_host_cpu_number(self, host_cpu_ref):
128 try:
129 return self.cpus[host_cpu_ref]['number']
130 except KeyError:
131 raise XendError('Invalid CPU Reference')
133 def get_host_cpu_load(self, host_cpu_ref):
134 return 0.0
138 #
139 # Getting host information.
140 #
142 def info(self):
143 return (self.nodeinfo() + self.physinfo() + self.xeninfo() +
144 self.xendinfo())
146 def nodeinfo(self):
147 (sys, host, rel, ver, mch) = os.uname()
148 return [['system', sys],
149 ['host', host],
150 ['release', rel],
151 ['version', ver],
152 ['machine', mch]]
154 def physinfo(self):
155 info = self.xc.physinfo()
157 info['nr_cpus'] = (info['nr_nodes'] *
158 info['sockets_per_node'] *
159 info['cores_per_socket'] *
160 info['threads_per_core'])
161 info['cpu_mhz'] = info['cpu_khz'] / 1000
162 # physinfo is in KiB
163 info['total_memory'] = info['total_memory'] / 1024
164 info['free_memory'] = info['free_memory'] / 1024
166 ITEM_ORDER = ['nr_cpus',
167 'nr_nodes',
168 'sockets_per_node',
169 'cores_per_socket',
170 'threads_per_core',
171 'cpu_mhz',
172 'hw_caps',
173 'total_memory',
174 'free_memory',
175 ]
177 return [[k, info[k]] for k in ITEM_ORDER]
180 def xeninfo(self):
181 info = self.xc.xeninfo()
183 ITEM_ORDER = ['xen_major',
184 'xen_minor',
185 'xen_extra',
186 'xen_caps',
187 'xen_pagesize',
188 'platform_params',
189 'xen_changeset',
190 'cc_compiler',
191 'cc_compile_by',
192 'cc_compile_domain',
193 'cc_compile_date',
194 ]
196 return [[k, info[k]] for k in ITEM_ORDER]
198 def xendinfo(self):
199 return [['xend_config_format', 3]]
201 # dictionary version of *info() functions to get rid of
202 # SXPisms.
203 def nodeinfo_dict(self):
204 return dict(self.nodeinfo())
205 def xendinfo_dict(self):
206 return dict(self.xendinfo())
207 def xeninfo_dict(self):
208 return dict(self.xeninfo())
209 def physinfo_dict(self):
210 return dict(self.physinfo())
211 def info_dict(self):
212 return dict(self.info())
215 def instance():
216 global inst
217 try:
218 inst
219 except:
220 inst = XendNode()
221 return inst