4 # Copyright (C) 2006, 2007 Google Inc.
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.
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.
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
21 """Debugging commands"""
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
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
39 def Delay(opts, args):
42 @param opts: the command line options selected by the user
44 @param args: should contain only one element, the duration
47 @return: the desired exit code
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, opts=opts)
59 def GenericOpCodes(opts, args):
60 """Send any opcode to the master.
62 @todo: The function is broken and needs to be converted to the
64 @param opts: the command line options selected by the user
66 @param args: should contain only one element, the path of
67 the file with the opcode definition
69 @return: the desired exit code
73 jex = cli.JobExecutor(cl=cl, verbose=opts.verbose, opts=opts)
78 ToStdout("Loading...")
79 for job_idx in range(opts.rep_job):
81 # pylint: disable-msg=W0142
82 op_data = simplejson.loads(utils.ReadFile(fname))
83 op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
84 op_list = op_list * opts.rep_op
85 jex.QueueJob("file %s/%d" % (fname, job_idx), *op_list)
86 op_cnt += len(op_list)
91 ToStdout("Submitting...")
97 ToStdout("Executing...")
100 if opts.timing_stats:
102 ToStdout("C:op %4d" % op_cnt)
103 ToStdout("C:job %4d" % job_cnt)
104 ToStdout("T:submit %4.4f" % (t2-t1))
105 ToStdout("T:exec %4.4f" % (t3-t2))
106 ToStdout("T:total %4.4f" % (t3-t1))
110 def TestAllocator(opts, args):
111 """Runs the test allocator opcode.
113 @param opts: the command line options selected by the user
115 @param args: should contain only one element, the iallocator name
117 @return: the desired exit code
121 disks = [{"size": utils.ParseUnit(val), "mode": 'w'}
122 for val in opts.disks.split(",")]
123 except errors.UnitParseError, err:
124 ToStderr("Invalid disks parameter '%s': %s", opts.disks, err)
127 nics = [val.split("/") for val in opts.nics.split(",")]
134 nic_dict = [{"mac": v[0], "ip": v[1], "bridge": v[2]} for v in nics]
136 if opts.tags is None:
139 opts.tags = opts.tags.split(",")
141 op = opcodes.OpTestAllocator(mode=opts.mode,
145 disk_template=opts.disk_template,
150 direction=opts.direction,
151 allocator=opts.iallocator,
153 result = SubmitOpCode(op, opts=opts)
154 ToStdout("%s" % result)
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"),
166 "[opts...] <duration>", "Executes a TestDelay OpCode"),
168 GenericOpCodes, [ArgFile(min=1)],
170 cli_option("--op-repeat", type="int", default="1", dest="rep_op",
171 help="Repeat the opcode sequence this number of times"),
172 cli_option("--job-repeat", type="int", default="1", dest="rep_job",
173 help="Repeat the job this number of times"),
174 cli_option("--timing-stats", default=False,
175 action="store_true", help="Show timing stats"),
177 "<op_list_file...>", "Submits jobs built from json files"
178 " containing a list of serialized opcodes"),
180 TestAllocator, ARGS_ONE_INSTANCE,
181 [cli_option("--dir", dest="direction",
182 default="in", choices=["in", "out"],
183 help="Show allocator input (in) or allocator"
186 cli_option("-m", "--mode", default="relocate",
187 choices=["relocate", "allocate"],
188 help="Request mode, either allocate or relocate"),
189 cli_option("--mem", default=128, type="unit",
190 help="Memory size for the instance (MiB)"),
191 cli_option("--disks", default="4096,4096",
192 help="Comma separated list of disk sizes (MiB)"),
194 cli_option("--nics", default="00:11:22:33:44:55",
195 help="Comma separated list of nics, each nic"
196 " definition is of form mac/ip/bridge, if"
197 " missing values are replace by None"),
199 cli_option("-p", "--vcpus", default=1, type="int",
200 help="Select number of VCPUs for the instance"),
201 cli_option("--tags", default=None,
202 help="Comma separated list of tags"),
204 "{opts...} <instance>", "Executes a TestAllocator OpCode"),
208 if __name__ == '__main__':
209 sys.exit(GenericMain(commands))