Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-debug @ 57d0151e

History | View | Annotate | Download (6.3 kB)

1 fd3ee040 Iustin Pop
#!/usr/bin/python
2 fd3ee040 Iustin Pop
#
3 fd3ee040 Iustin Pop
4 fd3ee040 Iustin Pop
# Copyright (C) 2006, 2007 Google Inc.
5 fd3ee040 Iustin Pop
#
6 fd3ee040 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 fd3ee040 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 fd3ee040 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 fd3ee040 Iustin Pop
# (at your option) any later version.
10 fd3ee040 Iustin Pop
#
11 fd3ee040 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 fd3ee040 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 fd3ee040 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 fd3ee040 Iustin Pop
# General Public License for more details.
15 fd3ee040 Iustin Pop
#
16 fd3ee040 Iustin Pop
# You should have received a copy of the GNU General Public License
17 fd3ee040 Iustin Pop
# along with this program; if not, write to the Free Software
18 fd3ee040 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 fd3ee040 Iustin Pop
# 02110-1301, USA.
20 fd3ee040 Iustin Pop
21 fd3ee040 Iustin Pop
22 fd3ee040 Iustin Pop
import sys
23 fd3ee040 Iustin Pop
import os
24 fd3ee040 Iustin Pop
import itertools
25 f1c66d13 Iustin Pop
import simplejson
26 f1c66d13 Iustin Pop
import time
27 f1c66d13 Iustin Pop
28 fd3ee040 Iustin Pop
from optparse import make_option
29 fd3ee040 Iustin Pop
from cStringIO import StringIO
30 fd3ee040 Iustin Pop
31 fd3ee040 Iustin Pop
from ganeti.cli import *
32 fd3ee040 Iustin Pop
from ganeti import opcodes
33 fd3ee040 Iustin Pop
from ganeti import logger
34 fd3ee040 Iustin Pop
from ganeti import constants
35 fd3ee040 Iustin Pop
from ganeti import utils
36 fd3ee040 Iustin Pop
from ganeti import errors
37 fd3ee040 Iustin Pop
38 fd3ee040 Iustin Pop
39 fd3ee040 Iustin Pop
def Delay(opts, args):
40 fd3ee040 Iustin Pop
  """Sleeps for a while
41 fd3ee040 Iustin Pop
42 fd3ee040 Iustin Pop
  """
43 fd3ee040 Iustin Pop
  delay = float(args[0])
44 fd3ee040 Iustin Pop
  op = opcodes.OpTestDelay(duration=delay,
45 fd3ee040 Iustin Pop
                           on_master=opts.on_master,
46 fd3ee040 Iustin Pop
                           on_nodes=opts.on_nodes)
47 fd3ee040 Iustin Pop
48 fd3ee040 Iustin Pop
  job = opcodes.Job(op_list=[op])
49 fd3ee040 Iustin Pop
  jid = SubmitJob(job)
50 fd3ee040 Iustin Pop
  print "Job id", jid
51 fd3ee040 Iustin Pop
  return 0
52 fd3ee040 Iustin Pop
53 fd3ee040 Iustin Pop
54 f1c66d13 Iustin Pop
def GenericOpCodes(opts, args):
55 f1c66d13 Iustin Pop
  """Send any opcode to the master
56 f1c66d13 Iustin Pop
57 f1c66d13 Iustin Pop
  """
58 f1c66d13 Iustin Pop
  fname = args[0]
59 f1c66d13 Iustin Pop
  op_data = simplejson.loads(open(fname).read())
60 f1c66d13 Iustin Pop
  op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
61 f1c66d13 Iustin Pop
  job = opcodes.Job(op_list=op_list)
62 f1c66d13 Iustin Pop
  jid = SubmitJob(job)
63 f1c66d13 Iustin Pop
  print "Job id:", jid
64 f1c66d13 Iustin Pop
  query = {
65 f1c66d13 Iustin Pop
    "object": "jobs",
66 f1c66d13 Iustin Pop
    "fields": ["status"],
67 f1c66d13 Iustin Pop
    "names": [jid],
68 f1c66d13 Iustin Pop
    }
69 f1c66d13 Iustin Pop
70 f1c66d13 Iustin Pop
  # wait for job to complete (either by success or failure)
71 f1c66d13 Iustin Pop
  while True:
72 f1c66d13 Iustin Pop
    jdata = SubmitQuery(query)
