burnin 6.08 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

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

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


def Feedback(msg):
    print msg


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")
55
56
57
58
59
60
61
62
63
  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
64
65

  options, args = parser.parse_args()
66
  if len(args) < 1 or options.os is None:
Iustin Pop's avatar
Iustin Pop committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    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")
  proc = mcpu.Processor()
  result = proc.ExecOpCode(opcodes.OpQueryNodes(output_fields=["name"]),
                             Feedback)
  nodelist = [data[0] for data in result]

  Feedback("- Testing global parameters")

  result = proc.ExecOpCode(opcodes.OpDiagnoseOS(), Feedback)

  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:
107
    Feedback("OS '%s' not found" % opts.os)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
    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,
                                    wait_for_sync=True)
      Feedback("- Add instance %s on node %s" % (instance_name, pnode))
      result = proc.ExecOpCode(op, Feedback)
      to_remove.append(instance_name)
      idx = next_idx


139
140
141
142
143
144
145
146
147
148
149
    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))
          result = proc.ExecOpCode(op, Feedback)
      else:
        Feedback("- Can't run replace1, not enough nodes")
Iustin Pop's avatar
Iustin Pop committed
150

151
152
153
154
155
156
157
158
159
160
161
    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))
          result = proc.ExecOpCode(op, Feedback)
      else:
        Feedback("- Can't run failovers, not enough nodes")
Iustin Pop's avatar
Iustin Pop committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

    # stop / start
    for instance_name in args:
      op = opcodes.OpShutdownInstance(instance_name=instance_name)
      Feedback("- Shutdown instance %s" % instance_name)
      result = proc.ExecOpCode(op, Feedback)
      op = opcodes.OpStartupInstance(instance_name=instance_name, force=False)
      Feedback("- Start instance %s" % instance_name)
      result = proc.ExecOpCode(op, Feedback)

  finally:
    # remove
    for instance_name in to_remove:
      op = opcodes.OpRemoveInstance(instance_name=instance_name)
      Feedback("- Remove instance %s" % instance_name)
      result = proc.ExecOpCode(op, Feedback)

  return 0

def main():
    """Main function"""

    opts, args = ParseOptions()
185
186
187
188
189
190
191
192
193
194
195
    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
196
197
198

if __name__ == "__main__":
    main()