ia64/xen-unstable
changeset 14432:cd8d2a4d46e4
Implement xm list through the Xen-API.
Signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
Signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
author | Ewan Mellor <ewan@xensource.com> |
---|---|
date | Thu Mar 15 23:03:22 2007 +0000 (2007-03-15) |
parents | 3c418dbb71b2 |
children | 487e37c22b28 |
files | tools/python/xen/xm/main.py |
line diff
1.1 --- a/tools/python/xen/xm/main.py Thu Mar 15 22:15:12 2007 +0000 1.2 +++ b/tools/python/xen/xm/main.py Thu Mar 15 23:03:22 2007 +0000 1.3 @@ -49,10 +49,9 @@ from xen.xend.XendConstants import * 1.4 1.5 from xen.xm.opts import OptionError, Opts, wrap, set_true 1.6 from xen.xm import console 1.7 -from xen.util import security 1.8 from xen.util.xmlrpclib2 import ServerProxy 1.9 +import XenAPI 1.10 1.11 -import XenAPI 1.12 1.13 # getopt.gnu_getopt is better, but only exists in Python 2.3+. Use 1.14 # getopt.getopt if gnu_getopt is not available. This will mean that options 1.15 @@ -675,10 +674,42 @@ def xm_restore(args): 1.16 1.17 1.18 def getDomains(domain_names, state, full = 0): 1.19 - if domain_names: 1.20 - return [server.xend.domain(dom, full) for dom in domain_names] 1.21 + if serverType == SERVER_XEN_API: 1.22 + def map2sxp(m): 1.23 + return ["domain"] + [[k, m[k]] for k in m.keys()] 1.24 + 1.25 + doms_sxp = [] 1.26 + doms_dict = [] 1.27 + dom_refs = server.xenapi.VM.get_all() 1.28 + for dom_ref in dom_refs: 1.29 + dom_rec = server.xenapi.VM.get_record(dom_ref) 1.30 + dom_metrics_ref = server.xenapi.VM.get_metrics(dom_ref) 1.31 + dom_metrics = server.xenapi.VM_metrics.get_record(dom_metrics_ref) 1.32 + dom_rec.update({'name': dom_rec['name_label'], 1.33 + 'memory_actual': int(dom_metrics['memory_actual'])/1024, 1.34 + 'vcpus': dom_metrics['vcpus_number'], 1.35 + 'state': '-----', 1.36 + 'cpu_time': dom_metrics['vcpus_utilisation']}) 1.37 + 1.38 + doms_sxp.append(map2sxp(dom_rec)) 1.39 + doms_dict.append(dom_rec) 1.40 + 1.41 + if domain_names: 1.42 + doms = [map2sxp(dom) for dom in doms_dict 1.43 + if dom["name"] in domain_names] 1.44 + 1.45 + if len(doms) > 0: 1.46 + return doms 1.47 + else: 1.48 + print "Error: no domains named '%s'" % domain_names 1.49 + sys.exit(-1) 1.50 + else: 1.51 + return doms_sxp 1.52 else: 1.53 - return server.xend.domains_with_state(True, state, full) 1.54 + if domain_names: 1.55 + return [server.xend.domain(dom, full) for dom in domain_names] 1.56 + else: 1.57 + return server.xend.domains_with_state(True, state, full) 1.58 1.59 1.60 def xm_list(args): 1.61 @@ -737,20 +768,39 @@ def parse_doms_info(info): 1.62 else: 1.63 up_time = time.time() - start_time 1.64 1.65 - return { 1.66 + parsed_info = { 1.67 'domid' : get_info('domid', str, ''), 1.68 'name' : get_info('name', str, '??'), 1.69 - 'mem' : get_info('memory_dynamic_min', int, 0), 1.70 'state' : get_info('state', str, ''), 1.71 - 'cpu_time' : get_info('cpu_time', float, 0.0), 1.72 + 1.73 # VCPUs is the number online when the VM is up, or the number 1.74 # configured otherwise. 1.75 'vcpus' : get_info('online_vcpus', int, 1.76 get_info('vcpus', int, 0)), 1.77 - 'up_time' : up_time, 1.78 - 'seclabel' : security.get_security_printlabel(info), 1.79 + 'up_time' : up_time 1.80 } 1.81 1.82 + # We're not supporting security stuff just yet via XenAPI 1.83 + 1.84 + if serverType != SERVER_XEN_API: 1.85 + from xen.util import security 1.86 + parsed_info['seclabel'] = security.get_security_printlabel(info) 1.87 + else: 1.88 + parsed_info['seclabel'] = "" 1.89 + 1.90 + if serverType == SERVER_XEN_API: 1.91 + parsed_info['mem'] = get_info('memory_actual', int, 0) / 1024 1.92 + cpu_times = get_info('cpu_time', lambda x : (x), 0.0) 1.93 + if sum(cpu_times.values()) > 0: 1.94 + parsed_info['cpu_time'] = sum(cpu_times.values()) / float(len(cpu_times.values())) 1.95 + else: 1.96 + parsed_info['cpu_time'] = 0 1.97 + else: 1.98 + parsed_info['mem'] = get_info('memory_dynamic_min', int,0) 1.99 + parsed_info['cpu_time'] = get_info('cpu_time', float, 0.0) 1.100 + 1.101 + return parsed_info 1.102 + 1.103 def check_sched_type(sched): 1.104 if serverType == SERVER_XEN_API: 1.105 current = server.xenapi.host.get_sched_policy(server.xenapi.session.get_this_host()) 1.106 @@ -799,17 +849,22 @@ def xm_label_list(doms): 1.107 output = [] 1.108 format = '%(name)-32s %(domid)3s %(mem)5d %(vcpus)5d %(state)10s ' \ 1.109 '%(cpu_time)8.1f %(seclabel)9s' 1.110 - 1.111 - for dom in doms: 1.112 - d = parse_doms_info(dom) 1.113 - if security.active_policy not in ['INACTIVE', 'NULL', 'DEFAULT']: 1.114 - if not d['seclabel']: 1.115 - d['seclabel'] = 'ERROR' 1.116 - elif security.active_policy in ['DEFAULT']: 1.117 - d['seclabel'] = 'DEFAULT' 1.118 - else: 1.119 - d['seclabel'] = 'INACTIVE' 1.120 - output.append((format % d, d['seclabel'])) 1.121 + 1.122 + if serverType != SERVER_XEN_API: 1.123 + from xen.util import security 1.124 + 1.125 + for dom in doms: 1.126 + d = parse_doms_info(dom) 1.127 + 1.128 + if security.active_policy not in ['INACTIVE', 'NULL', 'DEFAULT']: 1.129 + if not d['seclabel']: 1.130 + d['seclabel'] = 'ERROR' 1.131 + elif security.active_policy in ['DEFAULT']: 1.132 + d['seclabel'] = 'DEFAULT' 1.133 + else: 1.134 + d['seclabel'] = 'INACTIVE' 1.135 + 1.136 + output.append((format % d, d['seclabel'])) 1.137 1.138 #sort by labels 1.139 output.sort(lambda x,y: cmp( x[1].lower(), y[1].lower())) 1.140 @@ -818,7 +873,6 @@ def xm_label_list(doms): 1.141 1.142 1.143 def xm_vcpu_list(args): 1.144 - 1.145 if args: 1.146 dominfo = map(server.xend.domain.getVCPUInfo, args) 1.147 else: 1.148 @@ -1114,7 +1168,7 @@ def xm_mem_set(args): 1.149 dom = args[0] 1.150 1.151 if serverType == SERVER_XEN_API: 1.152 - mem_target = int_unit(args[1], 'k') * 1024 1.153 + mem_target = int_unit(args[1], 'm') * 1024 * 1024 1.154 server.xenapi.VM.set_memory_dynamic_max(get_single_vm(dom), mem_target) 1.155 server.xenapi.VM.set_memory_dynamic_min(get_single_vm(dom), mem_target) 1.156 else: 1.157 @@ -1695,13 +1749,17 @@ def parse_block_configuration(args): 1.158 if len(args) == 5: 1.159 vbd.append(['backend', args[4]]) 1.160 1.161 - # verify that policy permits attaching this resource 1.162 - if security.on(): 1.163 - dominfo = server.xend.domain(dom) 1.164 - label = security.get_security_printlabel(dominfo) 1.165 - else: 1.166 - label = None 1.167 - security.res_security_check(args[1], label) 1.168 + if serverType != SERVER_XEN_API: 1.169 + # verify that policy permits attaching this resource 1.170 + from xen.util import security 1.171 + 1.172 + if security.on(): 1.173 + dominfo = server.xend.domain(dom) 1.174 + label = security.get_security_printlabel(dominfo) 1.175 + else: 1.176 + label = None 1.177 + 1.178 + security.res_security_check(args[1], label) 1.179 1.180 return (dom, vbd) 1.181 1.182 @@ -2006,13 +2064,17 @@ def _run_cmd(cmd, cmd_name, args): 1.183 err(str(e)) 1.184 _usage(cmd_name) 1.185 print e.usage 1.186 - except security.ACMError, e: 1.187 - err(str(e)) 1.188 - except: 1.189 - print "Unexpected error:", sys.exc_info()[0] 1.190 - print 1.191 - print "Please report to xen-devel@lists.xensource.com" 1.192 - raise 1.193 + except Exception, e: 1.194 + if serverType != SERVER_XEN_API: 1.195 + from xen.util import security 1.196 + if isinstance(e, security.ACMError): 1.197 + err(str(e)) 1.198 + return False, 1 1.199 + else: 1.200 + print "Unexpected error:", sys.exc_info()[0] 1.201 + print 1.202 + print "Please report to xen-devel@lists.xensource.com" 1.203 + raise 1.204 1.205 return False, 1 1.206