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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 """This script provides cleanup routines on all DUTs."""
20 from platform import dist
23 from resources.libraries.python.ssh import SSH
26 def execute_command_ssh(ssh, cmd, sudo=False):
27 """Execute a command over ssh channel, and print outputs.
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
35 :returns return_code, stdout, stderr
36 :rtype: tuple(int, str, str)
39 ret, stdout, stderr = ssh.exec_command_sudo(cmd, timeout=60)
41 ret, stdout, stderr = ssh.exec_command(cmd, timeout=60)
43 print 'Executing: {cmd}'.format(cmd=cmd)
44 print '({ret}) {stdout} {stderr}'.format(ret=ret, stdout=stdout,
47 return ret, stdout, stdout
50 def uninstall_package(ssh, package):
51 """If there are packages installed, clean them up.
53 :param ssh: SSH() object connected to a node.
54 :param package: Package name.
55 :type ssh: SSH() object
58 if dist()[0] == 'Ubuntu':
59 ret, _, _ = ssh.exec_command("dpkg -l | grep {package}".format(
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)
69 def kill_process(ssh, process):
70 """If there are running processes, kill them.
72 :param ssh: SSH() object connected to a node.
73 :param process: Process name.
74 :type ssh: SSH() object
77 execute_command_ssh(ssh, 'killall -v -s 9 {process}'.format(
78 process=process), sudo=True)
82 """Testbed cleanup."""
84 parser = argparse.ArgumentParser()
85 parser.add_argument("-t", "--topo", required=True, help="Topology file")
87 args = parser.parse_args()
88 topology_file = args.topo
90 topology = load(open(topology_file).read())['nodes']
94 if topology[node]['type'] == "DUT":
95 print "###TI host: {}".format(topology[node]['host'])
96 ssh.connect(topology[node])
99 kill_process(ssh, 'qemu')
100 kill_process(ssh, 'l3fwd')
101 kill_process(ssh, 'testpmd')
104 uninstall_package(ssh, 'vpp')
105 uninstall_package(ssh, 'honeycomb')
109 ssh, 'rm -rf /var/log/honeycomb', sudo=True)
111 # Kill all containers.
113 ssh, 'docker rm --force $(sudo docker ps -q)', sudo=True)
115 # Destroy kubernetes.
117 ssh, 'kubeadm reset --force', sudo=True)
119 # Remove corefiles leftovers.
121 ssh, 'rm -f /tmp/*tar.lzo.lrz.xz*', sudo=True)
123 # Remove corefiles leftovers.
125 ssh, 'rm -f /tmp/*core*', sudo=True)
127 # Set interfaces in topology down.
128 for interface in topology[node]['interfaces']:
129 pci = topology[node]['interfaces'][interface]['pci_address']
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'))
136 if __name__ == "__main__":