Merge "Enable Daisy Euphrates merge jobs"
[releng.git] / modules / opnfv / utils / ovs_logger.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # Author: George Paraskevopoulos (geopar@intracom-telecom.com)
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import opnfv.utils.opnfv_logger as OPNFVLogger
11 import os
12 import time
13 import shutil
14
15 logger = OPNFVLogger.Logger('ovs_logger').getLogger()
16
17
18 class OVSLogger(object):
19
20     def __init__(self, basedir, ft_resdir):
21         self.ovs_dir = basedir
22         self.ft_resdir = ft_resdir
23         self.__mkdir_p(self.ovs_dir)
24         self.__mkdir_p(self.ft_resdir)
25
26     def __mkdir_p(self, dirpath):
27         if not os.path.exists(dirpath):
28             os.makedirs(dirpath)
29
30     def __ssh_host(self, ssh_conn):
31         try:
32             _, stdout, _ = ssh_conn.exec_command('hostname')
33             found_host = stdout.readline()
34             return found_host
35         except Exception as e:
36             logger.error(e)
37
38     def __dump_to_file(self, operation, host, text, timestamp=None):
39         ts = (timestamp if timestamp is not None
40               else time.strftime("%Y%m%d-%H%M%S"))
41         dumpdir = os.path.join(self.ovs_dir, ts)
42         self.__mkdir_p(dumpdir)
43         fname = '{0}_{1}'.format(operation, host)
44         with open(os.path.join(dumpdir, fname), 'w') as f:
45             f.write(text)
46
47     def __remote_cmd(self, ssh_conn, cmd):
48         try:
49             _, stdout, stderr = ssh_conn.exec_command(cmd)
50             errors = stderr.readlines()
51             if len(errors) > 0:
52                 host = self.__ssh_host(ssh_conn)
53                 logger.error(''.join(errors))
54                 raise Exception('Could not execute {0} in {1}'
55                                 .format(cmd, host))
56             output = ''.join(stdout.readlines())
57             return output
58         except Exception as e:
59             logger.error('[__remote_command(ssh_client, {0})]: {1}'
60                          .format(cmd, e))
61             return None
62
63     def create_artifact_archive(self):
64         shutil.make_archive(self.ovs_dir,
65                             'zip',
66                             root_dir=os.path.dirname(self.ovs_dir),
67                             base_dir=self.ovs_dir)
68         shutil.copy2('{0}.zip'.format(self.ovs_dir), self.ft_resdir)
69
70     def ofctl_dump_flows(self, ssh_conn, br='br-int',
71                          choose_table=None, timestamp=None):
72         try:
73             cmd = 'ovs-ofctl -OOpenFlow13 dump-flows {0}'.format(br)
74             if choose_table is not None:
75                 cmd = '{0} table={1}'.format(cmd, choose_table)
76             output = self.__remote_cmd(ssh_conn, cmd)
77             operation = 'ofctl_dump_flows'
78             host = self.__ssh_host(ssh_conn)
79             self.__dump_to_file(operation, host, output, timestamp=timestamp)
80             return output
81         except Exception as e:
82             logger.error('[ofctl_dump_flows(ssh_client, {0}, {1})]: {2}'
83                          .format(br, choose_table, e))
84             return None
85
86     def vsctl_show(self, ssh_conn, timestamp=None):
87         try:
88             cmd = 'ovs-vsctl show'
89             output = self.__remote_cmd(ssh_conn, cmd)
90             operation = 'vsctl_show'
91             host = self.__ssh_host(ssh_conn)
92             self.__dump_to_file(operation, host, output, timestamp=timestamp)
93             return output
94         except Exception as e:
95             logger.error('[vsctl_show(ssh_client)]: {0}'.format(e))
96             return None
97
98     def dump_ovs_logs(self, controller_clients, compute_clients,
99                       related_error=None, timestamp=None):
100         """
101         delete controller_clients argument because
102         that was producing an error in XCI installer.
103
104         For more information see:
105         TODO: https://jira.opnfv.org/browse/RELENG-314
106         """
107         del controller_clients
108         if timestamp is None:
109             timestamp = time.strftime("%Y%m%d-%H%M%S")
110         # clients = controller_clients + compute_clients
111         clients = compute_clients
112         for client in clients:
113             self.ofctl_dump_flows(client, timestamp=timestamp)
114             self.vsctl_show(client, timestamp=timestamp)
115
116         if related_error is not None:
117             dumpdir = os.path.join(self.ovs_dir, timestamp)
118             self.__mkdir_p(dumpdir)
119             with open(os.path.join(dumpdir, 'error'), 'w') as f:
120                 f.write(related_error)