1 # Copyright (C) 2007 Google Inc.
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.
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.
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
19 """Utilities for QA tests.
27 from ganeti import utils
40 """Initializes the colour constants.
43 global _INFO_SEQ, _WARNING_SEQ, _ERROR_SEQ, _RESET_SEQ
48 # Don't use colours if curses module can't be imported
53 _RESET_SEQ = curses.tigetstr("op")
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)
64 def AssertEqual(first, second, msg=None):
65 """Raises an error when values aren't equal.
68 if not first == second:
69 raise qa_error.Error(msg or '%r == %r' % (first, second))
72 def GetSSHCommand(node, cmd, strict=True):
73 """Builds SSH command to be executed.
76 args = [ 'ssh', '-oEscapeChar=none', '-oBatchMode=yes', '-l', 'root' ]
82 args.append('-oStrictHostKeyChecking=%s' % tmp)
83 args.append('-oClearAllForwardings=yes')
84 args.append('-oForwardAgent=yes')
87 if qa_config.options.dry_run:
92 args.append(prefix + cmd)
94 print 'SSH:', utils.ShellQuoteArgs(args)
99 def StartSSH(node, cmd, strict=True):
103 return subprocess.Popen(GetSSHCommand(node, cmd, strict=strict),
107 def GetCommandOutput(node, cmd):
108 """Returns the output of a command executed on the given node.
111 p = subprocess.Popen(GetSSHCommand(node, cmd),
112 shell=False, stdout=subprocess.PIPE)
113 AssertEqual(p.wait(), 0)
114 return p.stdout.read()
117 def UploadFile(node, src):
118 """Uploads a file to a node and returns the filename.
120 Caller needs to remove the returned file on the node when it's not needed
123 # Make sure nobody else has access to it while preserving local permissions
124 mode = os.stat(src).st_mode & 0700
126 cmd = ('tmp=$(tempfile --mode %o --prefix gnt) && '
127 '[[ -f "${tmp}" ]] && '
129 'echo "${tmp}"') % mode
133 p = subprocess.Popen(GetSSHCommand(node, cmd), shell=False, stdin=f,
134 stdout=subprocess.PIPE)
135 AssertEqual(p.wait(), 0)
137 # Return temporary filename
138 return p.stdout.read().strip()
143 def _ResolveName(cmd, key):
147 master = qa_config.GetMasterNode()
149 output = GetCommandOutput(master['primary'], utils.ShellQuoteArgs(cmd))
150 for line in output.splitlines():
151 (lkey, lvalue) = line.split(':', 1)
153 return lvalue.lstrip()
154 raise KeyError("Key not found")
157 def ResolveInstanceName(instance):
158 """Gets the full name of an instance.
161 return _ResolveName(['gnt-instance', 'info', instance['info']],
165 def ResolveNodeName(node):
166 """Gets the full name of a node.
169 return _ResolveName(['gnt-node', 'info', node['primary']],
173 def GetNodeInstances(node, secondaries=False):
174 """Gets a list of instances on a node.
177 master = qa_config.GetMasterNode()
179 node_name = ResolveNodeName(node)
181 # Get list of all instances
182 cmd = ['gnt-instance', 'list', '--separator=:', '--no-headers',
183 '--output=name,pnode,snodes']
184 output = GetCommandOutput(master['primary'], utils.ShellQuoteArgs(cmd))
187 for line in output.splitlines():
188 (name, pnode, snodes) = line.split(':', 2)
189 if ((not secondaries and pnode == node_name) or
190 (secondaries and node_name in snodes.split(','))):
191 instances.append(name)
196 def _PrintWithColor(text, seq):
212 def PrintWarning(text):
213 return _PrintWithColor(text, _WARNING_SEQ)
216 def PrintError(text):
217 return _PrintWithColor(text, _ERROR_SEQ)
221 return _PrintWithColor(text, _INFO_SEQ)