2 Virt management features
4 Copyright 2007, Red Hat, Inc
5 Michael DeHaan <mdehaan@redhat.com>
7 This software may be freely redistributed under the terms of the GNU
8 general public license.
10 You should have received a copy of the GNU General Public License
11 along with this program; if not, write to the Free Software
12 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 # warning: virt management is rather complicated
16 # to see a simple example of func, look at the
17 # service control module. API docs on how
18 # to use this to come.
29 VIRT_STATE_NAME_MAP
= {
39 class FuncLibvirtConnection(object):
43 description
= "Virtualization items through func."
47 cmd
= sub_process
.Popen("uname -r", shell
=True, stdout
=sub_process
.PIPE
)
48 output
= cmd
.communicate()[0]
50 if output
.find("xen") != -1:
51 conn
= libvirt
.open(None)
53 conn
= libvirt
.open("qemu:///system")
56 raise codes
.FuncException("hypervisor connection failure")
60 def find_vm(self
, vmid
):
62 Extra bonus feature: vmid = -1 returns a list of everything
68 # this block of code borrowed from virt-manager:
69 # get working domain's name
70 ids
= conn
.listDomainsID();
72 vm
= conn
.lookupByID(id)
75 names
= conn
.listDefinedDomains()
77 vm
= conn
.lookupByName(name
)
87 raise codes
.FuncException("virtual machine %s not found" % vmid
)
89 def shutdown(self
, vmid
):
90 return self
.find_vm(vmid
).shutdown()
92 def pause(self
, vmid
):
93 return self
.suspend(self
.conn
,vmid
)
95 def unpause(self
, vmid
):
96 return self
.resume(self
.conn
,vmid
)
98 def suspend(self
, vmid
):
99 return self
.find_vm(vmid
).suspend()
101 def resume(self
, vmid
):
102 return self
.find_vm(vmid
).resume()
104 def create(self
, vmid
):
105 return self
.find_vm(vmid
).create()
107 def destroy(self
, vmid
):
108 return self
.find_vm(vmid
).destroy()
110 def undefine(self
, vmid
):
111 return self
.find_vm(vmid
).undefine()
113 def get_status2(self
, vm
):
115 # print "DEBUG: state: %s" % state
116 return VIRT_STATE_NAME_MAP
.get(state
,"unknown")
118 def get_status(self
, vmid
):
119 state
= self
.find_vm(vmid
).info()[0]
120 return VIRT_STATE_NAME_MAP
.get(state
,"unknown")
124 class Virt(func_module
.FuncModule
):
126 def __get_conn(self
):
127 self
.conn
= FuncLibvirtConnection()
131 vms
= self
.list_vms()
134 state_blurb
= self
.conn
.get_status(vm
)
135 state
.append("%s %s" % (vm
,state_blurb
))
140 vms
= self
.list_vms()
143 data
= self
.conn
.find_vm(vm
).info()
144 # libvirt returns maxMem, memory, and cpuTime as long()'s, which
145 # xmlrpclib tries to convert to regular int's during serialization.
146 # This throws exceptions, so convert them to strings here and
147 # assume the other end of the xmlrpc connection can figure things
148 # out or doesn't care.
150 "state" : VIRT_STATE_NAME_MAP
.get(data
[0],"unknown"),
151 "maxMem" : str(data
[1]),
152 "memory" : str(data
[2]),
153 "nrVirtCpu" : data
[3],
154 "cpuTime" : str(data
[4])
160 self
.conn
= self
.__get
_conn
()
161 vms
= self
.conn
.find_vm(-1)
165 results
.append(x
.name())
170 def install(self
, server_name
, target_name
, system
=False):
173 Install a new virt system by way of a named cobbler profile.
177 # install("bootserver.example.org", "fc7webserver", True)
179 conn
= self
.__get
_conn
()
182 raise codes
.FuncException("no connection")
184 if not os
.path
.exists("/usr/bin/koan"):
185 raise codes
.FuncException("no /usr/bin/koan")
190 # TODO: FUTURE: set --virt-path in cobbler or here
194 "--virt-graphics", # enable VNC
195 "--%s=%s" % (target
, target_name
),
196 "--server=%s" % server_name
199 rc
= sub_process
.call(koan_args
,shell
=False)
203 raise codes
.FuncException("koan returned %d" % rc
)
206 def shutdown(self
, vmid
):
208 Make the machine with the given vmid stop running.
212 self
.conn
.shutdown(vmid
)
216 def pause(self
, vmid
):
219 Pause the machine with the given vmid.
222 self
.conn
.suspend(vmid
)
226 def unpause(self
, vmid
):
229 Unpause the machine with the given vmid.
233 self
.conn
.resume(vmid
)
237 def create(self
, vmid
):
240 Start the machine via the given mac address.
243 self
.conn
.create(vmid
)
247 def destroy(self
, vmid
):
250 Pull the virtual power from the virtual domain, giving it virtually no
251 time to virtually shut down.
254 self
.conn
.destroy(vmid
)
258 def undefine(self
, vmid
):
261 Stop a domain, and then wipe it from the face of the earth.
262 by deleting the disk image and it's configuration file.
266 self
.conn
.undefine(vmid
)
270 def get_status(self
, vmid
):
273 Return a state suitable for server consumption. Aka, codes.py values, not XM output.
277 return self
.conn
.get_status(vmid
)