gnt-debug 6.38 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/python
#

# Copyright (C) 2006, 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


import sys
import os
import itertools
25
26
27
import simplejson
import time

Iustin Pop's avatar
Iustin Pop committed
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
from optparse import make_option
from cStringIO import StringIO

from ganeti.cli import *
from ganeti import opcodes
from ganeti import logger
from ganeti import constants
from ganeti import utils
from ganeti import errors


def Delay(opts, args):
  """Sleeps for a while

  """
  delay = float(args[0])
  op = opcodes.OpTestDelay(duration=delay,
                           on_master=opts.on_master,
                           on_nodes=opts.on_nodes)

  job = opcodes.Job(op_list=[op])
  jid = SubmitJob(job)
  print "Job id", jid
  return 0


54
55
56
57
58
59
60
61
62
63
64
65
66
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
def GenericOpCodes(opts, args):
  """Send any opcode to the master

  """
  fname = args[0]
  op_data = simplejson.loads(open(fname).read())
  op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
  job = opcodes.Job(op_list=op_list)
  jid = SubmitJob(job)
  print "Job id:", jid
  query = {
    "object": "jobs",
    "fields": ["status"],
    "names": [jid],
    }

  # wait for job to complete (either by success or failure)
  while True:
    jdata = SubmitQuery(query)
    if not jdata:
      # job not found, gone away!
      print "Job lost!"
      return 1

    status = jdata[0][0]
    print status
    if status in (opcodes.Job.STATUS_SUCCESS, opcodes.Job.STATUS_FAIL):
      break

    # sleep between checks
    time.sleep(0.5)

  # job has finished, get and process its results
  query["fields"].extend(["op_list", "op_status", "op_result"])
  jdata = SubmitQuery(query)
  if not jdata:
    # job not found, gone away!
    print "Job lost!"
    return 1
  print jdata[0]
  status, op_list, op_status, op_result = jdata[0]
  for idx, op in enumerate(op_list):
    print idx, op.OP_ID, op_status[idx], op_result[idx]
  return 0


100
101
102
103
104
105
106
107
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
139
140
def TestAllocator(opts, args):
  """Runs the test allocator opcode"""

  try:
    disks = [{"size": utils.ParseUnit(val), "mode": 'w'}
             for val in opts.disks.split(",")]
  except errors.UnitParseError, err:
    print >> sys.stderr, "Invalid disks parameter '%s': %s" % (opts.disks, err)
    return 1

  nics = [val.split("/") for val in opts.nics.split(",")]
  for row in nics:
    while len(row) < 3:
      row.append(None)
    for i in range(3):
      if row[i] == '':
        row[i] = None
  nic_dict = [{"mac": v[0], "ip": v[1], "bridge": v[2]} for v in nics]

  if opts.tags is None:
    opts.tags = []
  else:
    opts.tags = opts.tags.split(",")

  op = opcodes.OpTestAllocator(mode=opts.mode,
                               name=args[0],
                               mem_size=opts.mem,
                               disks=disks,
                               disk_template=opts.disk_template,
                               nics=nic_dict,
                               os=opts.os_type,
                               vcpus=opts.vcpus,
                               tags=opts.tags,
                               direction=opts.direction,
                               allocator=opts.allocator,
                               )
  result = SubmitOpCode(op)
  print result
  return 0


Iustin Pop's avatar
Iustin Pop committed
141
142
143
144
145
146
147
148
149
150
151
commands = {
  'delay': (Delay, ARGS_ONE,
            [DEBUG_OPT,
             make_option("--no-master", dest="on_master", default=True,
                         action="store_false",
                         help="Do not sleep in the master code"),
             make_option("-n", dest="on_nodes", default=[],
                         action="append",
                         help="Select nodes to sleep on"),
             ],
            "[opts...] <duration>", "Executes a TestDelay OpCode"),
152
153
154
155
156
  'submit-job': (GenericOpCodes, ARGS_ONE,
                 [DEBUG_OPT,
                  ],
                 "<op_list_file>", "Submits a job built from a json-file"
                 " with a list of serialized opcodes"),
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  'allocator': (TestAllocator, ARGS_ONE,
                [DEBUG_OPT,
                 make_option("--dir", dest="direction",
                             default="in", choices=["in", "out"],
                             help="Show allocator input (in) or allocator"
                             " results (out)"),
                 make_option("--algorithm", dest="allocator",
                             default=None,
                             help="Allocator algorithm name"),
                 make_option("-m", "--mode", default="relocate",
                             choices=["relocate", "allocate"],
                             help="Request mode, either allocate or"
                             "relocate"),
                 cli_option("--mem", default=128, type="unit",
                            help="Memory size for the instance (MiB)"),
                 make_option("--disks", default="4096,4096",
                             help="Comma separated list of disk sizes (MiB)"),
                 make_option("-t", "--disk-template", default="drbd",
                             help="Select the disk template"),
                 make_option("--nics", default="00:11:22:33:44:55",
                             help="Comma separated list of nics, each nic"
                             " definition is of form mac/ip/bridge, if"
                             " missing values are replace by None"),
                 make_option("-o", "--os-type", default=None,
                             help="Select os for the instance"),
                 make_option("-p", "--vcpus", default=1, type="int",
                             help="Select number of VCPUs for the instance"),
                 make_option("--tags", default=None,
                             help="Comma separated list of tags"),
                 ],
                "{opts...} <instance>", "Executes a TestAllocator OpCode"),
Iustin Pop's avatar
Iustin Pop committed
188
189
190
191
192
  }


if __name__ == '__main__':
  sys.exit(GenericMain(commands))