3 # Copyright (c) 2018 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
49 def uninstall_package(ssh, package):
50 """If there are packages installed, clean them up.
52 :param ssh: SSH() object connected to a node.
53 :param package: Package name.
54 :type ssh: SSH() object
57 if dist()[0] == 'Ubuntu':
58 ret, _, _ = ssh.exec_command("dpkg -l | grep {package}".format(
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)
67 def kill_process(ssh, process):
68 """If there are running processes, kill them.
70 :param ssh: SSH() object connected to a node.
71 :param process: Process name.
72 :type ssh: SSH() object
75 execute_command_ssh(ssh, 'killall -v -s 9 {process}'.format(
76 process=process), sudo=True)
80 """Testbed cleanup."""
82 parser = argparse.ArgumentParser()
83 parser.add_argument("-t", "--topo", required=True, help="Topology file")
85 args = parser.parse_args()
86 topology_file = args.topo
88 topology = load(open(topology_file).read())['nodes']
92 if topology[node]['type'] == "DUT":
93 print "###TI host: {}".format(topology[node]['host'])
94 ssh.connect(topology[node])
97 kill_process(ssh, 'qemu')
98 kill_process(ssh, 'l3fwd')
99 kill_process(ssh, 'testpmd')
102 uninstall_package(ssh, 'vpp')
103 uninstall_package(ssh, 'honeycomb')
106 execute_command_ssh(ssh, 'rm -rf /var/log/honeycomb',
109 # Kill all containers.
110 execute_command_ssh(ssh, 'docker rm $(sudo docker ps -a -q)',
113 # Destroy kubernetes.
114 execute_command_ssh(ssh, 'kubeadm reset --force',
117 if __name__ == "__main__":