CSIT-472: Update of DPDK to 16.11 in Nested VM
[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 def ssh_ignore_error(ssh, cmd, sudo=False):
51     """Execute a command over ssh channel, ignore errors.
52
53     :param ssh: SSH() object connected to a node.
54     :param cmd: Command line to execute on remote node.
55     :type ssh: SSH() object
56     :type cmd: str
57     :return: stdout from the SSH command.
58     :rtype: str
59     """
60
61     if sudo:
62         ret, stdo, stde = ssh.exec_command_sudo(cmd)
63     else:
64         ret, stdo, stde = ssh.exec_command(cmd)
65
66     if ret != 0:
67         print 'Command execution failed: "{}"'.format(cmd)
68         print 'stdout: {0}'.format(stdo)
69         print 'stderr: {0}'.format(stde)
70
71     return stdo
72
73 def main():
74     """Copy and installation of VPP packages."""
75
76     parser = argparse.ArgumentParser()
77     parser.add_argument("-t", "--topo", required=True,
78                         help="Topology file")
79     parser.add_argument("-d", "--directory", required=True,
80                         help="Installation directory")
81     parser.add_argument("-p", "--packages", required=False, nargs='+',
82                         help="Packages paths to copy")
83     parser.add_argument("-c", "--cancel", help="Cancel installation",
84                         action="store_true")
85     args = parser.parse_args()
86     topology_file = args.topo
87     packages = args.packages
88     install_dir = args.directory
89     cancel_installation = args.cancel
90
91     work_file = open(topology_file)
92     topology = load(work_file.read())['nodes']
93
94     ssh = SSH()
95     for node in topology:
96         if topology[node]['type'] == "DUT":
97             print "###TI host: {}".format(topology[node]['host'])
98             ssh.connect(topology[node])
99
100             if cancel_installation:
101                 # Remove installation directory on DUT
102                 cmd = "rm -r {}".format(install_dir)
103                 stdout = ssh_ignore_error(ssh, cmd)
104                 print "###TI {}".format(stdout)
105
106                 cmd = "dpkg -l | grep vpp"
107                 ret, _, _ = ssh.exec_command(cmd)
108                 if ret == 0:
109                     # Try to fix interrupted installations
110                     cmd = 'dpkg --configure -a'
111                     stdout = ssh_no_error(ssh, cmd, sudo=True)
112                     print "###TI {}".format(stdout)
113                     # Try to remove installed vpp.* packages
114                     cmd = 'apt-get purge -y "vpp.*"'
115                     stdout = ssh_no_error(ssh, cmd, sudo=True)
116                     print "###TI {}".format(stdout)
117             else:
118                 # Create installation directory on DUT
119                 cmd = "mkdir {}".format(install_dir)
120                 stdout = ssh_no_error(ssh, cmd)
121                 print "###TI {}".format(stdout)
122
123                 # Copy packages from local path to installation dir
124                 for deb in packages:
125                     print "###TI scp: {}".format(deb)
126                     ssh.scp(local_path=deb, remote_path=install_dir)
127
128                 cmd = "dpkg -l | grep vpp"
129                 ret, _, _ = ssh.exec_command(cmd)
130                 if ret == 0:
131                     # Try to fix interrupted installations
132                     cmd = 'dpkg --configure -a'
133                     stdout = ssh_no_error(ssh, cmd, sudo=True)
134                     print "###TI {}".format(stdout)
135                     # Try to remove installed vpp.* packages
136                     cmd = 'apt-get purge -y "vpp.*"'
137                     stdout = ssh_no_error(ssh, cmd, sudo=True)
138                     print "###TI {}".format(stdout)
139
140                 # Installation of VPP deb packages
141                 cmd = "dpkg -i --force-all {}/*.deb".format(install_dir)
142                 stdout = ssh_no_error(ssh, cmd, sudo=True)
143                 print "###TI {}".format(stdout)
144
145 if __name__ == "__main__":
146     sys.exit(main())