Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-debug @ 85a87e21

History | View | Annotate | Download (6.9 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
"""Debugging commands"""
22

    
23
# pylint: disable-msg=W0401,W0614,C0103
24
# W0401: Wildcard import ganeti.cli
25
# W0614: Unused import %s from wildcard import (since we need cli)
26
# C0103: Invalid name gnt-backup
27

    
28
import sys
29
import simplejson
30
import time
31

    
32
from ganeti.cli import *
33
from ganeti import cli
34
from ganeti import opcodes
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
                           repeat=opts.repeat)
55
  SubmitOpCode(op, opts=opts)
56

    
57
  return 0
58

    
59

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

    
63
  @param opts: the command line options selected by the user
64
  @type args: list
65
  @param args: should contain only one element, the path of
66
      the file with the opcode definition
67
  @rtype: int
68
  @return: the desired exit code
69

    
70
  """
71
  cl = cli.GetClient()
72
  jex = cli.JobExecutor(cl=cl, verbose=opts.verbose, opts=opts)
73

    
74
  job_cnt = 0
75
  op_cnt = 0
76
  if opts.timing_stats:
77
    ToStdout("Loading...")
78
  for job_idx in range(opts.rep_job):
79
    for fname in args:
80
      # pylint: disable-msg=W0142
81
      op_data = simplejson.loads(utils.ReadFile(fname))
82
      op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
83
      op_list = op_list * opts.rep_op
84
      jex.QueueJob("file %s/%d" % (fname, job_idx), *op_list)
85
      op_cnt += len(op_list)
86
      job_cnt += 1
87

    
88
  if opts.timing_stats:
89
    t1 = time.time()
90
    ToStdout("Submitting...")
91

    
92
  jex.SubmitPending(each=opts.each)
93

    
94
  if opts.timing_stats:
95
    t2 = time.time()
96
    ToStdout("Executing...")
97

    
98
  jex.GetResults()
99
  if opts.timing_stats:
100
    t3 = time.time()
101
    ToStdout("C:op     %4d" % op_cnt)
102
    ToStdout("C:job    %4d" % job_cnt)
103
    ToStdout("T:submit %4.4f" % (t2-t1))
104
    ToStdout("T:exec   %4.4f" % (t3-t2))
105
    ToStdout("T:total  %4.4f" % (t3-t1))
106
  return 0
107

    
108

    
109
def TestAllocator(opts, args):
110
  """Runs the test allocator opcode.
111

    
112
  @param opts: the command line options selected by the user
113
  @type args: list
114
  @param args: should contain only one element, the iallocator name
115
  @rtype: int
116
  @return: the desired exit code
117

    
118
  """
119
  try:
120
    disks = [{"size": utils.ParseUnit(val), "mode": 'w'}
121
             for val in opts.disks.split(",")]
122
  except errors.UnitParseError, err:
123
    ToStderr("Invalid disks parameter '%s': %s", opts.disks, err)
124
    return 1
125

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

    
135
  if opts.tags is None:
136
    opts.tags = []
137
  else:
138
    opts.tags = opts.tags.split(",")
139

    
140
  op = opcodes.OpTestAllocator(mode=opts.mode,
141
                               name=args[0],
142
                               evac_nodes=args,
143
                               mem_size=opts.mem,
144
                               disks=disks,
145
                               disk_template=opts.disk_template,
146
                               nics=nic_dict,
147
                               os=opts.os,
148
                               vcpus=opts.vcpus,
149
                               tags=opts.tags,
150
                               direction=opts.direction,
151
                               allocator=opts.iallocator,
152
                               )
153
  result = SubmitOpCode(op, opts=opts)
154
  ToStdout("%s" % result)
155
  return 0
156

    
157

    
158
commands = {
159
  'delay': (
160
    Delay, [ArgUnknown(min=1, max=1)],
161
    [cli_option("--no-master", dest="on_master", default=True,
162
                action="store_false", help="Do not sleep in the master code"),
163
     cli_option("-n", dest="on_nodes", default=[],
164
                action="append", help="Select nodes to sleep on"),
165
     cli_option("-r", "--repeat", type="int", default="0", dest="repeat",
166
                help="Number of times to repeat the sleep"),
167
     ],
168
    "[opts...] <duration>", "Executes a TestDelay OpCode"),
169
  'submit-job': (
170
    GenericOpCodes, [ArgFile(min=1)],
171
    [VERBOSE_OPT,
172
     cli_option("--op-repeat", type="int", default="1", dest="rep_op",
173
                help="Repeat the opcode sequence this number of times"),
174
     cli_option("--job-repeat", type="int", default="1", dest="rep_job",
175
                help="Repeat the job this number of times"),
176
     cli_option("--timing-stats", default=False,
177
                action="store_true", help="Show timing stats"),
178
     cli_option("--each", default=False, action="store_true",
179
                help="Submit each job separately"),
180
     ],
181
    "<op_list_file...>", "Submits jobs built from json files"
182
    " containing a list of serialized opcodes"),
183
  'allocator': (
184
    TestAllocator, [ArgUnknown(min=1)],
185
    [cli_option("--dir", dest="direction",
186
                default="in", choices=["in", "out"],
187
                help="Show allocator input (in) or allocator"
188
                " results (out)"),
189
     IALLOCATOR_OPT,
190
     cli_option("-m", "--mode", default="relocate",
191
                choices=["relocate", "allocate", "multi-evacuate"],
192
                help="Request mode, either allocate or relocate"),
193
     cli_option("--mem", default=128, type="unit",
194
                help="Memory size for the instance (MiB)"),
195
     cli_option("--disks", default="4096,4096",
196
                help="Comma separated list of disk sizes (MiB)"),
197
     DISK_TEMPLATE_OPT,
198
     cli_option("--nics", default="00:11:22:33:44:55",
199
                help="Comma separated list of nics, each nic"
200
                " definition is of form mac/ip/bridge, if"
201
                " missing values are replace by None"),
202
     OS_OPT,
203
     cli_option("-p", "--vcpus", default=1, type="int",
204
                help="Select number of VCPUs for the instance"),
205
     cli_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))