[2e46f35] | 1 | #!/usr/bin/env python |
---|
[d743d60] | 2 | |
---|
| 3 | import sys |
---|
| 4 | |
---|
[6bedbdba] | 5 | from deter import topdl |
---|
| 6 | |
---|
[e83f2f2] | 7 | from federation.proof import proof |
---|
[d743d60] | 8 | from federation.remote_service import service_caller |
---|
| 9 | from federation.client_lib import client_opts, exit_with_fault, RPCException, \ |
---|
[c259a77] | 10 | wrangle_standard_options, do_rpc, get_experiment_names, save_certfile,\ |
---|
| 11 | ns_service_re |
---|
[d743d60] | 12 | |
---|
| 13 | class ns_topdl_opts(client_opts): |
---|
| 14 | def __init__(self): |
---|
| 15 | client_opts.__init__(self) |
---|
| 16 | self.add_option("--file", dest="file", |
---|
[62f3dd9] | 17 | action='callback', callback=self.expand_file, type='str', |
---|
[d743d60] | 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 | |
---|
[a0c2866] | 25 | try: |
---|
| 26 | cert, fid, url = wrangle_standard_options(opts) |
---|
| 27 | except RuntimeError, e: |
---|
| 28 | sys.exit("%s" %e) |
---|
[c259a77] | 29 | svcs = [] |
---|
| 30 | contents = '' |
---|
[d743d60] | 31 | if opts.file: |
---|
| 32 | try: |
---|
[c259a77] | 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)) |
---|
[d743d60] | 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, |
---|
[5d854e1] | 48 | url, opts.transport, cert, opts.trusted, |
---|
[d743d60] | 49 | serialize_only=opts.serialize_only, |
---|
| 50 | tracefile=opts.tracefile, |
---|
| 51 | caller=service_caller('Ns2Topdl'), |
---|
| 52 | responseBody="Ns2TopdlResponseBody") |
---|
| 53 | except RPCException, e: |
---|
[e83f2f2] | 54 | exit_with_fault(e, 'Ns2Topdl', opts) |
---|
[d743d60] | 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 | |
---|
[c259a77] | 69 | if len(svcs) > 0 : |
---|
| 70 | comments = '<!--\n%s\n-->' % '\n'.join(svcs) |
---|
| 71 | else: |
---|
| 72 | comments = '' |
---|
| 73 | |
---|
[d743d60] | 74 | if opts.outfile: |
---|
| 75 | try: |
---|
| 76 | f = open(opts.outfile, "w") |
---|
[c259a77] | 77 | print >>f, comments |
---|
[d743d60] | 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: |
---|
[c259a77] | 83 | print comments |
---|
[d743d60] | 84 | print topdl.topology_to_xml(top, top="experiment") |
---|
[e83f2f2] | 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) |
---|