]>
git.ipfire.org Git - 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
, uri
):
27 self
.connection
= None
29 def get_domain_from_name(self
, name
):
30 dom
= self
.con
.lookupByName(name
)
38 if self
.connection
== None:
40 self
.connection
= libvirt
.open(self
.uri
)
41 except BaseException
as error
:
42 self
.log
.error("Could not connect to: {}".format(self
.uri
))
44 self
.log
.debug("Connected to: {}".format(self
.uri
))
45 return self
.connection
47 return self
.connection
52 def __init__(self
, path
):
55 self
.path
= os
.path
.abspath(path
)
56 except BaseException
as e
:
57 self
.log
.error("Could not get absolute path")
59 self
.log
.debug(self
.path
)
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
))
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
))
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"]
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(",")
79 for file in self
.copy_from
:
81 file = os
.path
.normpath(self
.path
+ "/" + file)
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
)
90 def virtual_environ_setup(self
):
91 self
.virtual_environ
= virtual_environ(self
.virtual_environ_path
)
93 self
.virtual_networks
= self
.virtual_environ
.get_networks()
95 self
.virtual_machines
= self
.virtual_environ
.get_machines()
97 def virtual_environ_start(self
):
98 for name
in self
.virtual_environ
.network_names
:
99 self
.virtual_networks
[name
].define()
100 self
.virtual_networks
[name
].start()
102 for name
in self
.virtual_environ
.machine_names
:
103 self
.virtual_machines
[name
].define()
104 self
.virtual_machines
[name
].create_snapshot()
105 self
.virtual_machines
[name
].copy_in(self
.copy_from
, self
.copy_to
)
106 self
.virtual_machines
[name
].start()
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()
112 def load_recipe(self
):
114 self
.recipe
= recipe(self
.recipe_file
)
115 except BaseException
:
116 self
.log
.error("Failed to load recipe")
119 def run_recipe(self
):
120 for line
in self
.recipe
.recipe
:
121 return_value
= self
.virtual_machines
[line
[0]].cmd(line
[2])
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
))
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
))
130 self
.log
.debug("Command '{}' on {} returned with: {}".format(line
[2],line
[0],return_value
))
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()
138 for name
in self
.virtual_environ
.network_names
:
139 self
.virtual_networks
[name
].undefine()
142 if __name__
== "__main__":
145 parser
= argparse
.ArgumentParser()
147 parser
.add_argument("-d", "--directory", dest
="dir")
149 args
= parser
.parse_args()
151 currenttest
= test(args
.dir)
152 currenttest
.read_settings()
153 currenttest
.virtual_environ_setup()
154 currenttest
.load_recipe()
156 currenttest
.virtual_environ_start()
157 currenttest
.run_recipe()
158 except BaseException
as e
:
161 currenttest
.virtual_environ_stop()