File: crm_sh.in

package info (click to toggle)
pacemaker 1.0.9.1%2Bhg15626-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 34,668 kB
  • ctags: 5,645
  • sloc: xml: 87,444; ansic: 57,853; python: 11,479; sh: 5,255; makefile: 663; perl: 387; sed: 262
file content (361 lines) | stat: -rw-r--r-- 10,268 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!@PYTHON@
#
# Copyright (c) 2006 Andrew Beekhof
#                    All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#
#######################################################################

import os
import sys
import readline
import traceback
from popen2 import Popen3

# Module copied from Mercurial

sys.path.append("@CRM_DAEMON_DIR@")
import crm_utils as utl
import crm_commands as crm 


class ParseError(Exception):
    """Exception raised on errors in parsing the command line."""
class QuotingError(Exception):
    """Exception raised on errors in parsing the command line."""
class UnknownCommand(Exception):
    """Exception raised if command is not in the command table."""
class AmbiguousCommand(Exception):
    """Exception raised if command shortcut matches more than one command."""

table = {
    "^help": (
	crm.help, None, [('v', 'verbose', None, 'extra information')],
	"[-v]"),
    "^up":        (crm.up, None, [], None, "Move up a level in the heirarchy"),
    "^crm":       (crm.cd_, ["crm"], [], None),
    "^nodes":     (crm.cd_, ["crm"], [], None),
    "^resources": (crm.cd_, ["crm"], [], None),
    "^config":    (crm.cd_, ["crm"], [], None),
    "^list": (
	crm.do_list, ["nodes", "resources"], [('t', 'topic', "", '')],
	None),
    "^status": (
	crm.do_status, ["nodes", "resources"], [],
	"[list of objects]"),
    "^debug": (crm.toggle_flag, None, [('f', 'flag', 'debug', '')], None, "Toggle debugging information"),
    "^exit": (crm.exit, None, [], "exit"),
    "^debugstate": (
	crm.debugstate, None, [], 
	None,
	"Dump information about the internal state of the program"),
    }

global_opt_table = [
    ('q', 'quiet', None, 'suppress output'),
    ('v', 'verbose', None, 'enable additional output'),
    ('', 'debug', None, 'enable debugging output'),
    ('', 'devlog', None, 'enable developmental debugging output'),
    ('', 'debugger', None, 'start debugger'),
    ('', 'lsprof', None, 'print improved command execution profile'),
    ('', 'traceback', None, 'print traceback on exception'),
    ('', 'time', None, 'time how long the command takes'),
    ('', 'profile', None, 'print command execution profile'),
    ('', 'version', None, 'output version information and exit'),
    ('i', 'interactive', None, 'run in interactive mode'),
    ('h', 'help', None, 'display help and exit'),
    ]

def help_(text):
    utl.log_dev("Looking up help text for: %s" % text)
    if text == "short":
	utl.log_info("cluster.py [global options] [topic [topic...]] [command]")
	return
    if text:
	choice = findpossible(text, None, False)
	for key in choice.keys():
	    alias, e = choice[key]
	    text = alias[0]
	    utl.log_dev("Help text for: %s" % text)
	    if e:
		sub_cmd=""
		if len(e) > 4:
		    utl.log_info("\n"+e[4]+"\n")
		possible = findpossible("", text).keys()
		utl.log_dev("Possible sub-commands: "+repr(possible))
		possible.remove("up")
		possible.remove("help")
		possible.remove("exit")
		if text in possible:
		    possible.remove(text)
		if possible:
			sub_cmd=' ('+'|'.join(possible)+')'
		if e[3]:
		    utl.log_info("Usage: %s %s%s" % (text, e[3], sub_cmd))
		else:
		    utl.log_info("Usage: %s%s" % (text, sub_cmd))
	if choice:
	    return;
    utl.log_err("No help text available for: %s" % text)


# Stolen from Mercurial commands.py

def findpossible(cmd, topic=None, filter=True):
    """
    Return cmd -> (aliases, command table entry)
    for each matching command.
    Return debug commands (or their aliases) only if no normal command matches.
    """
    if not topic:
	topic = utl.crm_topic
	utl.log_dev("Searching in default topic: %s" % topic)
	
    utl.log_dev("Looking for completions of %s in %s" % (cmd, topic))
    choice = {}
    debugchoice = {}
    topicchoice = {}
    for e in table.keys():
	t = table[e]
	#utl.log_dev("Processing: %s / %s" % (e, repr(t)))
	aliases = e.lstrip("^").split("|")
	found = None
	if "^%s"%topic == e and t[0] == crm.cd_:
	    #utl.log_dev("Found: topic")
	    topicchoice[topic] = (aliases, table[e])
	if filter and t[1] and topic not in t[1]:
	    #utl.log_dev("Skip: filter")
	    continue
	elif cmd in aliases:
	    #utl.log_dev("Found: alias")
	    found = cmd
	else:
	    for a in aliases:
		if a.startswith(cmd):
		    #utl.log_dev("Found: alias prefix")
		    found = a
		    break
        if found is not None:
            if aliases[0].startswith("debug"):
                debugchoice[found] = (aliases, table[e])
            else:
                choice[found] = (aliases, table[e])

    if not choice and debugchoice:
        choice = debugchoice

    if not choice and topicchoice:
        choice = topicchoice

    return choice

