CSIT-138: Cleanup of vpp packages.
[csit.git] / resources / tools / topo_installation.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 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 copy and installation of VPP build deb packages.
17    As destinations are used all DUT nodes from the topology file."""
18
19 import sys
20 import argparse
21 from yaml import load
22
23 from resources.libraries.python.ssh import SSH
24
25 def ssh_no_error(ssh, cmd, sudo=False):
26     """Execute a command over ssh channel, and log and exit if the command
27     fails.
28
29     :param ssh: SSH() object connected to a node.
30     :param cmd: Command line to execute on remote node.
31     :type ssh: SSH() object
32     :type cmd: str
33     :return: stdout from the SSH command.
34     :rtype: str
35     """
36
37     if sudo:
38         ret, stdo, stde = ssh.exec_command_sudo(cmd)
39     else:
40         ret, stdo, stde = ssh.exec_command(cmd)
41
42     if ret != 0:
43         print 'Command execution failed: "{}"'.format(cmd)
44         print 'stdout: {0}'.format(stdo)
45         print 'stderr: {0}'.format(stde)
46         raise RuntimeError('Unexpected ssh command failure')
47
48     return stdo
49
50
51 def main():
52     """Copy and installation of VPP packages."""
53
54     parser = argparse.ArgumentParser()
55     parser.add_argument("-t", "--topo", required=True,
56                         help="Topology file")
57     parser.add_argument("-d", "--directory", required=True,
58                         help="Installation directory")
59     parser.add_argument("-p", "--packages", required=False, nargs='+',
60                         help="Packages paths to copy")
61     parser.add_argument("-c", "--cancel", help="Cancel installation",
62                         action="store_true")
63     args = parser.parse_args()
64     topology_file = args.topo
65     packages = args.packages
66     install_dir = args.directory
67     cancel_installation = args.cancel
68
69     work_file = open(topology_file)
70     topology = load(work_file.read())['nodes']
71
72     ssh = SSH()
73     for node in topology:
74         if topology[node]['type'] == "DUT":
75             print "###TI host: {}".format(topology[node]['host'])
76             ssh.connect(topology[node])
77
78             if cancel_installation:
79                 # Remove installation directory on DUT
80                 cmd = "rm -r {}".format(install_dir)
81                 stdout = ssh_no_error(ssh, cmd)
82                 print "###TI {}".format(stdout)
83
84                 cmd = "dpkg -l | grep vpp"
85                 ret, _, _ = ssh.exec_command(cmd)
86                 if ret == 0:
87                     # Try to remove installed vpp.* packages
88                     cmd = 'apt-get purge -y "vpp.*"'
89                     stdout = ssh_no_error(ssh, cmd, sudo=True)
90                     print "###TI {}".format(stdout)
91             else:
92                 # Create installation directory on DUT
93                 cmd = "mkdir {}".format(install_dir)
94                 stdout = ssh_no_error(ssh, cmd)
95                 print "###TI {}".format(stdout)
96
97                 # Copy packages from local path to installation dir
98                 for deb in packages:
99                     print "###TI scp: {}".format(deb)
100                     ssh.scp(local_path=deb, remote_path=install_dir)
101
102                 cmd = "dpkg -l | grep vpp"
103                 ret, _, _ = ssh.exec_command(cmd)
104                 if ret == 0:
105                     # Try to remove installed vpp.* packages
106                     cmd = 'apt-get purge -y "vpp.*"'
107                     stdout = ssh_no_error(ssh, cmd, sudo=True)
108                     print "###TI {}".format(stdout)
109
110                 # Installation of VPP deb packages
111                 cmd = "dpkg -i --force-all {}/*.deb".format(install_dir)
112                 stdout = ssh_no_error(ssh, cmd, sudo=True)
113                 print "###TI {}".format(stdout)
114
115 if __name__ == "__main__":
116     sys.exit(main())