root / scripts / gnt-debug @ f1c66d13
History | View | Annotate | Download (3.2 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 |
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 |
commands = { |
101 |
'delay': (Delay, ARGS_ONE, |
102 |
[DEBUG_OPT, |
103 |
make_option("--no-master", dest="on_master", default=True, |
104 |
action="store_false", |
105 |
help="Do not sleep in the master code"), |
106 |
make_option("-n", dest="on_nodes", default=[], |
107 |
action="append", |
108 |
help="Select nodes to sleep on"), |
109 |
], |
110 |
"[opts...] <duration>", "Executes a TestDelay OpCode"), |
111 |
'submit-job': (GenericOpCodes, ARGS_ONE, |
112 |
[DEBUG_OPT, |
113 |
], |
114 |
"<op_list_file>", "Submits a job built from a json-file" |
115 |
" with a list of serialized opcodes"), |
116 |
} |
117 |
|
118 |
|
119 |
if __name__ == '__main__': |
120 |
sys.exit(GenericMain(commands)) |