]>
Commit | Line | Data |
---|---|---|
5e7f6db7 JS |
1 | #!/usr/bin/python3 |
2 | ||
5e7f6db7 JS |
3 | |
4 | import libvirt | |
5 | ||
5e7f6db7 JS |
6 | import os |
7 | ||
8 | import configparser | |
9 | ||
14cd493f JS |
10 | from disk import disk |
11 | ||
5e7f6db7 JS |
12 | class 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 | ||
23 | class 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 | |
41ab240e JS |
51 | class RecipeExeption(Exception): |
52 | pass | |
53 | ||
54 | ||
55 | ||
5e7f6db7 JS |
56 | # Should read the test, check if the syntax are valid |
57 | # and return tuples with the ( host, command ) structure | |
58 | class recipe(): | |
4505a887 | 59 | def __init__(self, path, circle=[]): |
5e7f6db7 JS |
60 | self.log = log(4) |
61 | self.recipe_file = path | |
4505a887 JS |
62 | self.path = os.path.dirname(self.recipe_file) |
63 | self.log.debug("Path of recipe is: {}".format(self.recipe_file)) | |
41ab240e | 64 | self._recipe = None |
2f35f899 JS |
65 | self._machines = None |
66 | ||
67 | self.in_recursion = True | |
68 | if len(circle) == 0: | |
69 | self.in_recursion = False | |
70 | ||
4505a887 JS |
71 | self.circle = circle |
72 | self.log.debug(circle) | |
73 | self.log.debug(self.circle) | |
41ab240e | 74 | |
5e7f6db7 JS |
75 | if not os.path.isfile(self.recipe_file): |
76 | self.log.error("No such file: {}".format(self.recipe_file)) | |
77 | ||
78 | try: | |
79 | with open(self.recipe_file) as fobj: | |
80 | self.raw_recipe = fobj.readlines() | |
81 | except FileNotFoundError as error: | |
82 | self.log.error("No such file: {}".format(vm_xml_file)) | |
83 | ||
41ab240e JS |
84 | @property |
85 | def recipe(self): | |
86 | if not self._recipe: | |
87 | self.parse() | |
88 | ||
89 | return self._recipe | |
90 | ||
2f35f899 JS |
91 | @property |
92 | def machines(self): | |
93 | if not self._machines: | |
94 | self._machines = [] | |
95 | for line in self._recipe: | |
96 | if line[0] != "all" and line[0] not in self._machines: | |
97 | self._machines.append(line[0]) | |
98 | ||
99 | return self._machines | |
100 | ||
41ab240e JS |
101 | def parse(self): |
102 | self._recipe = [] | |
103 | i = 1 | |
5e7f6db7 | 104 | for line in self.raw_recipe: |
41ab240e JS |
105 | raw_line = line.split(":") |
106 | if len(raw_line) < 2: | |
107 | self.log.error("Error parsing the recipe in line {}".format(i)) | |
108 | raise RecipeExeption | |
07bf49a3 | 109 | cmd = raw_line[1].strip() |
41ab240e JS |
110 | raw_line = raw_line[0].strip().split(" ") |
111 | if len(raw_line) == 0: | |
112 | self.log.error("Failed to parse the recipe in line {}".format(i)) | |
113 | raise RecipeExeption | |
07bf49a3 JS |
114 | |
115 | if raw_line[0].strip() == "": | |
41ab240e JS |
116 | self.log.error("Failed to parse the recipe in line {}".format(i)) |
117 | raise RecipeExeption | |
07bf49a3 JS |
118 | |
119 | machine = raw_line[0].strip() | |
120 | ||
121 | if len(raw_line) == 2: | |
122 | extra = raw_line[1].strip() | |
123 | else: | |
124 | extra = "" | |
125 | ||
126 | # We could get a machine here or a include statement | |
127 | if machine == "include": | |
128 | path = cmd.strip() | |
129 | path = os.path.normpath(self.path + "/" + path) | |
130 | path = path + "/recipe" | |
131 | if path in self.circle: | |
132 | self.log.error("Detect import loop!") | |
133 | raise RecipeExeption | |
134 | self.circle.append(path) | |
135 | recipe_to_include = recipe(path, circle=self.circle) | |
136 | ||
137 | if machine == "include": | |
4505a887 JS |
138 | self._recipe.extend(recipe_to_include.recipe) |
139 | else: | |
67c8eded JS |
140 | # Support also something like 'alice,bob: echo' |
141 | machines = machine.split(",") | |
142 | for machine in machines: | |
143 | self._recipe.append((machine.strip(), extra.strip(), cmd.strip())) | |
41ab240e | 144 | i = i + 1 |
5e7f6db7 | 145 | |
2f35f899 JS |
146 | if not self.in_recursion: |
147 | tmp_recipe = [] | |
148 | for line in self._recipe: | |
149 | if line[0] != "all": | |
150 | tmp_recipe.append(line) | |
151 | else: | |
152 | for machine in self.machines: | |
153 | tmp_recipe.append((machine.strip(), line[1], line[2])) | |
154 | ||
155 | self._recipe = tmp_recipe | |
156 | ||
157 | ||
5e7f6db7 | 158 | |
5e7f6db7 JS |
159 | class test(): |
160 | def __init__(self, path): | |
161 | self.log = log(4) | |
162 | try: | |
163 | self.path = os.path.abspath(path) | |
164 | except BaseException as e: | |
165 | self.log.error("Could not get absolute path") | |
166 | ||
167 | self.log.debug(self.path) | |
168 | ||
169 | self.settings_file = "{}/settings".format(self.path) | |
170 | if not os.path.isfile(self.settings_file): | |
171 | self.log.error("No such file: {}".format(self.settings_file)) | |
172 | ||
173 | self.recipe_file = "{}/recipe".format(self.path) | |
174 | if not os.path.isfile(self.recipe_file): | |
175 | self.log.error("No such file: {}".format(self.recipe_file)) | |
176 | ||
177 | def read_settings(self): | |
178 | self.config = configparser.ConfigParser() | |
179 | self.config.read(self.settings_file) | |
180 | self.name = self.config["DEFAULT"]["Name"] | |
181 | self.description = self.config["DEFAULT"]["Description"] | |
14cd493f JS |
182 | self.copy_to = self.config["DEFAULT"]["Copy_to"] |
183 | self.copy_from = self.config["DEFAULT"]["Copy_from"] | |
184 | self.copy_from = self.copy_from.split(",") | |
185 | ||
186 | tmp = [] | |
187 | for file in self.copy_from: | |
188 | file = file.strip() | |
189 | file = os.path.normpath(self.path + "/" + file) | |
190 | tmp.append(file) | |
191 | ||
192 | self.copy_from = tmp | |
5e7f6db7 JS |
193 | |
194 | self.virtual_environ_name = self.config["VIRTUAL_ENVIRONMENT"]["Name"] | |
195 | self.virtual_environ_path = self.config["VIRTUAL_ENVIRONMENT"]["Path"] | |
196 | self.virtual_environ_path = os.path.normpath(self.path + "/" + self.virtual_environ_path) | |
197 | ||
198 | def virtual_environ_setup(self): | |
199 | self.virtual_environ = virtual_environ(self.virtual_environ_path) | |
200 | ||
201 | self.virtual_networks = self.virtual_environ.get_networks() | |
202 | ||
203 | self.virtual_machines = self.virtual_environ.get_machines() | |
204 | ||
205 | def virtual_environ_start(self): | |
3fa89b7c JS |
206 | for name in self.virtual_environ.network_names: |
207 | self.virtual_networks[name].define() | |
208 | self.virtual_networks[name].start() | |
5e7f6db7 | 209 | |
3fa89b7c JS |
210 | for name in self.virtual_environ.machine_names: |
211 | self.virtual_machines[name].define() | |
212 | self.virtual_machines[name].create_snapshot() | |
14cd493f | 213 | self.virtual_machines[name].copy_in(self.copy_from, self.copy_to) |
3fa89b7c | 214 | self.virtual_machines[name].start() |
5e7f6db7 | 215 | |
3fa89b7c JS |
216 | self.log.debug("Try to login on all machines") |
217 | for name in self.virtual_environ.machine_names: | |
218 | self.virtual_machines[name].login() | |
5e7f6db7 | 219 | |
3fa89b7c JS |
220 | def load_recipe(self): |
221 | try: | |
222 | self.recipe = recipe(self.recipe_file) | |
223 | except BaseException: | |
224 | self.log.error("Failed to load recipe") | |
225 | exit(1) | |
226 | ||
227 | def run_recipe(self): | |
228 | for line in self.recipe.recipe: | |
229 | return_value = self.virtual_machines[line[0]].cmd(line[2]) | |
bce7d520 JS |
230 | self.log.debug("Return value is: {}".format(return_value)) |
231 | if return_value != "0" and line[1] == "": | |
232 | self.log.error("Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value)) | |
3fa89b7c | 233 | return False |
bce7d520 JS |
234 | elif return_value == "0" and line[1] == "!": |
235 | self.log.error("Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value)) | |
3fa89b7c | 236 | return False |
bce7d520 JS |
237 | else: |
238 | self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value)) | |
3fa89b7c JS |
239 | |
240 | def virtual_environ_stop(self): | |
241 | for name in self.virtual_environ.machine_names: | |
242 | self.virtual_machines[name].shutdown() | |
243 | self.virtual_machines[name].revert_snapshot() | |
244 | self.virtual_machines[name].undefine() | |
245 | ||
246 | for name in self.virtual_environ.network_names: | |
247 | self.virtual_networks[name].undefine() | |
5e7f6db7 JS |
248 | |
249 | ||
5e7f6db7 JS |
250 | if __name__ == "__main__": |
251 | import argparse | |
252 | ||
253 | parser = argparse.ArgumentParser() | |
254 | ||
255 | parser.add_argument("-d", "--directory", dest="dir") | |
256 | ||
257 | args = parser.parse_args() | |
258 | ||
5e7f6db7 JS |
259 | currenttest = test(args.dir) |
260 | currenttest.read_settings() | |
10f65154 JS |
261 | currenttest.virtual_environ_setup() |
262 | currenttest.load_recipe() | |
0a4b6cfb JS |
263 | try: |
264 | currenttest.virtual_environ_start() | |
265 | currenttest.run_recipe() | |
266 | except BaseException as e: | |
267 | print(e) | |
268 | finally: | |
269 | currenttest.virtual_environ_stop() | |
270 |