Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Module for discovering Chrome OS test images and payloads.""" |
| 6 | |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 7 | import logging |
Chris Sosa | 481fbbf | 2013-02-27 15:31:11 -0800 | [diff] [blame] | 8 | import os |
Gilad Arnold | 1318001 | 2013-01-24 17:58:21 -0800 | [diff] [blame] | 9 | import re |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 10 | |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 11 | import common |
| 12 | from autotest_lib.client.common_lib import global_config |
Chris Sosa | 481fbbf | 2013-02-27 15:31:11 -0800 | [diff] [blame] | 13 | |
Alex Miller | dc41cfe | 2013-09-07 22:22:17 -0700 | [diff] [blame^] | 14 | # Make this easy for users to automatically keep devserver at ToT. |
| 15 | def update_devserver_checkout(): |
| 16 | """Bring the devserver checkout to ToT.""" |
| 17 | from autotest_lib.utils import build_externals, external_packages |
| 18 | tot = external_packages.find_top_of_autotest_tree() |
| 19 | install_dir = os.path.join(tot, build_externals.INSTALL_DIR) |
| 20 | build_externals.build_and_install_packages( |
| 21 | [external_packages.DevServerRepo()], install_dir) |
| 22 | |
| 23 | update_devserver_checkout() |
| 24 | from devserver import gsutil_util |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 25 | |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 26 | |
| 27 | # A string indicating a zip-file boundary within a URI path. This string must |
| 28 | # end with a '/', in order for standard basename code to work correctly for |
| 29 | # zip-encapsulated paths. |
| 30 | ZIPFILE_BOUNDARY = '//' |
Chris Sosa | b86d266 | 2013-05-08 18:04:54 -0700 | [diff] [blame] | 31 | ARCHIVE_URL_FORMAT = '%(archive_prefix)s/%(version)s' |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 32 | |
| 33 | |
| 34 | class TestImageError(BaseException): |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 35 | """Raised on any error in this module.""" |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 36 | pass |
| 37 | |
| 38 | |
Chris Sosa | 7287361 | 2013-09-06 14:54:55 -0700 | [diff] [blame] | 39 | class NotSingleItem(Exception): |
| 40 | """Raised when we want a single item but got multiple.""" |
| 41 | |
| 42 | |
Chris Sosa | b86d266 | 2013-05-08 18:04:54 -0700 | [diff] [blame] | 43 | def get_default_archive_url(board, build_version): |
| 44 | """Returns the default archive_url for the given board and build_version . |
Chris Sosa | fec1349 | 2013-03-28 11:19:17 -0700 | [diff] [blame] | 45 | |
Chris Sosa | b86d266 | 2013-05-08 18:04:54 -0700 | [diff] [blame] | 46 | @param board: the platform/board name |
| 47 | @param build_version: the full build version i.e. R27-3823.0.0-a2. |
Chris Sosa | fec1349 | 2013-03-28 11:19:17 -0700 | [diff] [blame] | 48 | """ |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 49 | archive_base = global_config.global_config.get_config_value( |
| 50 | 'CROS', 'image_storage_server') |
| 51 | archive_base = archive_base.rstrip('/') # Remove any trailing /'s. |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame] | 52 | |
| 53 | # TODO(garnold) adjustment to -he variant board names; should be removed |
| 54 | # once we switch to using artifacts from gs://chromeos-images/ |
| 55 | # (see chromium-os:38222) |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 56 | board = re.sub('-he$', '_he', board) |
Chris Sosa | b86d266 | 2013-05-08 18:04:54 -0700 | [diff] [blame] | 57 | archive_prefix = archive_base + '/%s-release' % board |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 58 | return ARCHIVE_URL_FORMAT % dict( |
Chris Sosa | b86d266 | 2013-05-08 18:04:54 -0700 | [diff] [blame] | 59 | archive_prefix=archive_prefix, version=build_version) |
| 60 | |
| 61 | |
| 62 | def get_archive_url_from_prefix(archive_prefix, build_version): |
| 63 | """Returns the gs archive_url given a particular archive_prefix. |
| 64 | |
| 65 | @param archive_prefix: Use the archive_prefix as the base of your URL |
| 66 | |
| 67 | construction (instead of config + board-release) e.g. |
| 68 | gs://my_location/my_super_awesome_directory. |
| 69 | @param build_version: the full build version i.e. R27-3823.0.0-a2. |
| 70 | """ |
| 71 | return ARCHIVE_URL_FORMAT % dict( |
| 72 | archive_prefix=archive_prefix, version=build_version) |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 73 | |
| 74 | |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame] | 75 | def gs_ls(pattern, archive_url, single): |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 76 | """Returns a list of URIs that match a given pattern. |
| 77 | |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame] | 78 | @param pattern: a regexp pattern to match (feeds into re.match). |
| 79 | @param archive_url: the gs uri where to search (see ARCHIVE_URL_FORMAT). |
| 80 | @param single: if true, expect a single match and return it. |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 81 | |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame] | 82 | @return A list of URIs (possibly an empty list). |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 83 | |
| 84 | """ |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame] | 85 | try: |
| 86 | logging.debug('Searching for pattern %s from url %s', pattern, |
| 87 | archive_url) |
| 88 | uri_list = gsutil_util.GetGSNamesWithWait( |
Chris Sosa | 7287361 | 2013-09-06 14:54:55 -0700 | [diff] [blame] | 89 | pattern, archive_url, err_str=__name__, timeout=1) |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame] | 90 | # Convert to the format our clients expect (full archive path). |
Chris Sosa | fec1349 | 2013-03-28 11:19:17 -0700 | [diff] [blame] | 91 | if uri_list: |
Chris Sosa | 7287361 | 2013-09-06 14:54:55 -0700 | [diff] [blame] | 92 | if not single or (single and len(uri_list) == 1): |
| 93 | return ['/'.join([archive_url, u]) for u in uri_list] |
| 94 | else: |
| 95 | raise NotSingleItem() |
Chris Sosa | fec1349 | 2013-03-28 11:19:17 -0700 | [diff] [blame] | 96 | |
| 97 | return [] |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame] | 98 | except gsutil_util.PatternNotSpecific as e: |
| 99 | raise TestImageError(str(e)) |
| 100 | except gsutil_util.GSUtilError: |
| 101 | return [] |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 102 | |
| 103 | |
Chris Sosa | fec1349 | 2013-03-28 11:19:17 -0700 | [diff] [blame] | 104 | def find_payload_uri(archive_url, delta=False, single=False): |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 105 | """Finds test payloads corresponding to a given board/release. |
| 106 | |
Chris Sosa | fec1349 | 2013-03-28 11:19:17 -0700 | [diff] [blame] | 107 | @param archive_url: Archive_url directory to find the payload. |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 108 | @param delta: if true, seek delta payloads to the given release |
| 109 | @param single: if true, expect a single match and return it, otherwise |
| 110 | None |
| 111 | |
| 112 | @return A (possibly empty) list of URIs, or a single (possibly None) URI if |
| 113 | |single| is True. |
| 114 | |
| 115 | @raise TestImageError if an error has occurred. |
| 116 | |
| 117 | """ |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 118 | if delta: |
Chris Sosa | 7287361 | 2013-09-06 14:54:55 -0700 | [diff] [blame] | 119 | pattern = '*_delta_*' |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 120 | else: |
Chris Sosa | 7287361 | 2013-09-06 14:54:55 -0700 | [diff] [blame] | 121 | pattern = '*_full_*' |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 122 | |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame] | 123 | payload_uri_list = gs_ls(pattern, archive_url, single) |
Chris Sosa | 641c929 | 2013-03-21 11:12:59 -0700 | [diff] [blame] | 124 | if not payload_uri_list: |
| 125 | return None if single else [] |
| 126 | |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame] | 127 | return payload_uri_list[0] if single else payload_uri_list |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 128 | |
| 129 | |
Chris Sosa | fec1349 | 2013-03-28 11:19:17 -0700 | [diff] [blame] | 130 | def find_image_uri(archive_url): |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 131 | """Returns a URI to a test image. |
| 132 | |
Chris Sosa | fec1349 | 2013-03-28 11:19:17 -0700 | [diff] [blame] | 133 | @param archive_url: archive_url directory to find the payload. |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 134 | |
| 135 | @return A URI to the desired image if found, None otherwise. It will most |
| 136 | likely be a file inside an image archive (image.zip), in which case |
| 137 | we'll be using ZIPFILE_BOUNDARY ('//') to denote a zip-encapsulated |
| 138 | file, for example: |
| 139 | gs://chromeos-image-archive/.../image.zip//chromiumos_test_image.bin |
| 140 | |
| 141 | @raise TestImageError if an error has occurred. |
| 142 | |
| 143 | """ |
Chris Sosa | 641c929 | 2013-03-21 11:12:59 -0700 | [diff] [blame] | 144 | image_archive = gs_ls('image.zip', archive_url, single=True) |
| 145 | if not image_archive: |
| 146 | return None |
| 147 | |
| 148 | return (image_archive[0] + ZIPFILE_BOUNDARY + 'chromiumos_test_image.bin') |