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