~ubuntu-branches/ubuntu/vivid/crmsh/vivid

« back to all changes in this revision

Viewing changes to modules/main.py

  • Committer: Package Import Robot
  • Author(s): Martin Loschwitz
  • Date: 2013-08-05 05:21:29 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130805052129-aooqqanxrctmy93m
Tags: 1.2.5+hg1034-1
New upstream checkout

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2008-2011 Dejan Muhamedagic <dmuhamedagic@suse.de>
2
 
 
2
#
3
3
# This program is free software; you can redistribute it and/or
4
4
# modify it under the terms of the GNU General Public
5
5
# License as published by the Free Software Foundation; either
6
6
# version 2 of the License, or (at your option) any later version.
7
 
 
7
#
8
8
# This software is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
11
# General Public License for more details.
12
 
 
12
#
13
13
# You should have received a copy of the GNU General Public
14
14
# License along with this library; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
21
import getopt
22
22
import atexit
23
23
 
24
 
from utils import *
 
24
from utils import wait4dc, is_pcmk_118, is_program
25
25
from userprefs import Options, UserPrefs
26
26
from vars import Vars
27
27
from ui import cmd_exit
28
 
from msg import *
 
28
from msg import ErrorBuffer, syntax_err, skill_err
 
29
from msg import common_warn, common_info, common_debug, common_err
29
30
from levels import Levels
30
31
 
 
32
user_args = None  # assigned in run()
 
33
 
 
34
 
31
35
def load_rc(rcfile):
32
 
    try: f = open(rcfile)
33
 
    except: return
 
36
    try:
 
37
        f = open(rcfile)
 
38
    except:
 
39
        return
34
40
    save_stdin = sys.stdin
35
41
    sys.stdin = f
36
42
    while True:
37
43
        inp = multi_input()
38
44
        if inp == None:
39
45
            break
40
 
        try: parse_line(levels, shlex.split(inp))
 
46
        try:
 
47
            parse_line(levels, shlex.split(inp))
41
48
        except ValueError, msg:
42
49
            common_err(msg)
43
50
    f.close()
44
51
    sys.stdin = save_stdin
45
52
 
 
53
 
46
54
def multi_input(prompt=''):
47
55
    """
48
56
    Get input from user
70
78
            break
71
79
    return ''.join(line)
72
80
 
 
81
 
73
82
def check_args(args, argsdim):
74
 
    if not argsdim: return True
 
83
    if not argsdim:
 
84
        return True
75
85
    if len(argsdim) == 1:
76
86
        minargs = argsdim[0]
77
87
        return len(args) >= minargs
100
110
#   (encoded as a small integer from 0 to 2)
101
111
# can the command cause transition to start (0 or 1)
102
112
#   used to check whether to wait4dc to end the transition
103
 
 
113
#
 
114
 
104
115
 
105
116
def show_usage(cmd):
106
117
    p = None
107
 
    try: p = cmd.__doc__
108
 
    except: pass
 
118
    try:
 
119
        p = cmd.__doc__
 
120
    except:
 
121
        pass
109
122
    if p:
110
123
        print >> sys.stderr, p
111
124
    else:
112
125
        syntax_err(cmd.__name__)
113
126
 
 
127
 
114
128
def parse_line(lvl, s):
115
 
    if not s: return True
116
 
    if s[0].startswith('#'): return True
 
129
    if not s:
 
130
        return True
 
131
    if s[0].startswith('#'):
 
132
        return True
117
133
    lvl.mark()
118
134
    pt = lvl.parse_root
119
135
    cmd = None
127
143
                if not options.interactive and i == len(s)-1:
128
144
                    set_interactive()
129
145
                lvl.new_level(pt[token], token)
130
 
                pt = lvl.parse_root # move to the next level
 
146
                pt = lvl.parse_root  # move to the next level
131
147
            else:
132
 
                cmd = pt[token] # terminal symbol
 
148
                cmd = pt[token]  # terminal symbol
133
149
                break  # and stop parsing
134
150
        else:
135
151
            syntax_err(s[i:])
136
152
            lvl.release()
137
153
            return False
138
 
    if cmd: # found a terminal symbol
 
154
    if cmd:  # found a terminal symbol
139
155
        if not user_prefs.check_skill_level(cmd[2]):
140
156
            lvl.release()
141
157
            skill_err(s[i])
147
163
            return False
148
164
        args = s[i:]
149
165
        d = lambda: cmd[0](*args)
150
 
        rv = d() # execute the command
 
166
        rv = d()  # execute the command
151
167
        # should we wait till the command takes effect?
152
 
        do_wait = user_prefs.get_wait() and rv != False and (cmd[3] == 1 or \
 
168
        do_wait = user_prefs.get_wait() and rv != False and (cmd[3] == 1 or
153
169
            (lvl.current_level.should_wait() and \
154
170
            (lvl.is_in_transit() or not options.interactive)))
155
171
        lvl.release()
159
175
        return rv != False
160
176
    return True
161
177
 
 
178
 
162
179
def exit_handler():
163
180
    '''
164
181
    Write the history file. Remove tmp files.
175
192
        except OSError:
176
193
            pass
177
194
 
 
195
 
178
196
def prereqs():
179
197
    proglist = "which cibadmin crm_resource crm_attribute crm_mon"
180
198
    for prog in proglist.split():
181
199
        if not is_program(prog):
182
 
            print >> sys.stderr, "%s not available, check your installation"%prog
 
200
            print >> sys.stderr, "%s not available, check your installation" % prog
183
201
            sys.exit(1)
184
202
 
 
203
 
185
204
# prefer the user set PATH
186
205
def envsetup():
187
206
    mybinpath = os.path.dirname(sys.argv[0])
189
208
        if p not in os.environ["PATH"].split(':'):
190
209
            os.environ['PATH'] = "%s:%s" % (os.environ['PATH'], p)
191
210
 
 
211
 
192
212
# three modes: interactive (no args supplied), batch (input from
193
213
# a file), half-interactive (args supplied, but not batch)
194
214
def cib_prompt():
195
215
    return vars.cib_in_use or "live"
196
216
 
 
217
 
197
218
def usage(rc):
198
219
    f = sys.stderr
199
220
    if rc == 0:
257
278
        # echo stop global_www | crm resource
258
279
        # crm configure property no-quorum-policy=ignore
259
280
        # crm ra info pengine
260
 
        # crm status 
 
281
        # crm status
261
282
 
262
283
    See the crm(8) man page or the crm help system for more details.
263
284
    """
