Revision f7e6f3c8 qa/qa_utils.py

b/qa/qa_utils.py
1 1
#
2 2
#
3 3

  
4
# Copyright (C) 2007 Google Inc.
4
# Copyright (C) 2007, 2011 Google Inc.
5 5
#
6 6
# This program is free software; you can redistribute it and/or modify
7 7
# it under the terms of the GNU General Public License as published by
......
28 28
import sys
29 29
import subprocess
30 30
import random
31
import tempfile
31 32

  
32 33
from ganeti import utils
33 34
from ganeti import compat
......
42 43
_ERROR_SEQ = None
43 44
_RESET_SEQ = None
44 45

  
46
_MULTIPLEXERS = {}
47

  
45 48

  
46 49
def _SetupColours():
47 50
  """Initializes the colour constants.
......
143 146
  return rcode
144 147

  
145 148

  
146
def GetSSHCommand(node, cmd, strict=True):
149
def GetSSHCommand(node, cmd, strict=True, opts=None):
147 150
  """Builds SSH command to be executed.
148 151

  
149 152
  @type node: string
150 153
  @param node: node the command should run on
151 154
  @type cmd: string
152
  @param cmd: command to be executed in the node
155
  @param cmd: command to be executed in the node; if None or empty
156
      string, no command will be executed
153 157
  @type strict: boolean
154 158
  @param strict: whether to enable strict host key checking
159
  @type opts: list
160
  @param opts: list of additional options
155 161

  
156 162
  """
157 163
  args = [ 'ssh', '-oEscapeChar=none', '-oBatchMode=yes', '-l', 'root', '-t' ]
......
163 169
  args.append('-oStrictHostKeyChecking=%s' % tmp)
164 170
  args.append('-oClearAllForwardings=yes')
165 171
  args.append('-oForwardAgent=yes')
172
  if opts:
173
    args.extend(opts)
174
  if node in _MULTIPLEXERS:
175
    spath = _MULTIPLEXERS[node][0]
176
    args.append('-oControlPath=%s' % spath)
177
    args.append('-oControlMaster=no')
166 178
  args.append(node)
167
  args.append(cmd)
179
  if cmd:
180
    args.append(cmd)
168 181

  
169 182
  return args
170 183

  
......
184 197
  return StartLocalCommand(GetSSHCommand(node, cmd, strict=strict))
185 198

  
186 199

  
200
def StartMultiplexer(node):
201
  """Starts a multiplexer command.
202

  
203
  @param node: the node for which to open the multiplexer
204

  
205
  """
206
  if node in _MULTIPLEXERS:
207
    return
208

  
209
  # Note: yes, we only need mktemp, since we'll remove the file anyway
210
  sname = tempfile.mktemp(prefix="ganeti-qa-multiplexer.")
211
  utils.RemoveFile(sname)
212
  opts = ["-N", "-oControlPath=%s" % sname, "-oControlMaster=yes"]
213
  print "Created socket at %s" % sname
214
  child = StartLocalCommand(GetSSHCommand(node, None, opts=opts))
215
  _MULTIPLEXERS[node] = (sname, child)
216

  
217

  
218
def CloseMultiplexers():
219
  """Closes all current multiplexers and cleans up.
220

  
221
  """
222
  for node in _MULTIPLEXERS.keys():
223
    (sname, child) = _MULTIPLEXERS.pop(node)
224
    utils.KillProcess(child.pid, timeout=10, waitpid=True)
225
    utils.RemoveFile(sname)
226

  
227

  
187 228
def GetCommandOutput(node, cmd):
188 229
  """Returns the output of a command executed on the given node.
189 230

  

Also available in: Unified diff