]>
git.ipfire.org Git - nitsi.git/blob - src/nitsi/virtual_environ.py
11 logger
= logging
.getLogger("nitsi.virtual_environ")
13 # Should return all vms and networks in a list
14 # and should provide the path to the necessary xml files
15 class Virtual_environ():
16 def __init__(self
, path
):
17 self
.log
= logger
.getChild(os
.path
.basename(os
.path
.abspath(path
)))
19 self
.path
= os
.path
.abspath(path
)
20 except BaseException
as e
:
21 self
.log
.error("Could not get absolute path")
24 self
.log
.debug(self
.path
)
26 self
.settings_file
= "{}/settings".format(self
.path
)
27 if not os
.path
.isfile(self
.settings_file
):
28 self
.log
.error("No such file: {}".format(self
.settings_file
))
30 self
.log
.debug(self
.settings_file
)
31 self
.config
= configparser
.ConfigParser()
32 self
.config
.read(self
.settings_file
)
33 self
.name
= self
.config
["DEFAULT"]["name"]
34 self
.machines_string
= self
.config
["DEFAULT"]["machines"]
35 self
.networks_string
= self
.config
["DEFAULT"]["networks"]
38 for machine
in self
.machines_string
.split(","):
39 self
.machines
.append(machine
.strip())
42 for network
in self
.networks_string
.split(","):
43 self
.networks
.append(network
.strip())
45 self
.log
.debug(self
.machines
)
46 self
.log
.debug(self
.networks
)
48 # Number of characters of the longest machine name
49 self
._longest
_machine
_name
= 0
51 self
.uri
= self
.config
["DEFAULT"]["uri"]
54 self
.con
= libvirt
.open(self
.uri
)
55 except BaseException
as error
:
56 self
.log
.error("Could not connect to: {}".format(self
.uri
))
59 self
.log
.debug("Connected to: {}".format(self
.uri
))
61 def get_networks(self
):
63 for _network
in self
.networks
:
64 self
.log
.debug(_network
)
65 networks
.setdefault(_network
, network
.Network(self
.con
, os
.path
.normpath(self
.path
+ "/" + self
.config
[_network
]["xml_file"])))
68 def get_machines(self
):
70 for _machine
in self
.machines
:
71 self
.log
.debug(_machine
)
72 machines
.setdefault(_machine
, machine
.Machine(
74 os
.path
.normpath(self
.path
+ "/" + self
.config
[_machine
]["xml_file"]),
75 os
.path
.normpath(self
.path
+ "/" + self
.config
[_machine
]["snapshot_xml_file"]),
76 self
.config
[_machine
]["image"],
77 self
.config
[_machine
]["root_uid"],
78 self
.config
[_machine
]["username"],
79 self
.config
[_machine
]["password"]))
84 def machine_names(self
):
88 def network_names(self
):
92 def longest_machine_name(self
):
93 if self
._longest
_machine
_name
:
94 return self
._longest
_machine
_name
96 for _machine
in self
.machines
:
97 if len(_machine
) > self
._longest
_machine
_name
:
98 self
._longest
_machine
_name
= len(_machine
)
100 return self
._longest
_machine
_name