]> git.ipfire.org Git - nitsi.git/blobdiff - src/nitsi/machine.py
Refactor disk class
[nitsi.git] / src / nitsi / machine.py
index b49fe302f825f664629078111014df57bcb81c84..9aed5a0180dda8019d606e0c5be10536291bec7d 100644 (file)
@@ -1,22 +1,26 @@
 #!/usr/bin/python3
 
-import xml.etree.ElementTree as ET
-
-from nitsi.disk import disk
-
-from nitsi.serial_connection import serial_connection
-
-import os
 import libvirt
-
 import logging
+import os
+import xml.etree.ElementTree as ET
+
+from . import disk
+from . import serial_connection
 
 logger = logging.getLogger("nitsi.machine")
 
-class machine():
+class Machine():
     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
+        # self.dom should be always defined
+        self.dom = None
+        # self.snapshot should be also at least None
+        self.snapshot = None
+
+        self.serial_con = None
+
         try:
             with open(vm_xml_file) as fobj:
                 self.vm_xml = fobj.read()
@@ -44,46 +48,62 @@ class machine():
             self.log.error("No such file: {}".format(self.image))
 
         self.root_uid = root_uid
-        self.disk = disk(image)
+        self.disk = disk.Disk(image)
 
         self.username = username
         self.password = password
 
     def define(self):
+        self.log.info("Defining virtual machine")
         self.dom = self.con.defineXML(self.vm_xml)
         if self.dom == None:
             self.log.error("Could not define VM")
             raise BaseException
 
     def start(self):
+        self.log.info("Starting virtual machine")
         if self.dom.create() < 0:
             self.log.error("Could not start VM")
             raise BaseException
 
     def shutdown(self):
         if self.is_running():
+            self.log.info("Shutting down virtual machine")
             if self.dom.shutdown() < 0:
                 self.log.error("Could not shutdown VM")
                 raise BaseException
         else:
-            self.log.error("Domain is not running")
+            self.log.warn("Cannot shutdown a not running domain")
 
     def undefine(self):
-        self.dom.undefine()
+        # We cannot undefine a not defined dom object
+        if self.dom != None:
+            self.log.info("Undefining virtual machine")
+            self.dom.undefine()
+        else:
+            self.log.warn("Cannot undefine a not defined domain")
 
     def create_snapshot(self):
-
+        self.log.info("Creating snapshot of virtual machine")
         self.snapshot = self.dom.snapshotCreateXML(self.snapshot_xml)
 
-        if not self.snapshot:
+        if self.snapshot == None:
             self.log.error("Could not create snapshot")
             raise BaseException
 
     def revert_snapshot(self):
-        self.dom.revertToSnapshot(self.snapshot)
-        self.snapshot.delete()
+        if self.snapshot != None:
+            self.log.info("Reverting snapshot")
+            self.dom.revertToSnapshot(self.snapshot)
+            self.log.info("Deleting snapshot")
+            self.snapshot.delete()
+        else:
+            self.log.warn("No active snapshot. Cannot revert and delete snapshot")
 
     def is_running(self):
+        # Only if we have a valid dom object we can check the dom state
+        if self.dom == None:
+            return False
 
         state, reason = self.dom.state()
 
@@ -109,7 +129,7 @@ class machine():
         return elem.text
 
     def check_is_booted_up(self):
-        serial_con = serial_connection(self.get_serial_device())
+        serial_con = serial_connection.SerialConnection(self.get_serial_device())
 
         serial_con.write("\n")
         # This will block till the domain is booted up
@@ -117,22 +137,50 @@ class machine():
 
         #serial_con.close()
 
-    def login(self):
+    # This function should initialize the serial connection
+    def serial_init(self, log_file=None, log_start_time=None, longest_machine_name=10):
         try:
-            self.serial_con = serial_connection(self.get_serial_device(), username=self.username)
-            self.serial_con.login(self.password)
+            self.serial_con = serial_connection.SerialConnection(self.get_serial_device(),
+                            username=self.username,
+                            password= self.password,
+                            log_file=log_file,
+                            log_start_time=log_start_time,
+                            name=self.name,
+                            longest_machine_name=longest_machine_name)
+        except BaseException as e:
+            self.log.error("Could initialize the serial console")
+            self.log.exception(e)
+            raise e
+
+
+    # This function should create a ready to use serial connection for this serial domain
+    def serial_connect(self):
+        try:
+            # Do the real connect
+            self.serial_con.connect()
         except BaseException as e:
             self.log.error("Could not connect to the domain via serial console")
+            self.log.exception(e)
+            raise e
+
+    def serial_disconnect(self):
+        try:
+            self.serial_con.disconnect()
+        except BaseException as e:
+            self.log.error("Could not disconnect from the serial console")
+            self.log.exception(e)
+            raise e
 
     def cmd(self, cmd):
         return self.serial_con.command(cmd)
 
     def copy_in(self, fr, to):
         try:
-            self.disk.mount(self.root_uid, "/")
+            self.disk.inspect()
+            self.disk.mount()
             self.disk.copy_in(fr, to)
         except BaseException as e:
             self.log.error(e)
         finally:
-            self.disk.umount("/")
-            self.disk.close()
\ No newline at end of file
+            self.disk.umount()
+            self.disk.close()