source: fedd/fedd_spewlog.py @ f7fc551

Last change on this file since f7fc551 was 2e46f35, checked in by mikeryan <mikeryan@…>, 14 years ago

switch to /usr/bin/env python to run python

  • Property mode set to 100755
File size: 2.8 KB
Line 
1#!/usr/bin/env python
2
3import sys
4import time
5
6from federation.proof import proof
7from federation.remote_service import service_caller
8from federation.client_lib import client_opts, exit_with_fault, RPCException, \
9        wrangle_standard_options, do_rpc, get_experiment_names, save_certfile
10
11class 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.
27parser = spew_opts()
28(opts, args) = parser.parse_args()
29
30try:
31    cert, fid, url = wrangle_standard_options(opts)
32except RuntimeError, e:
33    sys.exit("%s" %e)
34
35if opts.exp_name and opts.exp_certfile:
36    sys.exit("Only one of --experiment_cert and --experiment_name permitted");
37elif opts.exp_certfile:
38    exp_id = { 'fedid': fedid(file=opts.exp_certfile) }
39elif opts.exp_name:
40    exp_id = { 'localname' : opts.exp_name }
41else:
42    sys.exit("specify one of --experiment_cert and --experiment_name")
43
44if opts.logfile:
45    try:
46        out = open(opts.logfile, "w")
47    except IOError,e:
48        sys.exit("Cannot open logfile: %s" %e)
49else:
50    out = sys.stdout
51
52req = { 'experiment': exp_id }
53
54status = "starting"
55log = ""
56log_offset = 0
57update = opts.update
58while 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)
88print >>out
89print >>out, status
90out.close()
Note: See TracBrowser for help on using the repository browser.