Commit 21d399b7 by tarokkk

remove VMDriver class

parent 24530205
......@@ -4,46 +4,57 @@ import libvirt
import logging
class VMDriver:
'''Circle Virtal Machin driver main class
'''
connection = None
connection = None
def req_connection(original_function):
def req_connection(original_function):
'''Connection checking decorator for libvirt.
'''
def new_function(*args, **kwargs):
if args[0].connection is None:
logging.error("No connection to libvirt daemon.")
global connection
if connection is None:
connection = connect()
else:
return original_function(*args, **kwargs)
return_value = original_function(*args, **kwargs)
disconnect()
return return_value
return new_function
def connect(self, connection_string='qemu:///system'):
def connect(connection_string='qemu:///system'):
'''Connect to the libvirt daemon specified in the
connection_string or the local root.
'''
if self.connection is None:
self.connection = libvirt.open(connection_string)
global connection
if connection is None:
connection = libvirt.open(connection_string)
logging.debug("Connection estabilished to libvirt.")
else:
logging.error("There is already an active connection to libvirt.")
@req_connection
def disconnect(self, connection_string='qemu:///system'):
def disconnect():
'''Disconnect from the active libvirt daemon connection.
'''
self.connection.close()
self.connection = None
global connection
if connection is None:
logging.debug('There is no available libvirt conection.')
else:
connection.close()
logging.debug('Connection closed to libvirt.')
connection = None
@req_connection
def vm_define(self, vm):
@req_connection
def define(vm):
'''Define permanent virtual machine from xml
'''
self.connection.defineXML(vm.dump_xml())
connection.defineXML(vm.dump_xml())
logging.info("Virtual machine %s is defined from xml", vm.name)
@req_connection
def vm_create(self, vm):
@req_connection
def create(self, vm):
'''Create and start non-permanent virtual machine from xml
flags can be:
VIR_DOMAIN_NONE = 0
......@@ -55,66 +66,75 @@ class VMDriver:
self.connection.createXML(vm.dump_xml(), libvirt.VIR_DOMAIN_NONE)
logging.info("Virtual machine %s is created from xml", vm.name)
@req_connection
def vm_delete(self, vm):
@req_connection
def delete(name):
'''Destroy the running called 'name' virtual machine.
'''
domain = self.lookupByName(vm.name)
domain = lookupByName(name)
domain.destroy()
@req_connection
def list_domains(self):
return self.connection.listDefinedDomains()
@req_connection
def lookupByName(self, name):
@req_connection
def list_domains():
return connection.listDefinedDomains()
@req_connection
def lookupByName(name):
'''Return with the requested Domain
'''
try:
return self.connection.lookupByName(name)
return connection.lookupByName(name)
except libvirt.libvirtError as e:
logging.error(e.get_error_message())
@req_connection
def vm_undefine(self, name):
@req_connection
def undefine(name):
'''Undefine an already defined virtual machine.
If it's running it becomes transient (lsot on reboot)
'''
domain = self.lookupByName(name)
try:
domain = lookupByName(name)
domain.undefine()
except:
logging.error('Can not get VM with name %s', name)
@req_connection
def vm_start(self, name):
@req_connection
def start(self, name):
'''Start an already defined virtual machine.
'''
domain = self.lookupByName(name)
domain.create()
@req_connection
def vm_save(self, name, path):
@req_connection
def save(self, name, path):
'''Stop virtual machine and save its memory to path.
'''
domain = self.lookupByName(name)
domain.save(path)
def vm_resume(self, name):
@req_connection
def resume(self, name):
'''Resume stopped virtual machines.
'''
domain = self.lookupByName(name)
domain.resume()
def vm_reset(self, name):
@req_connection
def reset(self, name):
'''Reset (power reset) virtual machine.
'''
domain = self.lookupByName(name)
domain.reset()
def vm_reboot(self, name):
@req_connection
def reboot(self, name):
'''Reboot (with guest acpi support) virtual machine.
'''
domain = self.lookupByName(name)
domain.reboot()
#virDomainResume
# virDomainResume
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment