-
Notifications
You must be signed in to change notification settings - Fork 7
/
command.py
138 lines (105 loc) · 4.27 KB
/
command.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
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
# Copyright (c) 2019, Substratum LLC (https://substratum.net) and/or its affiliates. All rights reserved.
from __future__ import print_function
import tnt_config
class Command:
def __init__(self, name, fn, info):
self.name = name
self.run = fn
self.info = info
def display(self):
print("%s> %s" % ("\t{0:-<18}".format(self.name), self.info))
def run_for(self, the_input):
if len(the_input) > 0:
print("%s command does not take input (yet?)" % self.name)
return self.run()
class InputCommand:
def __init__(self, name, fn, info):
self.name = name
self.run = fn
self.info = info
def display(self):
print("%s> %s" % ("\t{0:-<18}".format(self.name), self.info))
def run_for(self, the_input):
return self.run(the_input)
class SelectCommand:
def __init__(self, name, fn, info):
self.name = name
self.info = info
self.sub_fn = fn
def display(self):
print(
"%s> Prompts for instance name then %s the instance(s) specified" %
("\t{0:-<18}".format(self.name), self.info)
)
def run(self):
self._choose_and_run(self.sub_fn)
def run_for(self, the_input):
index_names = self._cleanse_input(the_input)
if len(index_names) == 0:
self.run()
if 'all' in index_names:
index_names = tnt_config.INSTANCES.keys()
self._run_for_all(self.sub_fn, index_names)
def _choose_instances(self):
the_input = raw_input("\t%s: choose from %s (space-delimited) or 'all' (blank line to cancel): " % (self.name, sorted(tnt_config.INSTANCES.keys()))).strip()
# enable exit on blank line
if the_input == '':
return ['']
return self._cleanse_input(the_input)
def _cleanse_input(self, the_input):
names = self._split_input(the_input)
return self._cleanse_names(names)
def _split_input(self, the_input):
index_names = the_input.strip().split(' ')
# eliminate extra inner whitespace and filter out empty strings
return [index_name.strip() for index_name in index_names if index_name != '']
def _cleanse_names(self, index_names):
# eliminate duplicate names
index_names = set(index_names)
# check for unknown instances
for index_name in index_names:
if index_name not in tnt_config.INSTANCES.keys() and index_name != 'all':
print("\tno known instance called %s" % index_name)
return []
return index_names
def _choose_and_run(self, fn):
while True:
index_names = self._choose_instances()
if '' in index_names:
return
to_return = False
if 'all' in index_names:
index_names = tnt_config.INSTANCES.keys()
to_return = True
self._run_for_all(fn, index_names)
if to_return:
return
def _run_for_all(self, fn, index_names):
for index_name in sorted(index_names):
fn(tnt_config.INSTANCES[index_name])
class SetCommand(SelectCommand):
def __init__(self):
SelectCommand.__init__(self, "set", lambda: 0, "sets attributes on a Node before it is started")
def display(self):
print(
"%s> Prompts for instance name then sets an attribute on the instance(s) specified" %
"\t{0:-<18}".format(self.name)
)
def run(self):
print("run Usage: set <attribute> <value> [ <instance> [ <instance> ... ] ]")
def run_for(self, the_input):
input_words = self._split_input(the_input)
if len(input_words) < 2:
print("run_for Usage: set <attribute> <value> [ <instance> [ <instance> ... ] ]")
return
attribute = input_words.pop(0)
value = input_words.pop(0)
names = self._cleanse_names(input_words)
fn = lambda instance: self._set_attribute(attribute, value, instance)
if len(names) == 0:
self._choose_and_run(fn)
if 'all' in names:
names = tnt_config.INSTANCES.keys()
self._run_for_all(fn, names)
def _set_attribute(self, attribute, value, instance):
instance.attributes[attribute] = value