Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-debug @ 6099bfcf

History | View | Annotate | Download (7.1 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
# pylint: disable-msg=W0401,W0614
23
# W0401: Wildcard import ganeti.cli
24
# W0614: Unused import %s from wildcard import (since we need cli)
25

    
26
import sys
27
import simplejson
28
import time
29

    
30
from optparse import make_option
31

    
32
from ganeti.cli import *
33
from ganeti import opcodes
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
  @param opts: the command line options selected by the user
43
  @type args: list
44
  @param args: should contain only one element, the duration
45
      the sleep
46
  @rtype: int
47
  @return: the desired exit code
48

    
49
  """
50
  delay = float(args[0])
51
  op = opcodes.OpTestDelay(duration=delay,
52
                           on_master=opts.on_master,
53
                           on_nodes=opts.on_nodes)
54
  SubmitOpCode(op)
55

    
56
  return 0
57

    
58

    
59
def GenericOpCodes(opts, args):
60
  """Send any opcode to the master.
61

    
62
  @todo: The function is broken and needs to be converted to the
63
      current job queue API
64
  @param opts: the command line options selected by the user
65
  @type args: list
66
  @param args: should contain only one element, the path of
67
      the file with the opcode definition
68
  @rtype: int
69
  @return: the desired exit code
70

    
71
  """
72
  fname = args[0]
73
  op_data = simplejson.loads(open(fname).read())
74
  op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
75
  job = opcodes.Job(op_list=op_list)
76
  jid = SubmitJob(job)
77
  ToStdout("Job id: %s", jid)
78
  query = {
79
    "object": "jobs",
80
    "fields": ["status"],
81
    "names": [jid],
82
    }
83

    
84
  # wait for job to complete (either by success or failure)
85
  while True:
86
    jdata = SubmitQuery(query)
87
    if not jdata:
88
      # job not found, gone away!
89
      ToStderr("Job lost!")
90
      return 1
91

    
92
    status = jdata[0][0]
93
    ToStdout(status)
94
    if status in (opcodes.Job.STATUS_SUCCESS, opcodes.Job.STATUS_FAIL):
95
      break
96

    
97
    # sleep between checks
98
    time.sleep(0.5)
99

    
100
  # job has finished, get and process its results
101
  query["fields"].extend(["op_list", "op_status", "op_result"])
102
  jdata = SubmitQuery(query)
103
  if not jdata:
104
    # job not found, gone away!
105
    ToStderr("Job lost!")
106
    return 1
107
  ToStdout(jdata[0])
108
  status, op_list, op_status, op_result = jdata[0]
109
  for idx, op in enumerate(op_list):
110
    ToStdout("%s %s %s %s", idx, op.OP_ID, op_status[idx], op_result[idx])
111
  return 0
112

    
113

    
114
def TestAllocator(opts, args):
115
  """Runs the test allocator opcode.
116

    
117
  @param opts: the command line options selected by the user
118
  @type args: list
119
  @param args: should contain only one element, the iallocator name
120
  @rtype: int
121
  @return: the desired exit code
122

    
123
  """
124
  try:
125
    disks = [{"size": utils.ParseUnit(val), "mode": 'w'}
126
             for val in opts.disks.split(",")]
127
  except errors.UnitParseError, err:
128
    ToStderr("Invalid disks parameter '%s': %s", opts.disks, err)
129
    return 1
130

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

    
140
  if opts.tags is None:
141
    opts.tags = []
142
  else:
143
    opts.tags = opts.tags.split(",")
144

    
145
  op = opcodes.OpTestAllocator(mode=opts.mode,
146
                               name=args[0],
147
                               mem_size=opts.mem,
148
                               disks=disks,
149
                               disk_template=opts.disk_template,
150
                               nics=nic_dict,
151
                               os=opts.os_type,
152
                               vcpus=opts.vcpus,
153
                               tags=opts.tags,
154
                               direction=opts.direction,
155
                               allocator=opts.allocator,
156
                               )
157
  result = SubmitOpCode(op)
158
  ToStdout("%s" % result)
159
  return 0
160

    
161

    
162
commands = {
163
  'delay': (Delay, ARGS_ONE,
164
            [DEBUG_OPT,
165
             make_option("--no-master", dest="on_master", default=True,
166
                         action="store_false",
167
                         help="Do not sleep in the master code"),
168
             make_option("-n", dest="on_nodes", default=[],
169
                         action="append",
170
                         help="Select nodes to sleep on"),
171
             ],
172
            "[opts...] <duration>", "Executes a TestDelay OpCode"),
173
  'submit-job': (GenericOpCodes, ARGS_ONE,
174
                 [DEBUG_OPT,
175
                  ],
176
                 "<op_list_file>", "Submits a job built from a json-file"
177
                 " with a list of serialized opcodes"),
178
  'allocator': (TestAllocator, ARGS_ONE,
179
                [DEBUG_OPT,
180
                 make_option("--dir", dest="direction",
181
                             default="in", choices=["in", "out"],
182
                             help="Show allocator input (in) or allocator"
183
                             " results (out)"),
184
                 make_option("--algorithm", dest="allocator",
185
                             default=None,
186
                             help="Allocator algorithm name"),
187
                 make_option("-m", "--mode", default="relocate",
188
                             choices=["relocate", "allocate"],
189
                             help="Request mode, either allocate or"
190
                             "relocate"),
191
                 cli_option("--mem", default=128, type="unit",
192
                            help="Memory size for the instance (MiB)"),
193
                 make_option("--disks", default="4096,4096",
194
                             help="Comma separated list of disk sizes (MiB)"),
195
                 make_option("-t", "--disk-template", default="drbd",
196
                             help="Select the disk template"),
197
                 make_option("--nics", default="00:11:22:33:44:55",
198
                             help="Comma separated list of nics, each nic"
199
                             " definition is of form mac/ip/bridge, if"
200
                             " missing values are replace by None"),
201
                 make_option("-o", "--os-type", default=None,
202
                             help="Select os for the instance"),
203
                 make_option("-p", "--vcpus", default=1, type="int",
204
                             help="Select number of VCPUs for the instance"),
205
                 make_option("--tags", default=None,
206
                             help="Comma separated list of tags"),
207
                 ],
208
                "{opts...} <instance>", "Executes a TestAllocator OpCode"),
209
  }
210

    
211

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