]> git.ipfire.org Git - people/jschlag/nitsi.git/commitdiff
Add the ability to set all settings from the command line
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Sat, 23 Jun 2018 07:53:58 +0000 (09:53 +0200)
committerJonatan Schlag <jonatan.schlag@ipfire.org>
Sat, 23 Jun 2018 07:53:58 +0000 (09:53 +0200)
Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
nitsi.in
src/nitsi/test.py

index d2039de6b398784a04a3448b3384de922b81c424..657504cbb4757a0370305300049d70daf6f1be6a 100755 (executable)
--- a/nitsi.in
+++ b/nitsi.in
@@ -6,6 +6,7 @@ import logging
 from nitsi.logger import init_logging, Log_Formatter
 from nitsi.recipe import RecipeExeption
 import nitsi.test
+import nitsi.settings
 
 logger = logging.getLogger("nitsi")
 
@@ -19,13 +20,24 @@ logger.addHandler(ch)
 def main():
     parser = argparse.ArgumentParser()
 
-    parser.add_argument("-d", "--directory", dest="dir")
-
     parser.add_argument( "--log-level", choices=[ "debug", "error", "info", "warning" ], dest="log_level", default="info")
 
     parser.add_argument("-v", "--version", help="Display version and exit",
                     action="store_true", dest="version")
 
+    # Subparser for different commands
+    subparsers = parser.add_subparsers(dest="subparsers_name")
+
+    parser_run_test = subparsers.add_parser('run-test', help='run-test help')
+    parser_run_test.add_argument("-d", "--directory", dest="dir", default=None)
+    parser_run_test.add_argument("-r", "--recipe", dest="recipe", default=None)
+    parser_run_test.add_argument("-s", "--settings", dest="settings", default=None)
+    parser_run_test.add_argument("--name", dest="name", default=None)
+    parser_run_test.add_argument("--description", dest="desc", default=None)
+    parser_run_test.add_argument("--copy-to", dest="copy_to", default=None)
+    parser_run_test.add_argument("--copy-from", dest="copy_from", default=None, nargs='+')
+    parser_run_test.add_argument("--virtual-environment", dest="virtual_environ_path", default=None)
+
     args = parser.parse_args()
 
   # We just log the version and exit
@@ -46,36 +58,68 @@ def main():
     elif args.log_level == "error":
         logger.setLevel(logging.ERROR)
 
-    # For all other stuff we need logging to a file
-    log_dir = init_logging(args.dir)
-    # We now going to log everything to log_dir/genaral.log
-    fh = logging.FileHandler("{}/general.log".format(log_dir))
-    logger.addHandler(fh)
-    logger.debug("We now logging everything to {}/general.log".format(log_dir))
-
-    # here we run a test
-    try:
-        currenttest = nitsi.test.Test(log_dir, dir=args.dir)
-        currenttest.read_settings()
-        currenttest.virtual_environ_setup()
-        currenttest.load_recipe()
-    except RecipeExeption as e:
-        logger.exception(e)
-        return 2
-
-    try:
-        currenttest.virtual_environ_start()
-        currenttest.run_recipe()
-    except nitsi.test.TestException as e:
-        logger.exception(e)
-        return 1
-    except BaseException as e:
-        logger.exception(e)
-        return 3
-    finally:
-        currenttest.virtual_environ_stop()
-
-    return 0
+
+    if args.subparsers_name == "run-test":
+        if not (args.dir or args.recipe):
+            logger.error("You need to provide at least a director of a test '-d' or a recipe file '-r'")
+            return 4
+
+        if (args.dir and args.recipe) or (args.dir and args.settings):
+            logger.error("You cannot use the options '-d' and '-r' or '-d' and '-s' at the same time")
+            return 4
+
+        # For all other stuff we need logging to a file
+        if args.dir:
+            log_dir = init_logging(args.dir)
+        elif args.recipe:
+            log_dir = init_logging(args.recipe)
+
+        # We now going to log everything to log_dir/genaral.log
+        fh = logging.FileHandler("{}/general.log".format(log_dir))
+        logger.addHandler(fh)
+        logger.debug("We now logging everything to {}/general.log".format(log_dir))
+
+        # Build up settings dict
+        settings = {}
+        if args.name:
+            settings["name"] = args.name
+        if args.desc:
+            settings["description"] = args.desc
+        if args.copy_to:
+            settings["copy_to"] = args.copy_to
+        if args.copy_from:
+            settings["copy_from"] =  nitsi.settings.settings_parse_copy_from(args.copy_from)
+        if args.virtual_environ_path:
+            settings["virtual_environ_path"] = args.virtual_environ_path
+
+        # here we run a test
+        try:
+            currenttest = nitsi.test.Test(
+                log_dir,
+                dir=args.dir,
+                recipe_file=args.recipe,
+                settings_file=args.settings,
+                cmd_settings=settings)
+            currenttest.read_settings()
+            currenttest.virtual_environ_setup()
+            currenttest.load_recipe()
+        except RecipeExeption as e:
+            logger.exception(e)
+            return 2
+
+        try:
+            currenttest.virtual_environ_start()
+            currenttest.run_recipe()
+        except nitsi.test.TestException as e:
+            logger.exception(e)
+            return 1
+        except BaseException as e:
+            logger.exception(e)
+            return 3
+        finally:
+            currenttest.virtual_environ_stop()
+
+        return 0
 
 if __name__ == "__main__":
    return_value = main()
