blob: 5adf58d0de3801cf3fa6672e33a229548232a253 [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 Sosabc69dea2013-02-22 16:09:40 -08007import logging
Gilad Arnold13180012013-01-24 17:58:21 -08008import re
Gilad Arnold84eb60c2012-11-05 23:46:10 -08009
Chris Sosabc69dea2013-02-22 16:09:40 -080010import common
11from autotest_lib.client.common_lib import global_config
Chris Sosa67dbe592013-10-22 11:17:55 -070012from autotest_lib.utils import external_packages
Chris Sosa481fbbf2013-02-27 15:31:11 -080013
Chris Sosa67dbe592013-10-22 11:17:55 -070014from autotest_lib.site_utils.autoupdate import import_common
15devserver = import_common.download_and_import('devserver',
16 external_packages.DevServerRepo())
Alex Millerdc41cfe2013-09-07 22:22:17 -070017from devserver import gsutil_util
Chris Sosabc69dea2013-02-22 16:09:40 -080018
Gilad Arnold84eb60c2012-11-05 23:46:10 -080019
20# A string indicating a zip-file boundary within a URI path. This string must
21# end with a '/', in order for standard basename code to work correctly for
22# zip-encapsulated paths.
23ZIPFILE_BOUNDARY = '//'
Chris Sosab86d2662013-05-08 18:04:54 -070024ARCHIVE_URL_FORMAT = '%(archive_prefix)s/%(version)s'
Gilad Arnold84eb60c2012-11-05 23:46:10 -080025
26
27class TestImageError(BaseException):
Chris Sosabc69dea2013-02-22 16:09:40 -080028 """Raised on any error in this module."""
Gilad Arnold84eb60c2012-11-05 23:46:10 -080029 pass
30
31
Chris Sosa72873612013-09-06 14:54:55 -070032class NotSingleItem(Exception):
33 """Raised when we want a single item but got multiple."""
34
35
Chris Sosab86d2662013-05-08 18:04:54 -070036def get_default_archive_url(board, build_version):
37 """Returns the default archive_url for the given board and build_version .
Chris Sosafec13492013-03-28 11:19:17 -070038
Chris Sosab86d2662013-05-08 18:04:54 -070039 @param board: the platform/board name
40 @param build_version: the full build version i.e. R27-3823.0.0-a2.
Chris Sosafec13492013-03-28 11:19:17 -070041 """
Chris Sosabc69dea2013-02-22 16:09:40 -080042 archive_base = global_config.global_config.get_config_value(
43 'CROS', 'image_storage_server')
44 archive_base = archive_base.rstrip('/') # Remove any trailing /'s.
Chris Sosa1bf41c42013-02-27 11:34:23 -080045
46 # TODO(garnold) adjustment to -he variant board names; should be removed
47 # once we switch to using artifacts from gs://chromeos-images/
48 # (see chromium-os:38222)
Chris Sosabc69dea2013-02-22 16:09:40 -080049 board = re.sub('-he$', '_he', board)
Chris Sosab86d2662013-05-08 18:04:54 -070050 archive_prefix = archive_base + '/%s-release' % board
Chris Sosabc69dea2013-02-22 16:09:40 -080051 return ARCHIVE_URL_FORMAT % dict(
Chris Sosab86d2662013-05-08 18:04:54 -070052 archive_prefix=archive_prefix, version=build_version)
53
54
55def get_archive_url_from_prefix(archive_prefix, build_version):
56 """Returns the gs archive_url given a particular archive_prefix.
57
58 @param archive_prefix: Use the archive_prefix as the base of your URL
59
60 construction (instead of config + board-release) e.g.
61 gs://my_location/my_super_awesome_directory.
62 @param build_version: the full build version i.e. R27-3823.0.0-a2.
63 """
64 return ARCHIVE_URL_FORMAT % dict(
65 archive_prefix=archive_prefix, version=build_version)
Chris Sosabc69dea2013-02-22 16:09:40 -080066
67
Chris Sosa1bf41c42013-02-27 11:34:23 -080068def gs_ls(pattern, archive_url, single):
Gilad Arnold84eb60c2012-11-05 23:46:10 -080069 """Returns a list of URIs that match a given pattern.
70
Chris Sosa1bf41c42013-02-27 11:34:23 -080071 @param pattern: a regexp pattern to match (feeds into re.match).
72 @param archive_url: the gs uri where to search (see ARCHIVE_URL_FORMAT).
73 @param single: if true, expect a single match and return it.
Gilad Arnold84eb60c2012-11-05 23:46:10 -080074
Chris Sosa1bf41c42013-02-27 11:34:23 -080075 @return A list of URIs (possibly an empty list).
Gilad Arnold84eb60c2012-11-05 23:46:10 -080076
77 """
Chris Sosa1bf41c42013-02-27 11:34:23 -080078 try:
Ben Zhang4a2eab02016-08-19 15:09:01 -070079 logging.fatal('crbug.com/639314 Searching for pattern %s from url %s', pattern,
Chris Sosa1bf41c42013-02-27 11:34:23 -080080 archive_url)
81 uri_list = gsutil_util.GetGSNamesWithWait(
Chris Sosa72873612013-09-06 14:54:55 -070082 pattern, archive_url, err_str=__name__, timeout=1)
Ben Zhang4a2eab02016-08-19 15:09:01 -070083 logging.fatal("crbug.com/639314 uri_list={0}".format(uri_list))
Chris Sosa1bf41c42013-02-27 11:34:23 -080084 # Convert to the format our clients expect (full archive path).
Chris Sosafec13492013-03-28 11:19:17 -070085 if uri_list:
Chris Sosa72873612013-09-06 14:54:55 -070086 if not single or (single and len(uri_list) == 1):
Ben Zhang4a2eab02016-08-19 15:09:01 -070087 logging.fatal("crbug.com/639314 Normal return")
Chris Sosa72873612013-09-06 14:54:55 -070088 return ['/'.join([archive_url, u]) for u in uri_list]
89 else:
Ben Zhang4a2eab02016-08-19 15:09:01 -070090 logging.fatal("crbug.com/639314 NotSingleItem()")
Chris Sosa72873612013-09-06 14:54:55 -070091 raise NotSingleItem()
Ben Zhang4a2eab02016-08-19 15:09:01 -070092 logging.fatal("crbug.com/639314 return []")
Chris Sosafec13492013-03-28 11:19:17 -070093 return []
Chris Sosa1bf41c42013-02-27 11:34:23 -080094 except gsutil_util.PatternNotSpecific as e:
Ben Zhang4a2eab02016-08-19 15:09:01 -070095 logging.fatal("crbug.com/639314 gsutil_util.PatternNotSpecific")
Chris Sosa1bf41c42013-02-27 11:34:23 -080096 raise TestImageError(str(e))
97 except gsutil_util.GSUtilError:
Ben Zhang4a2eab02016-08-19 15:09:01 -070098 logging.fatal("crbug.com/639314 gsutil_util.GSUtilError")
Chris Sosa1bf41c42013-02-27 11:34:23 -080099 return []
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800100
101
Chris Sosafec13492013-03-28 11:19:17 -0700102def find_payload_uri(archive_url, delta=False, single=False):
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800103 """Finds test payloads corresponding to a given board/release.
104
Chris Sosafec13492013-03-28 11:19:17 -0700105 @param archive_url: Archive_url directory to find the payload.
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800106 @param delta: if true, seek delta payloads to the given release
107 @param single: if true, expect a single match and return it, otherwise
108 None
109
110 @return A (possibly empty) list of URIs, or a single (possibly None) URI if
111 |single| is True.
112
113 @raise TestImageError if an error has occurred.
114
115 """
Chris Sosabc69dea2013-02-22 16:09:40 -0800116 if delta:
Chris Sosa72873612013-09-06 14:54:55 -0700117 pattern = '*_delta_*'
Chris Sosabc69dea2013-02-22 16:09:40 -0800118 else:
Chris Sosa72873612013-09-06 14:54:55 -0700119 pattern = '*_full_*'
Chris Sosabc69dea2013-02-22 16:09:40 -0800120
Ben Zhang4a2eab02016-08-19 15:09:01 -0700121 logging.fatal("crbug.com/639314 find_payload_uri(archive_url={0}, delta={1}, single={2}):"
122 .format(archive_url, delta, single))
Chris Sosa1bf41c42013-02-27 11:34:23 -0800123 payload_uri_list = gs_ls(pattern, archive_url, single)
Ben Zhang4a2eab02016-08-19 15:09:01 -0700124 logging.fatal("crbug.com/639314 gs_ls() = {0}".format(payload_uri_list))
Chris Sosa641c9292013-03-21 11:12:59 -0700125 if not payload_uri_list:
126 return None if single else []
Ben Zhang4a2eab02016-08-19 15:09:01 -0700127 logging.fatal("crbug.com/639314 payload_uri_list is not empty")
Chris Sosa1bf41c42013-02-27 11:34:23 -0800128 return payload_uri_list[0] if single else payload_uri_list
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800129
130
Chris Sosafec13492013-03-28 11:19:17 -0700131def find_image_uri(archive_url):
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800132 """Returns a URI to a test image.
133
Chris Sosafec13492013-03-28 11:19:17 -0700134 @param archive_url: archive_url directory to find the payload.
Gilad Arnold84eb60c2012-11-05 23:46:10 -0800135
136 @return A URI to the desired image if found, None otherwise. It will most
137 likely be a file inside an image archive (image.zip), in which case
138 we'll be using ZIPFILE_BOUNDARY ('//') to denote a zip-encapsulated
139 file, for example:
140 gs://chromeos-image-archive/.../image.zip//chromiumos_test_image.bin
141
142 @raise TestImageError if an error has occurred.
143
144 """
Chris Sosa641c9292013-03-21 11:12:59 -0700145 image_archive = gs_ls('image.zip', archive_url, single=True)
146 if not image_archive:
147 return None
148
149 return (image_archive[0] + ZIPFILE_BOUNDARY + 'chromiumos_test_image.bin')