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