Skip to content

Commit 6dbeb80

Browse files
author
Stian Brattland
committed
Added support for configurations via file. Added better validation
1 parent d48e5f5 commit 6dbeb80

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

swarmconstraint/swarmconstraint.py

100755100644
Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ def __init__(self, args):
2323
self.logger.setLevel(logging.DEBUG)
2424

2525
def run(self):
26+
27+
if (not self.args['watch']):
28+
raise Exception('At least one node to watch must be provided.')
29+
30+
if (not self.args['toggle']):
31+
raise Exception('At least one node to toggle must be provided.')
32+
33+
if (not self.args['label']):
34+
raise Exception('At least one label must be provided.')
35+
36+
if (not self.args['prefix']):
37+
raise Exception('A prefix must be provided.')
38+
39+
self.logger.info('Watch {watch}.'.format(watch=','.join(self.args['watch'])))
40+
self.logger.info('Toggle the label(s) {labels} on {toggle}.'.format(labels=','.join(self.args['label']), toggle=','.join(self.args['toggle'])))
41+
self.logger.info('Prefix disabled labels with {prefix}.'.format(prefix=self.args['prefix']))
42+
2643
# Collect availability for watched nodes, and keep track of the collective
2744
# availability for all the watched nodes.
2845
nodes = self.getNodes()
@@ -162,21 +179,36 @@ def unPrefixNodeLabel(self, key, val, prefix):
162179
class FromFileAction(argparse.Action):
163180

164181
def __init__(self, option_strings, dest, nargs=None, **kwargs):
165-
if nargs is not None:
166-
raise ValueError("nargs not allowed")
167182
super(FromFileAction, self).__init__(option_strings, dest, **kwargs)
168183

169-
def __call__(self, parser, namespace, values, option_string=None):
170-
print('%r %r %r' % (namespace, values, option_string))
171-
setattr(namespace, self.dest, values)
184+
def __call__(self, parser, namespace, path, option_string=None):
185+
if (path):
186+
data = None
187+
with open(path) as f:
188+
data = json.load(f)
189+
190+
if (data is None):
191+
return
192+
193+
if ('watch' in data):
194+
namespace.watch += data['watch']
195+
196+
if (data['toggle']):
197+
namespace.toggle += data['toggle']
198+
199+
if ('label' in data):
200+
namespace.label += data['label']
201+
202+
return
172203

173204
def main():
174205
parser = argparse.ArgumentParser(description='Toggles one or more constraints depending on node availability')
175206
parser.add_argument('--watch', metavar='watch', action='append', default=[], help='A node which availability is to be watched.')
176207
parser.add_argument('--toggle', metavar='toggle', action='append', default=[], help='A node for which constraints are to be toggled. Defaults to all nodes.')
177-
parser.add_argument('--label', metavar='label', action='append', help='A label which is to be toggled according to availability for watched nodes.')
208+
parser.add_argument('--label', metavar='label', action='append', default=[], help='A label which is to be toggled according to availability for watched nodes.')
178209
parser.add_argument('--prefix', metavar='prefix', default='disabled', help='The prefix to use for disabled labels. Defaults to "disabled".')
179-
parser.add_argument('--from-file', metavar='fromFile', action=FromFileAction, help='A file which holds configurations.')
210+
parser.add_argument('fromFile', action=FromFileAction, help='A file which holds configurations.')
211+
180212
args = vars(parser.parse_args())
181213
se = SwarmConstraint(args)
182214

@@ -186,6 +218,9 @@ def main():
186218
time.sleep(10)
187219
except KeyboardInterrupt:
188220
break
221+
except Exception as err:
222+
print(err)
223+
break
189224

190225
if __name__ == '__main__':
191226
main()

0 commit comments

Comments
 (0)