]>
git.ipfire.org Git - people/ms/nitsi.git/blob - test.py
13 def __init__(self
, log_level
):
14 self
.log_level
= log_level
16 def debug(self
, string
):
17 if self
.log_level
>= 4:
18 print("DEBUG: {}".format(string
))
20 def error(self
, string
):
21 print("ERROR: {}".format(string
))
24 def __init__(self
, path
):
27 self
.path
= os
.path
.abspath(path
)
28 except BaseException
as e
:
29 self
.log
.error("Could not get absolute path")
31 self
.log
.debug(self
.path
)
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
))
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
))
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(",")
51 for file in self
.copy_from
:
53 file = os
.path
.normpath(self
.path
+ "/" + file)
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
)
62 def virtual_environ_setup(self
):
63 self
.virtual_environ
= virtual_environ(self
.virtual_environ_path
)
65 self
.virtual_networks
= self
.virtual_environ
.get_networks()
67 self
.virtual_machines
= self
.virtual_environ
.get_machines()
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()
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()
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()
84 def load_recipe(self
):
86 self
.recipe
= recipe(self
.recipe_file
)
87 for line
in self
.recipe
.recipe
:
89 except BaseException
as e
:
90 self
.log
.error("Failed to load recipe")
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
))
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
))
104 self
.log
.debug("Command '{}' on {} returned with: {}".format(line
[2],line
[0],return_value
))
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()
112 for name
in self
.virtual_environ
.network_names
:
113 self
.virtual_networks
[name
].undefine()