burnin 6.13 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/python
#

import sys
import optparse

from ganeti import opcodes
from ganeti import mcpu
from ganeti import objects
from ganeti import constants
from ganeti import cli
from ganeti import logger
13
14
from ganeti import errors
from ganeti import utils
Iustin Pop's avatar
Iustin Pop committed
15

16
USAGE = ("\tburnin -o OS_NAME [options...] instance_name ...")
Iustin Pop's avatar
Iustin Pop committed
17
18
19
20
21
22
23
24
25
26

def Usage():
  """Shows program usage information and exits the program."""

  print >> sys.stderr, "Usage:"
  print >> sys.stderr, USAGE
  sys.exit(2)


def Feedback(msg):
27
28
29
30
  """Simple function that prints out its argument.

  """
  print msg
Iustin Pop's avatar
Iustin Pop committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57


def ParseOptions():
  """Parses the command line options.

  In case of command line errors, it will show the usage and exit the
  program.

  Returns:
    (options, args), as returned by OptionParser.parse_args
  """

  parser = optparse.OptionParser(usage="\n%s" % USAGE,
                                 version="%%prog (ganeti) %s" %
                                 constants.RELEASE_VERSION,
                                 option_class=cli.CliOption)

  parser.add_option("-o", "--os", dest="os", default=None,
                    help="OS to use during burnin",
                    metavar="<OS>")
  parser.add_option("--os-size", dest="os_size", help="Disk size",
                    default=4 * 1024, type="unit", metavar="<size>")
  parser.add_option("--swap-size", dest="swap_size", help="Swap size",
                    default=4 * 1024, type="unit", metavar="<size>")
  parser.add_option("-v", "--verbose",
                    action="store_true", dest="verbose", default=False,
                    help="print command execution messages to stdout")
58
59
60
61
62
63
64
65
66
  parser.add_option("--do-replace1", dest="do_replace1",
                    help="Do disk replacement with the same secondary",
                    action="store_false", default=True)
  parser.add_option("--do-replace2", dest="do_replace2",
                    help="Do disk replacement with a different secondary",
                    action="store_false", default=True)
  parser.add_option("--do-failover", dest="do_failover",
                    help="Do instance failovers", action="store_false",
                    default=True)
Iustin Pop's avatar
Iustin Pop committed
67
68

  options, args = parser.parse_args()
69
  if len(args) < 1 or options.os is None:
Iustin Pop's avatar
Iustin Pop committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    Usage()

  return options, args


def BurninCluster(opts, args):
  """Test a cluster intensively.

  This will create instances and then start/stop/failover them.
  It is safe for existing instances but could impact performance.

  """

  logger.SetupLogging(debug=True, program="ganeti/burnin")
84
  proc = mcpu.Processor(feedback=Feedback)
85
  result = proc.ExecOpCode(opcodes.OpQueryNodes(output_fields=["name"],
86
                                                names=[]))
Iustin Pop's avatar
Iustin Pop committed
87
88
89
90
  nodelist = [data[0] for data in result]

  Feedback("- Testing global parameters")

91
  result = proc.ExecOpCode(opcodes.OpDiagnoseOS())
Iustin Pop's avatar
Iustin Pop committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

  if not result:
    Feedback("Can't get the OS list")
    return 1

  # filter non-valid OS-es
  oses = {}
  for node_name in result:
    oses[node_name] = [obj for obj in result[node_name]
                       if isinstance(obj, objects.OS)]

  fnode = oses.keys()[0]
  os_set = set([os_inst.name for os_inst in oses[fnode]])
  del oses[fnode]
  for node in oses:
    os_set &= set([os_inst.name for os_inst in oses[node]])

  if opts.os not in os_set:
110
    Feedback("OS '%s' not found" % opts.os)
Iustin Pop's avatar
Iustin Pop committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    return 1

  to_remove = []
  try:
    idx = 0
    for instance_name in args:
      next_idx = idx + 1
      if next_idx >= len(nodelist):
        next_idx = 0
      pnode = nodelist[idx]
      snode = nodelist[next_idx]
      if len(nodelist) > 1:
        tplate = constants.DT_REMOTE_RAID1
      else:
        tplate = constants.DT_PLAIN

      op = opcodes.OpCreateInstance(instance_name=instance_name, mem_size=128,
                                    disk_size=opts.os_size,
                                    swap_size=opts.swap_size,
                                    disk_template=tplate,
                                    mode=constants.INSTANCE_CREATE,
                                    os_type=opts.os, pnode=pnode,
                                    snode=snode, vcpus=1,
                                    start=True,
135
                                    ip_check=True,
Iustin Pop's avatar
Iustin Pop committed
136
137
                                    wait_for_sync=True)
      Feedback("- Add instance %s on node %s" % (instance_name, pnode))
138
      result = proc.ExecOpCode(op)
Iustin Pop's avatar
Iustin Pop committed
139
140
141
142
      to_remove.append(instance_name)
      idx = next_idx


143
144
145
146
147
148
149
150
    if opts.do_replace1:
      if len(nodelist) > 1:
        # failover
        for instance_name in args:
          op = opcodes.OpReplaceDisks(instance_name=instance_name,
                                      remote_node=None)

          Feedback("- Replace disks for instance %s" % (instance_name))
151
          result = proc.ExecOpCode(op)
152
153
      else:
        Feedback("- Can't run replace1, not enough nodes")
Iustin Pop's avatar
Iustin Pop committed
154

155
156
157
158
159
160
161
162
    if opts.do_failover:
      if len(nodelist) > 1:
        # failover
        for instance_name in args:
          op = opcodes.OpFailoverInstance(instance_name=instance_name,
                                          ignore_consistency=True)

          Feedback("- Failover instance %s" % (instance_name))
163
          result = proc.ExecOpCode(op)
164
165
      else:
        Feedback("- Can't run failovers, not enough nodes")
Iustin Pop's avatar
Iustin Pop committed
166
167
168
169
170

    # stop / start
    for instance_name in args:
      op = opcodes.OpShutdownInstance(instance_name=instance_name)
      Feedback("- Shutdown instance %s" % instance_name)
171
      result = proc.ExecOpCode(op)
Iustin Pop's avatar
Iustin Pop committed
172
173
      op = opcodes.OpStartupInstance(instance_name=instance_name, force=False)
      Feedback("- Start instance %s" % instance_name)
174
      result = proc.ExecOpCode(op)
Iustin Pop's avatar
Iustin Pop committed
175
176
177
178
179
180

  finally:
    # remove
    for instance_name in to_remove:
      op = opcodes.OpRemoveInstance(instance_name=instance_name)
      Feedback("- Remove instance %s" % instance_name)
181
      result = proc.ExecOpCode(op)
Iustin Pop's avatar
Iustin Pop committed
182
183
184
185

  return 0

def main():
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  """Main function"""

  opts, args = ParseOptions()
  try:
    utils.Lock('cmd', max_retries=15, debug=True)
  except errors.LockError, err:
    logger.ToStderr(str(err))
    return 1
  try:
    retval = BurninCluster(opts, args)
  finally:
    utils.Unlock('cmd')
    utils.LockCleanup()
  return retval
Iustin Pop's avatar
Iustin Pop committed
200
201

if __name__ == "__main__":
202
  main()