#!/usr/bin/env python import sys from deter import fedid from federation.proof import proof from federation.remote_service import service_caller from federation.client_lib import client_opts, exit_with_fault, RPCException, \ wrangle_standard_options, do_rpc, log_authentication class terminate_opts(client_opts): def __init__(self): client_opts.__init__(self) self.add_option("--experiment_cert", dest="exp_certfile", action='callback', callback=self.expand_file, type='str', help="experiment certificate file") self.add_option("--experiment_name", dest="exp_name", type="string", help="human readable experiment name") self.add_option("--force", dest="force", action="store_true", default=False, help="Force termination if experiment is in strange state") self.add_option("--logfile", dest="logfile", default=None, action='callback', callback=self.expand_file, type='str', help="File to write log to") self.add_option("--print_log", dest="print_log", default=False, action="store_true", help="Print deallocation log to standard output") # Main line parser = terminate_opts() (opts, args) = parser.parse_args() try: cert, fid, url = wrangle_standard_options(opts) except RuntimeError, e: sys.exit("%s" %e) if opts.exp_name and opts.exp_certfile: sys.exit("Only one of --experiment_cert and --experiment_name permitted") elif opts.exp_certfile: exp_id = { 'fedid': fedid(file=opts.exp_certfile) } elif opts.exp_name: exp_id = { 'localname' : opts.exp_name } else: sys.exit("Must give one of --experiment_cert or --experiment_name"); if opts.print_log and opts.logfile: sys.exit("Only one of --logfile and --print_log is permitted") elif opts.print_log: out = sys.stdout elif opts.logfile: try: out = open(opts.logfile, "w") except EnvironmentError, e: sys.exit("Cannot open logfile: %s" %e) else: out = None req = { 'experiment': exp_id, 'force': opts.force } try: resp_dict = do_rpc(req, url, opts.transport, cert, opts.trusted, serialize_only=opts.serialize_only, tracefile=opts.tracefile, caller=service_caller('Terminate'), responseBody='TerminateResponseBody') except RPCException, e: exit_with_fault(e, 'Terminate', opts) except RuntimeError, e: sys.exit("Error processing RPC: %s" % e) if out: log = resp_dict.get('deallocationLog', None) if log: print >>out, log out.close() else: out.close() sys.exit("No log returned") proof = proof.from_dict(resp_dict.get('proof', {})) if proof and opts.auth_log: log_authentication(opts.auth_log, 'Terminate', 'succeeded', proof)