mbligh | 38e9d78 | 2008-05-01 20:49:41 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 3 | import cgi, os, sys, urllib2 |
| 4 | import common |
| 5 | from autotest_lib.client.common_lib import global_config |
mbligh | 062ed15 | 2009-01-13 00:57:14 +0000 | [diff] [blame] | 6 | from autotest_lib.client.bin import utils |
showard | ef6fe02 | 2009-03-27 20:55:16 +0000 | [diff] [blame] | 7 | from autotest_lib.frontend.afe.json_rpc import serviceHandler |
mbligh | 38e9d78 | 2008-05-01 20:49:41 +0000 | [diff] [blame] | 8 | |
showard | ef6fe02 | 2009-03-27 20:55:16 +0000 | [diff] [blame] | 9 | _PAGE = """\ |
showard | a80823b | 2008-07-24 16:33:35 +0000 | [diff] [blame] | 10 | Status: 302 Found |
mbligh | e0869d0 | 2008-06-04 20:04:28 +0000 | [diff] [blame] | 11 | Content-Type: text/plain |
showard | 77f95db | 2008-07-17 16:59:38 +0000 | [diff] [blame] | 12 | Location: %s\r\n\r |
mbligh | 38e9d78 | 2008-05-01 20:49:41 +0000 | [diff] [blame] | 13 | """ |
| 14 | |
Simran Basi | 6e58d97 | 2014-09-18 14:18:44 -0700 | [diff] [blame] | 15 | GOOGLE_STORAGE_PATTERN = 'storage.cloud.google.com/' |
| 16 | |
showard | ef6fe02 | 2009-03-27 20:55:16 +0000 | [diff] [blame] | 17 | # Define function for retrieving logs |
mbligh | 062ed15 | 2009-01-13 00:57:14 +0000 | [diff] [blame] | 18 | def _retrieve_logs_dummy(job_path): |
| 19 | pass |
| 20 | |
mbligh | bafd9a7 | 2009-07-28 23:27:42 +0000 | [diff] [blame] | 21 | site_retrieve_logs = utils.import_site_function(__file__, |
| 22 | "autotest_lib.tko.site_retrieve_logs", "site_retrieve_logs", |
mbligh | 062ed15 | 2009-01-13 00:57:14 +0000 | [diff] [blame] | 23 | _retrieve_logs_dummy) |
| 24 | |
mbligh | bafd9a7 | 2009-07-28 23:27:42 +0000 | [diff] [blame] | 25 | site_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 | |
mbligh | 38e9d78 | 2008-05-01 20:49:41 +0000 | [diff] [blame] | 30 | form = cgi.FieldStorage(keep_blank_values=True) |
showard | ef6fe02 | 2009-03-27 20:55:16 +0000 | [diff] [blame] | 31 | # 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 | |
| 36 | def _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 |
mbligh | 38e9d78 | 2008-05-01 20:49:41 +0000 | [diff] [blame] | 44 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 45 | |
showard | 9484c31 | 2009-01-07 21:07:28 +0000 | [diff] [blame] | 46 | def find_repository_host(job_path): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 47 | """Find the machine holding the given logs and return a URL to the logs""" |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 48 | site_repo_info = site_find_repository_host(job_path) |
| 49 | if site_repo_info is not None: |
| 50 | return site_repo_info |
| 51 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 52 | config = global_config.global_config |
| 53 | drones = config.get_config_value('SCHEDULER', 'drones') |
| 54 | results_host = config.get_config_value('SCHEDULER', 'results_host') |
mbligh | ebf8164 | 2009-12-02 19:02:28 +0000 | [diff] [blame] | 55 | 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 Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 72 | return 'http', utils.normalize_hostname(drone), job_path |
mbligh | ebf8164 | 2009-12-02 19:02:28 +0000 | [diff] [blame] | 73 | except urllib2.URLError: |
| 74 | pass |
| 75 | |
Simran Basi | 6e58d97 | 2014-09-18 14:18:44 -0700 | [diff] [blame] | 76 | # 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 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 86 | |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 87 | def get_full_url(info, log_path): |
| 88 | if info is not None: |
| 89 | protocol, host, path = info |
| 90 | prefix = '%s://%s' % (protocol, host) |
showard | 9484c31 | 2009-01-07 21:07:28 +0000 | [diff] [blame] | 91 | else: |
| 92 | prefix = '' |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 93 | path = log_path |
showard | 9484c31 | 2009-01-07 21:07:28 +0000 | [diff] [blame] | 94 | |
showard | ef6fe02 | 2009-03-27 20:55:16 +0000 | [diff] [blame] | 95 | if _is_json_request: |
| 96 | return '%s/tko/jsonp_fetcher.cgi?%s' % (prefix, |
| 97 | os.environ['QUERY_STRING']) |
showard | 9484c31 | 2009-01-07 21:07:28 +0000 | [diff] [blame] | 98 | else: |
| 99 | return prefix + path |
| 100 | |
| 101 | |
showard | ef6fe02 | 2009-03-27 20:55:16 +0000 | [diff] [blame] | 102 | log_path = _get_requested_path() |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 103 | info = find_repository_host(log_path) |
mbligh | bafd9a7 | 2009-07-28 23:27:42 +0000 | [diff] [blame] | 104 | site_retrieve_logs(log_path) |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 105 | print _PAGE % get_full_url(info, log_path) |