Commit 21d399b7 by tarokkk

remove VMDriver class

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