We now use the logging module
[nitsi.git] / test.py
CommitLineData
5e7f6db7
JS
1#!/usr/bin/python3
2
5e7f6db7
JS
3
4import libvirt
5
5e7f6db7
JS
6import os
7
8import configparser
9
1ed8ca9f
JS
10from virtual_environ import virtual_environ
11from recipe import recipe
14cd493f 12
1ed8ca9f 13import logging
5e7f6db7 14
1ed8ca9f 15logger = logging.getLogger("nitsi.test")
5e7f6db7 16
5e7f6db7
JS
17class test():
18 def __init__(self, path):
5e7f6db7
JS
19 try:
20 self.path = os.path.abspath(path)
1ed8ca9f 21 self.log = logger.getChild(os.path.basename(self.path))
5e7f6db7 22 except BaseException as e:
1ed8ca9f 23 logger.error("Could not get absolute path")
5e7f6db7
JS
24
25 self.log.debug(self.path)
26
27 self.settings_file = "{}/settings".format(self.path)
28 if not os.path.isfile(self.settings_file):
29 self.log.error("No such file: {}".format(self.settings_file))
30
31 self.recipe_file = "{}/recipe".format(self.path)
32 if not os.path.isfile(self.recipe_file):
33 self.log.error("No such file: {}".format(self.recipe_file))
34
35 def read_settings(self):
36 self.config = configparser.ConfigParser()
37 self.config.read(self.settings_file)
38 self.name = self.config["DEFAULT"]["Name"]
39 self.description = self.config["DEFAULT"]["Description"]
14cd493f
JS
40 self.copy_to = self.config["DEFAULT"]["Copy_to"]
41 self.copy_from = self.config["DEFAULT"]["Copy_from"]
42 self.copy_from = self.copy_from.split(",")
43
44 tmp = []
45 for file in self.copy_from:
46 file = file.strip()
47 file = os.path.normpath(self.path + "/" + file)
48 tmp.append(file)
49
50 self.copy_from = tmp
5e7f6db7
JS
51
52 self.virtual_environ_name = self.config["VIRTUAL_ENVIRONMENT"]["Name"]
53 self.virtual_environ_path = self.config["VIRTUAL_ENVIRONMENT"]["Path"]
54 self.virtual_environ_path = os.path.normpath(self.path + "/" + self.virtual_environ_path)
55
56 def virtual_environ_setup(self):
57 self.virtual_environ = virtual_environ(self.virtual_environ_path)
58
59 self.virtual_networks = self.virtual_environ.get_networks()
60
61 self.virtual_machines = self.virtual_environ.get_machines()
62
63 def virtual_environ_start(self):
3fa89b7c
JS
64 for name in self.virtual_environ.network_names:
65 self.virtual_networks[name].define()
66 self.virtual_networks[name].start()
5e7f6db7 67
3fa89b7c
JS
68 for name in self.virtual_environ.machine_names:
69 self.virtual_machines[name].define()
70 self.virtual_machines[name].create_snapshot()
14cd493f 71 self.virtual_machines[name].copy_in(self.copy_from, self.copy_to)
3fa89b7c 72 self.virtual_machines[name].start()
5e7f6db7 73
3fa89b7c
JS
74 self.log.debug("Try to login on all machines")
75 for name in self.virtual_environ.machine_names:
1ed8ca9f 76 self.log.debug("Try to login on {}".format(name))
3fa89b7c 77 self.virtual_machines[name].login()
5e7f6db7 78
3fa89b7c
JS
79 def load_recipe(self):
80 try:
81 self.recipe = recipe(self.recipe_file)
4bc54b45
JS
82 for line in self.recipe.recipe:
83 self.log.debug(line)
84 except BaseException as e:
3fa89b7c 85 self.log.error("Failed to load recipe")
4bc54b45 86 raise e
3fa89b7c
JS
87
88 def run_recipe(self):
89 for line in self.recipe.recipe:
90 return_value = self.virtual_machines[line[0]].cmd(line[2])
bce7d520
JS
91 self.log.debug("Return value is: {}".format(return_value))
92 if return_value != "0" and line[1] == "":
93 self.log.error("Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value))
3fa89b7c 94 return False
bce7d520
JS
95 elif return_value == "0" and line[1] == "!":
96 self.log.error("Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value))
3fa89b7c 97 return False
bce7d520
JS
98 else:
99 self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value))
3fa89b7c
JS
100
101 def virtual_environ_stop(self):
102 for name in self.virtual_environ.machine_names:
103 self.virtual_machines[name].shutdown()
104 self.virtual_machines[name].revert_snapshot()
105 self.virtual_machines[name].undefine()
106
107 for name in self.virtual_environ.network_names:
108 self.virtual_networks[name].undefine()
5e7f6db7
JS
109
110
0a4b6cfb 111