Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-debug @ 3a24c527

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 constants
34
from ganeti import utils
35
from ganeti import errors
36

    
37

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

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

    
48
  return 0
49

    
50

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

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

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

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

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

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

    
96

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

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

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

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

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

    
137

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

    
187

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