1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | |
---|
5 | from deter import topdl |
---|
6 | |
---|
7 | from federation.proof import proof |
---|
8 | from federation.remote_service import service_caller |
---|
9 | from federation.client_lib import client_opts, exit_with_fault, RPCException, \ |
---|
10 | wrangle_standard_options, do_rpc, get_experiment_names, save_certfile,\ |
---|
11 | ns_service_re |
---|
12 | |
---|
13 | class ns_topdl_opts(client_opts): |
---|
14 | def __init__(self): |
---|
15 | client_opts.__init__(self) |
---|
16 | self.add_option("--file", dest="file", |
---|
17 | action='callback', callback=self.expand_file, type='str', |
---|
18 | help="experiment description file") |
---|
19 | self.add_option("--output", dest="outfile", type="string", |
---|
20 | help="output topdl file") |
---|
21 | |
---|
22 | parser = ns_topdl_opts() |
---|
23 | (opts, args) = parser.parse_args() |
---|
24 | |
---|
25 | try: |
---|
26 | cert, fid, url = wrangle_standard_options(opts) |
---|
27 | except RuntimeError, e: |
---|
28 | sys.exit("%s" %e) |
---|
29 | svcs = [] |
---|
30 | contents = '' |
---|
31 | if opts.file: |
---|
32 | try: |
---|
33 | for l in open(opts.file, 'r'): |
---|
34 | contents += l |
---|
35 | if ns_service_re.match(l): |
---|
36 | svcs.append('SERVICE: %s' % ns_service_re.match(l).group(1)) |
---|
37 | except EnvironmentError, e: |
---|
38 | sys.exit("Can't read %s: %s" % (opts.file, e)) |
---|
39 | else: |
---|
40 | sys.exit("Must specify an experiment description (--file)") |
---|
41 | |
---|
42 | msg = { 'description': { 'ns2description': contents }, } |
---|
43 | |
---|
44 | if opts.debug > 1: print >>sys.stderr, msg |
---|
45 | |
---|
46 | try: |
---|
47 | resp_dict = do_rpc(msg, |
---|
48 | url, opts.transport, cert, opts.trusted, |
---|
49 | serialize_only=opts.serialize_only, |
---|
50 | tracefile=opts.tracefile, |
---|
51 | caller=service_caller('Ns2Topdl'), |
---|
52 | responseBody="Ns2TopdlResponseBody") |
---|
53 | except RPCException, e: |
---|
54 | exit_with_fault(e, 'Ns2Topdl', opts) |
---|
55 | except RuntimeError, e: |
---|
56 | sys.exit("Error processing RPC: %s" % e) |
---|
57 | |
---|
58 | if 'experimentdescription' in resp_dict: |
---|
59 | if 'topdldescription' in resp_dict['experimentdescription']: |
---|
60 | exp = resp_dict['experimentdescription']['topdldescription'] |
---|
61 | top = topdl.Topology(**exp) |
---|
62 | else: |
---|
63 | sys.exit("Bad response: could not translate") |
---|
64 | elif opts.serialize_only: |
---|
65 | sys.exit(0) |
---|
66 | else: |
---|
67 | sys.exit("Bad response. %s" % e.message) |
---|
68 | |
---|
69 | if len(svcs) > 0 : |
---|
70 | comments = '<!--\n%s\n-->' % '\n'.join(svcs) |
---|
71 | else: |
---|
72 | comments = '' |
---|
73 | |
---|
74 | if opts.outfile: |
---|
75 | try: |
---|
76 | f = open(opts.outfile, "w") |
---|
77 | print >>f, comments |
---|
78 | print >>f, topdl.topology_to_xml(top, top="experiment") |
---|
79 | f.close() |
---|
80 | except EnvironmentError, e: |
---|
81 | sys.exit("Can't write to %s: %s" % (opts.outfile, e)) |
---|
82 | else: |
---|
83 | print comments |
---|
84 | print topdl.topology_to_xml(top, top="experiment") |
---|
85 | proof = proof.from_dict(resp_dict.get('proof', {})) |
---|
86 | if proof and opts.auth_log: |
---|
87 | log_authentication(opts.auth_log, 'New (create)', 'succeeded', proof) |
---|