| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import sys |
|---|
| 4 | import time |
|---|
| 5 | |
|---|
| 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, save_certfile |
|---|
| 10 | |
|---|
| 11 | class spew_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 name certificate file") |
|---|
| 17 | self.add_option("--experiment_name", dest="exp_name", |
|---|
| 18 | type="string", help="human readable experiment name") |
|---|
| 19 | self.add_option("--logfile", dest="logfile", default=None, |
|---|
| 20 | action='callback', callback=self.expand_file, type='str', |
|---|
| 21 | help="File to write log to") |
|---|
| 22 | self.add_option('--update_time', dest='update', type='int', default=10, |
|---|
| 23 | help='how often to update the printed log') |
|---|
| 24 | |
|---|
| 25 | # Keep requesting experiment status and printing updates to the log until the |
|---|
| 26 | # experiment is done being created. |
|---|
| 27 | parser = spew_opts() |
|---|
| 28 | (opts, args) = parser.parse_args() |
|---|
| 29 | |
|---|
| 30 | try: |
|---|
| 31 | cert, fid, url = wrangle_standard_options(opts) |
|---|
| 32 | except RuntimeError, e: |
|---|
| 33 | sys.exit("%s" %e) |
|---|
| 34 | |
|---|
| 35 | if opts.exp_name and opts.exp_certfile: |
|---|
| 36 | sys.exit("Only one of --experiment_cert and --experiment_name permitted"); |
|---|
| 37 | elif opts.exp_certfile: |
|---|
| 38 | exp_id = { 'fedid': fedid(file=opts.exp_certfile) } |
|---|
| 39 | elif opts.exp_name: |
|---|
| 40 | exp_id = { 'localname' : opts.exp_name } |
|---|
| 41 | else: |
|---|
| 42 | sys.exit("specify one of --experiment_cert and --experiment_name") |
|---|
| 43 | |
|---|
| 44 | if opts.logfile: |
|---|
| 45 | try: |
|---|
| 46 | out = open(opts.logfile, "w") |
|---|
| 47 | except IOError,e: |
|---|
| 48 | sys.exit("Cannot open logfile: %s" %e) |
|---|
| 49 | else: |
|---|
| 50 | out = sys.stdout |
|---|
| 51 | |
|---|
| 52 | req = { 'experiment': exp_id } |
|---|
| 53 | |
|---|
| 54 | status = "starting" |
|---|
| 55 | log = "" |
|---|
| 56 | log_offset = 0 |
|---|
| 57 | update = opts.update |
|---|
| 58 | while status == 'starting': |
|---|
| 59 | try: |
|---|
| 60 | resp_dict = do_rpc(req, |
|---|
| 61 | url, opts.transport, cert, opts.trusted, |
|---|
| 62 | serialize_only=opts.serialize_only, |
|---|
| 63 | tracefile=opts.tracefile, |
|---|
| 64 | caller=service_caller('Info'), responseBody="InfoResponseBody") |
|---|
| 65 | except RPCException, e: |
|---|
| 66 | exit_with_fault(e, 'Info (spewlog)', opts) |
|---|
| 67 | except RuntimeError, e: |
|---|
| 68 | sys.exit("Error processing RPC: %s" % e) |
|---|
| 69 | |
|---|
| 70 | proof = proof.from_dict(resp_dict.get('proof', {})) |
|---|
| 71 | if proof and opts.auth_log: |
|---|
| 72 | log_authentication(opts.auth_log, 'Info (spewlog)', 'succeeded', proof) |
|---|
| 73 | |
|---|
| 74 | if not opts.serialize_only: |
|---|
| 75 | status = resp_dict.get('experimentStatus', None) |
|---|
| 76 | else: |
|---|
| 77 | status = "active" |
|---|
| 78 | log = resp_dict.get('allocationLog', None) |
|---|
| 79 | if not status: |
|---|
| 80 | sys.exit("No status in Info response??") |
|---|
| 81 | if log: |
|---|
| 82 | if len(log) > log_offset: |
|---|
| 83 | print >>out, log[log_offset:], |
|---|
| 84 | out.flush() |
|---|
| 85 | log_offset = len(log) |
|---|
| 86 | if status == 'starting': |
|---|
| 87 | time.sleep(update) |
|---|
| 88 | print >>out |
|---|
| 89 | print >>out, status |
|---|
| 90 | out.close() |
|---|