Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-debug @ 7260cfbe

History | View | Annotate | Download (6.6 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 constants
36
from ganeti import utils
37
from ganeti import errors
38

    
39

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

    
43
  @param opts: the command line options selected by the user
44
  @type args: list
45
  @param args: should contain only one element, the duration
46
      the sleep
47
  @rtype: int
48
  @return: the desired exit code
49

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

    
57
  return 0
58

    
59

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

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

    
72
  """
73
  cl = cli.GetClient()
74
  jex = cli.JobExecutor(cl=cl, verbose=opts.verbose)
75

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

    
90
  if opts.timing_stats:
91
    t1 = time.time()
92
    ToStdout("Submitting...")
93

    
94
  jex.SubmitPending()
95

    
96
  if opts.timing_stats:
97
    t2 = time.time()
98
    ToStdout("Executing...")
99

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

    
110

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

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

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

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

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

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

    
158

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

    
208

    
209
if __name__ == '__main__':
210
  sys.exit(GenericMain(commands))