]> git.ipfire.org Git - nitsi.git/commitdiff
Improve substitution of the all: statement
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Sat, 28 Jul 2018 09:49:37 +0000 (11:49 +0200)
committerJonatan Schlag <jonatan.schlag@ipfire.org>
Sat, 28 Jul 2018 09:49:37 +0000 (11:49 +0200)
We now substitute the all: statement in 3 stages:

1. We check if we get a setting for it which states which machines
should substitute all: (The setting is not fully implemented yet but all
changes which need to be done in recipe.py are done)

2. We try to get all machines named in the recipe and try to substitute
all: with these list.

3. If all other methods faile dwe substitute all: with all amchines
named in the virtual environment.

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
src/nitsi/recipe.py
src/nitsi/test.py

index bb5ec5bebc786ecfedd54b8ad38ad455b533c98f..d98bac59b3a5590e9b3cff521c4cc7ec530324da 100644 (file)
@@ -16,7 +16,7 @@ class RecipeExeption(Exception):
 # Should read the test, check if the syntax are valid
 # and return tuples with the ( host, command ) structure
 class Recipe():
-    def __init__(self, path, circle=[], machines=[]):
+    def __init__(self, path, circle=[], machines=[], fallback_machines=[]):
         self.recipe_file = path
         try:
             self.path = os.path.dirname(self.recipe_file)
@@ -126,15 +126,46 @@ class Recipe():
                 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]))
+            # Increase the line number by one
+            i = i + 1
 
-                self._recipe = tmp_recipe
\ No newline at end of file
+        # Substitue the all statement
+        if not self.in_recursion:
+            self.log.debug("We are not in a recursion")
+            # We will store the machine names we use to substitute the all statement
+            # in tmp_machines to keep the code which actually does the substitution clear
+            tmp_machines = None
+
+            # Check if we get a setting to substitute the all statement
+            if len(self.machines) != 0:
+                tmp_machines = self.machines
+
+            # Second try to fill tmp_machines
+            if not tmp_machines:
+                #  dertermine machines we use in this recipe
+                tmp = []
+                for line in self.recipe:
+                    self.log.debug(line)
+                    if not line[0] in tmp and line[0] != "all":
+                        tmp.append(line[0])
+
+                self.log.debug("Machines except all in the recipe: {}".format(tmp))
+
+                # Check if we got anything else then all: in th recipe
+                if len(tmp) != 0:
+                    tmp_machines = tmp
+
+            # If we get here we are using all machines in the virtual environment as fallback value
+            if not tmp_machines:
+                tmp_machines = self._fallback_machines
+
+            tmp_recipe = []
+            for line in self._recipe:
+                if line[0] != "all":
+                    tmp_recipe.append(line)
+                else:
+                    for machine in tmp_machines:
+                        tmp_recipe.append((machine.strip(), line[1], line[2]))
+
+            self._recipe = tmp_recipe
\ No newline at end of file
index 7d17cec05d680d5bb64da37c03359de2608797fe..e3563e1178201fab2b084035bdda2e7e13cf01d7 100755 (executable)
@@ -190,7 +190,9 @@ class Test():
     def load_recipe(self):
         self.log.info("Going to load the recipe")
         try:
-            self.recipe = recipe.Recipe(self.recipe_file, machines=self.virtual_environ.machine_names)
+            self.recipe = recipe.Recipe(self.recipe_file,
+                fallback_machines=self.virtual_environ.machine_names)
+
             for line in self.recipe.recipe:
                 self.log.debug(line)