]> git.ipfire.org Git - nitsi.git/blobdiff - test.py
Rename the vm class to machine
[nitsi.git] / test.py
diff --git a/test.py b/test.py
index 271e809e82297b9707fdfb59b593a3e1003763c5..47c82c44a9cd0c32108b86b1ec3e619a93133272 100755 (executable)
--- a/test.py
+++ b/test.py
@@ -14,6 +14,8 @@ import os
 
 import configparser
 
+from disk import disk
+
 class log():
     def __init__(self, log_level):
         self.log_level = log_level
@@ -52,7 +54,7 @@ class libvirt_con():
         return self.connection
 
 
-class vm():
+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")
@@ -74,6 +76,7 @@ class vm():
             self.log.error("No such file: {}".format(self.image))
 
         self.root_uid = root_uid
+        self.disk = disk(image)
 
         self.username = username
         self.password = password
@@ -150,6 +153,15 @@ class vm():
     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.copy_in(fr, to)
+        except BaseException as e:
+            self.log.error(e)
+        finally:
+            self.disk.umount("/")
+            self.disk.close()
 
 class connection():
     def __init__(self, device, username=None):
@@ -239,41 +251,6 @@ class connection():
 
         return False
 
-    def readline2(self, pattern=None):
-        string = ""
-        string2 = b""
-        if pattern:
-            pattern = re.compile(pattern)
-
-        while 1:
-            char = self.con.read(1)
-            string = string + char.decode("utf-8")
-            string2 = string2 + char
-            #print(char)
-            print(char.decode("utf-8"), end="")
-
-            #print(string2)
-            if pattern and pattern.match(string):
-               #print("get here1")
-               #print(string2)
-               return {"string" : string, "return-code" : 1}
-
-            if char == b"\n":
-                #print(char)
-                #print(string2)
-                #print("get here2")
-                return {"return-code" : 0}
-
-    def check_logged_in(self, username):
-        pattern = "^\[" + username + "@.+\]#"
-        data = self.readline(pattern=pattern)
-        if data["return-code"] == 1:
-                print("We are logged in")
-                return True
-        else:
-            print("We are  not logged in")
-            return False
-
     def print_lines_in_buffer(self):
         while True:
             self.log.debug("Fill buffer ...")
@@ -414,6 +391,12 @@ class recipe():
         self.path = os.path.dirname(self.recipe_file)
         self.log.debug("Path of recipe is: {}".format(self.recipe_file))
         self._recipe = None
+        self._machines = None
+
+        self.in_recursion = True
+        if len(circle) == 0:
+            self.in_recursion = False
+
         self.circle = circle
         self.log.debug(circle)
         self.log.debug(self.circle)
@@ -434,6 +417,16 @@ class recipe():
 
         return self._recipe
 
+    @property
+    def machines(self):
+        if not self._machines:
+            self._machines = []
+            for line in self._recipe:
+                if line[0] != "all" and line[0] not in self._machines:
+                    self._machines.append(line[0])
+
+        return self._machines
+
     def parse(self):
         self._recipe = []
         i = 1
@@ -473,9 +466,24 @@ class recipe():
             if machine == "include":
                 self._recipe.extend(recipe_to_include.recipe)
             else:
-                self._recipe.append((machine.strip(), extra.strip(), cmd.strip()))
+                # Support also something like 'alice,bob: echo'
+                machines = machine.split(",")
+                for machine in machines:
+                    self._recipe.append((machine.strip(), extra.strip(), cmd.strip()))
             i = i + 1
 
+            if not self.in_recursion:
+                tmp_recipe = []
+                for line in self._recipe:
+                    if line[0] != "all":
+                        tmp_recipe.append(line)
+                    else:
+                        for machine in self.machines:
+                            tmp_recipe.append((machine.strip(), line[1], line[2]))
+
+                self._recipe = tmp_recipe
+
+
 
 class test():
     def __init__(self, path):
@@ -500,6 +508,17 @@ class test():
         self.config.read(self.settings_file)
         self.name = self.config["DEFAULT"]["Name"]
         self.description = self.config["DEFAULT"]["Description"]
+        self.copy_to = self.config["DEFAULT"]["Copy_to"]
+        self.copy_from = self.config["DEFAULT"]["Copy_from"]
+        self.copy_from = self.copy_from.split(",")
+
+        tmp = []
+        for file in self.copy_from:
+            file = file.strip()
+            file = os.path.normpath(self.path + "/" + file)
+            tmp.append(file)
+
+        self.copy_from = tmp
 
         self.virtual_environ_name = self.config["VIRTUAL_ENVIRONMENT"]["Name"]
         self.virtual_environ_path = self.config["VIRTUAL_ENVIRONMENT"]["Path"]
@@ -520,6 +539,7 @@ class test():
         for name in self.virtual_environ.machine_names:
             self.virtual_machines[name].define()
             self.virtual_machines[name].create_snapshot()
+            self.virtual_machines[name].copy_in(self.copy_from, self.copy_to)
             self.virtual_machines[name].start()
 
         self.log.debug("Try to login on all machines")
@@ -601,7 +621,7 @@ class virtual_environ():
         machines = {}
         for _machine in self.machines:
             self.log.debug(_machine)
-            machines.setdefault(_machine, vm(
+            machines.setdefault(_machine, machine(
                 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"],