Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-debug @ 746ea1da

History | View | Annotate | Download (6.3 kB)

1
#!/usr/bin/python
2
#
3

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

    
21

    
22
import sys
23
import os
24
import itertools
25
import simplejson
26
import time
27

    
28
from optparse import make_option
29
from cStringIO import StringIO
30

    
31
from ganeti.cli import *
32
from ganeti import opcodes
33
from ganeti import logger
34
from ganeti import constants
35
from ganeti import utils
36
from ganeti import errors
37

    
38

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

    
42
  """
43
  delay = float(args[0])
44
  op = opcodes.OpTestDelay(duration=delay,
45
                           on_master=opts.on_master,
46
                           on_nodes=opts.on_nodes)
47
  SubmitOpCode(op)
48

    
49
  return 0
50

    
51

    
52
def GenericOpCodes(opts, args):
53
  """Send any opcode to the master
54

    
55
  """
56
  fname = args[0]
57
  op_data = simplejson.loads(open(fname).read())
58
  op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
59
  job = opcodes.Job(op_list=op_list)
60
  jid = SubmitJob(job)
61
  print "Job id:", jid
62
  query = {
63
    "object": "jobs",
64
    "fields": ["status"],
65
    "names": [jid],
66
    }
67

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

    
76
    status = jdata[0][0]
77
    print status
78
    if status in (opcodes.Job.STATUS_SUCCESS, opcodes.Job.STATUS_FAIL):
79
      break
80

    
81
    # sleep between checks
82
    time.sleep(0.5)
83

    
84
  # job has finished, get and process its results
85
  query["fields"].extend(["op_list", "op_status", "op_result"])
86
  jdata = SubmitQuery(query)
87
  if not jdata:
88
    # job not found, gone away!
89
    print "Job lost!"
90
    return 1
91
  print jdata[0]
92
  status, op_list, op_status, op_result = jdata[0]
93
  for idx, op in enumerate(op_list):
94
    print idx, op.OP_ID, op_status[idx], op_result[idx]
95
  return 0
96

    
97

    
98
def TestAllocator(opts, args):
99
  """Runs the test allocator opcode"""
100

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

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

    
117
  if opts.tags is None:
118
    opts.tags = []
119
  else:
120
    opts.tags = opts.tags.split(",")
121

    
122
  op = opcodes.OpTestAllocator(mode=opts.mode,
123
                               name=args[0],
124
                               mem_size=opts.mem,
125
                               disks=disks,
126
                               disk_template=opts.disk_template,
127
                               nics=nic_dict,
128
                               os=opts.os_type,
129
                               vcpus=opts.vcpus,
130
                               tags=opts.tags,
131
                               direction=opts.direction,
132
                               allocator=opts.allocator,
133
                               )
134
  result = SubmitOpCode(op)
135
  print result
136
  return 0
137

    
138

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

    
188

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