Commit 21d399b7 by tarokkk

remove VMDriver class

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