-
Notifications
You must be signed in to change notification settings - Fork 0
/
sirikatacluster.py
executable file
·80 lines (67 loc) · 2.02 KB
/
sirikatacluster.py
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
#!/usr/bin/env python
"""
Usage: sirikata-cluster.py command [args]
This is the driver script for sirikata-cluster.
This version is the importable version.
"""
import cluster.util.config as config
import cluster.ec2 as ec2
import cluster.adhoc as adhoc
import sys
# Parse config options, currently only from the environment variables
config.env()
# Check that basic set of configuration options are available
config.check_config()
# Setup all our command handlers
handlers = ec2.NodeGroup.handlers + adhoc.NodeGroup.handlers
def usage(code=1):
print """
Usage: sirikata-cluster.py command [args]
Available commands:
"""
for k,v in handlers:
print k
print
exit(code)
# Create map of handlers by name for easy lookup
handlers_map = dict(handlers)
# Parse the command
args = list(sys.argv)
# Get rid of everything up to this script name
while args and args[0].endswith('.py'):
args.pop(0)
if not args:
usage()
# Match n next parts to a command
command = None
for nparts in range(1, len(args)+1):
command = ' '.join(args[0:nparts])
if command in handlers_map:
args = args[nparts:]
break
if command not in handlers_map:
usage()
# Split remaining args as positional and keyword
pargs = []
kwargs = {}
more_kwargs = True
for arg in args:
# Allows you to stop us from parsing kwargs, leaving them as
# positional arguments so that when a command is passed along to a
# subprocess (e.g. ssh) it can just be specified as additional
# arguments even if the subcommand has --key=value type
# arguments. We only accept this once so you can 'escape' it if
# your subcommand *also* needs '--' in it.
if arg == '--' and more_kwargs:
more_kwargs = False
continue
if more_kwargs and ('=' in arg or arg.startswith('--')):
if '=' in arg:
k,v = arg.split('=', 1)
else:
k,v = (arg,True)
if k.startswith('--'): k = k[2:]
kwargs[k] = v
else:
pargs.append(arg)
exit(handlers_map[command](*pargs, **kwargs))