Allocator framework, 1st part: allocator input generation
[ganeti-local] / scripts / gnt-debug
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 import sys
23 import os
24 import itertools
25 from optparse import make_option
26 from cStringIO import StringIO
27
28 from ganeti.cli import *
29 from ganeti import opcodes
30 from ganeti import logger
31 from ganeti import constants
32 from ganeti import utils
33 from ganeti import errors
34
35
36 def Delay(opts, args):
37   """Sleeps for a while
38
39   """
40   delay = float(args[0])
41   op = opcodes.OpTestDelay(duration=delay,
42                            on_master=opts.on_master,
43                            on_nodes=opts.on_nodes)
44
45   SubmitOpCode(op)
46   return 0
47
48
49 def TestAllocator(opts, args):
50   """Runs the test allocator opcode"""
51
52   try:
53     disks = [{"size": utils.ParseUnit(val), "mode": 'w'}
54              for val in opts.disks.split(",")]
55   except errors.UnitParseError, err:
56     print >> sys.stderr, "Invalid disks parameter '%s': %s" % (opts.disks, err)
57     return 1
58
59   nics = [val.split("/") for val in opts.nics.split(",")]
60   for row in nics:
61     while len(row) < 3:
62       row.append(None)
63     for i in range(3):
64       if row[i] == '':
65         row[i] = None
66   nic_dict = [{"mac": v[0], "ip": v[1], "bridge": v[2]} for v in nics]
67
68   if opts.tags is None:
69     opts.tags = []
70   else:
71     opts.tags = opts.tags.split(",")
72
73   op = opcodes.OpTestAllocator(mode=opts.mode,
74                                name=args[0],
75                                mem_size=opts.mem,
76                                disks=disks,
77                                disk_template=opts.disk_template,
78                                nics=nic_dict,
79                                os=opts.os_type,
80                                vcpus=opts.vcpus,
81                                tags=opts.tags,
82                                direction=opts.direction,
83                                allocator=opts.allocator,
84                                )
85   result = SubmitOpCode(op)
86   print result
87   return 0
88
89
90 commands = {
91   'delay': (Delay, ARGS_ONE,
92             [DEBUG_OPT,
93              make_option("--no-master", dest="on_master", default=True,
94                          action="store_false",
95                          help="Do not sleep in the master code"),
96              make_option("-n", dest="on_nodes", default=[],
97                          action="append",
98                          help="Select nodes to sleep on"),
99              ],
100             "[opts...] <duration>", "Executes a TestDelay OpCode"),
101   'allocator': (TestAllocator, ARGS_ONE,
102                 [DEBUG_OPT,
103                  make_option("--dir", dest="direction",
104                              default="in", choices=["in", "out"],
105                              help="Show allocator input (in) or allocator"
106                              " results (out)"),
107                  make_option("--algorithm", dest="allocator",
108                              default=None,
109                              help="Allocator algorithm name"),
110                  make_option("-m", "--mode", default="relocate",
111                              choices=["relocate", "allocate"],
112                              help="Request mode, either allocate or"
113                              "relocate"),
114                  cli_option("--mem", default=128, type="unit",
115                             help="Memory size for the instance (MiB)"),
116                  make_option("--disks", default="4096,4096",
117                              help="Comma separated list of disk sizes (MiB)"),
118                  make_option("-t", "--disk-template", default="drbd",
119                              help="Select the disk template"),
120                  make_option("--nics", default="00:11:22:33:44:55",
121                              help="Comma separated list of nics, each nic"
122                              " definition is of form mac/ip/bridge, if"
123                              " missing values are replace by None"),
124                  make_option("-o", "--os-type", default=None,
125                              help="Select os for the instance"),
126                  make_option("-p", "--vcpus", default=1, type="int",
127                              help="Select number of VCPUs for the instance"),
128                  make_option("--tags", default=None,
129                              help="Comma separated list of tags"),
130                  ],
131                 "{opts...} <instance>", "Executes a TestAllocator OpCode"),
132   }
133
134
135 if __name__ == '__main__':
136   sys.exit(GenericMain(commands))