From 7005787e231883c599bb66316d59b607bdc265a4 Mon Sep 17 00:00:00 2001 From: Jonatan Schlag Date: Sun, 29 Apr 2018 10:06:53 +0200 Subject: [PATCH] Open only one connection to the libvirt daemon Signed-off-by: Jonatan Schlag --- machine.py | 6 ++---- network.py | 4 ++-- test.py | 28 ---------------------------- virtual_environ.py | 13 ++++++++++++- 4 files changed, 16 insertions(+), 35 deletions(-) diff --git a/machine.py b/machine.py index 035c010..a67cbcb 100644 --- a/machine.py +++ b/machine.py @@ -6,16 +6,14 @@ from disk import disk from serial_connection import serial_connection -from test import libvirt_con - import os import libvirt class machine(): - def __init__(self, vm_xml_file, snapshot_xml_file, image, root_uid, username, password): + def __init__(self, libvirt_con, vm_xml_file, snapshot_xml_file, image, root_uid, username, password): self.log = log(4) - self.con = libvirt_con("qemu:///system") + self.con = libvirt_con try: with open(vm_xml_file) as fobj: self.vm_xml = fobj.read() diff --git a/network.py b/network.py index 223930b..cd684e9 100644 --- a/network.py +++ b/network.py @@ -2,9 +2,9 @@ # # A class which define and undefine a virtual network based on an xml file class network(): - def __init__(self, network_xml_file): + def __init__(self, libvirt_con, network_xml_file): self.log = log(4) - self.con = libvirt_con("qemu:///system") + self.con = libvirt_con try: with open(network_xml_file) as fobj: self.network_xml = fobj.read() diff --git a/test.py b/test.py index 67f824e..89fe75c 100755 --- a/test.py +++ b/test.py @@ -20,34 +20,6 @@ class log(): def error(self, string): print("ERROR: {}".format(string)) -class libvirt_con(): - def __init__(self, uri): - self.log = log(4) - self.uri = uri - self.connection = None - - def get_domain_from_name(self, name): - dom = self.con.lookupByName(name) - - if dom == None: - raise BaseException - return dom - - @property - def con(self): - if self.connection == None: - try: - self.connection = libvirt.open(self.uri) - except BaseException as error: - self.log.error("Could not connect to: {}".format(self.uri)) - - self.log.debug("Connected to: {}".format(self.uri)) - return self.connection - - return self.connection - - - class test(): def __init__(self, path): self.log = log(4) diff --git a/virtual_environ.py b/virtual_environ.py index aaa07f1..d857072 100644 --- a/virtual_environ.py +++ b/virtual_environ.py @@ -6,6 +6,7 @@ from network import network import os import configparser +import libvirt # Should return all vms and networks in a list # and should provide the path to the necessary xml files @@ -41,11 +42,20 @@ class virtual_environ(): self.log.debug(self.machines) self.log.debug(self.networks) + self.uri = self.config["DEFAULT"]["uri"] + + try: + self.con = libvirt.open(self.uri) + except BaseException as error: + self.log.error("Could not connect to: {}".format(self.uri)) + + self.log.debug("Connected to: {}".format(self.uri)) + def get_networks(self): networks = {} for _network in self.networks: self.log.debug(_network) - networks.setdefault(_network, network(os.path.normpath(self.path + "/" + self.config[_network]["xml_file"]))) + networks.setdefault(_network, network(self.con, os.path.normpath(self.path + "/" + self.config[_network]["xml_file"]))) return networks def get_machines(self): @@ -53,6 +63,7 @@ class virtual_environ(): for _machine in self.machines: self.log.debug(_machine) machines.setdefault(_machine, machine( + self.con, os.path.normpath(self.path + "/" + self.config[_machine]["xml_file"]), os.path.normpath(self.path + "/" + self.config[_machine]["snapshot_xml_file"]), self.config[_machine]["image"], -- 2.39.2