FIX: Add SRIOV cleanup
[csit.git] / resources / tools / scripts / topo_cleanup.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2019 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """This script provides cleanup routines on all DUTs."""
17
18 import argparse
19 import sys
20 from platform import dist
21 from yaml import load
22
23 from resources.libraries.python.ssh import SSH
24
25
26 def execute_command_ssh(ssh, cmd, sudo=False):
27     """Execute a command over ssh channel, and print outputs.
28
29     :param ssh: SSH() object connected to a node.
30     :param cmd: Command line to execute on remote node.
31     :param sudo: Run command with sudo privilege level..
32     :type ssh: SSH() object
33     :type cmd: str
34     :type sudo: bool
35     :returns return_code, stdout, stderr
36     :rtype: tuple(int, str, str)
37     """
38     if sudo:
39         ret, stdout, stderr = ssh.exec_command_sudo(cmd, timeout=60)
40     else:
41         ret, stdout, stderr = ssh.exec_command(cmd, timeout=60)
42
43     print 'Executing: {cmd}'.format(cmd=cmd)
44     print '({ret}) {stdout} {stderr}'.format(ret=ret, stdout=stdout,
45                                              stderr=stderr)
46
47     return ret, stdout, stdout
48
49 def uninstall_package(ssh, package):
50     """If there are packages installed, clean them up.
51
52     :param ssh: SSH() object connected to a node.
53     :param package: Package name.
54     :type ssh: SSH() object
55     :type package: str
56     """
57     if dist()[0] == 'Ubuntu':
58         ret, _, _ = ssh.exec_command("dpkg -l | grep {package}".format(
59             package=package))
60         if ret == 0:
61             # Try to fix interrupted installations first.
62             execute_command_ssh(ssh, 'dpkg --configure -a', sudo=True)
63             # Try to remove installed packages
64             execute_command_ssh(ssh, 'apt-get purge -y "*{package}*"'.format(
65                 package=package), sudo=True)
66
67 def kill_process(ssh, process):
68     """If there are running processes, kill them.
69
70     :param ssh: SSH() object connected to a node.
71     :param process: Process name.
72     :type ssh: SSH() object
73     :type process: str
74     """
75     execute_command_ssh(ssh, 'killall -v -s 9 {process}'.format(
76         process=process), sudo=True)
77
78
79 def main():
80     """Testbed cleanup."""
81
82     parser = argparse.ArgumentParser()
83     parser.add_argument("-t", "--topo", required=True, help="Topology file")
84
85     args = parser.parse_args()
86     topology_file = args.topo
87
88     topology = load(open(topology_file).read())['nodes']
89
90     ssh = SSH()
91     for node in topology:
92         if topology[node]['type'] == "DUT":
93             print "###TI host: {}".format(topology[node]['host'])
94             ssh.connect(topology[node])
95
96             # Kill processes.
97             kill_process(ssh, 'qemu')
98             kill_process(ssh, 'l3fwd')
99             kill_process(ssh, 'testpmd')
100
101             # Uninstall packages
102             uninstall_package(ssh, 'vpp')
103             uninstall_package(ssh, 'honeycomb')
104
105             # Remove HC logs.
106             execute_command_ssh(
107                 ssh, 'rm -rf /var/log/honeycomb', sudo=True)
108
109             # Kill all containers.
110             execute_command_ssh(
111                 ssh, 'docker rm --force $(sudo docker ps -q)', sudo=True)
112
113             # Destroy kubernetes.
114             execute_command_ssh(
115                 ssh, 'kubeadm reset --force', sudo=True)
116
117             # Remove corefiles leftovers.
118             execute_command_ssh(
119                 ssh, 'rm -f /tmp/*tar.lzo.lrz.xz*', sudo=True)
120
121             # Remove corefiles leftovers.
122             execute_command_ssh(
123                 ssh, 'rm -f /tmp/*core*', sudo=True)
124
125             # Set interfaces in topology down.
126             for interface in topology[node]['interfaces']:
127                 pci = topology[node]['interfaces'][interface]['pci_address']
128                 execute_command_ssh(
129                     ssh, "ip link set "
130                     "$(basename /sys/bus/pci/devices/{pci}/net/*) down".
131                     format(pci=pci), sudo=True)
132
133
134 if __name__ == "__main__":
135     sys.exit(main())