burnin 7.14 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]")
71
72
73
  parser.add_option("-n", "--nodes", dest="nodes", default="",
                    help="Comma separated list of nodes to perform the burnin"
                    " on (defaults to all nodes)")
Iustin Pop's avatar
Iustin Pop committed
74
75

  options, args = parser.parse_args()
76
  if len(args) < 1 or options.os is None:
Iustin Pop's avatar
Iustin Pop committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    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")
91
  proc = mcpu.Processor(feedback=Feedback)
92
93
94
95
96
97
98
99
100
101
102
  if opts.nodes:
    names = opts.nodes.split(",")
  else:
    names = []
  try:
    result = proc.ExecOpCode(opcodes.OpQueryNodes(output_fields=["name"],
                                                  names=names))
  except errors.GenericError, err:
    err_code, msg = cli.FormatError(err)
    Feedback(msg)
    return err_code
Iustin Pop's avatar
Iustin Pop committed
103
104
105
106
  nodelist = [data[0] for data in result]

  Feedback("- Testing global parameters")

107
  result = proc.ExecOpCode(opcodes.OpDiagnoseOS())
Iustin Pop's avatar
Iustin Pop committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

  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:
126
    Feedback("OS '%s' not found" % opts.os)
Iustin Pop's avatar
Iustin Pop committed
127
128
129
    return 1

  to_remove = []
130
131
132
133
134
135
136
  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
137
138
139
140
141
142
143
144
145
  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:
146
        tplate = disk_template
Iustin Pop's avatar
Iustin Pop committed
147
148
149
150
151
152
153
154
155
156
157
      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,
158
                                    ip_check=True,
Iustin Pop's avatar
Iustin Pop committed
159
160
                                    wait_for_sync=True)
      Feedback("- Add instance %s on node %s" % (instance_name, pnode))
161
      result = proc.ExecOpCode(op)
Iustin Pop's avatar
Iustin Pop committed
162
163
164
165
      to_remove.append(instance_name)
      idx = next_idx


166
167
168
169
170
    if opts.do_replace1:
      if len(nodelist) > 1:
        # failover
        for instance_name in args:
          op = opcodes.OpReplaceDisks(instance_name=instance_name,
171
172
173
                                      remote_node=None,
                                      mode=constants.REPLACE_DISK_ALL,
                                      disks=["sda", "sdb"])
174
175

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

180
181
182
183
184
185
186
187
    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))
188
          result = proc.ExecOpCode(op)
189
190
      else:
        Feedback("- Can't run failovers, not enough nodes")
Iustin Pop's avatar
Iustin Pop committed
191
192
193
194
195

    # stop / start
    for instance_name in args:
      op = opcodes.OpShutdownInstance(instance_name=instance_name)
      Feedback("- Shutdown instance %s" % instance_name)
196
      result = proc.ExecOpCode(op)
Iustin Pop's avatar
Iustin Pop committed
197
198
      op = opcodes.OpStartupInstance(instance_name=instance_name, force=False)
      Feedback("- Start instance %s" % instance_name)
199
      result = proc.ExecOpCode(op)
Iustin Pop's avatar
Iustin Pop committed
200
201
202
203
204
205

  finally:
    # remove
    for instance_name in to_remove:
      op = opcodes.OpRemoveInstance(instance_name=instance_name)
      Feedback("- Remove instance %s" % instance_name)
206
      result = proc.ExecOpCode(op)
Iustin Pop's avatar
Iustin Pop committed
207
208
209
210

  return 0

def main():
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  """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
225
226

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