73 f1c66d13 Iustin Pop
    if not jdata:
74 f1c66d13 Iustin Pop
      # job not found, gone away!
75 f1c66d13 Iustin Pop
      print "Job lost!"
76 f1c66d13 Iustin Pop
      return 1
77 f1c66d13 Iustin Pop
78 f1c66d13 Iustin Pop
    status = jdata[0][0]
79 f1c66d13 Iustin Pop
    print status
80 f1c66d13 Iustin Pop
    if status in (opcodes.Job.STATUS_SUCCESS, opcodes.Job.STATUS_FAIL):
81 f1c66d13 Iustin Pop
      break
82 f1c66d13 Iustin Pop
83 f1c66d13 Iustin Pop
    # sleep between checks
84 f1c66d13 Iustin Pop
    time.sleep(0.5)
85 f1c66d13 Iustin Pop
86 f1c66d13 Iustin Pop
  # job has finished, get and process its results
87 f1c66d13 Iustin Pop
  query["fields"].extend(["op_list", "op_status", "op_result"])
88 f1c66d13 Iustin Pop
  jdata = SubmitQuery(query)
89 f1c66d13 Iustin Pop
  if not jdata:
90 f1c66d13 Iustin Pop
    # job not found, gone away!
91 f1c66d13 Iustin Pop
    print "Job lost!"
92 f1c66d13 Iustin Pop
    return 1
93 f1c66d13 Iustin Pop
  print jdata[0]
94 f1c66d13 Iustin Pop
  status, op_list, op_status, op_result = jdata[0]
95 f1c66d13 Iustin Pop
  for idx, op in enumerate(op_list):
96 f1c66d13 Iustin Pop
    print idx, op.OP_ID, op_status[idx], op_result[idx]
97 f1c66d13 Iustin Pop
  return 0
98 f1c66d13 Iustin Pop
99 f1c66d13 Iustin Pop
100 d61df03e Iustin Pop
def TestAllocator(opts, args):
101 d61df03e Iustin Pop
  """Runs the test allocator opcode"""
102 d61df03e Iustin Pop
103 d61df03e Iustin Pop
  try:
104 d61df03e Iustin Pop
    disks = [{"size": utils.ParseUnit(val), "mode": 'w'}
105 d61df03e Iustin Pop
             for val in opts.disks.split(",")]
106 d61df03e Iustin Pop
  except errors.UnitParseError, err:
107 d61df03e Iustin Pop
    print >> sys.stderr, "Invalid disks parameter '%s': %s" % (opts.disks, err)
108 d61df03e Iustin Pop
    return 1
109 d61df03e Iustin Pop
110 d61df03e Iustin Pop
  nics = [val.split("/") for val in opts.nics.split(",")]
111 d61df03e Iustin Pop
  for row in nics:
112 d61df03e Iustin Pop
    while len(row) < 3:
113 d61df03e Iustin Pop
      row.append(None)
114 d61df03e Iustin Pop
    for i in range(3):
115 d61df03e Iustin Pop
      if row[i] == '':
116 d61df03e Iustin Pop
        row[i] = None
117 d61df03e Iustin Pop
  nic_dict = [{"mac": v[0], "ip": v[1], "bridge": v[2]} for v in nics]
118 d61df03e Iustin Pop
119 d61df03e Iustin Pop
  if opts.tags is None:
120 d61df03e Iustin Pop
    opts.tags = []
121 d61df03e Iustin Pop
  else:
122 d61df03e Iustin Pop
    opts.tags = opts.tags.split(",")
123 d61df03e Iustin Pop
124 d61df03e Iustin Pop
  op = opcodes.OpTestAllocator(mode=opts.mode,
125 d61df03e Iustin Pop
                               name=args[0],
126 d61df03e Iustin Pop
                               mem_size=opts.mem,
127 d61df03e Iustin Pop
                               disks=disks,
128 d61df03e Iustin Pop
                               disk_template=opts.disk_template,
129 d61df03e Iustin Pop
                               nics=nic_dict,
130 d61df03e Iustin Pop
                               os=opts.os_type,
131 d61df03e Iustin Pop
                               vcpus=opts.vcpus,
132 d61df03e Iustin Pop
                               tags=opts.tags,
133 d61df03e Iustin Pop
                               direction=opts.direction,
134 d61df03e Iustin Pop
                               allocator=opts.allocator,
135 d61df03e Iustin Pop
                               )
