Merge branch 'senslab2' of ssh://git.f-lab.fr/git/sfa into senslab2
[sfa.git] / clientbin / sfiAddLinks.py
1 #! /usr/bin/env python
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 from sfa.rspecs.version_manager import VersionManager
9
10 logger.enable_console()
11 command = Commands(usage="%prog [options] node1 node2...",
12                    description="Add links to the RSpec. " +
13                    "This command reads in an RSpec and outputs a modified " +
14                    "RSpec. Use this to add links to your slivers")
15 command.add_linkfile_option()
16 command.prep()
17
18 if not command.opts.linkfile:
19     print "Missing link list -- exiting"
20     command.parser.print_help()
21     sys.exit(1)
22     
23 if command.opts.infile:
24     infile=file(command.opts.infile)
25 else:
26     infile=sys.stdin
27 if command.opts.outfile:
28     outfile=file(command.opts.outfile,"w")
29 else:
30     outfile=sys.stdout
31 ad_rspec = RSpec(infile)
32 links = file(command.opts.linkfile).read().split('\n')
33 link_tuples = map(lambda x: tuple(x.split()), links)
34
35 version_manager = VersionManager()
36 try:
37     type = ad_rspec.version.type
38     version_num = ad_rspec.version.version
39     request_version = version_manager._get_version(type, version_num, 'request')    
40     request_rspec = RSpec(version=request_version)
41     request_rspec.version.merge(ad_rspec)
42     request_rspec.version.add_link_requests(link_tuples)
43 except:
44     logger.log_exc("sfiAddLinks FAILED with links %s" % links)
45     sys.exit(1)
46 print >>outfile, request_rspec.toxml()
47 sys.exit(0)