blob: 6405eaab9dbbbdd2d5f67e155a4a4e84f1c38da6 [file] [log] [blame]
Attila Fazekasaeeeefd2013-08-06 17:01:56 +02001# 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
15import shlex
16import subprocess
17
18from tempest.openstack.common import log as logging
19
20LOG = 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
27def sudo_cmd_call(cmd):
28 args = shlex.split(cmd)
29 subprocess_args = {'stdout': subprocess.PIPE,
30 'stderr': subprocess.STDOUT}
31 try:
Vladislav Kuzmin4c930bd2014-01-31 05:13:12 -050032 proc = subprocess.Popen(['/usr/bin/sudo', '-n'] + args,
33 **subprocess_args)
Attila Fazekasaeeeefd2013-08-06 17:01:56 +020034 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
42def ip_addr_raw():
43 return sudo_cmd_call("ip a")
44
45
46def ip_route_raw():
47 return sudo_cmd_call("ip r")
48
49
50def ip_ns_raw():
51 return sudo_cmd_call("ip netns list")
52
53
54def iptables_raw(table):
55 return sudo_cmd_call("iptables -v -S -t " + table)
56
57
58def ip_ns_list():
59 return ip_ns_raw().split()
60
61
62def ip_ns_exec(ns, cmd):
63 return sudo_cmd_call(" ".join(("ip netns exec", ns, cmd)))
64
65
66def ip_ns_addr(ns):
67 return ip_ns_exec(ns, "ip a")
68
69
70def ip_ns_route(ns):
71 return ip_ns_exec(ns, "ip r")
72
73
74def iptables_ns(ns, table):
75 return ip_ns_exec(ns, "iptables -v -S -t " + table)