]> git.ipfire.org Git - nitsi.git/blobdiff - machine.py
Add .gitignore
[nitsi.git] / machine.py
index 035c010a608a3b4a6e2cdb0bb2082b42eba566b0..7d5f148335d4d7643606ae4aa92000eacbe9848c 100644 (file)
@@ -6,21 +6,31 @@ from disk import disk
 
 from serial_connection import serial_connection
 
-from test import libvirt_con
-
 import os
 import libvirt
 
+import logging
+
+logger = logging.getLogger("nitsi.machine")
 
 class machine():
-    def __init__(self, vm_xml_file, snapshot_xml_file, image, root_uid, username, password):
-        self.log = log(4)
-        self.con = libvirt_con("qemu:///system")
+    def __init__(self, libvirt_con, vm_xml_file, snapshot_xml_file, image, root_uid, username, password):
+        self.log = logger.getChild(os.path.basename(vm_xml_file))
+        self.con = libvirt_con
         try:
             with open(vm_xml_file) as fobj:
                 self.vm_xml = fobj.read()
         except FileNotFoundError as error:
-            self.log.error("No such file: {}".format(vm_xml_file))
+            logger.error("No such file: {}".format(vm_xml_file))
+
+        try:
+            self.name = self.get_name()
+        except BaseException as error:
+            logger.error("Could not get name of the machine: {}".format(vm_xml_file))
+            raise error
+
+        self.log = logger.getChild(self.name)
+        self.log.debug("Name of this machine is {}".format(self.name))
 
         try:
             with open(snapshot_xml_file) as fobj:
@@ -40,7 +50,7 @@ class machine():
         self.password = password
 
     def define(self):
-        self.dom = self.con.con.defineXML(self.vm_xml)
+        self.dom = self.con.defineXML(self.vm_xml)
         if self.dom == None:
             self.log.error("Could not define VM")
             raise BaseException
@@ -92,6 +102,12 @@ class machine():
         elem = xml_root.find("./devices/serial/source")
         return elem.get("path")
 
+    def get_name(self):
+        xml_root = ET.fromstring(self.vm_xml)
+
+        elem = xml_root.find("./name")
+        return elem.text
+
     def check_is_booted_up(self):
         serial_con = serial_connection(self.get_serial_device())