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