269
290
vars = Vars.getInstance()
270
291
levels = Levels.getInstance()
271
292
 
 
293
 
272
294
def set_interactive():
273
295
    '''Set the interactive option only if we're on a tty.'''
274
296
    if sys.stdin.isatty():
275
297
        options.interactive = True
276
298
 
 
299
 
277
300
def xdg_file(name, xdg_name, obj_type, semantics):
278
301
    if not name or not xdg_name:
279
302
        return name
293
316
            common_debug("moving %s to %s" % (name, new))
294
317
        os.rename(name, new)
295
318
    return new
 
319
 
 
320
 
296
321
def mv_user_files():
297
 
    vars.hist_file = xdg_file(vars.hist_file, \
298
 
        vars.xdg_map["history"], "f", "cache")
299
 
    vars.rc_file = xdg_file(vars.rc_file, \
300
 
        vars.xdg_map["rc"], "f", "config")
301
 
    vars.index_file = xdg_file(vars.index_file, \
302
 
        vars.xdg_map["help_index"], "f", "cache")
303
 
    vars.tmpl_conf_dir = xdg_file(vars.tmpl_conf_dir, \
304
 
        vars.xdg_map["crmconf"], "d", "config")
 
322
    vars.hist_file = xdg_file(vars.hist_file,
 
323
                              vars.xdg_map["history"], "f", "cache")
 
324
    vars.rc_file = xdg_file(vars.rc_file,
 
325
                            vars.xdg_map["rc"], "f", "config")
 
326
    vars.index_file = xdg_file(vars.index_file,
 
327
                               vars.xdg_map["help_index"], "f", "cache")
 
328
    vars.tmpl_conf_dir = xdg_file(vars.tmpl_conf_dir,
 
329
                                  vars.xdg_map["crmconf"], "d", "config")
 
330
 
305
331
 
306
332
def compatibility_setup():
307
333
    if is_pcmk_118():
318
344
        vars.simulate_programs["ptest"] = ""
319
345
        vars.simulate_programs["simulate"] = ""
320
346
 
 
347
 
321
348
def do_work():
322
349
    global user_args
323
350
    compatibility_setup()
381
408
            rc = 1
382
409
            common_err(msg)
383
410
 
 
411
 
384
412
def run():
385
413
    global user_args
386
414
    envsetup()
398
426
        options.interactive = True
399
427
 
400
428
    try:
401
 
        opts, user_args = getopt.getopt(sys.argv[1:], \
402
 
            'whdc:f:FX:RD:H:', ("wait", "version", "help", "debug", \
403
 
            "cib=", "file=", "force", "profile=", \
404
 
            "regression-tests", "display=", "history="))
 
429
        opts, user_args = getopt.getopt(
 
430
            sys.argv[1:],
 
431
            'whdc:f:FX:RD:H:',
 
432
            ("wait", "version", "help", "debug",
 
433
             "cib=", "file=", "force", "profile=",
 
434
             "regression-tests", "display=", "history="))
405
435
        for o, p in opts:
406
436
            if o in ("-h", "--help"):
407
437
                usage(0)
408
438
            elif o in ("--version"):
409
 
                print >> sys.stdout,("%s" % vars.crm_version)
 
439
                print >> sys.stdout, ("%s" % vars.crm_version)
410
440
                sys.exit(0)
411
441
            elif o == "-d":
412
442
                user_prefs.set_debug()
436
466
    if options.profile:
437
467
        import cProfile
438
468
        cProfile.run('main.do_work()', options.profile)
439
 
        print "python -c 'import pstats; s = pstats.Stats(\"%s\"); s.sort_stats(\"cumulative\").print_stats()' | less" % options.profile
 
469
        # print how to use the profile file, but don't disturb
 
470
        # the regression tests
 
471
        if not options.regression_tests:
 
472
            stats_cmd = "; ".join(['import pstats',
 
473
                                   's = pstats.Stats("%s")' % options.profile,
 
474
                                   's.sort_stats("cumulative").print_stats()'])
 
475
            print "python -c '%s' | less" % (stats_cmd)
440
476
 
441
477
    else:
442
478
        do_work()