def findcmd(cmd):
    """Return (aliases, command table entry) for command string."""
    choice = findpossible(cmd)

    if choice.has_key(cmd):
	#utl.log_dev("Choice has: %s" % cmd)
        return choice[cmd]

    if len(choice) > 1:
        clist = choice.keys()
        clist.sort()
        raise AmbiguousCommand(cmd, clist)

    if choice:
	#utl.log_dev("Returning first: %s" % (repr(choice.values()[0])))
        return choice.values()[0]

    raise UnknownCommand(cmd)

def find_completer(start, i):
    choice = findpossible(start)
    if not choice:
	return None
    elif len(choice.keys()) < i:
	return None
    return choice.keys()[i]

def parse(args):
    options = {}
    cmdoptions = {}
    
    try:
	args = utl.fancyopts(args, global_opt_table, options)
    except utl.getopt.GetoptError, inst:
	raise ParseError(None, inst)

    if args:
	cmd, args = args[0], args[1:]
	utl.log_dev("Initial Command: %s" % cmd)
	aliases, i = findcmd(cmd)
	cmd = aliases[0]
	utl.log_dev("Found Command: %s" % cmd)
	defaults = []
	if defaults:
	    args = defaults.split() + args
	c = list(i[2])
    else:
	cmd = None
	c = []

    # combine global options into local
    for o in global_opt_table:
	c.append((o[0], o[1], options[o[1]], o[3]))
	
    try:
	args = utl.fancyopts(args, c, cmdoptions)
    except utl.getopt.GetoptError, inst:
	raise ParseError(cmd, inst)

    # separate global options back out
    for o in global_opt_table:
	n = o[1]
	options[n] = cmdoptions[n]
	del cmdoptions[n]

    utl.log_dev("args: %s\ncmdoptions: %s" 
	      % (repr(args), repr(cmdoptions)))
    return (cmd, cmd and i[0] or None, args, options, cmdoptions)

def main_loop(args):
    cmd = None
    if not args:
	return 0

    try:
	cmd, func, cmd_args, ignore, cmd_options = parse(args)

	if func == crm.cd_:
	    cmd_options["topic"] = cmd

	utl.log_dev("Func Command: %s" % cmd)
	utl.log_dev("Func Args: %s" % repr(cmd_args))
	utl.log_dev("Func Opts: %s" % repr(cmd_options))
	    
	if not cmd:
	    utl.log_dev(repr(args))
	    return 0
	d = lambda: func(*cmd_args, **cmd_options)
	return d()

    except crm.HelpRequest, inst:
	help_(inst.args[0])
	return 0

    except crm.ReparseRequest:
	return main_loop(cmd_args)

    except ParseError, inst:
	if inst.args[0]:
	    utl.log_err("%s: %s\n" % (inst.args[0], inst.args[1]))
	    help_(inst.args[0])
	else:
	    utl.log_err("%s\n" % inst.args[1])
	    help_('short')

    except AmbiguousCommand, inst:
	utl.log_info("%s: command '%s' is ambiguous:\n    %s\n" 
		 % (" ".join(utl.topic_stack), inst.args[0], " ".join(inst.args[1])))

    except UnknownCommand, inst:
	utl.log_err("%s: unknown command '%s'\n" % (" ".join(utl.topic_stack), inst.args[0]))
	help_(utl.crm_topic)

    except IOError, inst:
	if hasattr(inst, "code"):
	    utl.log_err("abort: %s\n" % inst)
	elif hasattr(inst, "reason"):
	    utl.log_err("abort: error: %s\n" % inst.reason[1])
	elif hasattr(inst, "args"):
	    utl.log_err("broken pipe\n")
	elif getattr(inst, "strerror", None):
	    if getattr(inst, "filename", None):
		utl.log_err("abort: %s - %s\n" % (inst.strerror, inst.filename))
	    else:
		utl.log_err("abort: %s\n" % inst.strerror)
	else:
	    raise
    except OSError, inst:
	if hasattr(inst, "filename"):
	    utl.log_err("abort: %s: %s\n" % (inst.strerror, inst.filename))
	else:
	    utl.log_err("abort: %s\n" % inst.strerror)
	    
    except TypeError, inst:
	# was this an argument error?
	tb = traceback.extract_tb(sys.exc_info()[2])
	if len(tb) > 2: # no
	    raise
	utl.log_err((inst, "\n"))
	utl.log_err(("%s: invalid arguments\n" % cmd))
	help_(cmd)
	raise
    except SystemExit, inst:
	# Exit gracefully 
	utl.exit_(inst.code)

    except:
	utl.log_err("** unknown exception encountered, details follow\n")
	raise
	
    return -1

args = sys.argv[1:]
utl.init_readline(find_completer)
try:
    cmd, f_ignore, a_ignore, utl.global_opts, o_ignore = parse(args)
except:
    pass

if len(sys.argv) == 1:
    utl.global_opts["interactive"] = 1
elif not utl.global_opts.has_key("interactive"):
    utl.global_opts["interactive"] = 0

while True:
    rc = 0
    if args:
	utl.set_topic(utl.topic_stack[-1])
	rc = main_loop(args)
	utl.log_debug("rc: %s" % (repr(rc)))

    if not utl.global_opts["interactive"]:
	utl.exit_(rc)

    try:
	text = raw_input(" ".join(utl.topic_stack) +" # ")
	args = utl.create_argv(text)

    except QuotingError, inst:
        if inst.args[1]:
            utl.log_err("%s.  Found tokens: %s\n" % (inst.args[0], inst.args[1]))
        else:
            utl.log_err("%s\n" % inst.args[0])

    except KeyboardInterrupt:
        pass

    except EOFError:
        utl.exit_(0)