]> git.ipfire.org Git - nitsi.git/blame_incremental - test.py
We now use the logging module
[nitsi.git] / test.py
... / ...
CommitLineData
1#!/usr/bin/python3
2
3
4import libvirt
5
6import os
7
8import configparser
9
10from virtual_environ import virtual_environ
11from recipe import recipe
12
13import logging
14
15logger = logging.getLogger("nitsi.test")
16
17class test():
18 def __init__(self, path):
19 try:
20 self.path = os.path.abspath(path)
21 self.log = logger.getChild(os.path.basename(self.path))
22 except BaseException as e:
23 logger.error("Could not get absolute path")
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"]
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
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):
64 for name in self.virtual_environ.network_names:
65 self.virtual_networks[name].define()
66 self.virtual_networks[name].start()
67
68 for name in self.virtual_environ.machine_names:
69 self.virtual_machines[name].define()
70 self.virtual_machines[name].create_snapshot()
71 self.virtual_machines[name].copy_in(self.copy_from, self.copy_to)
72 self.virtual_machines[name].start()
73
74 self.log.debug("Try to login on all machines")
75 for name in self.virtual_environ.machine_names:
76 self.log.debug("Try to login on {}".format(name))
77 self.virtual_machines[name].login()
78
79 def load_recipe(self):
80 try:
81 self.recipe = recipe(self.recipe_file)
82 for line in self.recipe.recipe:
83 self.log.debug(line)
84 except BaseException as e:
85 self.log.error("Failed to load recipe")
86 raise e
87
88 def run_recipe(self):
89 for line in self.recipe.recipe:
90 return_value = self.virtual_machines[line[0]].cmd(line[2])
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))
94 return False
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))
97 return False
98 else:
99 self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value))
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()
109
110
111