index 9cc1188c2b1bdb09144af37055b0eecb1758104e..3ee793637223ac4793302a8986cdfb67a46816eb 100755 (executable)
@@ -8,6 +8,7 @@ import time
 
 from . import recipe
 from . import virtual_environ
+from . import settings
 
 logger = logging.getLogger("nitsi.test")
 
@@ -107,36 +108,32 @@ class Test():
             self.settings["copy_from"] = self.config.get("GENERAL", "copy_from", fallback=None)
             self.settings["virtual_environ_path"] = self.config.get("VIRTUAL_ENVIRONMENT", "path", fallback=None)
 
-        if not self.settings["virtual_environ_path"]:
-            self.log.error("No path for virtual environment found.")
-            raise TestException("No path for virtual environment found.")
-
-        self.settings["virtual_environ_path"] = os.path.normpath(self.path + "/" + self.settings["virtual_environ_path"])
+            # We need to parse some settings here because they are loaded from a settings file
+            if not os.path.isabs(self.settings["virtual_environ_path"]):
+                self.settings["virtual_environ_path"] = os.path.normpath(os.path.dirname(
+                    self.settings_file) + "/" + self.settings["virtual_environ_path"])
 
-        # Parse copy_from setting
-        if self.settings["copy_from"]:
-            self.log.debug("Going to parse the copy_from setting.")
-            self.settings["copy_from"] = self.settings["copy_from"].split(",")
+            # Parse copy_from setting
+            if self.settings["copy_from"]:
+                self.settings["copy_from"] = settings.settings_parse_copy_from(self.settings["copy_from"],
+                path=os.path.dirname(self.settings_file))
 
-            tmp = []
-            for file in self.settings["copy_from"]:
-                file = file.strip()
-                # If file is empty we do not want to add it to the list
-                if not file == "":
-                    # If we get an absolut path we do nothing
-                    # If not we add self.path to get an absolut path
-                    if not os.path.isabs(file):
-                        file = os.path.normpath(self.path + "/" + file)
+        # Update all settings from the cmd
+        self.settings.update(self.cmd_settings)
 
-                    # We need to check if file is a valid file or dir
-                    if not (os.path.isdir(file) or os.path.isfile(file)):
-                        raise TestException("'{}' is not a valid file nor a valid directory".format(file))
+        if not os.path.isabs(self.settings["virtual_environ_path"]):
+            self.settings["virtual_environ_path"] = os.path.abspath(self.settings["virtual_environ_path"])
 
-                    self.log.debug("'{}' will be copied into all images".format(file))
-                    tmp.append(file)
 
-            self.settings["copy_from"] = tmp
+        # Check if we get at least a valid a valid path to virtual environ
+        if not self.settings["virtual_environ_path"]:
+            self.log.error("No path for virtual environment found.")
+            raise TestException("No path for virtual environment found.")
 
+        # Print all settings for debugging purpose
+        self.log.debug("Settings are:")
+        for key in self.settings:
+            self.log.debug("{}: {}".format(key, self.settings[key]))
 
 
     def virtual_environ_setup(self):