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