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:
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 copy and installation of VPP build deb packages.
17 As destinations are used all DUT nodes from the topology file."""
23 from resources.libraries.python.ssh import SSH
26 def ssh_no_error(ssh, cmd, sudo=False):
27 """Execute a command over ssh channel, and log and exit if the command
30 :param ssh: SSH() object connected to a node.
31 :param cmd: Command line to execute on remote node.
32 :type ssh: SSH() object
34 :return: stdout from the SSH command.
39 ret, stdo, stde = ssh.exec_command_sudo(cmd, timeout=60)
41 ret, stdo, stde = ssh.exec_command(cmd, timeout=60)
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')
52 def ssh_ignore_error(ssh, cmd, sudo=False):
53 """Execute a command over ssh channel, ignore errors.
55 :param ssh: SSH() object connected to a node.
56 :param cmd: Command line to execute on remote node.
57 :type ssh: SSH() object
59 :return: stdout from the SSH command.
64 ret, stdo, stde = ssh.exec_command_sudo(cmd)
66 ret, stdo, stde = ssh.exec_command(cmd)
69 print 'Command execution failed: "{}"'.format(cmd)
70 print 'stdout: {0}'.format(stdo)
71 print 'stderr: {0}'.format(stde)
77 """Copy and installation of VPP packages."""
79 parser = argparse.ArgumentParser()
80 parser.add_argument("-t", "--topo", required=True,
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",
88 args = parser.parse_args()
89 topology_file = args.topo
90 packages = args.packages
91 install_dir = args.directory
92 cancel_installation = args.cancel
94 work_file = open(topology_file)
95 topology = load(work_file.read())['nodes']
99 if topology[node]['type'] == "DUT":
100 print "###TI host: {}".format(topology[node]['host'])
101 ssh.connect(topology[node])
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)
109 cmd = "dpkg -l | grep vpp"
110 ret, _, _ = ssh.exec_command(cmd)
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)
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)
126 # Copy packages from local path to installation dir
128 print "###TI scp: {}".format(deb)
129 ssh.scp(local_path=deb, remote_path=install_dir)
131 cmd = "dpkg -l | grep vpp"
132 ret, _, _ = ssh.exec_command(cmd)
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)
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)
148 if __name__ == "__main__":