[d743d60] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | |
---|
| 5 | from federation.remote_service import service_caller |
---|
| 6 | from federation.client_lib import client_opts, exit_with_fault, RPCException, \ |
---|
| 7 | wrangle_standard_options, do_rpc, get_experiment_names, save_certfile |
---|
| 8 | |
---|
| 9 | class fedd_create_opts(client_opts): |
---|
| 10 | """ |
---|
| 11 | Extra options that create needs. Help entries should explain them. |
---|
| 12 | """ |
---|
| 13 | def __init__(self): |
---|
| 14 | client_opts.__init__(self) |
---|
| 15 | self.add_option("--experiment_cert", dest="out_certfile", |
---|
| 16 | type="string", help="output certificate file") |
---|
| 17 | self.add_option("--experiment_name", dest="exp_name", |
---|
| 18 | type="string", help="Suggested experiment name") |
---|
| 19 | self.add_option("--file", dest="file", |
---|
| 20 | help="experiment description file") |
---|
| 21 | self.add_option("--project", action="store", dest="project", |
---|
| 22 | type="string", |
---|
| 23 | help="Project to export from master") |
---|
| 24 | self.add_option("--master", dest="master", |
---|
| 25 | help="Master testbed in the federation (pseudo project export)") |
---|
| 26 | self.add_option("--service", dest="service", action="append", |
---|
| 27 | type="string", default=[], |
---|
| 28 | help="Service description name:exporters:importers:attrs") |
---|
| 29 | |
---|
| 30 | def parse_service(svc): |
---|
| 31 | """ |
---|
| 32 | Pasre a service entry into a hash representing a service entry in a |
---|
| 33 | message. The format is: |
---|
| 34 | svc_name:exporter(s):importer(s):attr=val,attr=val |
---|
| 35 | The svc_name is teh service name, exporter is the exporting testbeds |
---|
| 36 | (comma-separated) importer is the importing testbeds (if any) and the rest |
---|
| 37 | are attr=val pairs that will become attributes of the service. These |
---|
| 38 | include parameters and other such stuff. |
---|
| 39 | """ |
---|
| 40 | |
---|
| 41 | terms = svc.split(':') |
---|
| 42 | svcd = { } |
---|
| 43 | if len(terms) < 2 or len(terms[0]) == 0 or len(terms[1]) == 0: |
---|
| 44 | sys.exit("Bad service description '%s': Not enough terms" % svc) |
---|
| 45 | |
---|
| 46 | svcd['name'] = terms[0] |
---|
| 47 | svcd['export'] = terms[1].split(",") |
---|
| 48 | if len(terms) > 2 and len(terms[2]) > 0: |
---|
| 49 | svcd['import'] = terms[2].split(",") |
---|
| 50 | if len(terms) > 3 and len(terms[3]) > 0: |
---|
| 51 | svcd['fedAttr'] = [ ] |
---|
| 52 | for t in terms[3].split(","): |
---|
| 53 | i = t.find("=") |
---|
| 54 | if i != -1 : |
---|
| 55 | svcd['fedAttr'].append( |
---|
| 56 | {'attribute': t[0:i], 'value': t[i+1:]}) |
---|
| 57 | else: |
---|
| 58 | sys.exit("Bad service attribute '%s': no equals sign" % t) |
---|
| 59 | return svcd |
---|
| 60 | |
---|
| 61 | def project_export_service(master, project): |
---|
| 62 | """ |
---|
| 63 | Output the dict representation of a project_export service for this project |
---|
| 64 | and master. |
---|
| 65 | """ |
---|
| 66 | return { |
---|
| 67 | 'name': 'project_export', |
---|
| 68 | 'export': [master], |
---|
| 69 | 'importall': True, |
---|
| 70 | 'fedAttr': [ |
---|
| 71 | { 'attribute': 'project', 'value': project }, |
---|
| 72 | ], |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | # Main line |
---|
| 76 | parser = fedd_create_opts() |
---|
| 77 | (opts, args) = parser.parse_args() |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | cert, fid = wrangle_standard_options(opts) |
---|
| 81 | |
---|
| 82 | if opts.file: |
---|
| 83 | try: |
---|
| 84 | exp_desc = "".join([ line for line in open(opts.file, 'r')]) |
---|
| 85 | except EnvironmentError: |
---|
| 86 | sys.exit("Cannot read description file (%s)" %opts.file) |
---|
| 87 | else: |
---|
| 88 | sys.exit("Must specify an experiment description (--file)") |
---|
| 89 | |
---|
| 90 | out_certfile = opts.out_certfile |
---|
| 91 | |
---|
| 92 | # Fill in services |
---|
| 93 | svcs = [] |
---|
| 94 | if opts.master and opts.project: |
---|
| 95 | svcs.append(project_export_service(opts.master, opts.project)) |
---|
| 96 | svcs.extend([ parse_service(s) for s in opts.service]) |
---|
| 97 | if not svcs: |
---|
| 98 | print >>sys.stderr, "Warning:Neither master/project nor services requested" |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | msg = { } |
---|
| 102 | |
---|
| 103 | if opts.exp_name: |
---|
| 104 | msg['experimentID'] = { 'localname': opts.exp_name } |
---|
| 105 | |
---|
| 106 | if opts.debug > 1: print >>sys.stderr, msg |
---|
| 107 | |
---|
| 108 | try: |
---|
| 109 | resp_dict = do_rpc(msg, |
---|
| 110 | opts.url, opts.transport, cert, opts.trusted, |
---|
| 111 | serialize_only=opts.serialize_only, |
---|
| 112 | tracefile=opts.tracefile, |
---|
| 113 | caller=service_caller('New'), responseBody="NewResponseBody") |
---|
| 114 | except RPCException, e: |
---|
| 115 | exit_with_fault(e) |
---|
| 116 | except RuntimeError, e: |
---|
| 117 | sys.exit("Error processing RPC: %s" % e) |
---|
| 118 | |
---|
| 119 | if opts.debug > 1: print >>sys.stderr, resp_dict |
---|
| 120 | |
---|
| 121 | try: |
---|
| 122 | save_certfile(opts.out_certfile, resp_dict.get('experimentAccess', None)) |
---|
| 123 | except EnvironmentError, e: |
---|
| 124 | sys.exit('Could not write to %s: %s' % (opts.out_certfile, e)) |
---|
| 125 | |
---|
| 126 | e_fedid, e_local = get_experiment_names(resp_dict.get('experimentID', None)) |
---|
| 127 | if not e_fedid and not e_local and opts.serialize_only: |
---|
| 128 | e_local = "serialize" |
---|
| 129 | |
---|
| 130 | msg = { 'experimentdescription': { 'ns2description': exp_desc }, } |
---|
| 131 | |
---|
| 132 | if svcs: |
---|
| 133 | msg['service'] = svcs |
---|
| 134 | |
---|
| 135 | if e_fedid: |
---|
| 136 | msg['experimentID'] = { 'fedid': e_fedid } |
---|
| 137 | elif e_local: |
---|
| 138 | msg['experimentID'] = { 'localname': e_local } |
---|
| 139 | else: |
---|
| 140 | sys.exit("New did not return an experiment ID??") |
---|
| 141 | |
---|
| 142 | if opts.debug > 1: print >>sys.stderr, msg |
---|
| 143 | |
---|
| 144 | try: |
---|
| 145 | resp_dict = do_rpc(msg, |
---|
| 146 | opts.url, opts.transport, cert, opts.trusted, |
---|
| 147 | serialize_only=opts.serialize_only, |
---|
| 148 | tracefile=opts.tracefile, |
---|
| 149 | caller=service_caller('Create'), responseBody="CreateResponseBody") |
---|
| 150 | except RPCException, e: |
---|
| 151 | exit_with_fault(e) |
---|
| 152 | except RuntimeError, e: |
---|
| 153 | sys.exit("Error processing RPC: %s" % e) |
---|
| 154 | |
---|
| 155 | if opts.debug > 1: print >>sys.stderr, resp_dict |
---|
| 156 | |
---|
| 157 | e_fedid, e_local = get_experiment_names(resp_dict.get('experimentID', None)) |
---|
| 158 | st = resp_dict.get('experimentStatus', None) |
---|
| 159 | |
---|
| 160 | if e_local: print "localname: %s" % e_local |
---|
| 161 | if e_fedid: print "fedid: %s" % e_fedid |
---|
| 162 | if st: print "status: %s" % st |
---|
| 163 | |
---|