]> git.ipfire.org Git - nitsi.git/commitdiff
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 035c010a608a3b4a6e2cdb0bb2082b42eba566b0..a67cbcb58d266864c75a3dd2a0f5599a2eef3860 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 223930becc20006436949bd7698f5f514db4164d..cd684e9d4d62eefbd6ee7479c5e9f1449f3f8e98 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 67f824e1f4411fd883979e22fc68938eb0384cb9..89fe75cc7ddc53a3f650bb2ff03712ac9e8f3f20 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 aaa07f1fdcda5ea6f859ab132b5fd5e40fc470d1..d8570729211d10fb0ca8b02e3c15d7becbdd7313 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"],