Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Helper class for interacting with the Dev Server.""" |
| 6 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 7 | import distutils.version |
| 8 | import errno |
| 9 | import os |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 10 | import random |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 11 | import re |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 12 | import shutil |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 13 | import time |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 14 | |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 15 | import lockfile |
| 16 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 17 | import build_artifact |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 18 | import gsutil_util |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 19 | import log_util |
| 20 | |
| 21 | |
| 22 | # Module-local log function. |
| 23 | def _Log(message, *args, **kwargs): |
| 24 | return log_util.LogWithTag('UTIL', message, *args, **kwargs) |
| 25 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 26 | |
| 27 | AU_BASE = 'au' |
| 28 | NTON_DIR_SUFFIX = '_nton' |
| 29 | MTON_DIR_SUFFIX = '_mton' |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 30 | DEV_BUILD_PREFIX = 'dev' |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 31 | UPLOADED_LIST = 'UPLOADED' |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 32 | DEVSERVER_LOCK_FILE = 'devserver' |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 33 | |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 34 | |
| 35 | def CommaSeparatedList(value_list, is_quoted=False): |
| 36 | """Concatenates a list of strings. |
| 37 | |
| 38 | This turns ['a', 'b', 'c'] into a single string 'a, b and c'. It optionally |
| 39 | adds quotes (`a') around each element. Used for logging. |
| 40 | |
| 41 | """ |
| 42 | if is_quoted: |
| 43 | value_list = ["`" + value + "'" for value in value_list] |
| 44 | |
| 45 | if len(value_list) > 1: |
| 46 | return (', '.join(value_list[:-1]) + ' and ' + value_list[-1]) |
| 47 | elif value_list: |
| 48 | return value_list[0] |
| 49 | else: |
| 50 | return '' |
| 51 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 52 | class DevServerUtilError(Exception): |
| 53 | """Exception classes used by this module.""" |
| 54 | pass |
| 55 | |
| 56 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 57 | def ParsePayloadList(archive_url, payload_list): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 58 | """Parse and return the full/delta payload URLs. |
| 59 | |
| 60 | Args: |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 61 | archive_url: The URL of the Google Storage bucket. |
| 62 | payload_list: A list filenames. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 63 | |
| 64 | Returns: |
| 65 | Tuple of 3 payloads URLs: (full, nton, mton). |
| 66 | |
| 67 | Raises: |
| 68 | DevServerUtilError: If payloads missing or invalid. |
| 69 | """ |
| 70 | full_payload_url = None |
| 71 | mton_payload_url = None |
| 72 | nton_payload_url = None |
| 73 | for payload in payload_list: |
| 74 | if '_full_' in payload: |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 75 | full_payload_url = '/'.join([archive_url, payload]) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 76 | elif '_delta_' in payload: |
| 77 | # e.g. chromeos_{from_version}_{to_version}_x86-generic_delta_dev.bin |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 78 | from_version, to_version = payload.split('_')[1:3] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 79 | if from_version == to_version: |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 80 | nton_payload_url = '/'.join([archive_url, payload]) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 81 | else: |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 82 | mton_payload_url = '/'.join([archive_url, payload]) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 83 | |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 84 | if not full_payload_url: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 85 | raise DevServerUtilError( |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 86 | 'Full payload is missing or has unexpected name format.', payload_list) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 87 | |
| 88 | return full_payload_url, nton_payload_url, mton_payload_url |
| 89 | |
| 90 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 91 | def IsAvailable(pattern_list, uploaded_list): |
| 92 | """Checks whether the target artifacts we wait for are available. |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 93 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 94 | This method searches the uploaded_list for a match for every pattern |
| 95 | in the pattern_list. It aborts and returns false if no filename |
| 96 | matches a given pattern. |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 97 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 98 | Args: |
| 99 | pattern_list: List of regular expression patterns to identify |
| 100 | the target artifacts. |
| 101 | uploaded_list: List of all uploaded files. |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 102 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 103 | Returns: |
| 104 | True if there is a match for every pattern; false otherwise. |
| 105 | """ |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 106 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 107 | # Pre-compile the regular expression patterns |
| 108 | compiled_patterns = [] |
| 109 | for p in pattern_list: |
| 110 | compiled_patterns.append(re.compile(p)) |
| 111 | |
| 112 | for pattern in compiled_patterns: |
| 113 | found = False |
| 114 | for filename in uploaded_list: |
| 115 | if re.search(pattern, filename): |
| 116 | found = True |
| 117 | break |
| 118 | if not found: |
| 119 | return False |
| 120 | |
| 121 | return True |
| 122 | |
| 123 | |
| 124 | def WaitUntilAvailable(to_wait_list, archive_url, err_str, timeout=600, |
| 125 | delay=10): |
| 126 | """Waits until all target artifacts are available in Google Storage or |
| 127 | until the request times out. |
| 128 | |
| 129 | This method polls Google Storage until all target artifacts are |
| 130 | available or until the timeout occurs. Because we may not know the |
| 131 | exact name of the target artifacts, the method accepts to_wait_list, a |
| 132 | list of filename patterns, to identify whether an artifact whose name |
| 133 | matches the pattern exists (e.g. use pattern '_full_' to search for |
| 134 | the full payload 'chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin'). |
| 135 | |
| 136 | Args: |
| 137 | to_wait_list: List of regular expression patterns to identify |
| 138 | the target artifacts. |
| 139 | archive_url: URL of the Google Storage bucket. |
| 140 | err_str: String to display in the error message. |
| 141 | |
| 142 | Returns: |
| 143 | The list of artifacts in the Google Storage bucket. |
| 144 | |
| 145 | Raises: |
| 146 | DevServerUtilError: If timeout occurs. |
| 147 | """ |
| 148 | |
| 149 | cmd = 'gsutil cat %s/%s' % (archive_url, UPLOADED_LIST) |
| 150 | msg = 'Failed to get a list of uploaded files.' |
| 151 | |
| 152 | deadline = time.time() + timeout |
| 153 | while time.time() < deadline: |
Yu-Ju Hong | 9278405 | 2012-08-22 13:08:54 -0700 | [diff] [blame] | 154 | uploaded_list = [] |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 155 | to_delay = delay + random.uniform(.5 * delay, 1.5 * delay) |
Yu-Ju Hong | 9278405 | 2012-08-22 13:08:54 -0700 | [diff] [blame] | 156 | try: |
| 157 | # Run "gsutil cat" to retrieve the list. |
| 158 | uploaded_list = gsutil_util.GSUtilRun(cmd, msg).splitlines() |
| 159 | except gsutil_util.GSUtilError: |
| 160 | # For backward compatibility, fallling back to use "gsutil ls" |
| 161 | # when the manifest file is not present. |
| 162 | cmd = 'gsutil ls %s/*' % archive_url |
| 163 | msg = 'Failed to list payloads.' |
| 164 | payload_list = gsutil_util.GSUtilRun(cmd, msg).splitlines() |
| 165 | for payload in payload_list: |
| 166 | uploaded_list.append(payload.rsplit('/', 1)[1]) |
| 167 | |
| 168 | # Check if all target artifacts are available. |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 169 | if IsAvailable(to_wait_list, uploaded_list): |
| 170 | return uploaded_list |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 171 | _Log('Retrying in %f seconds...%s' % (to_delay, err_str)) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 172 | time.sleep(to_delay) |
| 173 | |
| 174 | raise DevServerUtilError('Missing %s for %s.' % (err_str, archive_url)) |
| 175 | |
| 176 | |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 177 | def GatherArtifactDownloads(main_staging_dir, archive_url, build_dir, build, |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 178 | timeout=600, delay=10): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 179 | """Generates artifacts that we mean to download and install for autotest. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 180 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 181 | This method generates the list of artifacts we will need for autotest. These |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 182 | artifacts are instances of build_artifact.BuildArtifact. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 183 | |
| 184 | Note, these artifacts can be downloaded asynchronously iff |
| 185 | !artifact.Synchronous(). |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 186 | """ |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 187 | |
| 188 | # Wait up to 10 minutes for the full payload to be uploaded because we |
| 189 | # do not know the exact name of the full payload. |
| 190 | |
| 191 | # We also wait for 'autotest.tar' because we do not know what type of |
| 192 | # autotest tarballs (tar or tar.bz2) is available |
| 193 | # (crosbug.com/32312). This dependency can be removed once all |
| 194 | # branches move to the new 'tar' format. |
| 195 | to_wait_list = ['_full_', 'autotest.tar'] |
| 196 | err_str = 'full payload or autotest tarball' |
| 197 | uploaded_list = WaitUntilAvailable(to_wait_list, archive_url, err_str, |
| 198 | timeout=600) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 199 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 200 | # First we gather the urls/paths for the update payloads. |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 201 | full_url, nton_url, mton_url = ParsePayloadList(archive_url, uploaded_list) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 202 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 203 | full_payload = os.path.join(build_dir, build_artifact.ROOT_UPDATE) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 204 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 205 | artifacts = [] |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 206 | artifacts.append(build_artifact.BuildArtifact( |
| 207 | full_url, main_staging_dir, full_payload, synchronous=True)) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 208 | |
| 209 | if nton_url: |
| 210 | nton_payload = os.path.join(build_dir, AU_BASE, build + NTON_DIR_SUFFIX, |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 211 | build_artifact.ROOT_UPDATE) |
| 212 | artifacts.append(build_artifact.AUTestPayloadBuildArtifact( |
| 213 | nton_url, main_staging_dir, nton_payload)) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 214 | |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 215 | if mton_url: |
| 216 | mton_payload = os.path.join(build_dir, AU_BASE, build + MTON_DIR_SUFFIX, |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 217 | build_artifact.ROOT_UPDATE) |
| 218 | artifacts.append(build_artifact.AUTestPayloadBuildArtifact( |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 219 | mton_url, main_staging_dir, mton_payload)) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 220 | |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 221 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 222 | # Gather information about autotest tarballs. Use autotest.tar if available. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 223 | if build_artifact.AUTOTEST_PACKAGE in uploaded_list: |
| 224 | autotest_url = '%s/%s' % (archive_url, build_artifact.AUTOTEST_PACKAGE) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 225 | else: |
| 226 | # Use autotest.tar.bz for backward compatibility. This can be |
| 227 | # removed once all branches start using "autotest.tar" |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 228 | autotest_url = '%s/%s' % ( |
| 229 | archive_url, build_artifact.AUTOTEST_ZIPPED_PACKAGE) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 230 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 231 | # Next we gather the miscellaneous payloads. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 232 | stateful_url = archive_url + '/' + build_artifact.STATEFUL_UPDATE |
| 233 | test_suites_url = (archive_url + '/' + build_artifact.TEST_SUITES_PACKAGE) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 234 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 235 | stateful_payload = os.path.join(build_dir, build_artifact.STATEFUL_UPDATE) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 236 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 237 | artifacts.append(build_artifact.BuildArtifact( |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 238 | stateful_url, main_staging_dir, stateful_payload, synchronous=True)) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 239 | artifacts.append(build_artifact.AutotestTarballBuildArtifact( |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 240 | autotest_url, main_staging_dir, build_dir)) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 241 | artifacts.append(build_artifact.TarballBuildArtifact( |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 242 | test_suites_url, main_staging_dir, build_dir, synchronous=True)) |
| 243 | return artifacts |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 244 | |
| 245 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 246 | def GatherSymbolArtifactDownloads(temp_download_dir, archive_url, staging_dir, |
| 247 | timeout=600, delay=10): |
| 248 | """Generates debug symbol artifacts that we mean to download and stage. |
| 249 | |
| 250 | This method generates the list of artifacts we will need to |
| 251 | symbolicate crash dumps that occur during autotest runs. These |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 252 | artifacts are instances of build_artifact.BuildArtifact. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 253 | |
| 254 | This will poll google storage until the debug symbol artifact becomes |
| 255 | available, or until the 10 minute timeout is up. |
| 256 | |
| 257 | @param temp_download_dir: the tempdir into which we're downloading artifacts |
| 258 | prior to staging them. |
| 259 | @param archive_url: the google storage url of the bucket where the debug |
| 260 | symbols for the desired build are stored. |
| 261 | @param staging_dir: the dir into which to stage the symbols |
| 262 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 263 | @return an iterable of one DebugTarballBuildArtifact pointing to the right |
| 264 | debug symbols. This is an iterable so that it's similar to |
| 265 | GatherArtifactDownloads. Also, it's possible that someday we might |
| 266 | have more than one. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 267 | """ |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 268 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 269 | artifact_name = build_artifact.DEBUG_SYMBOLS |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 270 | WaitUntilAvailable([artifact_name], archive_url, 'debug symbols', |
| 271 | timeout=timeout, delay=delay) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 272 | artifact = build_artifact.DebugTarballBuildArtifact( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 273 | archive_url + '/' + artifact_name, |
| 274 | temp_download_dir, |
| 275 | staging_dir) |
| 276 | return [artifact] |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 277 | |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 278 | |
| 279 | def GatherImageArchiveArtifactDownloads(temp_download_dir, archive_url, |
| 280 | staging_dir, image_file_list, |
| 281 | timeout=600, delay=10): |
| 282 | """Generates image archive artifact(s) for downloading / staging. |
| 283 | |
| 284 | Generates the list of artifacts that are used for extracting Chrome OS images |
| 285 | from. Currently, it returns a single artifact, which is a zipfile configured |
| 286 | to extract a given list of images. It first polls Google Storage unti lthe |
| 287 | desired artifacts become available (or a timeout expires). |
| 288 | |
| 289 | Args: |
| 290 | temp_download_dir: temporary directory, used for downloading artifacts |
| 291 | archive_url: URI to the bucket where the artifacts are stored |
| 292 | staging_dir: directory into which to stage the extracted files |
| 293 | image_file_list: list of image files to be extracted |
| 294 | Returns: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 295 | list of downloadable artifacts (of type ZipfileBuildArtifact), currently |
| 296 | containing a single obejct |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 297 | """ |
| 298 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 299 | artifact_name = build_artifact.IMAGE_ARCHIVE |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 300 | WaitUntilAvailable([artifact_name], archive_url, 'image archive', |
| 301 | timeout=timeout, delay=delay) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 302 | artifact = build_artifact.ZipfileBuildArtifact( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 303 | archive_url + '/' + artifact_name, |
| 304 | temp_download_dir, staging_dir, |
| 305 | unzip_file_list=image_file_list) |
| 306 | return [artifact] |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 307 | |
| 308 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 309 | def PrepareBuildDirectory(build_dir): |
| 310 | """Preliminary staging of installation directory for build. |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 311 | |
| 312 | Args: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 313 | build_dir: Directory to install build components into. |
| 314 | """ |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 315 | if not os.path.isdir(build_dir): |
| 316 | os.path.makedirs(build_dir) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 317 | |
| 318 | # Create blank chromiumos_test_image.bin. Otherwise the Dev Server will |
| 319 | # try to rebuild it unnecessarily. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 320 | test_image = os.path.join(build_dir, build_artifact.TEST_IMAGE) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 321 | open(test_image, 'a').close() |
| 322 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 323 | |
| 324 | def SafeSandboxAccess(static_dir, path): |
| 325 | """Verify that the path is in static_dir. |
| 326 | |
| 327 | Args: |
| 328 | static_dir: Directory where builds are served from. |
| 329 | path: Path to verify. |
| 330 | |
| 331 | Returns: |
| 332 | True if path is in static_dir, False otherwise |
| 333 | """ |
| 334 | static_dir = os.path.realpath(static_dir) |
| 335 | path = os.path.realpath(path) |
| 336 | return (path.startswith(static_dir) and path != static_dir) |
| 337 | |
| 338 | |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 339 | def AcquireLock(static_dir, tag, create_once=True): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 340 | """Acquires a lock for a given tag. |
| 341 | |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 342 | Creates a directory for the specified tag, and atomically creates a lock file |
| 343 | in it. This tells other components the resource/task represented by the tag |
| 344 | is unavailable. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 345 | |
| 346 | Args: |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 347 | static_dir: Directory where builds are served from. |
| 348 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 349 | create_once: Determines whether the directory must be freshly created; this |
| 350 | preserves previous semantics of the lock acquisition. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 351 | |
| 352 | Returns: |
| 353 | Path to the created directory or None if creation failed. |
| 354 | |
| 355 | Raises: |
| 356 | DevServerUtilError: If lock can't be acquired. |
| 357 | """ |
| 358 | build_dir = os.path.join(static_dir, tag) |
| 359 | if not SafeSandboxAccess(static_dir, build_dir): |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 360 | raise DevServerUtilError('Invalid tag "%s".' % tag) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 361 | |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 362 | # Create the directory. |
| 363 | is_created = False |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 364 | try: |
| 365 | os.makedirs(build_dir) |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 366 | is_created = True |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 367 | except OSError, e: |
| 368 | if e.errno == errno.EEXIST: |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 369 | if create_once: |
| 370 | raise DevServerUtilError(str(e)) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 371 | else: |
| 372 | raise |
| 373 | |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 374 | # Lock the directory. |
| 375 | try: |
| 376 | lock = lockfile.FileLock(os.path.join(build_dir, DEVSERVER_LOCK_FILE)) |
| 377 | lock.acquire(timeout=0) |
| 378 | except lockfile.AlreadyLocked, e: |
| 379 | raise DevServerUtilError(str(e)) |
| 380 | except: |
| 381 | # In any other case, remove the directory if we actually created it, so |
| 382 | # that subsequent attempts won't fail to re-create it. |
| 383 | if is_created: |
| 384 | shutil.rmtree(build_dir) |
| 385 | raise |
| 386 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 387 | return build_dir |
| 388 | |
| 389 | |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 390 | def ReleaseLock(static_dir, tag, destroy=False): |
| 391 | """Releases the lock for a given tag. |
| 392 | |
| 393 | Optionally, removes the locked directory entirely. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 394 | |
| 395 | Args: |
| 396 | static_dir: Directory where builds are served from. |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 397 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 398 | destroy: Determines whether the locked directory should be removed |
| 399 | entirely. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 400 | |
| 401 | Raises: |
| 402 | DevServerUtilError: If lock can't be released. |
| 403 | """ |
| 404 | build_dir = os.path.join(static_dir, tag) |
| 405 | if not SafeSandboxAccess(static_dir, build_dir): |
| 406 | raise DevServerUtilError('Invaid tag "%s".' % tag) |
| 407 | |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 408 | lock = lockfile.FileLock(os.path.join(build_dir, DEVSERVER_LOCK_FILE)) |
Gilad Arnold | ea162aa | 2012-09-24 16:57:59 -0700 | [diff] [blame] | 409 | try: |
| 410 | lock.break_lock() |
| 411 | if destroy: |
| 412 | shutil.rmtree(build_dir) |
| 413 | except Exception, e: |
| 414 | raise DevServerUtilError(str(e)) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 415 | |
| 416 | |
| 417 | def FindMatchingBoards(static_dir, board): |
| 418 | """Returns a list of boards given a partial board name. |
| 419 | |
| 420 | Args: |
| 421 | static_dir: Directory where builds are served from. |
| 422 | board: Partial board name for this build; e.g. x86-generic. |
| 423 | |
| 424 | Returns: |
| 425 | Returns a list of boards given a partial board. |
| 426 | """ |
| 427 | return [brd for brd in os.listdir(static_dir) if board in brd] |
| 428 | |
| 429 | |
| 430 | def FindMatchingBuilds(static_dir, board, build): |
| 431 | """Returns a list of matching builds given a board and partial build. |
| 432 | |
| 433 | Args: |
| 434 | static_dir: Directory where builds are served from. |
| 435 | board: Partial board name for this build; e.g. x86-generic-release. |
| 436 | build: Partial build string to look for; e.g. R17-1234. |
| 437 | |
| 438 | Returns: |
| 439 | Returns a list of (board, build) tuples given a partial board and build. |
| 440 | """ |
| 441 | matches = [] |
| 442 | for brd in FindMatchingBoards(static_dir, board): |
| 443 | a = [(brd, bld) for bld in |
| 444 | os.listdir(os.path.join(static_dir, brd)) if build in bld] |
| 445 | matches.extend(a) |
| 446 | return matches |
| 447 | |
| 448 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 449 | def GetLatestBuildVersion(static_dir, target, milestone=None): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 450 | """Retrieves the latest build version for a given board. |
| 451 | |
| 452 | Args: |
| 453 | static_dir: Directory where builds are served from. |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 454 | target: The build target, typically a combination of the board and the |
| 455 | type of build e.g. x86-mario-release. |
| 456 | milestone: For latest build set to None, for builds only in a specific |
| 457 | milestone set to a str of format Rxx (e.g. R16). Default: None. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 458 | |
| 459 | Returns: |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 460 | If latest found, a full build string is returned e.g. R17-1234.0.0-a1-b983. |
| 461 | If no latest is found for some reason or another a '' string is returned. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 462 | |
| 463 | Raises: |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 464 | DevServerUtilError: If for some reason the latest build cannot be |
| 465 | deteremined, this could be due to the dir not existing or no builds |
| 466 | being present after filtering on milestone. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 467 | """ |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 468 | target_path = os.path.join(static_dir, target) |
| 469 | if not os.path.isdir(target_path): |
| 470 | raise DevServerUtilError('Cannot find path %s' % target_path) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 471 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 472 | builds = [distutils.version.LooseVersion(build) for build in |
| 473 | os.listdir(target_path)] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 474 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 475 | if milestone and builds: |
| 476 | # Check if milestone Rxx is in the string representation of the build. |
| 477 | builds = filter(lambda x: milestone.upper() in str(x), builds) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 478 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 479 | if not builds: |
| 480 | raise DevServerUtilError('Could not determine build for %s' % target) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 481 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 482 | return str(max(builds)) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 483 | |
| 484 | |
| 485 | def CloneBuild(static_dir, board, build, tag, force=False): |
| 486 | """Clone an official build into the developer sandbox. |
| 487 | |
| 488 | Developer sandbox directory must already exist. |
| 489 | |
| 490 | Args: |
| 491 | static_dir: Directory where builds are served from. |
| 492 | board: Fully qualified board name; e.g. x86-generic-release. |
| 493 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 494 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 495 | force: Force re-creation of build_dir even if it already exists. |
| 496 | |
| 497 | Returns: |
| 498 | The path to the new build. |
| 499 | """ |
| 500 | # Create the developer build directory. |
| 501 | dev_static_dir = os.path.join(static_dir, DEV_BUILD_PREFIX) |
| 502 | dev_build_dir = os.path.join(dev_static_dir, tag) |
| 503 | official_build_dir = os.path.join(static_dir, board, build) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 504 | _Log('Cloning %s -> %s' % (official_build_dir, dev_build_dir)) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 505 | dev_build_exists = False |
| 506 | try: |
| 507 | AcquireLock(dev_static_dir, tag) |
| 508 | except DevServerUtilError: |
| 509 | dev_build_exists = True |
| 510 | if force: |
| 511 | dev_build_exists = False |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 512 | ReleaseLock(dev_static_dir, tag, destroy=True) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 513 | AcquireLock(dev_static_dir, tag) |
| 514 | |
| 515 | # Make a copy of the official build, only take necessary files. |
| 516 | if not dev_build_exists: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 517 | copy_list = [build_artifact.TEST_IMAGE, |
| 518 | build_artifact.ROOT_UPDATE, |
| 519 | build_artifact.STATEFUL_UPDATE] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 520 | for f in copy_list: |
| 521 | shutil.copy(os.path.join(official_build_dir, f), dev_build_dir) |
| 522 | |
| 523 | return dev_build_dir |
| 524 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 525 | |
| 526 | def GetControlFile(static_dir, build, control_path): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 527 | """Attempts to pull the requested control file from the Dev Server. |
| 528 | |
| 529 | Args: |
| 530 | static_dir: Directory where builds are served from. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 531 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 532 | control_path: Path to control file on Dev Server relative to Autotest root. |
| 533 | |
| 534 | Raises: |
| 535 | DevServerUtilError: If lock can't be acquired. |
| 536 | |
| 537 | Returns: |
| 538 | Content of the requested control file. |
| 539 | """ |
Scott Zawalski | 1572d15 | 2012-01-16 14:36:02 -0500 | [diff] [blame] | 540 | # Be forgiving if the user passes in the control_path with a leading / |
| 541 | control_path = control_path.lstrip('/') |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 542 | control_path = os.path.join(static_dir, build, 'autotest', |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 543 | control_path) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 544 | if not SafeSandboxAccess(static_dir, control_path): |
| 545 | raise DevServerUtilError('Invaid control file "%s".' % control_path) |
| 546 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 547 | if not os.path.exists(control_path): |
| 548 | # TODO(scottz): Come up with some sort of error mechanism. |
| 549 | # crosbug.com/25040 |
| 550 | return 'Unknown control path %s' % control_path |
| 551 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 552 | with open(control_path, 'r') as control_file: |
| 553 | return control_file.read() |
| 554 | |
| 555 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 556 | def GetControlFileList(static_dir, build): |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 557 | """List all control|control. files in the specified board/build path. |
| 558 | |
| 559 | Args: |
| 560 | static_dir: Directory where builds are served from. |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 561 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 562 | |
| 563 | Raises: |
| 564 | DevServerUtilError: If path is outside of sandbox. |
| 565 | |
| 566 | Returns: |
| 567 | String of each file separated by a newline. |
| 568 | """ |
Scott Zawalski | 1572d15 | 2012-01-16 14:36:02 -0500 | [diff] [blame] | 569 | autotest_dir = os.path.join(static_dir, build, 'autotest/') |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 570 | if not SafeSandboxAccess(static_dir, autotest_dir): |
| 571 | raise DevServerUtilError('Autotest dir not in sandbox "%s".' % autotest_dir) |
| 572 | |
| 573 | control_files = set() |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 574 | if not os.path.exists(autotest_dir): |
| 575 | # TODO(scottz): Come up with some sort of error mechanism. |
| 576 | # crosbug.com/25040 |
| 577 | return 'Unknown build path %s' % autotest_dir |
| 578 | |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 579 | for entry in os.walk(autotest_dir): |
| 580 | dir_path, _, files = entry |
| 581 | for file_entry in files: |
| 582 | if file_entry.startswith('control.') or file_entry == 'control': |
| 583 | control_files.add(os.path.join(dir_path, |
Chris Sosa | ea148d9 | 2012-03-06 16:22:04 -0800 | [diff] [blame] | 584 | file_entry).replace(autotest_dir, '')) |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 585 | |
| 586 | return '\n'.join(control_files) |
| 587 | |
| 588 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 589 | def ListAutoupdateTargets(static_dir, board, build): |
| 590 | """Returns a list of autoupdate test targets for the given board, build. |
| 591 | |
| 592 | Args: |
| 593 | static_dir: Directory where builds are served from. |
| 594 | board: Fully qualified board name; e.g. x86-generic-release. |
| 595 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 596 | |
| 597 | Returns: |
| 598 | List of autoupdate test targets; e.g. ['0.14.747.0-r2bf8859c-b2927_nton'] |
| 599 | """ |
| 600 | return os.listdir(os.path.join(static_dir, board, build, AU_BASE)) |