Statistics
| Branch: | Tag: | Revision:

root / qa / qa_utils.py @ 23269c5b

History | View | Annotate | Download (3.9 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, msg=None):
65
  """Raises an error when values aren't equal.
66

67
  """
68
  if not first == second:
69
    raise qa_error.Error(msg or '%r == %r' % (first, second))
70

    
71

    
72
def GetSSHCommand(node, cmd, strict=True):
73
  """Builds SSH command to be executed.
74

75
  """
76
  args = [ 'ssh', '-oEscapeChar=none', '-oBatchMode=yes', '-l', 'root' ]
77

    
78
  if strict:
79
    tmp = 'yes'
80
  else:
81
    tmp = 'no'
82
  args.append('-oStrictHostKeyChecking=%s' % tmp)
83
  args.append('-oClearAllForwardings=yes')
84
  args.append('-oForwardAgent=yes')
85
  args.append(node)
86

    
87
  if qa_config.options.dry_run:
88
    prefix = 'exit 0; '
89
  else:
90
    prefix = ''
91

    
92
  args.append(prefix + cmd)
93

    
94
  print 'SSH:', utils.ShellQuoteArgs(args)
95

    
96
  return args
97

    
98

    
99
def StartSSH(node, cmd, strict=True):
100
  """Starts SSH.
101

102
  """
103
  args = GetSSHCommand(node, cmd, strict=strict)
104
  return subprocess.Popen(args, shell=False)
105

    
106

    
107
def UploadFile(node, src):
108
  """Uploads a file to a node and returns the filename.
109

110
  Caller needs to remove the returned file on the node when it's not needed
111
  anymore.
112
  """
113
  # Make sure nobody else has access to it while preserving local permissions
114
  mode = os.stat(src).st_mode & 0700
115

    
116
  cmd = ('tmp=$(tempfile --mode %o --prefix gnt) && '
117
         '[[ -f "${tmp}" ]] && '
118
         'cat > "${tmp}" && '
119
         'echo "${tmp}"') % mode
120

    
121
  f = open(src, 'r')
122
  try:
123
    p = subprocess.Popen(GetSSHCommand(node, cmd), shell=False, stdin=f,
124
                         stdout=subprocess.PIPE)
125
    AssertEqual(p.wait(), 0)
126

    
127
    # Return temporary filename
128
    return p.stdout.read().strip()
129
  finally:
130
    f.close()
131

    
132

    
133
def ResolveInstanceName(instance):
134
  """Gets the full name of an instance.
135

136
  """
137
  master = qa_config.GetMasterNode()
138

    
139
  info_cmd = utils.ShellQuoteArgs(['gnt-instance', 'info', instance['name']])
140
  sed_cmd = utils.ShellQuoteArgs(['sed', '-n', '-e', 's/^Instance name: *//p'])
141

    
142
  cmd = '%s | %s' % (info_cmd, sed_cmd)
143
  ssh_cmd = GetSSHCommand(master['primary'], cmd)
144
  p = subprocess.Popen(ssh_cmd, shell=False, stdout=subprocess.PIPE)
145
  AssertEqual(p.wait(), 0)
146

    
147
  return p.stdout.read().strip()
148

    
149

    
150
def _PrintWithColor(text, seq):
151
  f = sys.stdout
152

    
153
  if not f.isatty():
154
    seq = None
155

    
156
  if seq:
157
    f.write(seq)
158

    
159
  f.write(text)
160
  f.write("\n")
161

    
162
  if seq:
163
    f.write(_RESET_SEQ)
164

    
165

    
166
def PrintWarning(text):
167
  return _PrintWithColor(text, _WARNING_SEQ)
168

    
169

    
170
def PrintError(f, text):
171
  return _PrintWithColor(text, _ERROR_SEQ)
172

    
173

    
174
def PrintInfo(f, text):
175
  return _PrintWithColor(text, _INFO_SEQ)