Add accidentally deleted NAT setup command
[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
50 def uninstall_package(ssh, package):
51     """If there are packages installed, clean them up.
52
53     :param ssh: SSH() object connected to a node.
54     :param package: Package name.
55     :type ssh: SSH() object
56     :type package: str
57     """
58     if dist()[0] == 'Ubuntu':
59         ret, _, _ = ssh.exec_command("dpkg -l | grep {package}".format(
60             package=package))
61         if ret == 0:
62             # Try to fix interrupted installations first.
63             execute_command_ssh(ssh, 'dpkg --configure -a', sudo=True)
64             # Try to remove installed packages
65             execute_command_ssh(ssh, 'apt-get purge -y "*{package}*"'.format(
66                 package=package), sudo=True)
67
68
69 def kill_process(ssh, process):
70     """If there are running processes, kill them.
71
72     :param ssh: SSH() object connected to a node.
73     :param process: Process name.
74     :type ssh: SSH() object
75     :type process: str
76     """
77     execute_command_ssh(ssh, 'killall -v -s 9 {process}'.format(
78         process=process), sudo=True)
79
80
81 def main():
82     """Testbed cleanup."""
83
84     parser = argparse.ArgumentParser()
85     parser.add_argument("-t", "--topo", required=True, help="Topology file")
86
87     args = parser.parse_args()
88     topology_file = args.topo
89
90     topology = load(open(topology_file).read())['nodes']
91
92     ssh = SSH()
93     for node in topology:
94         if topology[node]['type'] == "DUT":
95             print "###TI host: {}".format(topology[node]['host'])
96             ssh.connect(topology[node])
97
98             # Kill processes.
99             kill_process(ssh, 'qemu')
100             kill_process(ssh, 'l3fwd')
101             kill_process(ssh, 'testpmd')
102
103             # Uninstall packages
104             uninstall_package(ssh, 'vpp')
105             uninstall_package(ssh, 'honeycomb')
106
107             # Remove HC logs.
108             execute_command_ssh(
109                 ssh, 'rm -rf /var/log/honeycomb', sudo=True)
110
111             # Kill all containers.
112             execute_command_ssh(
113                 ssh, 'docker rm --force $(sudo docker ps -q)', sudo=True)
114
115             # Destroy kubernetes.
116             execute_command_ssh(
117                 ssh, 'kubeadm reset --force', sudo=True)
118
119             # Remove corefiles leftovers.
120             execute_command_ssh(
121                 ssh, 'rm -f /tmp/*tar.lzo.lrz.xz*', sudo=True)
122
123             # Remove corefiles leftovers.
124             execute_command_ssh(
125                 ssh, 'rm -f /tmp/*core*', sudo=True)
126
127             # Set interfaces in topology down.
128             for interface in topology[node]['interfaces']:
129                 pci = topology[node]['interfaces'][interface]['pci_address']
130                 execute_command_ssh(
131                     ssh, "[[ -d {path}/{pci}/net ]] && "
132                     "sudo ip link set $(basename {path}/{pci}/net/*) down".
133                     format(pci=pci, path='/sys/bus/pci/devices'))
134
135
136 if __name__ == "__main__":
137     sys.exit(main())