burnin 6.75 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
  parser.add_option("--no-replace1", dest="do_replace1",
59
60
                    help="Do disk replacement with the same secondary",
                    action="store_false", default=True)
61
  parser.add_option("--no-replace2", dest="do_replace2",
62
63
                    help="Do disk replacement with a different secondary",
                    action="store_false", default=True)
64
  parser.add_option("--no-failover", dest="do_failover",
65
66
                    help="Do instance failovers", action="store_false",
                    default=True)
67
68
69
70
  parser.add_option("-t", "--disk-template", dest="disk_template",
                    choices=("remote_raid1", "drbd8"), default="remote_raid1",
                    help="Template type for network mirroring (remote_raid1"
                    " or drbd8) [remote_raid1]")
Iustin Pop's avatar
Iustin Pop committed
71
72

  options, args = parser.parse_args()
73
  if len(args) < 1 or options.os is None:
Iustin Pop's avatar
Iustin Pop committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    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")
88
  proc = mcpu.Processor(feedback=Feedback)
89
  result = proc.ExecOpCode(opcodes.OpQueryNodes(output_fields=["name"],
90
                                                names=[]))
Iustin Pop's avatar
Iustin Pop committed
91
92
93
94
  nodelist = [data[0] for data in result]

  Feedback("- Testing global parameters")

95
  result = proc.ExecOpCode(opcodes.OpDiagnoseOS())
Iustin Pop's avatar
Iustin Pop committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

  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:
114
    Feedback("OS '%s' not found" % opts.os)
Iustin Pop's avatar
Iustin Pop committed
115
116
117
    return 1

  to_remove = []
118
119
120
121
122
123
124
  if opts.disk_template == "remote_raid1":
    disk_template = constants.DT_REMOTE_RAID1
  elif opts.disk_template == "drbd8":
    disk_template = constants.DT_DRBD8
  else:
    Feedback("Unknown disk template '%s'" % opts.disk_template)
    return 1
Iustin Pop's avatar
Iustin Pop committed
125
126
127
128
129
130
131
132
133
  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:
134
        tplate = disk_template
Iustin Pop's avatar
Iustin Pop committed
135
136
137
138
139
140
141
142
143
144
145
      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,
146
                                    ip_check=True,
Iustin Pop's avatar
Iustin Pop committed
147
148
                                    wait_for_sync=True)
      Feedback("- Add instance %s on node %s" % (instance_name, pnode))
149
      result = proc.ExecOpCode(op)
Iustin Pop's avatar
Iustin Pop committed
150
151
152
153
      to_remove.append(instance_name)
      idx = next_idx


154
155
156
157
158
    if opts.do_replace1:
      if len(nodelist) > 1:
        # failover
        for instance_name in args:
          op = opcodes.OpReplaceDisks(instance_name=instance_name,
159
160
161
                                      remote_node=None,
                                      mode=constants.REPLACE_DISK_ALL,
                                      disks=["sda", "sdb"])
162
163

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

168
169
170
171
172
173
174
175
    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))
176
          result = proc.ExecOpCode(op)
177
178
      else:
        Feedback("- Can't run failovers, not enough nodes")
Iustin Pop's avatar
Iustin Pop committed
179
180
181
182
183

    # stop / start
    for instance_name in args:
      op = opcodes.OpShutdownInstance(instance_name=instance_name)
      Feedback("- Shutdown instance %s" % instance_name)
184
      result = proc.ExecOpCode(op)
Iustin Pop's avatar
Iustin Pop committed
185
186
      op = opcodes.OpStartupInstance(instance_name=instance_name, force=False)
      Feedback("- Start instance %s" % instance_name)
187
      result = proc.ExecOpCode(op)
Iustin Pop's avatar
Iustin Pop committed
188
189
190
191
192
193

  finally:
    # remove
    for instance_name in to_remove:
      op = opcodes.OpRemoveInstance(instance_name=instance_name)
      Feedback("- Remove instance %s" % instance_name)
194
      result = proc.ExecOpCode(op)
Iustin Pop's avatar
Iustin Pop committed
195
196
197
198

  return 0

def main():
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  """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
213
214

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