source: fedd/fedd_create.py @ d743d60

axis_examplecompt_changesinfo-opsversion-3.01version-3.02
Last change on this file since d743d60 was d743d60, checked in by Ted Faber <faber@…>, 14 years ago

Totally refactor fedd_client.py into component scripts. The previous layout
have become a twisty hell of misdirected OOP and learning python run amok.
This version is actually pretty readable and will be much easier to build on.

  • Property mode set to 100755
File size: 5.0 KB
Line 
1#!/usr/local/bin/python
2
3import sys
4
5from federation.remote_service import service_caller
6from federation.client_lib import client_opts, exit_with_fault, RPCException, \
7        wrangle_standard_options, do_rpc, get_experiment_names, save_certfile
8
9class 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
30def 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
61def 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
76parser = fedd_create_opts()
77(opts, args) = parser.parse_args()
78
79
80cert, fid = wrangle_standard_options(opts)
81
82if 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)
87else:
88    sys.exit("Must specify an experiment description (--file)")
89
90out_certfile = opts.out_certfile
91
92# Fill in services
93svcs = []
94if opts.master and opts.project:
95    svcs.append(project_export_service(opts.master, opts.project))
96svcs.extend([ parse_service(s) for s in opts.service])
97if not svcs:
98    print >>sys.stderr, "Warning:Neither master/project nor services requested"
99
100
101msg = { }
102
103if opts.exp_name:
104    msg['experimentID'] = { 'localname': opts.exp_name }
105
106if opts.debug > 1: print >>sys.stderr, msg
107
108try:
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")
114except RPCException, e:
115    exit_with_fault(e)
116except RuntimeError, e:
117    sys.exit("Error processing RPC: %s" % e)
118
119if opts.debug > 1: print >>sys.stderr, resp_dict
120
121try:
122    save_certfile(opts.out_certfile, resp_dict.get('experimentAccess', None))
123except EnvironmentError, e:
124    sys.exit('Could not write to %s: %s' %  (opts.out_certfile, e))
125
126e_fedid, e_local = get_experiment_names(resp_dict.get('experimentID', None))
127if not e_fedid and not e_local and opts.serialize_only:
128    e_local = "serialize"
129
130msg = { 'experimentdescription': { 'ns2description': exp_desc }, }
131
132if svcs:
133    msg['service'] = svcs
134
135if e_fedid:
136    msg['experimentID'] = { 'fedid': e_fedid }
137elif e_local:
138    msg['experimentID'] = { 'localname': e_local }
139else:
140    sys.exit("New did not return an experiment ID??")
141
142if opts.debug > 1: print >>sys.stderr, msg
143
144try:
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")
150except RPCException, e:
151    exit_with_fault(e)
152except RuntimeError, e:
153    sys.exit("Error processing RPC: %s" % e)
154
155if opts.debug > 1: print >>sys.stderr, resp_dict
156
157e_fedid, e_local = get_experiment_names(resp_dict.get('experimentID', None))
158st = resp_dict.get('experimentStatus', None)
159
160if e_local: print "localname: %s" % e_local
161if e_fedid: print "fedid: %s" % e_fedid
162if st: print "status: %s" % st
163
Note: See TracBrowser for help on using the repository browser.