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:
Daniel Dehennin 2018-06-29 16:34:15 +02:00
parent d8fc5d10a3
commit cac835e1ee
1 changed files with 54 additions and 38 deletions

View File

@ -1,7 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
from pyeole import ihm
from pyeole import process from pyeole import process
from pyeole.log import init_logging
from creole.client import CreoleClient from creole.client import CreoleClient
from tempfile import mkstemp from tempfile import mkstemp
@ -13,23 +14,7 @@ import re
LOG_FILE = '/var/log/one/eole-one-node.log' LOG_FILE = '/var/log/one/eole-one-node.log'
#= Configure Logger === logger = None
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 ===
class RunCmdError(Exception): class RunCmdError(Exception):
pass pass
@ -55,8 +40,14 @@ class OneClient():
res = process.system_out(command) res = process.system_out(command)
if res[0] == 0: if res[0] == 0:
self.auth = res[1].split(':') 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): def __run_cmd__(self, cmd):
logger.debug(u"OneClient: execute command '{cmd}'".format(cmd=cmd))
command = list(cmd) command = list(cmd)
command.extend(['--user', self.auth[0]]) command.extend(['--user', self.auth[0]])
command.extend(['--password', self.auth[1]]) command.extend(['--password', self.auth[1]])
@ -77,11 +68,14 @@ class OneClient():
else: else:
return res return res
else: else:
msg = u"OneClient: error executing '{cmd}': '{stdout}', '{stderr}'"
logger.error(msg.format(cmd=cmd, stdout=res[1], stderr=res[2]))
return False return False
def get_hosts(self): def get_hosts(self):
""" get the list of hosts """ get the list of hosts
""" """
logger.debug(u"OneClient: list all hosts")
cmd = ['onehost', 'list'] cmd = ['onehost', 'list']
cmd.extend(['-l', 'ID,NAME']) cmd.extend(['-l', 'ID,NAME'])
res = self.__run_cmd__(cmd) res = self.__run_cmd__(cmd)
@ -90,6 +84,7 @@ class OneClient():
def get_clusters(self): def get_clusters(self):
""" get the cluster list """ get the cluster list
""" """
logger.debug(u"OneClient: list all clusters")
cmd = ['onecluster', 'list'] cmd = ['onecluster', 'list']
cmd.extend(['-l', 'ID,NAME']) cmd.extend(['-l', 'ID,NAME'])
return self.__run_cmd__(cmd) return self.__run_cmd__(cmd)
@ -97,11 +92,13 @@ class OneClient():
def get_networks(self): def get_networks(self):
""" get the virtual network list """ get the virtual network list
""" """
logger.debug(u"OneClient: list all networks")
cmd = ['onevnet', 'list'] cmd = ['onevnet', 'list']
cmd.extend(['-l', 'ID,NAME']) cmd.extend(['-l', 'ID,NAME'])
return self.__run_cmd__(cmd) return self.__run_cmd__(cmd)
def get_cluster_id_by_name(self, name): def get_cluster_id_by_name(self, name):
logger.debug(u"OneClient: get cluster named '{name}'".format(name=name))
cmd = ['onecluster', 'list'] cmd = ['onecluster', 'list']
cmd.extend(['-f', 'NAME={0}'.format(name)]) cmd.extend(['-f', 'NAME={0}'.format(name)])
res = self.__run_cmd__(cmd) res = self.__run_cmd__(cmd)
@ -109,6 +106,7 @@ class OneClient():
return ID return ID
def get_vnet_id_by_name(self, name): def get_vnet_id_by_name(self, name):
logger.debug(u"OneClient: get network named '{name}'".format(name=name))
cmd = ['onevnet', 'list'] cmd = ['onevnet', 'list']
cmd.extend(['-f', 'NAME={0}'.format(name)]) cmd.extend(['-f', 'NAME={0}'.format(name)])
res = self.__run_cmd__(cmd) res = self.__run_cmd__(cmd)
@ -118,29 +116,32 @@ class OneClient():
def create_network(self, templatefile, cluster, vnet_name): def create_network(self, templatefile, cluster, vnet_name):
""" Create a network """ Create a network
""" """
cmd = ['onevnet', 'create'] logger.info(u"OneClient: create network named '{name}'".format(name=vnet_name))
cmd.extend(['--user', self.auth[0]]) cmd = ['onevnet', 'create', templatefile]
cmd.extend(['--password', self.auth[1][:-1]]) res = self.__run_cmd__(cmd)
#cmd.extend(['-c', cluster]) if res and res[0] == 0:
cmd.append(templatefile) msg = u"OneClient: attach vnet '{vnet}' to cluster '{cluster}'"
res = process.system_out(cmd) logger.info(msg.format(vnet=vnet_name, cluster=cluster))
if res[0] == 0:
clt_id = self.get_cluster_id_by_name(cluster) clt_id = self.get_cluster_id_by_name(cluster)
vnet_id = self.get_vnet_id_by_name(vnet_name) vnet_id = self.get_vnet_id_by_name(vnet_name)
res = self.__run_cmd__(['onecluster', 'addvnet', clt_id, vnet_id]) res = self.__run_cmd__(['onecluster', 'addvnet', clt_id, vnet_id])
os.remove(templatefile)
if not res: if not res:
print("Error attaching {0} vnet to {1} cluster".format(vnet_name, cluster)) print("Error attaching {0} vnet to {1} cluster".format(vnet_name, cluster))
return False return False
else:
return True os.remove(templatefile)
else: 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 return False
def update_network(self, templatefile, cluster, vnet_name): def update_network(self, templatefile, cluster, vnet_name):
""" Update a network """ 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) vnet_id = self.get_vnet_id_by_name(vnet_name)
cmd = ['onevnet', 'update'] cmd = ['onevnet', 'update']
cmd.extend(['--user', self.auth[0]]) cmd.extend(['--user', self.auth[0]])
@ -152,10 +153,11 @@ class OneClient():
os.remove(templatefile) os.remove(templatefile)
return True return True
else: 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 return False
def delete_network(self, vnet_id): def delete_network(self, vnet_id):
logger.info(u"OneClient: delete network with ID '{id}'".format(id=vnet_id))
cmd = ['onevnet', 'delete'] cmd = ['onevnet', 'delete']
cmd.extend(['--user', self.auth[0]]) cmd.extend(['--user', self.auth[0]])
cmd.extend(['--password', self.auth[1][:-1]]) cmd.extend(['--password', self.auth[1][:-1]])
@ -163,34 +165,36 @@ class OneClient():
res = process.system_out(cmd) res = process.system_out(cmd)
if res[0] == 0: if res[0] == 0:
ihm.print_line("Network {0} deleted".format(vnet_id)) logger.info(u"Network {0} deleted".format(vnet_id))
return True return True
else: else:
logger.error("Error deleting network {0}".format(vnet_id)) logger.error(u"Error deleting network {0}".format(vnet_id))
ihm.print_line("Error deleting network {0}".format(vnet_id))
return False return False
class OneNetwork(): class OneNetwork():
def create(self, one_client): def create(self, one_client):
logger.debug(u"OneNetwork: create network named '{name}'".format(name=self.zone))
tmpl_file = self.create_template() tmpl_file = self.create_template()
if one_client.create_network(tmpl_file, self.cluster, self.zone): 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 return True
else: 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 return False
def update(self, one_client): def update(self, one_client):
logger.debug(u"OneNetwork: update network named '{name}'".format(name=self.zone))
tmpl_file = self.create_template(True) tmpl_file = self.create_template(True)
if one_client.update_network(tmpl_file, self.cluster, self.zone): 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 return True
else: 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 return False
def manage(self, one_client): def manage(self, one_client):
logger.debug(u"OneNetwork: manage network named '{name}'".format(name=self.zone))
found = False found = False
vnet = one_client.get_networks() vnet = one_client.get_networks()
network_name = self.zone network_name = self.zone
@ -219,10 +223,12 @@ class OneNetworkL3(OneNetwork):
self.cluster = cluster self.cluster = cluster
def create_template(self, update=False): 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-') fd, tmp_path = mkstemp(prefix='oneVnet-')
template = open(tmp_path, 'w') template = open(tmp_path, 'w')
template.write('NAME = "{0}"\n'.format(self.zone)) template.write('NAME = "{0}"\n'.format(self.zone))
template.write('VN_MAD = ovswitch\n') template.write('VN_MAD = ovswitch\n')
if (update is False): if (update is False):
if self.vnet_rg_start and self.vnet_rg_size: if self.vnet_rg_start and self.vnet_rg_size:
template.write('AR=[\n') template.write('AR=[\n')
@ -260,6 +266,7 @@ class OneNetworkL2(OneNetwork):
self.cluster = cluster self.cluster = cluster
def create_template(self,update=False): 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-') fd, tmp_path = mkstemp(prefix='oneVnet-')
template = open(tmp_path, 'w') template = open(tmp_path, 'w')
template.write('NAME = "{0}"\n'.format(self.zone)) template.write('NAME = "{0}"\n'.format(self.zone))
@ -286,6 +293,15 @@ class OneNetworkL2(OneNetwork):
return tmp_path return tmp_path
def main(): 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() client = CreoleClient()
one_client = OneClient('oneadmin') one_client = OneClient('oneadmin')
networks = [] networks = []
@ -345,7 +361,7 @@ def main():
if not network.manage(one_client): if not network.manage(one_client):
exit(1) exit(1)
else: 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() networks = one_client.get_networks()
for net in networks: for net in networks: