Implement node daemon conectivity tests
[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 import simplejson
26 import time
27
28 from optparse import make_option
29 from cStringIO import StringIO
30
31 from ganeti.cli import *
32 from ganeti import opcodes
33 from ganeti import logger
34 from ganeti import constants
35 from ganeti import utils
36 from ganeti import errors
37
38
39 def Delay(opts, args):
40   """Sleeps for a while
41
42   """
43   delay = float(args[0])
44   op = opcodes.OpTestDelay(duration=delay,
45                            on_master=opts.on_master,
46                            on_nodes=opts.on_nodes)
47
48   job = opcodes.Job(op_list=[op])
49   jid = SubmitJob(job)
50   print "Job id", jid
51   return 0
52
53
54 def GenericOpCodes(opts, args):
55   """Send any opcode to the master
56
57   """
58   fname = args[0]
59   op_data = simplejson.loads(open(fname).read())
60   op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
61   job = opcodes.Job(op_list=op_list)
62   jid = SubmitJob(job)
63   print "Job id:", jid
64   query = {
65     "object": "jobs",
66     "fields": ["status"],
67     "names": [jid],
68     }
69
70   # wait for job to complete (either by success or failure)
71   while True:
72     jdata = SubmitQuery(query)
73     if not jdata:
74       # job not found, gone away!
75       print "Job lost!"
76       return 1
77
78     status = jdata[0][0]
79     print status
80     if status in (opcodes.Job.STATUS_SUCCESS, opcodes.Job.STATUS_FAIL):
81       break
82
83     # sleep between checks
84     time.sleep(0.5)
85
86   # job has finished, get and process its results
87   query["fields"].extend(["op_list", "op_status", "op_result"])
88   jdata = SubmitQuery(query)
89   if not jdata:
90     # job not found, gone away!
91     print "Job lost!"
92     return 1
93   print jdata[0]
94   status, op_list, op_status, op_result = jdata[0]
95   for idx, op in enumerate(op_list):
96     print idx, op.OP_ID, op_status[idx], op_result[idx]
97   return 0
98
99
100 def TestAllocator(opts, args):
101   """Runs the test allocator opcode"""
102
103   try:
104     disks = [{"size": utils.ParseUnit(val), "mode": 'w'}
105              for val in opts.disks.split(",")]
106   except errors.UnitParseError, err:
107     print >> sys.stderr, "Invalid disks parameter '%s': %s" % (opts.disks, err)
108     return 1
109
110   nics = [val.split("/") for val in opts.nics.split(",")]
111   for row in nics:
112     while len(row) < 3:
113       row.append(None)
114     for i in range(3):
115       if row[i] == '':
116         row[i] = None
117   nic_dict = [{"mac": v[0], "ip": v[1], "bridge": v[2]} for v in nics]
118
119   if opts.tags is None:
120     opts.tags = []
121   else:
122     opts.tags = opts.tags.split(",")
123
124   op = opcodes.OpTestAllocator(mode=opts.mode,
125                                name=args[0],
126                                mem_size=opts.mem,
127                                disks=disks,
128                                disk_template=opts.disk_template,
129                                nics=nic_dict,
130                                os=opts.os_type,
131                                vcpus=opts.vcpus,
132                                tags=opts.tags,
133                                direction=opts.direction,
134                                allocator=opts.allocator,
135                                )
136   result = SubmitOpCode(op)
137   print result
138   return 0
139
140
141 commands = {
142   'delay': (Delay, ARGS_ONE,
143             [DEBUG_OPT,
144              make_option("--no-master", dest="on_master", default=True,
145                          action="store_false",
146                          help="Do not sleep in the master code"),
147              make_option("-n", dest="on_nodes", default=[],
148                          action="append",
149                          help="Select nodes to sleep on"),
150              ],
151             "[opts...] <duration>", "Executes a TestDelay OpCode"),
152   'submit-job': (GenericOpCodes, ARGS_ONE,
153                  [DEBUG_OPT,
154                   ],
155                  "<op_list_file>", "Submits a job built from a json-file"
156                  " with a list of serialized opcodes"),
157   'allocator': (TestAllocator, ARGS_ONE,
158                 [DEBUG_OPT,
159                  make_option("--dir", dest="direction",
160                              default="in", choices=["in", "out"],
161                              help="Show allocator input (in) or allocator"
162                              " results (out)"),
163                  make_option("--algorithm", dest="allocator",
164                              default=None,
165                              help="Allocator algorithm name"),
166                  make_option("-m", "--mode", default="relocate",
167                              choices=["relocate", "allocate"],
168                              help="Request mode, either allocate or"
169                              "relocate"),
170                  cli_option("--mem", default=128, type="unit",
171                             help="Memory size for the instance (MiB)"),
172                  make_option("--disks", default="4096,4096",
173                              help="Comma separated list of disk sizes (MiB)"),
174                  make_option("-t", "--disk-template", default="drbd",
175                              help="Select the disk template"),
176                  make_option("--nics", default="00:11:22:33:44:55",
177                              help="Comma separated list of nics, each nic"
178                              " definition is of form mac/ip/bridge, if"
179                              " missing values are replace by None"),
180                  make_option("-o", "--os-type", default=None,
181                              help="Select os for the instance"),
182                  make_option("-p", "--vcpus", default=1, type="int",
183                              help="Select number of VCPUs for the instance"),
184                  make_option("--tags", default=None,
185                              help="Comma separated list of tags"),
186                  ],
187                 "{opts...} <instance>", "Executes a TestAllocator OpCode"),
188   }
189
190
191 if __name__ == '__main__':
192   sys.exit(GenericMain(commands))