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} |
| 31 | try: |
Vladislav Kuzmin | 4c930bd | 2014-01-31 05:13:12 -0500 | [diff] [blame] | 32 | proc = subprocess.Popen(['/usr/bin/sudo', '-n'] + args, |
| 33 | **subprocess_args) |
Attila Fazekas | aeeeefd | 2013-08-06 17:01:56 +0200 | [diff] [blame] | 34 | return proc.communicate()[0] |
| 35 | if proc.returncode != 0: |
| 36 | LOG.error(cmd + "returned with: " + |
| 37 | proc.returncode + "exit status") |
| 38 | except subprocess.CalledProcessError as e: |
| 39 | LOG.error("command output:\n%s" % e.output) |
| 40 | |
| 41 | |
| 42 | def ip_addr_raw(): |
| 43 | return sudo_cmd_call("ip a") |
| 44 | |
| 45 | |
| 46 | def ip_route_raw(): |
| 47 | return sudo_cmd_call("ip r") |
| 48 | |
| 49 | |
| 50 | def ip_ns_raw(): |
| 51 | return sudo_cmd_call("ip netns list") |
| 52 | |
| 53 | |
| 54 | def iptables_raw(table): |
| 55 | return sudo_cmd_call("iptables -v -S -t " + table) |
| 56 | |
| 57 | |
| 58 | def ip_ns_list(): |
| 59 | return ip_ns_raw().split() |
| 60 | |
| 61 | |
| 62 | def ip_ns_exec(ns, cmd): |
| 63 | return sudo_cmd_call(" ".join(("ip netns exec", ns, cmd))) |
| 64 | |
| 65 | |
| 66 | def ip_ns_addr(ns): |
| 67 | return ip_ns_exec(ns, "ip a") |
| 68 | |
| 69 | |
| 70 | def ip_ns_route(ns): |
| 71 | return ip_ns_exec(ns, "ip r") |
| 72 | |
| 73 | |
| 74 | def iptables_ns(ns, table): |
| 75 | return ip_ns_exec(ns, "iptables -v -S -t " + table) |