blob: e1254815e523856639425511e376e2c9de7d6cf7 [file] [log] [blame]
mbligh38e9d782008-05-01 20:49:41 +00001#!/usr/bin/python
2
showard170873e2009-01-07 00:22:26 +00003import cgi, os, sys, urllib2
4import common
5from autotest_lib.client.common_lib import global_config
mbligh062ed152009-01-13 00:57:14 +00006from autotest_lib.client.bin import utils
showardef6fe022009-03-27 20:55:16 +00007from autotest_lib.frontend.afe.json_rpc import serviceHandler
mbligh38e9d782008-05-01 20:49:41 +00008
showardef6fe022009-03-27 20:55:16 +00009_PAGE = """\
showarda80823b2008-07-24 16:33:35 +000010Status: 302 Found
mblighe0869d02008-06-04 20:04:28 +000011Content-Type: text/plain
showard77f95db2008-07-17 16:59:38 +000012Location: %s\r\n\r
mbligh38e9d782008-05-01 20:49:41 +000013"""
14
Simran Basi6e58d972014-09-18 14:18:44 -070015GOOGLE_STORAGE_PATTERN = 'storage.cloud.google.com/'
16
showardef6fe022009-03-27 20:55:16 +000017# Define function for retrieving logs
mbligh062ed152009-01-13 00:57:14 +000018def _retrieve_logs_dummy(job_path):
19 pass
20
mblighbafd9a72009-07-28 23:27:42 +000021site_retrieve_logs = utils.import_site_function(__file__,
22 "autotest_lib.tko.site_retrieve_logs", "site_retrieve_logs",
mbligh062ed152009-01-13 00:57:14 +000023 _retrieve_logs_dummy)
24
mblighbafd9a72009-07-28 23:27:42 +000025site_find_repository_host = utils.import_site_function(__file__,
26 "autotest_lib.tko.site_retrieve_logs", "site_find_repository_host",
27 _retrieve_logs_dummy)
28
29
mbligh38e9d782008-05-01 20:49:41 +000030form = cgi.FieldStorage(keep_blank_values=True)
showardef6fe022009-03-27 20:55:16 +000031# determine if this is a JSON-RPC request. we support both so that the new TKO
32# client can use its RPC client code, but the old TKO can still use simple GET
33# params.
34_is_json_request = form.has_key('callback')
35
36def _get_requested_path():
37 if _is_json_request:
38 request_data = form['request'].value
39 request = serviceHandler.ServiceHandler.translateRequest(request_data)
40 parameters = request['params'][0]
41 return parameters['path']
42
43 return form['job'].value
mbligh38e9d782008-05-01 20:49:41 +000044
showard170873e2009-01-07 00:22:26 +000045
showard9484c312009-01-07 21:07:28 +000046def find_repository_host(job_path):
showard170873e2009-01-07 00:22:26 +000047 """Find the machine holding the given logs and return a URL to the logs"""
Dale Curtis74a314b2011-06-23 14:55:46 -070048 site_repo_info = site_find_repository_host(job_path)
49 if site_repo_info is not None:
50 return site_repo_info
51
showard170873e2009-01-07 00:22:26 +000052 config = global_config.global_config
53 drones = config.get_config_value('SCHEDULER', 'drones')
54 results_host = config.get_config_value('SCHEDULER', 'results_host')
mblighebf81642009-12-02 19:02:28 +000055 archive_host = config.get_config_value('SCHEDULER', 'archive_host',
56 default='')
57 results_repos = [results_host]
58 for drone in drones.split(','):
59 drone = drone.strip()
60 if drone not in results_repos:
61 results_repos.append(drone)
62
63 if archive_host and archive_host not in results_repos:
64 results_repos.append(archive_host)
65
66 for drone in results_repos:
67 if drone == 'localhost':
68 continue
69 http_path = 'http://%s%s' % (drone, job_path)
70 try:
71 utils.urlopen(http_path)
Dale Curtis74a314b2011-06-23 14:55:46 -070072 return 'http', utils.normalize_hostname(drone), job_path
mblighebf81642009-12-02 19:02:28 +000073 except urllib2.URLError:
74 pass
75
Simran Basi6e58d972014-09-18 14:18:44 -070076 # If the URL requested is a test result, it is now either on the local
77 # host or in Google Storage.
78 if job_path.startswith('/results/'):
79 # We only care about the path after '/results/'.
80 job_relative_path = job_path[9:]
81 if not os.path.exists(os.path.join('/usr/local/autotest/results',
82 job_relative_path)):
83 gsuri = utils.get_offload_gsuri().split('gs://')[1]
84 return ['https', GOOGLE_STORAGE_PATTERN, gsuri + job_relative_path]
85
showard170873e2009-01-07 00:22:26 +000086
Dale Curtis74a314b2011-06-23 14:55:46 -070087def get_full_url(info, log_path):
88 if info is not None:
89 protocol, host, path = info
90 prefix = '%s://%s' % (protocol, host)
showard9484c312009-01-07 21:07:28 +000091 else:
92 prefix = ''
Dale Curtis74a314b2011-06-23 14:55:46 -070093 path = log_path
showard9484c312009-01-07 21:07:28 +000094
showardef6fe022009-03-27 20:55:16 +000095 if _is_json_request:
96 return '%s/tko/jsonp_fetcher.cgi?%s' % (prefix,
97 os.environ['QUERY_STRING'])
showard9484c312009-01-07 21:07:28 +000098 else:
99 return prefix + path
100
101
showardef6fe022009-03-27 20:55:16 +0000102log_path = _get_requested_path()
Dale Curtis74a314b2011-06-23 14:55:46 -0700103info = find_repository_host(log_path)
mblighbafd9a72009-07-28 23:27:42 +0000104site_retrieve_logs(log_path)
Dale Curtis74a314b2011-06-23 14:55:46 -0700105print _PAGE % get_full_url(info, log_path)