root / qa / qa_utils.py @ dfe11bad
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 |
# Don't use colours if stdout isn't a terminal
|
46 |
if not sys.stdout.isatty(): |
47 |
return
|
48 |
|
49 |
try:
|
50 |
import curses |
51 |
except ImportError: |
52 |
# Don't use colours if curses module can't be imported
|
53 |
return
|
54 |
|
55 |
curses.setupterm() |
56 |
|
57 |
_RESET_SEQ = curses.tigetstr("op")
|
58 |
|
59 |
setaf = curses.tigetstr("setaf")
|
60 |
_INFO_SEQ = curses.tparm(setaf, curses.COLOR_GREEN) |
61 |
_WARNING_SEQ = curses.tparm(setaf, curses.COLOR_YELLOW) |
62 |
_ERROR_SEQ = curses.tparm(setaf, curses.COLOR_RED) |
63 |
|
64 |
|
65 |
_SetupColours() |
66 |
|
67 |
|
68 |
def AssertEqual(first, second): |
69 |
"""Raises an error when values aren't equal.
|
70 |
|
71 |
"""
|
72 |
if not first == second: |
73 |
raise qa_error.Error('%r == %r' % (first, second)) |
74 |
|
75 |
|
76 |
def AssertNotEqual(first, second): |
77 |
"""Raises an error when values are equal.
|
78 |
|
79 |
"""
|
80 |
if not first != second: |
81 |
raise qa_error.Error('%r != %r' % (first, second)) |
82 |
|
83 |
|
84 |
def GetSSHCommand(node, cmd, strict=True): |
85 |
"""Builds SSH command to be executed.
|
86 |
|
87 |
"""
|
88 |
args = [ 'ssh', '-oEscapeChar=none', '-oBatchMode=yes', '-l', 'root' ] |
89 |
|
90 |
if strict:
|
91 |
tmp = 'yes'
|
92 |
else:
|
93 |
tmp = 'no'
|
94 |
args.append('-oStrictHostKeyChecking=%s' % tmp)
|
95 |
args.append('-oClearAllForwardings=yes')
|
96 |
args.append('-oForwardAgent=yes')
|
97 |
args.append(node) |
98 |
|
99 |
if qa_config.options.dry_run:
|
100 |
prefix = 'exit 0; '
|
101 |
else:
|
102 |
prefix = ''
|
103 |
|
104 |
args.append(prefix + cmd) |
105 |
|
106 |
print 'SSH:', utils.ShellQuoteArgs(args) |
107 |
|
108 |
return args
|
109 |
|
110 |
|
111 |
def StartSSH(node, cmd, strict=True): |
112 |
"""Starts SSH.
|
113 |
|
114 |
"""
|
115 |
return subprocess.Popen(GetSSHCommand(node, cmd, strict=strict),
|
116 |
shell=False)
|
117 |
|
118 |
|
119 |
def GetCommandOutput(node, cmd): |
120 |
"""Returns the output of a command executed on the given node.
|
121 |
|
122 |
"""
|
123 |
p = subprocess.Popen(GetSSHCommand(node, cmd), |
124 |
shell=False, stdout=subprocess.PIPE)
|
125 |
AssertEqual(p.wait(), 0)
|
126 |
return p.stdout.read()
|
127 |
|
128 |
|
129 |
def UploadFile(node, src): |
130 |
"""Uploads a file to a node and returns the filename.
|
131 |
|
132 |
Caller needs to remove the returned file on the node when it's not needed
|
133 |
anymore.
|
134 |
"""
|
135 |
# Make sure nobody else has access to it while preserving local permissions
|
136 |
mode = os.stat(src).st_mode & 0700
|
137 |
|
138 |
cmd = ('tmp=$(tempfile --mode %o --prefix gnt) && '
|
139 |
'[[ -f "${tmp}" ]] && '
|
140 |
'cat > "${tmp}" && '
|
141 |
'echo "${tmp}"') % mode
|
142 |
|
143 |
f = open(src, 'r') |
144 |
try:
|
145 |
p = subprocess.Popen(GetSSHCommand(node, cmd), shell=False, stdin=f,
|
146 |
stdout=subprocess.PIPE) |
147 |
AssertEqual(p.wait(), 0)
|
148 |
|
149 |
# Return temporary filename
|
150 |
return p.stdout.read().strip()
|
151 |
finally:
|
152 |
f.close() |
153 |
|
154 |
|
155 |
def _ResolveName(cmd, key): |
156 |
"""Helper function.
|
157 |
|
158 |
"""
|
159 |
master = qa_config.GetMasterNode() |
160 |
|
161 |
output = GetCommandOutput(master['primary'], utils.ShellQuoteArgs(cmd))
|
162 |
for line in output.splitlines(): |
163 |
(lkey, lvalue) = line.split(':', 1) |
164 |
if lkey == key:
|
165 |
return lvalue.lstrip()
|
166 |
raise KeyError("Key not found") |
167 |
|
168 |
|
169 |
def ResolveInstanceName(instance): |
170 |
"""Gets the full name of an instance.
|
171 |
|
172 |
"""
|
173 |
return _ResolveName(['gnt-instance', 'info', instance['name']], |
174 |
'Instance name')
|
175 |
|
176 |
|
177 |
def ResolveNodeName(node): |
178 |
"""Gets the full name of a node.
|
179 |
|
180 |
"""
|
181 |
return _ResolveName(['gnt-node', 'info', node['primary']], |
182 |
'Node name')
|
183 |
|
184 |
|
185 |
def GetNodeInstances(node, secondaries=False): |
186 |
"""Gets a list of instances on a node.
|
187 |
|
188 |
"""
|
189 |
master = qa_config.GetMasterNode() |
190 |
|
191 |
node_name = ResolveNodeName(node) |
192 |
|
193 |
# Get list of all instances
|
194 |
cmd = ['gnt-instance', 'list', '--separator=:', '--no-headers', |
195 |
'--output=name,pnode,snodes']
|
196 |
output = GetCommandOutput(master['primary'], utils.ShellQuoteArgs(cmd))
|
197 |
|
198 |
instances = [] |
199 |
for line in output.splitlines(): |
200 |
(name, pnode, snodes) = line.split(':', 2) |
201 |
if ((not secondaries and pnode == node_name) or |
202 |
(secondaries and node_name in snodes.split(','))): |
203 |
instances.append(name) |
204 |
|
205 |
return instances
|
206 |
|
207 |
|
208 |
def _FormatWithColor(text, seq): |
209 |
if not seq: |
210 |
return text
|
211 |
return "%s%s%s" % (seq, text, _RESET_SEQ) |
212 |
|
213 |
|
214 |
FormatWarning = lambda text: _FormatWithColor(text, _WARNING_SEQ)
|
215 |
FormatError = lambda text: _FormatWithColor(text, _ERROR_SEQ)
|
216 |
FormatInfo = lambda text: _FormatWithColor(text, _INFO_SEQ)
|