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 | action='callback', callback=self.expand_file, type='str', |
---|
15 | help="experiment name certificate file") |
---|
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, |
---|
19 | action='callback', callback=self.expand_file, type='str', |
---|
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 | |
---|
29 | cert, fid, url = wrangle_standard_options(opts) |
---|
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("specify one of --experiment_cert and --experiment_name") |
---|
39 | |
---|
40 | if opts.logfile: |
---|
41 | try: |
---|
42 | out = open(opts.logfile, "w") |
---|
43 | except IOError,e: |
---|
44 | sys.exit("Cannot open logfile: %s" %e) |
---|
45 | else: |
---|
46 | out = sys.stdout |
---|
47 | |
---|
48 | req = { 'experiment': exp_id } |
---|
49 | |
---|
50 | status = "starting" |
---|
51 | log = "" |
---|
52 | log_offset = 0 |
---|
53 | update = opts.update |
---|
54 | while status == 'starting': |
---|
55 | try: |
---|
56 | resp_dict = do_rpc(req, |
---|
57 | url, opts.transport, cert, opts.trusted, |
---|
58 | serialize_only=opts.serialize_only, |
---|
59 | tracefile=opts.tracefile, |
---|
60 | caller=service_caller('Info'), responseBody="InfoResponseBody") |
---|
61 | except RPCException, e: |
---|
62 | exit_with_fault(e) |
---|
63 | except RuntimeError, e: |
---|
64 | sys.exit("Error processing RPC: %s" % e) |
---|
65 | |
---|
66 | if not opts.serialize_only: |
---|
67 | status = resp_dict.get('experimentStatus', None) |
---|
68 | else: |
---|
69 | status = "active" |
---|
70 | log = resp_dict.get('allocationLog', None) |
---|
71 | if not status: |
---|
72 | sys.exit("No status in Info response??") |
---|
73 | if log: |
---|
74 | if len(log) > log_offset: |
---|
75 | print >>out, log[log_offset:], |
---|
76 | out.flush() |
---|
77 | log_offset = len(log) |
---|
78 | if status == 'starting': |
---|
79 | time.sleep(update) |
---|
80 | print >>out |
---|
81 | print >>out, status |
---|
82 | out.close() |
---|