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