aboutsummaryrefslogtreecommitdiffstats
path: root/resources/tools/scripts/topo_cleanup.py
blob: 51a7dcead58045e111b250ee5a3642200244f68a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python

# Copyright (c) 2018 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This script provides cleanup routines on all DUTs."""

import argparse
import sys
from platform import dist
from yaml import load

from resources.libraries.python.ssh import SSH


def execute_command_ssh(ssh, cmd, sudo=False):
    """Execute a command over ssh channel, and print outputs.

    :param ssh: SSH() object connected to a node.
    :param cmd: Command line to execute on remote node.
    :param sudo: Run command with sudo privilege level..
    :type ssh: SSH() object
    :type cmd: str
    :type sudo: bool
    :returns return_code, stdout, stderr
    :rtype: tuple(int, str, str)
    """
    if sudo:
        ret, stdout, stderr = ssh.exec_command_sudo(cmd, timeout=60)
    else:
        ret, stdout, stderr = ssh.exec_command(cmd, timeout=60)

    print 'Executing: {cmd}'.format(cmd=cmd)
    print '({ret}) {stdout} {stderr}'.format(ret=ret, stdout=stdout,
                                             stderr=stderr)

    return ret, stdout, stdout

def uninstall_package(ssh, package):
    """If there are packages installed, clean them up.

    :param ssh: SSH() object connected to a node.
    :param package: Package name.
    :type ssh: SSH() object
    :type package: str
    """
    if dist()[0] == 'Ubuntu':
        ret, _, _ = ssh.exec_command("dpkg -l | grep {package}".format(
            package=package))
        if ret == 0:
            # Try to fix interrupted installations first.
            execute_command_ssh(ssh, 'dpkg --configure -a', sudo=True)
            # Try to remove installed packages
            execute_command_ssh(ssh, 'apt-get purge -y "{package}.*"'.format(
                package=package), sudo=True)

def kill_process(ssh, process):
    """If there are running processes, kill them.

    :param ssh: SSH() object connected to a node.
    :param process: Process name.
    :type ssh: SSH() object
    :type process: str
    """
    execute_command_ssh(ssh, 'killall -v -s 9 {process}'.format(
        process=process), sudo=True)


def main():
    """Testbed cleanup."""

    parser = argparse.ArgumentParser()
    parser.add_argument("-t", "--topo", required=True, help="Topology file")

    args = parser.parse_args()
    topology_file = args.topo

    topology = load(open(topology_file).read())['nodes']

    ssh = SSH()
    for node in topology:
        if topology[node]['type'] == "DUT":
            print "###TI host: {}".format(topology[node]['host'])
            ssh.connect(topology[node])

            # Kill processes.
            kill_process(ssh, 'qemu')
            kill_process(ssh, 'l3fwd')
            kill_process(ssh, 'testpmd')

            # Uninstall packages
            uninstall_package(ssh, 'vpp')
            uninstall_package(ssh, 'honeycomb')

            # Remove HC logs.
            execute_command_ssh(ssh, 'rm -rf /var/log/honeycomb',
                                sudo=True)

            # Kill all containers.
            execute_command_ssh(ssh, 'docker rm --force $(sudo docker ps -q)',
                                sudo=True)

            # Destroy kubernetes.
            execute_command_ssh(ssh, 'kubeadm reset --force',
                                sudo=True)

if __name__ == "__main__":
    sys.exit(main())