Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-debug @ 42c067b7

History | View | Annotate | Download (6.3 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

    
22
# pylint: disable-msg=W0401,W0614
23
# W0401: Wildcard import ganeti.cli
24
# W0614: Unused import %s from wildcard import (since we need cli)
25

    
26
import sys
27
import simplejson
28
import time
29

    
30
from optparse import make_option
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)
75

    
76
  for fname in args:
77
    op_data = simplejson.loads(open(fname).read())
78
    op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
79
    jex.QueueJob("file %s" % fname, *op_list)
80

    
81
  jex.GetResults()
82
  return 0
83

    
84

    
85
def TestAllocator(opts, args):
86
  """Runs the test allocator opcode.
87

    
88
  @param opts: the command line options selected by the user
89
  @type args: list
90
  @param args: should contain only one element, the iallocator name
91
  @rtype: int
92
  @return: the desired exit code
93

    
94
  """
95
  try:
96
    disks = [{"size": utils.ParseUnit(val), "mode": 'w'}
97
             for val in opts.disks.split(",")]
98
  except errors.UnitParseError, err:
99
    ToStderr("Invalid disks parameter '%s': %s", opts.disks, err)
100
    return 1
101

    
102
  nics = [val.split("/") for val in opts.nics.split(",")]
103
  for row in nics:
104
    while len(row) < 3:
105
      row.append(None)
106
    for i in range(3):
107
      if row[i] == '':
108
        row[i] = None
109
  nic_dict = [{"mac": v[0], "ip": v[1], "bridge": v[2]} for v in nics]
110

    
111
  if opts.tags is None:
112
    opts.tags = []
113
  else:
114
    opts.tags = opts.tags.split(",")
115

    
116
  op = opcodes.OpTestAllocator(mode=opts.mode,
117
                               name=args[0],
118
                               mem_size=opts.mem,
119
                               disks=disks,
120
                               disk_template=opts.disk_template,
121
                               nics=nic_dict,
122
                               os=opts.os_type,
123
                               vcpus=opts.vcpus,
124
                               tags=opts.tags,
125
                               direction=opts.direction,
126
                               allocator=opts.allocator,
127
                               )
128
  result = SubmitOpCode(op)
129
  ToStdout("%s" % result)
130
  return 0
131

    
132

    
133
commands = {
134
  'delay': (Delay, ARGS_ONE,
135
            [DEBUG_OPT,
136
             make_option("--no-master", dest="on_master", default=True,
137
                         action="store_false",
138
                         help="Do not sleep in the master code"),
139
             make_option("-n", dest="on_nodes", default=[],
140
                         action="append",
141
                         help="Select nodes to sleep on"),
142
             ],
143
            "[opts...] <duration>", "Executes a TestDelay OpCode"),
144
  'submit-job': (GenericOpCodes, ARGS_ATLEAST(1),
145
                 [DEBUG_OPT,
146
                  ],
147
                 "<op_list_file...>", "Submits jobs built from json files"
148
                 " containing a list of serialized opcodes"),
149
  'allocator': (TestAllocator, ARGS_ONE,
150
                [DEBUG_OPT,
151
                 make_option("--dir", dest="direction",
152
                             default="in", choices=["in", "out"],
153
                             help="Show allocator input (in) or allocator"
154
                             " results (out)"),
155
                 make_option("--algorithm", dest="allocator",
156
                             default=None,
157
                             help="Allocator algorithm name"),
158
                 make_option("-m", "--mode", default="relocate",
159
                             choices=["relocate", "allocate"],
160
                             help="Request mode, either allocate or"
161
                             " relocate"),
162
                 cli_option("--mem", default=128, type="unit",
163
                            help="Memory size for the instance (MiB)"),
164
                 make_option("--disks", default="4096,4096",
165
                             help="Comma separated list of disk sizes (MiB)"),
166
                 make_option("-t", "--disk-template", default="drbd",
167
                             help="Select the disk template"),
168
                 make_option("--nics", default="00:11:22:33:44:55",
169
                             help="Comma separated list of nics, each nic"
170
                             " definition is of form mac/ip/bridge, if"
171
                             " missing values are replace by None"),
172
                 make_option("-o", "--os-type", default=None,
173
                             help="Select os for the instance"),
174
                 make_option("-p", "--vcpus", default=1, type="int",
175
                             help="Select number of VCPUs for the instance"),
176
                 make_option("--tags", default=None,
177
                             help="Comma separated list of tags"),
178
                 ],
179
                "{opts...} <instance>", "Executes a TestAllocator OpCode"),
180
  }
181

    
182

    
183
if __name__ == '__main__':
184
  sys.exit(GenericMain(commands))