blob: 2ab008d52be16efcc7c0ebeae3d90167672b1cc7 [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}
Elena Ezhovaa5268942014-05-21 14:55:15 +040031 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 Fazekasaeeeefd2013-08-06 17:01:56 +020038
39
40def ip_addr_raw():
41 return sudo_cmd_call("ip a")
42
43
44def ip_route_raw():
45 return sudo_cmd_call("ip r")
46
47
48def ip_ns_raw():
49 return sudo_cmd_call("ip netns list")
50
51
52def iptables_raw(table):
Sean Dague8976c9d2014-07-02 11:12:26 -040053 return sudo_cmd_call("iptables --line-numbers -L -nv -t " + table)
Attila Fazekasaeeeefd2013-08-06 17:01:56 +020054
55
56def ip_ns_list():
57 return ip_ns_raw().split()
58
59
60def ip_ns_exec(ns, cmd):
61 return sudo_cmd_call(" ".join(("ip netns exec", ns, cmd)))
62
63
64def ip_ns_addr(ns):
65 return ip_ns_exec(ns, "ip a")
66
67
68def ip_ns_route(ns):
69 return ip_ns_exec(ns, "ip r")
70
71
72def iptables_ns(ns, table):
73 return ip_ns_exec(ns, "iptables -v -S -t " + table)
Attila Fazekas6bfd6492014-02-26 21:25:53 +010074
75
76def ovs_db_dump():
77 return sudo_cmd_call("ovsdb-client dump")
Elena Ezhova6e73f462014-04-18 17:38:13 +040078
79
80def 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