Planner: add migration script to drop planner tables from autotest_web database
[autotest-zwu.git] / tko / retrieve_logs.cgi
blobe4b5b2e7288cd21abe504898efba44a5e42f4f8b
1 #!/usr/bin/python
3 import cgi, os, sys, urllib2
4 import common
5 from autotest_lib.client.common_lib import global_config
6 from autotest_lib.client.bin import utils
7 from autotest_lib.frontend.afe.json_rpc import serviceHandler
9 _PAGE = """\
10 Status: 302 Found
11 Content-Type: text/plain
12 Location: %s\r\n\r
13 """
15 # Define function for retrieving logs
16 def _retrieve_logs_dummy(job_path):
17 pass
19 site_retrieve_logs = utils.import_site_function(__file__,
20 "autotest_lib.tko.site_retrieve_logs", "site_retrieve_logs",
21 _retrieve_logs_dummy)
23 site_find_repository_host = utils.import_site_function(__file__,
24 "autotest_lib.tko.site_retrieve_logs", "site_find_repository_host",
25 _retrieve_logs_dummy)
28 form = cgi.FieldStorage(keep_blank_values=True)
29 # determine if this is a JSON-RPC request. we support both so that the new TKO
30 # client can use its RPC client code, but the old TKO can still use simple GET
31 # params.
32 _is_json_request = form.has_key('callback')
34 def _get_requested_path():
35 if _is_json_request:
36 request_data = form['request'].value
37 request = serviceHandler.ServiceHandler.translateRequest(request_data)
38 parameters = request['params'][0]
39 return parameters['path']
41 return form['job'].value
44 def find_repository_host(job_path):
45 """Find the machine holding the given logs and return a URL to the logs"""
46 config = global_config.global_config
47 drones = config.get_config_value('SCHEDULER', 'drones')
48 results_host = config.get_config_value('SCHEDULER', 'results_host')
49 archive_host = config.get_config_value('SCHEDULER', 'archive_host',
50 default='')
51 results_repos = [results_host]
52 for drone in drones.split(','):
53 drone = drone.strip()
54 if drone not in results_repos:
55 results_repos.append(drone)
57 if archive_host and archive_host not in results_repos:
58 results_repos.append(archive_host)
60 for drone in results_repos:
61 if drone == 'localhost':
62 continue
63 http_path = 'http://%s%s' % (drone, job_path)
64 try:
65 utils.urlopen(http_path)
66 return drone
67 except urllib2.URLError:
68 pass
70 return site_find_repository_host(log_path)
73 def get_full_url(host, path):
74 if host:
75 if ':' in host:
76 host, port = host.split(':')
77 prefix = 'http://%s:%s' % (utils.normalize_hostname(host), port)
78 else:
79 prefix = 'http://%s' % utils.normalize_hostname(host)
80 else:
81 prefix = ''
83 if _is_json_request:
84 return '%s/tko/jsonp_fetcher.cgi?%s' % (prefix,
85 os.environ['QUERY_STRING'])
86 else:
87 return prefix + path
90 log_path = _get_requested_path()
91 host = find_repository_host(log_path)
92 site_retrieve_logs(log_path)
93 print _PAGE % get_full_url(host, log_path)