]> git.ipfire.org Git - nitsi.git/blame - test.py
self.con is already the connection to the libvirt daemon
[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
14cd493f
JS
10from disk import disk
11
5e7f6db7
JS
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
5e7f6db7
JS
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"]
14cd493f
JS
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
5e7f6db7
JS
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):
3fa89b7c
JS
70 for name in self.virtual_environ.network_names:
71 self.virtual_networks[name].define()
72 self.virtual_networks[name].start()
5e7f6db7 73
3fa89b7c
JS
74 for name in self.virtual_environ.machine_names:
75 self.virtual_machines[name].define()
76 self.virtual_machines[name].create_snapshot()
14cd493f 77 self.virtual_machines[name].copy_in(self.copy_from, self.copy_to)
3fa89b7c 78 self.virtual_machines[name].start()
5e7f6db7 79
3fa89b7c
JS
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()
5e7f6db7 83
3fa89b7c
JS
84 def load_recipe(self):
85 try:
86 self.recipe = recipe(self.recipe_file)
87 except BaseException:
88 self.log.error("Failed to load recipe")
89 exit(1)
90
91 def run_recipe(self):
92 for line in self.recipe.recipe:
93 return_value = self.virtual_machines[line[0]].cmd(line[2])
bce7d520
JS
94 self.log.debug("Return value is: {}".format(return_value))
95 if return_value != "0" and line[1] == "":
96 self.log.error("Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value))
3fa89b7c 97 return False
bce7d520
JS
98 elif return_value == "0" and line[1] == "!":
99 self.log.error("Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value))
3fa89b7c 100 return False
bce7d520
JS
101 else:
102 self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value))
3fa89b7c
JS
103
104 def virtual_environ_stop(self):
105 for name in self.virtual_environ.machine_names:
106 self.virtual_machines[name].shutdown()
107 self.virtual_machines[name].revert_snapshot()
108 self.virtual_machines[name].undefine()
109
110 for name in self.virtual_environ.network_names:
111 self.virtual_networks[name].undefine()
5e7f6db7
JS
112
113
0a4b6cfb 114