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