blob: e4050c9eb20ea1a245300861a2d6e0f8fbe86571 [file] [log] [blame]
Gilad Arnold84eb60c2012-11-05 23:46:10 -08001# 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 Sosadb6d3ac2013-10-10 17:59:31 -07007import imp
Chris Sosabc69dea2013-02-22 16:09:40 -08008import logging
Chris Sosa481fbbf2013-02-27 15:31:11 -08009import os
Gilad Arnold13180012013-01-24 17:58:21 -080010import re
Gilad Arnold84eb60c2012-11-05 23:46:10 -080011
Chris Sosabc69dea2013-02-22 16:09:40 -080012import common
13from autotest_lib.client.common_lib import global_config
Chris Sosa481fbbf2013-02-27 15:31:11 -080014
Alex Millerdc41cfe2013-09-07 22:22:17 -070015def 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
Chris Sosadb6d3ac2013-10-10 17:59:31 -070023
Alex Millerdc41cfe2013-09-07 22:22:17 -070024update_devserver_checkout()
Chris Sosadb6d3ac2013-10-10 17:59:31 -070025# Note this is needed to find the devserver module since we install a new
26# version in update_devserver_checkout.
27devserver = imp.load_module('devserver', *imp.find_module('devserver'))
Alex Millerdc41cfe2013-09-07 22:22:17 -070028from devserver import gsutil_util
Chris Sosabc69dea2013-02-22 16:09:40 -080029
Gilad Arnold84eb60c2012-11-05 23:46:10 -080030
31# A string indicating a zip-file boundary within a URI path. This string must
32# end with a '/', in order for standard basename code to work correctly for
33# zip-encapsulated paths.
34ZIPFILE_BOUNDARY = '//'
Chris Sosab86d2662013-05-08 18:04:54 -070035ARCHIVE_URL_FORMAT = '%(archive_prefix)s/%(version)s'
Gilad Arnold84eb60c2012-11-05 23:46:10 -080036
37
38class TestImageError(BaseException):
Chris Sosabc69dea2013-02-22 16:09:40 -080039 """Raised on any error in this module."""
Gilad Arnold84eb60c2012-11-05 23:46:10 -080040 pass
41
42
Chris Sosa72873612013-09-06 14:54:55 -070043class NotSingleItem(Exception):
44 """Raised when we want a single item but got multiple."""
45
46
Chris Sosab86d2662013-05-08 18:04:54 -070047def get_default_archive_url(board, build_version):
48 """Returns the default archive_url for the given board and build_version .
Chris Sosafec13492013-03-28 11:19:17 -070049
Chris Sosab86d2662013-05-08 18:04:54 -070050 @param board: the platform/board name
51 @param build_version: the full build version i.e. R27-3823.0.0-a2.
Chris Sosafec13492013-03-28 11:19:17 -070052 """
Chris Sosabc69dea2013-02-22 16:09:40 -080053 archive_base = global_config.global_config.get_config_value(
54 'CROS', 'image_storage_server')
55 archive_base = archive_base.rstrip('/') # Remove any trailing /'s.
Chris Sosa1bf41c42013-02-27 11:34:23 -080056
57 # TODO(garnold) adjustment to -he variant board names; should be removed
58 # once we switch to using artifacts from gs://chromeos-images/
59 # (see chromium-os:38222)
Chris Sosabc69dea2013-02-22 16:09:40 -080060 board = re.sub('-he$', '_he', board)
Chris Sosab86d2662013-05-08 18:04:54 -070061 archive_prefix = archive_base + '/%s-release' % board
Chris Sosabc69dea2013-02-22 16:09:40 -080062 return ARCHIVE_URL_FORMAT % dict(
Chris Sosab86d2662013-05-08 18:04:54 -070063 archive_prefix=archive_prefix, version=build_version)
64
65
66def get_archive_url_from_prefix(archive_prefix, build_version):
67 """Returns the gs archive_url given a particular archive_prefix.
68
69 @param archive_prefix: Use the archive_prefix as the base of your URL
70
71 construction (instead of config + board-release) e.g.
72 gs://my_location/my_super_awesome_directory.
73 @param build_version: the full build version i.e. R27-3823.0.0-a2.
74 """
75 return ARCHIVE_URL_FORMAT % dict(
76 archive_prefix=archive_prefix, version=build_version)
Chris Sosabc69dea2013-02-22 16:09:40 -080077
78
Chris Sosa1bf41c42013-02-27 11:34:23 -080079def gs_ls(pattern, archive_url, single):
Gilad Arnold84eb60c2012-11-05 23:46:10 -080080 """Returns a list of URIs that match a given pattern.
81
Chris Sosa1bf41c42013-02-27 11:34:23 -080082 @param pattern: a regexp pattern to match (feeds into re.match).
83 @param archive_url: the gs uri where to search (see ARCHIVE_URL_FORMAT).
84 @param single: if true, expect a single match and return it.
Gilad Arnold84eb60c2012-11-05 23:46:10 -080085
Chris Sosa1bf41c42013-02-27 11:34:23 -080086 @return A list of URIs (possibly an empty list).
Gilad Arnold84eb60c2012-11-05 23:46:10 -080087
88 """
Chris Sosa1bf41c42013-02-27 11:34:23 -080089 try:
90 logging.debug('Searching for pattern %s from url %s', pattern,
91 archive_url)
92 uri_list = gsutil_util.GetGSNamesWithWait(
Chris Sosa72873612013-09-06 14:54:55 -070093 pattern, archive_url, err_str=__name__, timeout=1)
Chris Sosa1bf41c42013-02-27 11:34:23 -080094 # Convert to the format our clients expect (full archive path).
Chris Sosafec13492013-03-28 11:19:17 -070095 if uri_list:
Chris Sosa72873612013-09-06 14:54:55 -070096 if not single or (single and len(uri_list) == 1):
97 return ['/'.join([archive_url, u]) for u in uri_list]
98 else:
99 raise NotSingleItem()
Chris Sosafec13492013-03-28 11:19:17 -0700100
101 return []
Chris Sosa1bf41c42013-02-27 11:34:23 -0800102 except gsutil_util.PatternNotSpecific as e:
103 raise TestImageError(str(e))
104 except gsutil_util.GSUtilError:
105 return []
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800106
107
Chris Sosafec13492013-03-28 11:19:17 -0700108def find_payload_uri(archive_url, delta=False, single=False):
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800109 """Finds test payloads corresponding to a given board/release.
110
Chris Sosafec13492013-03-28 11:19:17 -0700111 @param archive_url: Archive_url directory to find the payload.
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800112 @param delta: if true, seek delta payloads to the given release
113 @param single: if true, expect a single match and return it, otherwise
114 None
115
116 @return A (possibly empty) list of URIs, or a single (possibly None) URI if
117 |single| is True.
118
119 @raise TestImageError if an error has occurred.
120
121 """
Chris Sosabc69dea2013-02-22 16:09:40 -0800122 if delta:
Chris Sosa72873612013-09-06 14:54:55 -0700123 pattern = '*_delta_*'
Chris Sosabc69dea2013-02-22 16:09:40 -0800124 else:
Chris Sosa72873612013-09-06 14:54:55 -0700125 pattern = '*_full_*'
Chris Sosabc69dea2013-02-22 16:09:40 -0800126
Chris Sosa1bf41c42013-02-27 11:34:23 -0800127 payload_uri_list = gs_ls(pattern, archive_url, single)
Chris Sosa641c9292013-03-21 11:12:59 -0700128 if not payload_uri_list:
129 return None if single else []
130
Chris Sosa1bf41c42013-02-27 11:34:23 -0800131 return payload_uri_list[0] if single else payload_uri_list
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800132
133
Chris Sosafec13492013-03-28 11:19:17 -0700134def find_image_uri(archive_url):
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800135 """Returns a URI to a test image.
136
Chris Sosafec13492013-03-28 11:19:17 -0700137 @param archive_url: archive_url directory to find the payload.
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800138
139 @return A URI to the desired image if found, None otherwise. It will most
140 likely be a file inside an image archive (image.zip), in which case
141 we'll be using ZIPFILE_BOUNDARY ('//') to denote a zip-encapsulated
142 file, for example:
143 gs://chromeos-image-archive/.../image.zip//chromiumos_test_image.bin
144
145 @raise TestImageError if an error has occurred.
146
147 """
Chris Sosa641c9292013-03-21 11:12:59 -0700148 image_archive = gs_ls('image.zip', archive_url, single=True)
149 if not image_archive:
150 return None
151
152 return (image_archive[0] + ZIPFILE_BOUNDARY + 'chromiumos_test_image.bin')