Open only one connection to the libvirt daemon
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Sun, 29 Apr 2018 08:06:53 +0000 (10:06 +0200)
committerJonatan Schlag <jonatan.schlag@ipfire.org>
Sun, 29 Apr 2018 08:06:53 +0000 (10:06 +0200)
Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
machine.py
network.py
test.py
virtual_environ.py

index 035c010..a67cbcb 100644 (file)
@@ -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()
index 223930b..cd684e9 100644 (file)
@@ -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 (executable)
--- 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)
index aaa07f1..d857072 100644 (file)
@@ -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"],