1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | |
---|
5 | from federation.fedid import fedid |
---|
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, log_authentication |
---|
10 | |
---|
11 | class terminate_opts(client_opts): |
---|
12 | def __init__(self): |
---|
13 | client_opts.__init__(self) |
---|
14 | self.add_option("--experiment_cert", dest="exp_certfile", |
---|
15 | action='callback', callback=self.expand_file, type='str', |
---|
16 | help="experiment certificate file") |
---|
17 | self.add_option("--experiment_name", dest="exp_name", |
---|
18 | type="string", help="human readable experiment name") |
---|
19 | self.add_option("--force", dest="force", |
---|
20 | action="store_true", default=False, |
---|
21 | help="Force termination if experiment is in strange state") |
---|
22 | self.add_option("--logfile", dest="logfile", default=None, |
---|
23 | action='callback', callback=self.expand_file, type='str', |
---|
24 | help="File to write log to") |
---|
25 | self.add_option("--print_log", dest="print_log", default=False, |
---|
26 | action="store_true", |
---|
27 | help="Print deallocation log to standard output") |
---|
28 | |
---|
29 | # Main line |
---|
30 | parser = terminate_opts() |
---|
31 | (opts, args) = parser.parse_args() |
---|
32 | |
---|
33 | try: |
---|
34 | cert, fid, url = wrangle_standard_options(opts) |
---|
35 | except RuntimeError, e: |
---|
36 | sys.exit("%s" %e) |
---|
37 | |
---|
38 | if opts.exp_name and opts.exp_certfile: |
---|
39 | sys.exit("Only one of --experiment_cert and --experiment_name permitted") |
---|
40 | elif opts.exp_certfile: |
---|
41 | exp_id = { 'fedid': fedid(file=opts.exp_certfile) } |
---|
42 | elif opts.exp_name: |
---|
43 | exp_id = { 'localname' : opts.exp_name } |
---|
44 | else: |
---|
45 | sys.exit("Must give one of --experiment_cert or --experiment_name"); |
---|
46 | |
---|
47 | |
---|
48 | if opts.print_log and opts.logfile: |
---|
49 | sys.exit("Only one of --logfile and --print_log is permitted") |
---|
50 | elif opts.print_log: |
---|
51 | out = sys.stdout |
---|
52 | elif opts.logfile: |
---|
53 | try: |
---|
54 | out = open(opts.logfile, "w") |
---|
55 | except EnvironmentError, e: |
---|
56 | sys.exit("Cannot open logfile: %s" %e) |
---|
57 | else: |
---|
58 | out = None |
---|
59 | |
---|
60 | req = { 'experiment': exp_id, 'force': opts.force } |
---|
61 | try: |
---|
62 | resp_dict = do_rpc(req, |
---|
63 | url, opts.transport, cert, opts.trusted, |
---|
64 | serialize_only=opts.serialize_only, |
---|
65 | tracefile=opts.tracefile, |
---|
66 | caller=service_caller('Terminate'), |
---|
67 | responseBody='TerminateResponseBody') |
---|
68 | except RPCException, e: |
---|
69 | exit_with_fault(e, 'Terminate', opts) |
---|
70 | except RuntimeError, e: |
---|
71 | sys.exit("Error processing RPC: %s" % e) |
---|
72 | |
---|
73 | if out: |
---|
74 | log = resp_dict.get('deallocationLog', None) |
---|
75 | if log: |
---|
76 | print >>out, log |
---|
77 | out.close() |
---|
78 | else: |
---|
79 | out.close() |
---|
80 | sys.exit("No log returned") |
---|
81 | proof = proof.from_dict(resp_dict.get('proof', {})) |
---|
82 | if proof and opts.auth_log: |
---|
83 | log_authentication(opts.auth_log, 'Terminate', 'succeeded', proof) |
---|