Refactor disk class
[nitsi.git] / nitsi.in
CommitLineData
1ed8ca9f 1#!/usr/bin/python3
f72f2f70 2
7741c94d 3import argparse
6632e137 4import logging
7aa9213e 5import os
1ed8ca9f 6
383a38cd 7from nitsi.logger import init_logging, Log_Formatter
61b44c10 8from nitsi.recipe import RecipeExeption
ee227ea1 9import nitsi.test
7aa9213e 10from nitsi.settings import settings_parse_copy_from, NitsiSettings
61b44c10 11
1ed8ca9f 12logger = logging.getLogger("nitsi")
1ed8ca9f 13
68190093
JS
14# create console handler
15ch = logging.StreamHandler()
383a38cd 16formatter = Log_Formatter()
1ed8ca9f 17ch.setFormatter(formatter)
68190093 18# add the handler to the logger
1ed8ca9f 19logger.addHandler(ch)
f72f2f70 20
7741c94d 21def main():
f72f2f70
JS
22 parser = argparse.ArgumentParser()
23
68190093
JS
24 parser.add_argument( "--log-level", choices=[ "debug", "error", "info", "warning" ], dest="log_level", default="info")
25
75b554ca 26 parser.add_argument("-v", "--version", help="Display version and exit",
4cba2549
JS
27 action="store_true", dest="version")
28
1795f5e8
JS
29 # Subparser for different commands
30 subparsers = parser.add_subparsers(dest="subparsers_name")
31
32 parser_run_test = subparsers.add_parser('run-test', help='run-test help')
33 parser_run_test.add_argument("-d", "--directory", dest="dir", default=None)
34 parser_run_test.add_argument("-r", "--recipe", dest="recipe", default=None)
35 parser_run_test.add_argument("-s", "--settings", dest="settings", default=None)
31ebadfb 36 parser_run_test.add_argument("--default-settings", dest="default_settings", default=None)
1795f5e8
JS
37 parser_run_test.add_argument("--name", dest="name", default=None)
38 parser_run_test.add_argument("--description", dest="desc", default=None)
39 parser_run_test.add_argument("--copy-to", dest="copy_to", default=None)
40 parser_run_test.add_argument("--copy-from", dest="copy_from", default=None, nargs='+')
41 parser_run_test.add_argument("--virtual-environment", dest="virtual_environ_path", default=None)
bee8ead7 42 parser_run_test.add_argument("--interactive-error-handling", dest="interactive_error_handling", default=False, action="store_true")
1795f5e8 43
f72f2f70
JS
44 args = parser.parse_args()
45
68190093 46 # We just log the version and exit
4cba2549
JS
47 if args.version:
48 logger.info("nitsi version: {}".format("@PACKAGE_VERSION@"))
7741c94d
JS
49 return 0
50
68190093
JS
51
52 # Set the log level
53 # We are doing this after we logged the version
54 # to avoid that the version is not shown because of the log level
55 if args.log_level == "info":
56 logger.setLevel(logging.INFO)
57 elif args.log_level == "debug":
58 logger.setLevel(logging.DEBUG)
59 elif args.log_level == "warning":
60 logger.setLevel(logging.WARNING)
61 elif args.log_level == "error":
62 logger.setLevel(logging.ERROR)
63
dd9bd26a 64 # Here we are going to run a test
1795f5e8
JS
65 if args.subparsers_name == "run-test":
66 if not (args.dir or args.recipe):
67 logger.error("You need to provide at least a director of a test '-d' or a recipe file '-r'")
68 return 4
69
70 if (args.dir and args.recipe) or (args.dir and args.settings):
71 logger.error("You cannot use the options '-d' and '-r' or '-d' and '-s' at the same time")
72 return 4
73
74 # For all other stuff we need logging to a file
75 if args.dir:
76 log_dir = init_logging(args.dir)
77 elif args.recipe:
78 log_dir = init_logging(args.recipe)
79
80 # We now going to log everything to log_dir/genaral.log
81 fh = logging.FileHandler("{}/general.log".format(log_dir))
82 logger.addHandler(fh)
83 logger.debug("We now logging everything to {}/general.log".format(log_dir))
84
7aa9213e 85 # Build up settings
31ebadfb 86 settings = nitsi.settings.NitsiSettings(priority_list=["cmd", "settings-file", "default-settings-file", "nitsi-default"])
7aa9213e 87
1795f5e8 88 if args.name:
7aa9213e 89 settings.set_config_value("name", args.name, type="cmd")
1795f5e8 90 if args.desc:
7aa9213e 91 settings.set_config_value("description", args.desc, type="cmd")
1795f5e8 92 if args.copy_to:
7aa9213e 93 settings.set_config_value("copy_to", args.copy_to, type="cmd")
1795f5e8 94 if args.copy_from:
7aa9213e 95 settings.set_config_value("copy_from", nitsi.settings.settings_parse_copy_from(args.copy_from), type="cmd")
1795f5e8 96 if args.virtual_environ_path:
7aa9213e
JS
97 path = args.virtual_environ_path
98 if not os.path.isabs(path):
99 path = os.path.abspath(path)
100 settings.set_config_value("virtual_environ_path", path, type="cmd")
bee8ead7
JS
101 if args.interactive_error_handling:
102 settings.set_config_value("interactive_error_handling", True, type="cmd")
1795f5e8
JS
103
104 # here we run a test
105 try:
106 currenttest = nitsi.test.Test(
107 log_dir,
108 dir=args.dir,
109 recipe_file=args.recipe,
110 settings_file=args.settings,
31ebadfb
JS
111 settings=settings,
112 default_settings_file=args.default_settings)
3dac9881 113 currenttest.virtual_environ_setup_stage_1()
1795f5e8 114 currenttest.load_recipe()
3dac9881 115 currenttest.virtual_environ_setup_stage_2()
1795f5e8
JS
116 except RecipeExeption as e:
117 logger.exception(e)
118 return 2
119
120 try:
121 currenttest.virtual_environ_start()
122 currenttest.run_recipe()
123 except nitsi.test.TestException as e:
124 logger.exception(e)
125 return 1
126 except BaseException as e:
127 logger.exception(e)
128 return 3
129 finally:
130 currenttest.virtual_environ_stop()
131
132 return 0
7741c94d
JS
133
134if __name__ == "__main__":
2ffa26cb
JS
135 return_value = main()
136 logger.debug("Return value of main is: {}".format(return_value))
137 exit(return_value)