From: Jonatan Schlag Date: Sun, 13 May 2018 17:17:21 +0000 (+0200) Subject: Merge branch 'master' of ssh://git.ipfire.org/pub/git/people/jschlag/nitsi X-Git-Url: http://git.ipfire.org/?p=nitsi.git;a=commitdiff_plain;h=4cbb829aec6393925475763f5fa9ee5cd341ddc4;hp=a7c774c7ad93ba2d40403f75e5583da3fb21a278 Merge branch 'master' of ssh://git.ipfire.org/pub/git/people/jschlag/nitsi --- diff --git a/nitsi.in b/nitsi.in index 2090e0d..f45dbe6 100755 --- a/nitsi.in +++ b/nitsi.in @@ -3,6 +3,11 @@ from nitsi.test import test from nitsi.logger import init_logging import logging +import argparse + +from nitsi.recipe import RecipeExeption + +from nitsi.test import TestException logger = logging.getLogger("nitsi") logger.setLevel(logging.DEBUG) @@ -15,36 +20,52 @@ ch.setFormatter(formatter) # add the handlers to the logger logger.addHandler(ch) -if __name__ == "__main__": - import argparse - +def main(): parser = argparse.ArgumentParser() parser.add_argument("-d", "--directory", dest="dir") - parser.add_argument("-v" "--version", help="Display version and exit", + parser.add_argument("-v", "--version", help="Display version and exit", action="store_true", dest="version") args = parser.parse_args() + # We just log the version and exit if args.version: logger.info("nitsi version: {}".format("@PACKAGE_VERSION@")) - else: - 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)) - fh.setLevel(logging.DEBUG) - logger.addHandler(fh) - logger.debug("We now logging everything to {}/general.log".format(log_dir)) + return 0 + + # 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)) + fh.setLevel(logging.DEBUG) + logger.addHandler(fh) + logger.debug("We now logging everything to {}/general.log".format(log_dir)) + # here we run a test + try: currenttest = test(args.dir, log_dir) currenttest.read_settings() currenttest.virtual_environ_setup() currenttest.load_recipe() - try: - currenttest.virtual_environ_start() - currenttest.run_recipe() - except BaseException as e: - print(e) - finally: - currenttest.virtual_environ_stop() \ No newline at end of file + except RecipeExeption as e: + logger.exception(e) + exit(2) + + try: + currenttest.virtual_environ_start() + currenttest.run_recipe() + except 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__": + main() \ No newline at end of file diff --git a/src/nitsi/recipe.py b/src/nitsi/recipe.py index b4d3b44..394f50b 100644 --- a/src/nitsi/recipe.py +++ b/src/nitsi/recipe.py @@ -9,7 +9,8 @@ logger = logging.getLogger("nitsi.recipe") class RecipeExeption(Exception): - pass + def __init__(self, message): + self.message = message @@ -39,13 +40,14 @@ class recipe(): if not os.path.isfile(self.recipe_file): self.log.error("{} is not a file".format(self.recipe_file)) - raise RecipeExeption + raise RecipeExeption("{} is not a file".format(self.recipe_file)()) try: with open(self.recipe_file) as fobj: self.raw_recipe = fobj.readlines() except FileNotFoundError as error: self.log.error("No such file: {}".format(vm_xml_file)) + raise error @property def recipe(self): @@ -71,16 +73,16 @@ class recipe(): raw_line = line.split(":", 1) if len(raw_line) < 2: self.log.error("Error parsing the recipe in line {}".format(i)) - raise RecipeExeption + raise RecipeExeption("Error parsing the recipe in line {}".format(i)) cmd = raw_line[1].strip() raw_line = raw_line[0].strip().split(" ") if len(raw_line) == 0: self.log.error("Failed to parse the recipe in line {}".format(i)) - raise RecipeExeption + raise RecipeExeption("Failed to parse the recipe in line {}".format(i)) if raw_line[0].strip() == "": self.log.error("Failed to parse the recipe in line {}".format(i)) - raise RecipeExeption + raise RecipeExeption("Failed to parse the recipe in line {}".format(i)) machine = raw_line[0].strip() @@ -96,7 +98,7 @@ class recipe(): path = path + "/recipe" if path in self.circle: self.log.error("Detect import loop!") - raise RecipeExeption + raise RecipeExeption("Detect import loop!") self.circle.append(path) recipe_to_include = recipe(path, circle=self.circle) diff --git a/src/nitsi/test.py b/src/nitsi/test.py index ecbe5f0..1f7728f 100755 --- a/src/nitsi/test.py +++ b/src/nitsi/test.py @@ -16,6 +16,11 @@ import logging logger = logging.getLogger("nitsi.test") + +class TestException(Exception): + def __init__(self, message): + self.message = message + class test(): def __init__(self, path, log_path): try: @@ -105,11 +110,9 @@ class test(): return_value = self.virtual_machines[line[0]].cmd(line[2]) self.log.debug("Return value is: {}".format(return_value)) if return_value != "0" and line[1] == "": - self.log.error("Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value)) - return False + raise TestException("Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value)) elif return_value == "0" and line[1] == "!": - self.log.error("Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value)) - return False + raise TestException("Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value)) else: self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value)) diff --git a/test/recipe b/test/recipe deleted file mode 100644 index c035b97..0000000 --- a/test/recipe +++ /dev/null @@ -1,6 +0,0 @@ -alice: echo "Hello World" -bob: echo "Hello World" -alice: ls -l -all: blkid -alice,bob !: eecho "This is a comma seperated list" -include: ../test2 diff --git a/test/settings b/test/settings deleted file mode 100644 index d390dc0..0000000 --- a/test/settings +++ /dev/null @@ -1,12 +0,0 @@ -[DEFAULT] -Name = Hello World -Description = This is a - short description. - -Copy_from = -Copy_to = /root/ - -[VIRTUAL_ENVIRONMENT] -Name = basic -path = ../virtual-environment/basic - diff --git a/test2/recipe b/test2/recipe deleted file mode 100644 index 7f96a65..0000000 --- a/test2/recipe +++ /dev/null @@ -1,2 +0,0 @@ -alice: echo "This is test2 on alice" -include: ../test3 diff --git a/test2/settings b/test2/settings deleted file mode 100644 index 616e1a0..0000000 --- a/test2/settings +++ /dev/null @@ -1,9 +0,0 @@ -[DEFAULT] -Name = Hello World -Description = This is a - short description. - -[VIRTUAL_ENVIRONMENT] -Name = basic -path = ../virtual-environment/basic - diff --git a/test3/recipe b/test3/recipe deleted file mode 100644 index 5382623..0000000 --- a/test3/recipe +++ /dev/null @@ -1,2 +0,0 @@ -alice: echo "This is test3 on alice" -all: echo "This is test3 on all" \ No newline at end of file diff --git a/test3/settings b/test3/settings deleted file mode 100644 index 616e1a0..0000000 --- a/test3/settings +++ /dev/null @@ -1,9 +0,0 @@ -[DEFAULT] -Name = Hello World -Description = This is a - short description. - -[VIRTUAL_ENVIRONMENT] -Name = basic -path = ../virtual-environment/basic - diff --git a/virtual-environment/basic/machines/alice/machine.xml b/virtual-environment/basic/machines/alice/machine.xml deleted file mode 100644 index 2f7d0f5..0000000 --- a/virtual-environment/basic/machines/alice/machine.xml +++ /dev/null @@ -1,111 +0,0 @@ - - alice - 76eb4b45-158b-435d-84eb-0bdc0aafd1f8 - 1048576 - 1048576 - 1 - - hvm - - - - - - - - - Broadwell-noTSX - - - - - - - destroy - restart - restart - - - - - - /usr/bin/kvm - - - - -
- - -
- - - -
- - - -
- - - -
- - - -
- - - - - -
- - - - - -
- - - - - - - - - -
- - - -
- - -
- - - - - - - - -
- -