root / qa / qa_utils.py @ e8ae0c20
History | View | Annotate | Download (5.1 kB)
1 |
# Copyright (C) 2007 Google Inc.
|
---|---|
2 |
#
|
3 |
# This program is free software; you can redistribute it and/or modify
|
4 |
# it under the terms of the GNU General Public License as published by
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
6 |
# (at your option) any later version.
|
7 |
#
|
8 |
# This program is distributed in the hope that it will be useful, but
|
9 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
11 |
# General Public License for more details.
|
12 |
#
|
13 |
# You should have received a copy of the GNU General Public License
|
14 |
# along with this program; if not, write to the Free Software
|
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
16 |
# 02110-1301, USA.
|
17 |
|
18 |
|
19 |
"""Utilities for QA tests.
|
20 |
|
21 |
"""
|
22 |
|
23 |
import os |
24 |
import sys |
25 |
import subprocess |
26 |
|
27 |
from ganeti import utils |
28 |
|
29 |
import qa_config |
30 |
import qa_error |
31 |
|
32 |
|
33 |
_INFO_SEQ = None
|
34 |
_WARNING_SEQ = None
|
35 |
_ERROR_SEQ = None
|
36 |
_RESET_SEQ = None
|
37 |
|
38 |
|
39 |
def _SetupColours(): |
40 |
"""Initializes the colour constants.
|
41 |
|
42 |
"""
|
43 |
global _INFO_SEQ, _WARNING_SEQ, _ERROR_SEQ, _RESET_SEQ
|
44 |
|
45 |
try:
|
46 |
import curses |
47 |
except ImportError: |
48 |
# Don't use colours if curses module can't be imported
|
49 |
return
|
50 |
|
51 |
curses.setupterm() |
52 |
|
53 |
_RESET_SEQ = curses.tigetstr("op")
|
54 |
|
55 |
setaf = curses.tigetstr("setaf")
|
56 |
_INFO_SEQ = curses.tparm(setaf, curses.COLOR_GREEN) |
57 |
_WARNING_SEQ = curses.tparm(setaf, curses.COLOR_YELLOW) |
58 |
_ERROR_SEQ = curses.tparm(setaf, curses.COLOR_RED) |
59 |
|
60 |
|
61 |
_SetupColours() |
62 |
|
63 |
|
64 |
def AssertEqual(first, second): |
65 |
"""Raises an error when values aren't equal.
|
66 |
|
67 |
"""
|
68 |
if not first == second: |
69 |
raise qa_error.Error('%r == %r' % (first, second)) |
70 |
|
71 |
|
72 |
def AssertNotEqual(first, second): |
73 |
"""Raises an error when values are equal.
|
74 |
|
75 |
"""
|
76 |
if not first != second: |
77 |
raise qa_error.Error('%r != %r' % (first, second)) |
78 |
|
79 |
|
80 |
def GetSSHCommand(node, cmd, strict=True): |
81 |
"""Builds SSH command to be executed.
|
82 |
|
83 |
"""
|
84 |
args = [ 'ssh', '-oEscapeChar=none', '-oBatchMode=yes', '-l', 'root' ] |
85 |
|
86 |
if strict:
|
87 |
tmp = 'yes'
|
88 |
else:
|
89 |
tmp = 'no'
|
90 |
args.append('-oStrictHostKeyChecking=%s' % tmp)
|
91 |
args.append('-oClearAllForwardings=yes')
|
92 |
args.append('-oForwardAgent=yes')
|
93 |
args.append(node) |
94 |
|
95 |
if qa_config.options.dry_run:
|
96 |
prefix = 'exit 0; '
|
97 |
else:
|
98 |
prefix = ''
|
99 |
|
100 |
args.append(prefix + cmd) |
101 |
|
102 |
print 'SSH:', utils.ShellQuoteArgs(args) |
103 |
|
104 |
return args
|
105 |
|
106 |
|
107 |
def StartSSH(node, cmd, strict=True): |
108 |
"""Starts SSH.
|
109 |
|
110 |
"""
|
111 |
return subprocess.Popen(GetSSHCommand(node, cmd, strict=strict),
|
112 |
shell=False)
|
113 |
|
114 |
|
115 |
def GetCommandOutput(node, cmd): |
116 |
"""Returns the output of a command executed on the given node.
|
117 |
|
118 |
"""
|
119 |
p = subprocess.Popen(GetSSHCommand(node, cmd), |
120 |
shell=False, stdout=subprocess.PIPE)
|
121 |
AssertEqual(p.wait(), 0)
|
122 |
return p.stdout.read()
|
123 |
|
124 |
|
125 |
def UploadFile(node, src): |
126 |
"""Uploads a file to a node and returns the filename.
|
127 |
|
128 |
Caller needs to remove the returned file on the node when it's not needed
|
129 |
anymore.
|
130 |
"""
|
131 |
# Make sure nobody else has access to it while preserving local permissions
|
132 |
mode = os.stat(src).st_mode & 0700
|
133 |
|
134 |
cmd = ('tmp=$(tempfile --mode %o --prefix gnt) && '
|
135 |
'[[ -f "${tmp}" ]] && '
|
136 |
'cat > "${tmp}" && '
|
137 |
'echo "${tmp}"') % mode
|
138 |
|
139 |
f = open(src, 'r') |
140 |
try:
|
141 |
p = subprocess.Popen(GetSSHCommand(node, cmd), shell=False, stdin=f,
|
142 |
stdout=subprocess.PIPE) |
143 |
AssertEqual(p.wait(), 0)
|
144 |
|
145 |
# Return temporary filename
|
146 |
return p.stdout.read().strip()
|
147 |
finally:
|
148 |
f.close() |
149 |
|
150 |
|
151 |
def _ResolveName(cmd, key): |
152 |
"""Helper function.
|
153 |
|
154 |
"""
|
155 |
master = qa_config.GetMasterNode() |
156 |
|
157 |
output = GetCommandOutput(master['primary'], utils.ShellQuoteArgs(cmd))
|
158 |
for line in output.splitlines(): |
159 |
(lkey, lvalue) = line.split(':', 1) |
160 |
if lkey == key:
|
161 |
return lvalue.lstrip()
|
162 |
raise KeyError("Key not found") |
163 |
|
164 |
|
165 |
def ResolveInstanceName(instance): |
166 |
"""Gets the full name of an instance.
|
167 |
|
168 |
"""
|
169 |
return _ResolveName(['gnt-instance', 'info', instance['name']], |
170 |
'Instance name')
|
171 |
|
172 |
|
173 |
def ResolveNodeName(node): |
174 |
"""Gets the full name of a node.
|
175 |
|
176 |
"""
|
177 |
return _ResolveName(['gnt-node', 'info', node['primary']], |
178 |
'Node name')
|
179 |
|
180 |
|
181 |
def GetNodeInstances(node, secondaries=False): |
182 |
"""Gets a list of instances on a node.
|
183 |
|
184 |
"""
|
185 |
master = qa_config.GetMasterNode() |
186 |
|
187 |
node_name = ResolveNodeName(node) |
188 |
|
189 |
# Get list of all instances
|
190 |
cmd = ['gnt-instance', 'list', '--separator=:', '--no-headers', |
191 |
'--output=name,pnode,snodes']
|
192 |
output = GetCommandOutput(master['primary'], utils.ShellQuoteArgs(cmd))
|
193 |
|
194 |
instances = [] |
195 |
for line in output.splitlines(): |
196 |
(name, pnode, snodes) = line.split(':', 2) |
197 |
if ((not secondaries and pnode == node_name) or |
198 |
(secondaries and node_name in snodes.split(','))): |
199 |
instances.append(name) |
200 |
|
201 |
return instances
|
202 |
|
203 |
|
204 |
def _PrintWithColor(text, seq): |
205 |
f = sys.stdout |
206 |
|
207 |
if not f.isatty(): |
208 |
seq = None
|
209 |
|
210 |
if seq:
|
211 |
f.write(seq) |
212 |
|
213 |
f.write(text) |
214 |
f.write("\n")
|
215 |
|
216 |
if seq:
|
217 |
f.write(_RESET_SEQ) |
218 |
|
219 |
|
220 |
def PrintWarning(text): |
221 |
return _PrintWithColor(text, _WARNING_SEQ)
|
222 |
|
223 |
|
224 |
def PrintError(text): |
225 |
return _PrintWithColor(text, _ERROR_SEQ)
|
226 |
|
227 |
|
228 |
def PrintInfo(text): |
229 |
return _PrintWithColor(text, _INFO_SEQ)
|