1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | |
---|
5 | from deter import topdl |
---|
6 | from deter import fedid |
---|
7 | |
---|
8 | from federation.proof import proof |
---|
9 | from federation.remote_service import service_caller |
---|
10 | from federation.client_lib import client_opts, exit_with_fault, RPCException,\ |
---|
11 | wrangle_standard_options, do_rpc, get_experiment_names, info_format, \ |
---|
12 | log_authentication |
---|
13 | |
---|
14 | class operation_opts(client_opts): |
---|
15 | def __init__(self): |
---|
16 | client_opts.__init__(self) |
---|
17 | self.add_option("--experiment_cert", dest="exp_certfile", |
---|
18 | action='callback', callback=self.expand_file, type='str', |
---|
19 | help="experiment certificate file") |
---|
20 | self.add_option("--experiment_name", dest="exp_name", |
---|
21 | type="string", help="human readable experiment name") |
---|
22 | self.add_option('--target', dest='targets', action='append', |
---|
23 | help='targets of the operation') |
---|
24 | self.add_option('--operation', dest='op', help='operation to carry out') |
---|
25 | self.add_option('--param', dest='params', action='append', |
---|
26 | help='parameters to the operation as name=value') |
---|
27 | |
---|
28 | parser = operation_opts() |
---|
29 | (opts, args) = parser.parse_args() |
---|
30 | try: |
---|
31 | cert, fid, url = wrangle_standard_options(opts) |
---|
32 | except RuntimeError, e: |
---|
33 | sys.exit("%s" % e) |
---|
34 | |
---|
35 | if opts.exp_name and opts.exp_certfile: |
---|
36 | sys.exit("Only one of --experiment_cert and --experiment_name permitted") |
---|
37 | elif opts.exp_certfile: |
---|
38 | exp_id = { 'fedid': fedid(file=opts.exp_certfile) } |
---|
39 | elif opts.exp_name: |
---|
40 | exp_id = { 'localname' : opts.exp_name } |
---|
41 | else: |
---|
42 | sys.exit("specify one of --experiment_cert and --experiment_name") |
---|
43 | |
---|
44 | if not opts.op: sys.exit('specifiy an --operation') |
---|
45 | if not opts.targets: sys.exit('specify one or more targets') |
---|
46 | |
---|
47 | req = { 'experiment': exp_id, 'operation': opts.op, 'target': opts.targets} |
---|
48 | if opts.params: |
---|
49 | param = [] |
---|
50 | for p in opts.params: |
---|
51 | try: |
---|
52 | a, v = p.split('=') |
---|
53 | except ValueError: |
---|
54 | sys.exit('Parameters must be in name=value: %s' %p) |
---|
55 | param.append({'attribute': a, 'value': v}) |
---|
56 | req['parameter'] = param |
---|
57 | |
---|
58 | try: |
---|
59 | resp_dict = do_rpc(req, |
---|
60 | url, opts.transport, cert, opts.trusted, |
---|
61 | serialize_only=opts.serialize_only, |
---|
62 | tracefile=opts.tracefile, |
---|
63 | caller=service_caller('Operation'), |
---|
64 | responseBody='OperationResponseBody') |
---|
65 | except RPCException, e: |
---|
66 | exit_with_fault(e, 'Ftopo', opts) |
---|
67 | except RuntimeError, e: |
---|
68 | sys.exit("Error processing RPC: %s" % e) |
---|
69 | |
---|
70 | for s in resp_dict.get('status', []): |
---|
71 | print "Target: %s Message: %s (code: %d)" % \ |
---|
72 | (s.get('target', ''), s.get('description',''), s.get('code', -1)) |
---|
73 | |
---|
74 | proof = proof.from_dict(resp_dict.get('proof', {})) |
---|
75 | if proof and opts.auth_log: |
---|
76 | log_authentication(opts.auth_log, 'Operation', 'succeeded', proof) |
---|