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