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