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