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 |
Gilad Arnold | 1318001 | 2013-01-24 17:58:21 -0800 | [diff] [blame] | 8 | import re |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 9 | |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 10 | import common |
| 11 | from autotest_lib.client.common_lib import global_config |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame^] | 12 | from devserver import gsutil_util |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 13 | |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 14 | |
| 15 | # A string indicating a zip-file boundary within a URI path. This string must |
| 16 | # end with a '/', in order for standard basename code to work correctly for |
| 17 | # zip-encapsulated paths. |
| 18 | ZIPFILE_BOUNDARY = '//' |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 19 | ARCHIVE_URL_FORMAT = '%(archive_base)s/%(board)s-release/%(branch)s-%(release)s' |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 20 | |
| 21 | |
| 22 | class TestImageError(BaseException): |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 23 | """Raised on any error in this module.""" |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 24 | pass |
| 25 | |
| 26 | |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 27 | def _get_archive_url(board, branch, release): |
| 28 | """Returns the gs archive_url for the respective arguments.""" |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 29 | archive_base = global_config.global_config.get_config_value( |
| 30 | 'CROS', 'image_storage_server') |
| 31 | archive_base = archive_base.rstrip('/') # Remove any trailing /'s. |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame^] | 32 | |
| 33 | # TODO(garnold) adjustment to -he variant board names; should be removed |
| 34 | # once we switch to using artifacts from gs://chromeos-images/ |
| 35 | # (see chromium-os:38222) |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 36 | board = re.sub('-he$', '_he', board) |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame^] | 37 | |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 38 | return ARCHIVE_URL_FORMAT % dict( |
| 39 | archive_base=archive_base, board=board, branch=branch, |
| 40 | release=release) |
| 41 | |
| 42 | |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame^] | 43 | def gs_ls(pattern, archive_url, single): |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 44 | """Returns a list of URIs that match a given pattern. |
| 45 | |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame^] | 46 | @param pattern: a regexp pattern to match (feeds into re.match). |
| 47 | @param archive_url: the gs uri where to search (see ARCHIVE_URL_FORMAT). |
| 48 | @param single: if true, expect a single match and return it. |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 49 | |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame^] | 50 | @return A list of URIs (possibly an empty list). |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 51 | |
| 52 | """ |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame^] | 53 | try: |
| 54 | logging.debug('Searching for pattern %s from url %s', pattern, |
| 55 | archive_url) |
| 56 | uri_list = gsutil_util.GetGSNamesWithWait( |
| 57 | pattern, archive_url, err_str=__name__, single_item=single, |
| 58 | timeout=1) |
| 59 | # Convert to the format our clients expect (full archive path). |
| 60 | return ['/'.join([archive_url, u]) for u in uri_list] |
| 61 | except gsutil_util.PatternNotSpecific as e: |
| 62 | raise TestImageError(str(e)) |
| 63 | except gsutil_util.GSUtilError: |
| 64 | return [] |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def find_payload_uri(board, release, branch, delta=False, |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 68 | single=False, archive_url=None): |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 69 | """Finds test payloads corresponding to a given board/release. |
| 70 | |
| 71 | @param board: the platform name (string) |
| 72 | @param release: the release version (string), without milestone and |
| 73 | attempt/build counters |
| 74 | @param branch: the release's branch name |
| 75 | @param delta: if true, seek delta payloads to the given release |
| 76 | @param single: if true, expect a single match and return it, otherwise |
| 77 | None |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 78 | @param archive_url: Optional archive_url directory to find the payload. |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 79 | |
| 80 | @return A (possibly empty) list of URIs, or a single (possibly None) URI if |
| 81 | |single| is True. |
| 82 | |
| 83 | @raise TestImageError if an error has occurred. |
| 84 | |
| 85 | """ |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 86 | if not archive_url: |
| 87 | archive_url = _get_archive_url(board, branch, release) |
Gilad Arnold | 1318001 | 2013-01-24 17:58:21 -0800 | [diff] [blame] | 88 | |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 89 | if delta: |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame^] | 90 | pattern = '.*_delta_.*' |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 91 | else: |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame^] | 92 | pattern = '.*_full_.*' |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 93 | |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame^] | 94 | payload_uri_list = gs_ls(pattern, archive_url, single) |
| 95 | return payload_uri_list[0] if single else payload_uri_list |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 96 | |
| 97 | |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 98 | def find_image_uri(board, release, branch, archive_url=None): |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 99 | """Returns a URI to a test image. |
| 100 | |
| 101 | @param board: the platform name (string) |
| 102 | @param release: the release version (string), without milestone and |
| 103 | attempt/build counters |
| 104 | @param branch: the release's branch name |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 105 | @param archive_url: Optional archive_url directory to find the payload. |
Gilad Arnold | 84eb60c | 2012-11-05 23:46:10 -0800 | [diff] [blame] | 106 | |
| 107 | @return A URI to the desired image if found, None otherwise. It will most |
| 108 | likely be a file inside an image archive (image.zip), in which case |
| 109 | we'll be using ZIPFILE_BOUNDARY ('//') to denote a zip-encapsulated |
| 110 | file, for example: |
| 111 | gs://chromeos-image-archive/.../image.zip//chromiumos_test_image.bin |
| 112 | |
| 113 | @raise TestImageError if an error has occurred. |
| 114 | |
| 115 | """ |
Chris Sosa | bc69dea | 2013-02-22 16:09:40 -0800 | [diff] [blame] | 116 | if not archive_url: |
| 117 | archive_url = _get_archive_url(board, branch, release) |
Gilad Arnold | 1318001 | 2013-01-24 17:58:21 -0800 | [diff] [blame] | 118 | |
Chris Sosa | 1bf41c4 | 2013-02-27 11:34:23 -0800 | [diff] [blame^] | 119 | image_archive = gs_ls('image.zip', archive_url, single=True)[0] |
| 120 | return (image_archive + ZIPFILE_BOUNDARY + 'chromiumos_test_image.bin') |