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
22 # pylint: disable-msg=W0401,W0614
23 # W0401: Wildcard import ganeti.cli
24 # W0614: Unused import %s from wildcard import (since we need cli)
30 from ganeti.cli import *
31 from ganeti import cli
32 from ganeti import opcodes
33 from ganeti import constants
34 from ganeti import utils
35 from ganeti import errors
38 def Delay(opts, args):
41 @param opts: the command line options selected by the user
43 @param args: should contain only one element, the duration
46 @return: the desired exit code
49 delay = float(args[0])
50 op = opcodes.OpTestDelay(duration=delay,
51 on_master=opts.on_master,
52 on_nodes=opts.on_nodes)
58 def GenericOpCodes(opts, args):
59 """Send any opcode to the master.
61 @todo: The function is broken and needs to be converted to the
63 @param opts: the command line options selected by the user
65 @param args: should contain only one element, the path of
66 the file with the opcode definition
68 @return: the desired exit code
72 jex = cli.JobExecutor(cl=cl)
75 op_data = simplejson.loads(open(fname).read())
76 op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
77 jex.QueueJob("file %s" % fname, *op_list)
83 def TestAllocator(opts, args):
84 """Runs the test allocator opcode.
86 @param opts: the command line options selected by the user
88 @param args: should contain only one element, the iallocator name
90 @return: the desired exit code
94 disks = [{"size": utils.ParseUnit(val), "mode": 'w'}
95 for val in opts.disks.split(",")]
96 except errors.UnitParseError, err:
97 ToStderr("Invalid disks parameter '%s': %s", opts.disks, err)
100 nics = [val.split("/") for val in opts.nics.split(",")]
107 nic_dict = [{"mac": v[0], "ip": v[1], "bridge": v[2]} for v in nics]
109 if opts.tags is None:
112 opts.tags = opts.tags.split(",")
114 op = opcodes.OpTestAllocator(mode=opts.mode,
118 disk_template=opts.disk_template,
123 direction=opts.direction,
124 allocator=opts.allocator,
126 result = SubmitOpCode(op)
127 ToStdout("%s" % result)
132 'delay': (Delay, [ArgUnknown(min=1, max=1)],
134 cli_option("--no-master", dest="on_master", default=True,
135 action="store_false",
136 help="Do not sleep in the master code"),
137 cli_option("-n", dest="on_nodes", default=[],
139 help="Select nodes to sleep on"),
141 "[opts...] <duration>", "Executes a TestDelay OpCode"),
142 'submit-job': (GenericOpCodes, [ArgFile(min=1)], [DEBUG_OPT],
143 "<op_list_file...>", "Submits jobs built from json files"
144 " containing a list of serialized opcodes"),
145 'allocator': (TestAllocator, [ArgInstance(min=1, max=1)],
147 cli_option("--dir", dest="direction",
148 default="in", choices=["in", "out"],
149 help="Show allocator input (in) or allocator"
151 cli_option("--algorithm", dest="allocator",
153 help="Allocator algorithm name"),
154 cli_option("-m", "--mode", default="relocate",
155 choices=["relocate", "allocate"],
156 help="Request mode, either allocate or"
158 cli_option("--mem", default=128, type="unit",
159 help="Memory size for the instance (MiB)"),
160 cli_option("--disks", default="4096,4096",
161 help="Comma separated list of disk sizes (MiB)"),
162 cli_option("-t", "--disk-template", default="drbd",
163 help="Select the disk template"),
164 cli_option("--nics", default="00:11:22:33:44:55",
165 help="Comma separated list of nics, each nic"
166 " definition is of form mac/ip/bridge, if"
167 " missing values are replace by None"),
168 cli_option("-o", "--os-type", default=None,
169 help="Select os for the instance"),
170 cli_option("-p", "--vcpus", default=1, type="int",
171 help="Select number of VCPUs for the instance"),
172 cli_option("--tags", default=None,
173 help="Comma separated list of tags"),
175 "{opts...} <instance>", "Executes a TestAllocator OpCode"),
179 if __name__ == '__main__':
180 sys.exit(GenericMain(commands))