]>
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 | ||
2fa4467d JS |
10 | from nitsi.virtual_environ import virtual_environ |
11 | from nitsi.recipe import recipe | |
14cd493f | 12 | |
1ed8ca9f | 13 | import logging |
5e7f6db7 | 14 | |
1ed8ca9f | 15 | logger = logging.getLogger("nitsi.test") |
5e7f6db7 | 16 | |
5e7f6db7 JS |
17 | class test(): |
18 | def __init__(self, path): | |
5e7f6db7 JS |
19 | try: |
20 | self.path = os.path.abspath(path) | |
1ed8ca9f | 21 | self.log = logger.getChild(os.path.basename(self.path)) |
5e7f6db7 | 22 | except BaseException as e: |
1ed8ca9f | 23 | logger.error("Could not get absolute path") |
5e7f6db7 | 24 | |
2fa4467d | 25 | self.log.debug("Path of this test is: {}".format(self.path)) |
5e7f6db7 JS |
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"] | |
14cd493f JS |
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 | |
5e7f6db7 JS |
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): | |
3fa89b7c JS |
64 | for name in self.virtual_environ.network_names: |
65 | self.virtual_networks[name].define() | |
66 | self.virtual_networks[name].start() | |
5e7f6db7 | 67 | |
3fa89b7c JS |
68 | for name in self.virtual_environ.machine_names: |
69 | self.virtual_machines[name].define() | |
70 | self.virtual_machines[name].create_snapshot() | |
14cd493f | 71 | self.virtual_machines[name].copy_in(self.copy_from, self.copy_to) |
3fa89b7c | 72 | self.virtual_machines[name].start() |
5e7f6db7 | 73 | |
3fa89b7c JS |
74 | self.log.debug("Try to login on all machines") |
75 | for name in self.virtual_environ.machine_names: | |
1ed8ca9f | 76 | self.log.debug("Try to login on {}".format(name)) |
3fa89b7c | 77 | self.virtual_machines[name].login() |
5e7f6db7 | 78 | |
3fa89b7c | 79 | def load_recipe(self): |
2fa4467d | 80 | self.log.info("Going to load the recipe") |
3fa89b7c JS |
81 | try: |
82 | self.recipe = recipe(self.recipe_file) | |
4bc54b45 JS |
83 | for line in self.recipe.recipe: |
84 | self.log.debug(line) | |
2fa4467d JS |
85 | |
86 | self.log.debug("This was the recipe") | |
4bc54b45 | 87 | except BaseException as e: |
3fa89b7c | 88 | self.log.error("Failed to load recipe") |
4bc54b45 | 89 | raise e |
3fa89b7c JS |
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 |