a little nicer wrt pep8
[sfa.git] / clientbin / sfiAddAttribute.py
1 #!/usr/bin/env python3
2
3 import sys
4
5 from sfa.util.sfalogging import logger
6 from sfa.client.sfi_commands import Commands
7 from sfa.rspecs.rspec import RSpec
8
9 logger.enable_console()
10 command = Commands(usage="%prog [options] [node1 node2...]",
11                    description="Add sliver attributes to the RSpec. " +
12                    "This command reads in an RSpec and outputs a modified " +
13                    "RSpec. Use this to add attributes to individual nodes " +
14                    "in your slice.  If no nodes are specified, the " +
15                    "attributes will be added to ALL nodes.",
16                    epilog="NOTE: Only admins can actually set these " +
17                    "attributes, with the exception of --delegations")
18 command.add_nodefile_option()
19 command.add_attribute_options()
20 command.prep()
21
22 if command.opts.infile:
23     attrs = command.get_attribute_dict()
24     rspec = RSpec(command.opts.infile)
25     nodes = []
26     if command.opts.nodefile:
27         f = open(command.opts.nodefile, "r")
28         nodes = f.read().split()
29         f.close()
30
31     for name in attrs:
32         print(name, attrs[name], file=sys.stderr)
33         for value in attrs[name]:
34             if not nodes:
35                 try:
36                     rspec.version.add_default_sliver_attribute(name, value)
37                 except:
38                     logger.log_exc(
39                         "sfiAddAttribute FAILED on all nodes: %s=%s" % (name, value))
40             else:
41                 for node in nodes:
42                     try:
43                         rspec.version.add_sliver_attribute(node, name, value)
44                     except:
45                         logger.log_exc(
46                             "sfiAddAttribute FAILED on node %s: %s=%s" % (node, name, value))
47
48     print(rspec.toxml())