Attila Fazekas | aeeeefd | 2013-08-06 17:01:56 +0200 | [diff] [blame] | 1 | # All Rights Reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import shlex |
| 16 | import subprocess |
| 17 | |
| 18 | from tempest.openstack.common import log as logging |
| 19 | |
| 20 | LOG = logging.getLogger(__name__) |
| 21 | |
| 22 | # NOTE(afazekas): |
| 23 | # These commands assumes the tempest node is the same as |
| 24 | # the only one service node. all-in-one installation. |
| 25 | |
| 26 | |
| 27 | def sudo_cmd_call(cmd): |
| 28 | args = shlex.split(cmd) |
| 29 | subprocess_args = {'stdout': subprocess.PIPE, |
| 30 | 'stderr': subprocess.STDOUT} |
Elena Ezhova | a526894 | 2014-05-21 14:55:15 +0400 | [diff] [blame] | 31 | proc = subprocess.Popen(['/usr/bin/sudo', '-n'] + args, |
| 32 | **subprocess_args) |
| 33 | stdout = proc.communicate()[0] |
| 34 | if proc.returncode != 0: |
| 35 | LOG.error(("Command {0} returned with exit status {1}," |
| 36 | "output {2}").format(cmd, proc.returncode, stdout)) |
| 37 | return stdout |
Attila Fazekas | aeeeefd | 2013-08-06 17:01:56 +0200 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def ip_addr_raw(): |
| 41 | return sudo_cmd_call("ip a") |
| 42 | |
| 43 | |
| 44 | def ip_route_raw(): |
| 45 | return sudo_cmd_call("ip r") |
| 46 | |
| 47 | |
| 48 | def ip_ns_raw(): |
| 49 | return sudo_cmd_call("ip netns list") |
| 50 | |
| 51 | |
| 52 | def iptables_raw(table): |
Sean Dague | 8976c9d | 2014-07-02 11:12:26 -0400 | [diff] [blame] | 53 | return sudo_cmd_call("iptables --line-numbers -L -nv -t " + table) |
Attila Fazekas | aeeeefd | 2013-08-06 17:01:56 +0200 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def ip_ns_list(): |
| 57 | return ip_ns_raw().split() |
| 58 | |
| 59 | |
| 60 | def ip_ns_exec(ns, cmd): |
| 61 | return sudo_cmd_call(" ".join(("ip netns exec", ns, cmd))) |
| 62 | |
| 63 | |
| 64 | def ip_ns_addr(ns): |
| 65 | return ip_ns_exec(ns, "ip a") |
| 66 | |
| 67 | |
| 68 | def ip_ns_route(ns): |
| 69 | return ip_ns_exec(ns, "ip r") |
| 70 | |
| 71 | |
| 72 | def iptables_ns(ns, table): |
| 73 | return ip_ns_exec(ns, "iptables -v -S -t " + table) |
Attila Fazekas | 6bfd649 | 2014-02-26 21:25:53 +0100 | [diff] [blame] | 74 | |
| 75 | |
| 76 | def ovs_db_dump(): |
| 77 | return sudo_cmd_call("ovsdb-client dump") |
Elena Ezhova | 6e73f46 | 2014-04-18 17:38:13 +0400 | [diff] [blame] | 78 | |
| 79 | |
| 80 | def copy_file_to_host(file_from, dest, host, username, pkey): |
| 81 | dest = "%s@%s:%s" % (username, host, dest) |
| 82 | cmd = "scp -v -o UserKnownHostsFile=/dev/null " \ |
| 83 | "-o StrictHostKeyChecking=no " \ |
| 84 | "-i %(pkey)s %(file1)s %(dest)s" % {'pkey': pkey, |
| 85 | 'file1': file_from, |
| 86 | 'dest': dest} |
| 87 | args = shlex.split(cmd) |
| 88 | subprocess_args = {'stdout': subprocess.PIPE, |
| 89 | 'stderr': subprocess.STDOUT} |
| 90 | proc = subprocess.Popen(args, **subprocess_args) |
| 91 | stdout, stderr = proc.communicate() |
| 92 | if proc.returncode != 0: |
| 93 | LOG.error(("Command {0} returned with exit status {1}," |
| 94 | "output {2}, error {3}").format(cmd, proc.returncode, |
| 95 | stdout, stderr)) |
| 96 | return stdout |