Better logging when managing OpenNebula network in postservice
* postservice/31-one-netmng: use pyeole.log. Do not use pyeole.ihm. Add logging to follown what's going on. Ref: #24387
This commit is contained in:
parent
d8fc5d10a3
commit
cac835e1ee
|
@ -1,7 +1,8 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from pyeole import ihm
|
||||
from pyeole import process
|
||||
from pyeole.log import init_logging
|
||||
|
||||
from creole.client import CreoleClient
|
||||
from tempfile import mkstemp
|
||||
|
||||
|
@ -13,23 +14,7 @@ import re
|
|||
|
||||
LOG_FILE = '/var/log/one/eole-one-node.log'
|
||||
|
||||
#= Configure Logger ===
|
||||
logger = logging.getLogger(__name__)
|
||||
#std_handler = logging.StreamHandler(sys.stdout)
|
||||
file_handler = logging.FileHandler(LOG_FILE)
|
||||
|
||||
logger.setLevel(logging.INFO)
|
||||
#std_handler.setLevel(logging.INFO)
|
||||
file_handler.setLevel(logging.DEBUG)
|
||||
|
||||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
#std_handler.setFormatter(formatter)
|
||||
file_handler.setFormatter(formatter)
|
||||
|
||||
#logger.addHandler(std_handler)
|
||||
logger.addHandler(file_handler)
|
||||
#= End Logger ===
|
||||
|
||||
logger = None
|
||||
|
||||
class RunCmdError(Exception):
|
||||
pass
|
||||
|
@ -55,8 +40,14 @@ class OneClient():
|
|||
res = process.system_out(command)
|
||||
if res[0] == 0:
|
||||
self.auth = res[1].split(':')
|
||||
else:
|
||||
msg = u"OneClient: unable to authenticate: '{stdout}', '{stderr}'"
|
||||
logger.error(msg.format(stdout=res[1], stderr=res[2]))
|
||||
|
||||
|
||||
def __run_cmd__(self, cmd):
|
||||
logger.debug(u"OneClient: execute command '{cmd}'".format(cmd=cmd))
|
||||
|
||||
command = list(cmd)
|
||||
command.extend(['--user', self.auth[0]])
|
||||
command.extend(['--password', self.auth[1]])
|
||||
|
@ -77,11 +68,14 @@ class OneClient():
|
|||
else:
|
||||
return res
|
||||
else:
|
||||
msg = u"OneClient: error executing '{cmd}': '{stdout}', '{stderr}'"
|
||||
logger.error(msg.format(cmd=cmd, stdout=res[1], stderr=res[2]))
|
||||
return False
|
||||
|
||||
def get_hosts(self):
|
||||
""" get the list of hosts
|
||||
"""
|
||||
logger.debug(u"OneClient: list all hosts")
|
||||
cmd = ['onehost', 'list']
|
||||
cmd.extend(['-l', 'ID,NAME'])
|
||||
res = self.__run_cmd__(cmd)
|
||||
|
@ -90,6 +84,7 @@ class OneClient():
|
|||
def get_clusters(self):
|
||||
""" get the cluster list
|
||||
"""
|
||||
logger.debug(u"OneClient: list all clusters")
|
||||
cmd = ['onecluster', 'list']
|
||||
cmd.extend(['-l', 'ID,NAME'])
|
||||
return self.__run_cmd__(cmd)
|
||||
|
@ -97,11 +92,13 @@ class OneClient():
|
|||
def get_networks(self):
|
||||
""" get the virtual network list
|
||||
"""
|
||||
logger.debug(u"OneClient: list all networks")
|
||||
cmd = ['onevnet', 'list']
|
||||
cmd.extend(['-l', 'ID,NAME'])
|
||||
return self.__run_cmd__(cmd)
|
||||
|
||||
def get_cluster_id_by_name(self, name):
|
||||
logger.debug(u"OneClient: get cluster named '{name}'".format(name=name))
|
||||
cmd = ['onecluster', 'list']
|
||||
cmd.extend(['-f', 'NAME={0}'.format(name)])
|
||||
res = self.__run_cmd__(cmd)
|
||||
|
@ -109,6 +106,7 @@ class OneClient():
|
|||
return ID
|
||||
|
||||
def get_vnet_id_by_name(self, name):
|
||||
logger.debug(u"OneClient: get network named '{name}'".format(name=name))
|
||||
cmd = ['onevnet', 'list']
|
||||
cmd.extend(['-f', 'NAME={0}'.format(name)])
|
||||
res = self.__run_cmd__(cmd)
|
||||
|
@ -118,29 +116,32 @@ class OneClient():
|
|||
def create_network(self, templatefile, cluster, vnet_name):
|
||||
""" Create a network
|
||||
"""
|
||||
cmd = ['onevnet', 'create']
|
||||
cmd.extend(['--user', self.auth[0]])
|
||||
cmd.extend(['--password', self.auth[1][:-1]])
|
||||
#cmd.extend(['-c', cluster])
|
||||
cmd.append(templatefile)
|
||||
res = process.system_out(cmd)
|
||||
if res[0] == 0:
|
||||
logger.info(u"OneClient: create network named '{name}'".format(name=vnet_name))
|
||||
cmd = ['onevnet', 'create', templatefile]
|
||||
res = self.__run_cmd__(cmd)
|
||||
if res and res[0] == 0:
|
||||
msg = u"OneClient: attach vnet '{vnet}' to cluster '{cluster}'"
|
||||
logger.info(msg.format(vnet=vnet_name, cluster=cluster))
|
||||
|
||||
clt_id = self.get_cluster_id_by_name(cluster)
|
||||
vnet_id = self.get_vnet_id_by_name(vnet_name)
|
||||
res = self.__run_cmd__(['onecluster', 'addvnet', clt_id, vnet_id])
|
||||
os.remove(templatefile)
|
||||
|
||||
if not res:
|
||||
print("Error attaching {0} vnet to {1} cluster".format(vnet_name, cluster))
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
os.remove(templatefile)
|
||||
|
||||
else:
|
||||
logger.error("Creation of virtual network with template {0} failed".format(templatefile))
|
||||
logger.error(u"Creation of virtual network with template {0} failed".format(templatefile))
|
||||
return False
|
||||
|
||||
|
||||
def update_network(self, templatefile, cluster, vnet_name):
|
||||
""" Update a network
|
||||
"""
|
||||
logger.info(u"OneClient: update network named '{name}'".format(name=vnet_name))
|
||||
vnet_id = self.get_vnet_id_by_name(vnet_name)
|
||||
cmd = ['onevnet', 'update']
|
||||
cmd.extend(['--user', self.auth[0]])
|
||||
|
@ -152,10 +153,11 @@ class OneClient():
|
|||
os.remove(templatefile)
|
||||
return True
|
||||
else:
|
||||
logger.error("Update of virtual network with template {0} failed".format(templatefile))
|
||||
logger.error(u"Update of virtual network with template {0} failed".format(templatefile))
|
||||
return False
|
||||
|
||||
def delete_network(self, vnet_id):
|
||||
logger.info(u"OneClient: delete network with ID '{id}'".format(id=vnet_id))
|
||||
cmd = ['onevnet', 'delete']
|
||||
cmd.extend(['--user', self.auth[0]])
|
||||
cmd.extend(['--password', self.auth[1][:-1]])
|
||||
|
@ -163,34 +165,36 @@ class OneClient():
|
|||
|
||||
res = process.system_out(cmd)
|
||||
if res[0] == 0:
|
||||
ihm.print_line("Network {0} deleted".format(vnet_id))
|
||||
logger.info(u"Network {0} deleted".format(vnet_id))
|
||||
return True
|
||||
else:
|
||||
logger.error("Error deleting network {0}".format(vnet_id))
|
||||
ihm.print_line("Error deleting network {0}".format(vnet_id))
|
||||
logger.error(u"Error deleting network {0}".format(vnet_id))
|
||||
return False
|
||||
|
||||
|
||||
class OneNetwork():
|
||||
def create(self, one_client):
|
||||
logger.debug(u"OneNetwork: create network named '{name}'".format(name=self.zone))
|
||||
tmpl_file = self.create_template()
|
||||
if one_client.create_network(tmpl_file, self.cluster, self.zone):
|
||||
ihm.print_line("Virtual network {0} created".format(self.zone))
|
||||
logger.info(u"Virtual network {0} created".format(self.zone))
|
||||
return True
|
||||
else:
|
||||
ihm.print_line("Error Creating virtual network {0}".format(self.zone))
|
||||
logger.error(u"Error Creating virtual network {0}".format(self.zone))
|
||||
return False
|
||||
|
||||
def update(self, one_client):
|
||||
logger.debug(u"OneNetwork: update network named '{name}'".format(name=self.zone))
|
||||
tmpl_file = self.create_template(True)
|
||||
if one_client.update_network(tmpl_file, self.cluster, self.zone):
|
||||
ihm.print_line("Virtual network {0} updated".format(self.zone))
|
||||
logger.info(u"Virtual network {0} updated".format(self.zone))
|
||||
return True
|
||||
else:
|
||||
ihm.print_line("Error Updating virtual network {0}".format(self.zone))
|
||||
logger.error(u"Error Updating virtual network {0}".format(self.zone))
|
||||
return False
|
||||
|
||||
def manage(self, one_client):
|
||||
logger.debug(u"OneNetwork: manage network named '{name}'".format(name=self.zone))
|
||||
found = False
|
||||
vnet = one_client.get_networks()
|
||||
network_name = self.zone
|
||||
|
@ -219,10 +223,12 @@ class OneNetworkL3(OneNetwork):
|
|||
self.cluster = cluster
|
||||
|
||||
def create_template(self, update=False):
|
||||
logger.debug(u"OneNetworkL3: create template for network named '{name}'".format(name=self.zone))
|
||||
fd, tmp_path = mkstemp(prefix='oneVnet-')
|
||||
template = open(tmp_path, 'w')
|
||||
template.write('NAME = "{0}"\n'.format(self.zone))
|
||||
template.write('VN_MAD = ovswitch\n')
|
||||
|
||||
if (update is False):
|
||||
if self.vnet_rg_start and self.vnet_rg_size:
|
||||
template.write('AR=[\n')
|
||||
|
@ -260,6 +266,7 @@ class OneNetworkL2(OneNetwork):
|
|||
self.cluster = cluster
|
||||
|
||||
def create_template(self,update=False):
|
||||
logger.debug(u"OneNetworkL2: create template for network named '{name}'".format(name=self.zone))
|
||||
fd, tmp_path = mkstemp(prefix='oneVnet-')
|
||||
template = open(tmp_path, 'w')
|
||||
template.write('NAME = "{0}"\n'.format(self.zone))
|
||||
|
@ -286,6 +293,15 @@ class OneNetworkL2(OneNetwork):
|
|||
return tmp_path
|
||||
|
||||
def main():
|
||||
global logger
|
||||
logger = init_logging(name='postservice.opennebula.network',
|
||||
level=u'INFO',
|
||||
as_root=True,
|
||||
console=True,
|
||||
syslog=True)
|
||||
|
||||
logger.debug(u"Configure OpenNebula networks")
|
||||
|
||||
client = CreoleClient()
|
||||
one_client = OneClient('oneadmin')
|
||||
networks = []
|
||||
|
@ -345,7 +361,7 @@ def main():
|
|||
if not network.manage(one_client):
|
||||
exit(1)
|
||||
else:
|
||||
ihm.print_line(u'Open vSwitch disabled no need to configure virtual networks')
|
||||
logger.info(u'Open vSwitch disabled no need to configure virtual networks')
|
||||
|
||||
networks = one_client.get_networks()
|
||||
for net in networks:
|
||||
|
|
Loading…
Reference in New Issue