forked from riptano/ccm
/
ccm
executable file
·74 lines (63 loc) · 1.96 KB
/
ccm
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
#!/usr/bin/python
import os, sys
L = os.path.realpath(__file__).split(os.path.sep)[:-1]
root = os.path.sep.join(L)
sys.path.append(os.path.join(root, 'cmds'))
import command, common
from cluster_cmds import *
from cluster_cass_cmds import *
from node_cmds import *
from node_cass_cmds import *
def get_command(kind, cmd):
cmd_name = kind.lower().capitalize() + cmd.lower().capitalize() + "Cmd"
try:
klass = globals()[cmd_name]
except KeyError:
return None
if not issubclass(klass, command.Cmd):
return None
return klass()
def print_global_usage():
print "Usage:"
print " ccm <cluster_cmd> [options]"
print " ccm <node_name> <node_cmd> [options]"
print ""
print "Where <cluster_cmd> is one of"
for cmd_name in cluster_cmds():
cmd = get_command("cluster", cmd_name)
if not cmd:
print "Internal error, unknown command {0}".format(cmd_name)
exit(1)
print " {0:14} {1}".format(cmd_name, cmd.description())
print "or <node_name> is the name of a node of the current cluster and <node_cmd> is one of"
for cmd_name in node_cmds():
cmd = get_command("node", cmd_name)
if not cmd:
print "Internal error, unknown command {0}".format(cmd_name)
exit(1)
print " {0:14} {1}".format(cmd_name, cmd.description())
exit(1)
if len(sys.argv) <= 1:
print "Missing arguments"
print_global_usage()
arg1 = sys.argv[1].lower()
if arg1 in cluster_cmds():
kind = 'cluster'
cmd = arg1
cmd_args = sys.argv[2:]
else:
if len(sys.argv) <= 2:
print "Missing arguments"
print_global_usage()
kind = 'node'
node = arg1
cmd = sys.argv[2]
cmd_args = [node] + sys.argv[3:]
cmd = get_command(kind, cmd)
if not cmd:
print "Unknown node or command: {0}".format(arg1)
exit(1)
parser = cmd.get_parser()
(options, args) = parser.parse_args(cmd_args)
cmd.validate(parser, options, args)
cmd.run()