136 d61df03e Iustin Pop
  result = SubmitOpCode(op)
137 d61df03e Iustin Pop
  print result
138 d61df03e Iustin Pop
  return 0
139 d61df03e Iustin Pop
140 d61df03e Iustin Pop
141 fd3ee040 Iustin Pop
commands = {
142 fd3ee040 Iustin Pop
  'delay': (Delay, ARGS_ONE,
143 fd3ee040 Iustin Pop
            [DEBUG_OPT,
144 fd3ee040 Iustin Pop
             make_option("--no-master", dest="on_master", default=True,
145 fd3ee040 Iustin Pop
                         action="store_false",
146 fd3ee040 Iustin Pop
                         help="Do not sleep in the master code"),
147 fd3ee040 Iustin Pop
             make_option("-n", dest="on_nodes", default=[],
148 fd3ee040 Iustin Pop
                         action="append",
149 fd3ee040 Iustin Pop
                         help="Select nodes to sleep on"),
150 fd3ee040 Iustin Pop
             ],
151 57d0151e Iustin Pop
            "Executes a TestDelay OpCode"),
152 f1c66d13 Iustin Pop
  'submit-job': (GenericOpCodes, ARGS_ONE,
153 f1c66d13 Iustin Pop
                 [DEBUG_OPT,
154 f1c66d13 Iustin Pop
                  ],
155 57d0151e Iustin Pop
                 "Submits a job built from a json-file"
156 f1c66d13 Iustin Pop
                 " with a list of serialized opcodes"),
157 d61df03e Iustin Pop
  'allocator': (TestAllocator, ARGS_ONE,
158 d61df03e Iustin Pop
                [DEBUG_OPT,
159 d61df03e Iustin Pop
                 make_option("--dir", dest="direction",
160 d61df03e Iustin Pop
                             default="in", choices=["in", "out"],
161 d61df03e Iustin Pop
                             help="Show allocator input (in) or allocator"
162 d61df03e Iustin Pop
                             " results (out)"),
163 d61df03e Iustin Pop
                 make_option("--algorithm", dest="allocator",
164 d61df03e Iustin Pop
                             default=None,
165 d61df03e Iustin Pop
                             help="Allocator algorithm name"),
166 d61df03e Iustin Pop
                 make_option("-m", "--mode", default="relocate",
167 d61df03e Iustin Pop
                             choices=["relocate", "allocate"],
168 d61df03e Iustin Pop
                             help="Request mode, either allocate or"
169 d61df03e Iustin Pop
                             "relocate"),
170 d61df03e Iustin Pop
                 cli_option("--mem", default=128, type="unit",
171 d61df03e Iustin Pop
                            help="Memory size for the instance (MiB)"),
172 d61df03e Iustin Pop
                 make_option("--disks", default="4096,4096",
173 d61df03e Iustin Pop
                             help="Comma separated list of disk sizes (MiB)"),
174 d61df03e Iustin Pop
                 make_option("-t", "--disk-template", default="drbd",
175 d61df03e Iustin Pop
                             help="Select the disk template"),
176 d61df03e Iustin Pop
                 make_option("--nics", default="00:11:22:33:44:55",
177 d61df03e Iustin Pop
                             help="Comma separated list of nics, each nic"
178 d61df03e Iustin Pop
                             " definition is of form mac/ip/bridge, if"
179 d61df03e Iustin Pop
                             " missing values are replace by None"),
180 d61df03e Iustin Pop
                 make_option("-o", "--os-type", default=None,
181 d61df03e Iustin Pop
                             help="Select os for the instance"),
182 d61df03e Iustin Pop
                 make_option("-p", "--vcpus", default=1, type="int",
183 d61df03e Iustin Pop
                             help="Select number of VCPUs for the instance"),
184 d61df03e Iustin Pop
                 make_option("--tags", default=None,
185 d61df03e Iustin Pop
                             help="Comma separated list of tags"),
186 d61df03e Iustin Pop
                 ],
187 57d0151e Iustin Pop
                "Executes a TestAllocator OpCode"),
188 fd3ee040 Iustin Pop
  }
189 fd3ee040 Iustin Pop
190 fd3ee040 Iustin Pop
191 fd3ee040 Iustin Pop
if __name__ == '__main__':
192 fd3ee040 Iustin Pop
  sys.exit(GenericMain(commands))