Improving error message and undefining of network objects
[nitsi.git] / src / nitsi / network.py
CommitLineData
3ab701aa
JS
1#!/usr/bin/python3
2
1ed8ca9f
JS
3import logging
4import os
5
6logger = logging.getLogger("nitsi.network")
7
3ab701aa
JS
8# # A class which define and undefine a virtual network based on an xml file
9class network():
7005787e 10 def __init__(self, libvirt_con, network_xml_file):
768e12a1 11 self.log = logger.getChild(os.path.basename(os.path.dirname(network_xml_file)))
7005787e 12 self.con = libvirt_con
6ec03557 13 self.network = None
3ab701aa
JS
14 try:
15 with open(network_xml_file) as fobj:
6ec03557 16 self.log.info("Reading xml file for network")
3ab701aa
JS
17 self.network_xml = fobj.read()
18 except FileNotFoundError as error:
14f7e5e0 19 self.log.error("No such file: {}".format(network_xml_file))
6ec03557 20 raise error
3ab701aa
JS
21
22 def define(self):
6ec03557 23 self.log.info("Going to define network")
8b744f18 24 self.network = self.con.networkDefineXML(self.network_xml)
3ab701aa
JS
25
26 if network == None:
27 self.log.error("Failed to define virtual network")
28
29 def start(self):
6ec03557 30 self.log.info("Starting Network")
3ab701aa
JS
31 self.network.create()
32
33 def undefine(self):
6ec03557
JS
34 if self.network != None:
35 if self.network.isActive() == 1:
36 self.log.info("Shutting down network")
37 self.network.destroy()
38
39 self.log.info("Undefining network")
40 self.network.undefine()