Move recipe class into an own file
[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
23class libvirt_con():
24 def __init__(self, uri):
25 self.log = log(4)
26 self.uri = uri
27 self.connection = None
28
29 def get_domain_from_name(self, name):
30 dom = self.con.lookupByName(name)
31
32 if dom == None:
33 raise BaseException
34 return dom
35
36 @property
37 def con(self):
38 if self.connection == None:
39 try:
40 self.connection = libvirt.open(self.uri)
41 except BaseException as error:
42 self.log.error("Could not connect to: {}".format(self.uri))
43
44 self.log.debug("Connected to: {}".format(self.uri))
45 return self.connection
46
47 return self.connection
48
49
5e7f6db7 50
5e7f6db7
JS
51class test():
52 def __init__(self, path):
53 self.log = log(4)
54 try:
55 self.path = os.path.abspath(path)
56 except BaseException as e:
57 self.log.error("Could not get absolute path")
58
59 self.log.debug(self.path)
60
61 self.settings_file = "{}/settings".format(self.path)
62 if not os.path.isfile(self.settings_file):
63 self.log.error("No such file: {}".format(self.settings_file))
64
65 self.recipe_file = "{}/recipe".format(self.path)
66 if not os.path.isfile(self.recipe_file):
67 self.log.error("No such file: {}".format(self.recipe_file))
68
69 def read_settings(self):
70 self.config = configparser.ConfigParser()
71 self.config.read(self.settings_file)
72 self.name = self.config["DEFAULT"]["Name"]
73 self.description = self.config["DEFAULT"]["Description"]
14cd493f
JS
74 self.copy_to = self.config["DEFAULT"]["Copy_to"]
75 self.copy_from = self.config["DEFAULT"]["Copy_from"]
76 self.copy_from = self.copy_from.split(",")
77
78 tmp = []
79 for file in self.copy_from:
80 file = file.strip()
81 file = os.path.normpath(self.path + "/" + file)
82 tmp.append(file)
83
84 self.copy_from = tmp
5e7f6db7
JS
85
86 self.virtual_environ_name = self.config["VIRTUAL_ENVIRONMENT"]["Name"]
87 self.virtual_environ_path = self.config["VIRTUAL_ENVIRONMENT"]["Path"]
88 self.virtual_environ_path = os.path.normpath(self.path + "/" + self.virtual_environ_path)
89
90 def virtual_environ_setup(self):
91 self.virtual_environ = virtual_environ(self.virtual_environ_path)
92
93 self.virtual_networks = self.virtual_environ.get_networks()
94
95 self.virtual_machines = self.virtual_environ.get_machines()
96
97 def virtual_environ_start(self):
3fa89b7c
JS
98 for name in self.virtual_environ.network_names:
99 self.virtual_networks[name].define()
100 self.virtual_networks[name].start()
5e7f6db7 101
3fa89b7c
JS
102 for name in self.virtual_environ.machine_names:
103 self.virtual_machines[name].define()
104 self.virtual_machines[name].create_snapshot()
14cd493f 105 self.virtual_machines[name].copy_in(self.copy_from, self.copy_to)
3fa89b7c 106 self.virtual_machines[name].start()
5e7f6db7 107
3fa89b7c
JS
108 self.log.debug("Try to login on all machines")
109 for name in self.virtual_environ.machine_names:
110 self.virtual_machines[name].login()
5e7f6db7 111
3fa89b7c
JS
112 def load_recipe(self):
113 try:
114 self.recipe = recipe(self.recipe_file)
115 except BaseException:
116 self.log.error("Failed to load recipe")
117 exit(1)
118
119 def run_recipe(self):
120 for line in self.recipe.recipe:
121 return_value = self.virtual_machines[line[0]].cmd(line[2])
bce7d520
JS
122 self.log.debug("Return value is: {}".format(return_value))
123 if return_value != "0" and line[1] == "":
124 self.log.error("Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value))
3fa89b7c 125 return False
bce7d520
JS
126 elif return_value == "0" and line[1] == "!":
127 self.log.error("Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value))
3fa89b7c 128 return False
bce7d520
JS
129 else:
130 self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value))
3fa89b7c
JS
131
132 def virtual_environ_stop(self):
133 for name in self.virtual_environ.machine_names:
134 self.virtual_machines[name].shutdown()
135 self.virtual_machines[name].revert_snapshot()
136 self.virtual_machines[name].undefine()
137
138 for name in self.virtual_environ.network_names:
139 self.virtual_networks[name].undefine()
5e7f6db7
JS
140
141
5e7f6db7
JS
142if __name__ == "__main__":
143 import argparse
144
145 parser = argparse.ArgumentParser()
146
147 parser.add_argument("-d", "--directory", dest="dir")
148
149 args = parser.parse_args()
150
5e7f6db7
JS
151 currenttest = test(args.dir)
152 currenttest.read_settings()
10f65154
JS
153 currenttest.virtual_environ_setup()
154 currenttest.load_recipe()
0a4b6cfb
JS
155 try:
156 currenttest.virtual_environ_start()
157 currenttest.run_recipe()
158 except BaseException as e:
159 print(e)
160 finally:
161 currenttest.virtual_environ_stop()
162