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