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 | |
| 7 | import cherrypy |
| 8 | import distutils.version |
| 9 | import errno |
| 10 | import os |
| 11 | import shutil |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 12 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 13 | import downloadable_artifact |
| 14 | import gsutil_util |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 15 | |
| 16 | AU_BASE = 'au' |
| 17 | NTON_DIR_SUFFIX = '_nton' |
| 18 | MTON_DIR_SUFFIX = '_mton' |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 19 | DEV_BUILD_PREFIX = 'dev' |
| 20 | |
| 21 | |
| 22 | class DevServerUtilError(Exception): |
| 23 | """Exception classes used by this module.""" |
| 24 | pass |
| 25 | |
| 26 | |
| 27 | def ParsePayloadList(payload_list): |
| 28 | """Parse and return the full/delta payload URLs. |
| 29 | |
| 30 | Args: |
| 31 | payload_list: A list of Google Storage URLs. |
| 32 | |
| 33 | Returns: |
| 34 | Tuple of 3 payloads URLs: (full, nton, mton). |
| 35 | |
| 36 | Raises: |
| 37 | DevServerUtilError: If payloads missing or invalid. |
| 38 | """ |
| 39 | full_payload_url = None |
| 40 | mton_payload_url = None |
| 41 | nton_payload_url = None |
| 42 | for payload in payload_list: |
| 43 | if '_full_' in payload: |
| 44 | full_payload_url = payload |
| 45 | elif '_delta_' in payload: |
| 46 | # e.g. chromeos_{from_version}_{to_version}_x86-generic_delta_dev.bin |
| 47 | from_version, to_version = payload.rsplit('/', 1)[1].split('_')[1:3] |
| 48 | if from_version == to_version: |
| 49 | nton_payload_url = payload |
| 50 | else: |
| 51 | mton_payload_url = payload |
| 52 | |
Scott Zawalski | 90afd6f | 2012-04-11 07:05:36 -0700 | [diff] [blame^] | 53 | if not full_payload_url or not nton_payload_url or not mton_payload_url: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 54 | raise DevServerUtilError( |
| 55 | 'Payloads are missing or have unexpected name formats.', payload_list) |
| 56 | |
| 57 | return full_payload_url, nton_payload_url, mton_payload_url |
| 58 | |
| 59 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 60 | def GatherArtifactDownloads(main_staging_dir, archive_url, build, build_dir): |
| 61 | """Generates artifacts that we mean to download and install for autotest. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 62 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 63 | This method generates the list of artifacts we will need for autotest. These |
| 64 | artifacts are instances of downloadable_artifact.DownloadableArtifact.Note, |
| 65 | these artifacts can be downloaded asynchronously iff !artifact.Synchronous(). |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 66 | """ |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 67 | cmd = 'gsutil ls %s/*.bin' % archive_url |
| 68 | msg = 'Failed to get a list of payloads.' |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 69 | payload_list = gsutil_util.GSUtilRun(cmd, msg).splitlines() |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 70 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 71 | # First we gather the urls/paths for the update payloads. |
| 72 | full_url, nton_url, mton_url = ParsePayloadList(payload_list) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 73 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 74 | full_payload = os.path.join(build_dir, downloadable_artifact.ROOT_UPDATE) |
| 75 | nton_payload = os.path.join(build_dir, AU_BASE, build + NTON_DIR_SUFFIX, |
| 76 | downloadable_artifact.ROOT_UPDATE) |
Scott Zawalski | 90afd6f | 2012-04-11 07:05:36 -0700 | [diff] [blame^] | 77 | mton_payload = os.path.join(build_dir, AU_BASE, build + MTON_DIR_SUFFIX, |
| 78 | downloadable_artifact.ROOT_UPDATE) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 79 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 80 | artifacts = [] |
| 81 | artifacts.append(downloadable_artifact.DownloadableArtifact(full_url, |
| 82 | main_staging_dir, full_payload, synchronous=True)) |
| 83 | artifacts.append(downloadable_artifact.AUTestPayload(nton_url, |
| 84 | main_staging_dir, nton_payload)) |
Scott Zawalski | 90afd6f | 2012-04-11 07:05:36 -0700 | [diff] [blame^] | 85 | artifacts.append(downloadable_artifact.AUTestPayload(mton_url, |
| 86 | main_staging_dir, mton_payload)) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 87 | |
| 88 | # Next we gather the miscellaneous payloads. |
| 89 | stateful_url = archive_url + '/' + downloadable_artifact.STATEFUL_UPDATE |
| 90 | autotest_url = archive_url + '/' + downloadable_artifact.AUTOTEST_PACKAGE |
| 91 | test_suites_url = (archive_url + '/' + |
| 92 | downloadable_artifact.TEST_SUITES_PACKAGE) |
| 93 | |
| 94 | stateful_payload = os.path.join(build_dir, |
| 95 | downloadable_artifact.STATEFUL_UPDATE) |
| 96 | |
| 97 | artifacts.append(downloadable_artifact.DownloadableArtifact( |
| 98 | stateful_url, main_staging_dir, stateful_payload, synchronous=True)) |
| 99 | artifacts.append(downloadable_artifact.AutotestTarball( |
| 100 | autotest_url, main_staging_dir, build_dir)) |
| 101 | artifacts.append(downloadable_artifact.Tarball( |
| 102 | test_suites_url, main_staging_dir, build_dir, synchronous=True)) |
| 103 | return artifacts |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 104 | |
| 105 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 106 | def PrepareBuildDirectory(build_dir): |
| 107 | """Preliminary staging of installation directory for build. |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 108 | |
| 109 | Args: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 110 | build_dir: Directory to install build components into. |
| 111 | """ |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 112 | if not os.path.isdir(build_dir): |
| 113 | os.path.makedirs(build_dir) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 114 | |
| 115 | # Create blank chromiumos_test_image.bin. Otherwise the Dev Server will |
| 116 | # try to rebuild it unnecessarily. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 117 | test_image = os.path.join(build_dir, downloadable_artifact.TEST_IMAGE) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 118 | open(test_image, 'a').close() |
| 119 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 120 | |
| 121 | def SafeSandboxAccess(static_dir, path): |
| 122 | """Verify that the path is in static_dir. |
| 123 | |
| 124 | Args: |
| 125 | static_dir: Directory where builds are served from. |
| 126 | path: Path to verify. |
| 127 | |
| 128 | Returns: |
| 129 | True if path is in static_dir, False otherwise |
| 130 | """ |
| 131 | static_dir = os.path.realpath(static_dir) |
| 132 | path = os.path.realpath(path) |
| 133 | return (path.startswith(static_dir) and path != static_dir) |
| 134 | |
| 135 | |
| 136 | def AcquireLock(static_dir, tag): |
| 137 | """Acquires a lock for a given tag. |
| 138 | |
| 139 | Creates a directory for the specified tag, telling other |
| 140 | components the resource/task represented by the tag is unavailable. |
| 141 | |
| 142 | Args: |
| 143 | static_dir: Directory where builds are served from. |
| 144 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 145 | |
| 146 | Returns: |
| 147 | Path to the created directory or None if creation failed. |
| 148 | |
| 149 | Raises: |
| 150 | DevServerUtilError: If lock can't be acquired. |
| 151 | """ |
| 152 | build_dir = os.path.join(static_dir, tag) |
| 153 | if not SafeSandboxAccess(static_dir, build_dir): |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 154 | raise DevServerUtilError('Invalid tag "%s".' % tag) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 155 | |
| 156 | try: |
| 157 | os.makedirs(build_dir) |
| 158 | except OSError, e: |
| 159 | if e.errno == errno.EEXIST: |
| 160 | raise DevServerUtilError(str(e)) |
| 161 | else: |
| 162 | raise |
| 163 | |
| 164 | return build_dir |
| 165 | |
| 166 | |
| 167 | def ReleaseLock(static_dir, tag): |
| 168 | """Releases the lock for a given tag. Removes lock directory content. |
| 169 | |
| 170 | Args: |
| 171 | static_dir: Directory where builds are served from. |
| 172 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 173 | |
| 174 | Raises: |
| 175 | DevServerUtilError: If lock can't be released. |
| 176 | """ |
| 177 | build_dir = os.path.join(static_dir, tag) |
| 178 | if not SafeSandboxAccess(static_dir, build_dir): |
| 179 | raise DevServerUtilError('Invaid tag "%s".' % tag) |
| 180 | |
| 181 | shutil.rmtree(build_dir) |
| 182 | |
| 183 | |
| 184 | def FindMatchingBoards(static_dir, board): |
| 185 | """Returns a list of boards given a partial board name. |
| 186 | |
| 187 | Args: |
| 188 | static_dir: Directory where builds are served from. |
| 189 | board: Partial board name for this build; e.g. x86-generic. |
| 190 | |
| 191 | Returns: |
| 192 | Returns a list of boards given a partial board. |
| 193 | """ |
| 194 | return [brd for brd in os.listdir(static_dir) if board in brd] |
| 195 | |
| 196 | |
| 197 | def FindMatchingBuilds(static_dir, board, build): |
| 198 | """Returns a list of matching builds given a board and partial build. |
| 199 | |
| 200 | Args: |
| 201 | static_dir: Directory where builds are served from. |
| 202 | board: Partial board name for this build; e.g. x86-generic-release. |
| 203 | build: Partial build string to look for; e.g. R17-1234. |
| 204 | |
| 205 | Returns: |
| 206 | Returns a list of (board, build) tuples given a partial board and build. |
| 207 | """ |
| 208 | matches = [] |
| 209 | for brd in FindMatchingBoards(static_dir, board): |
| 210 | a = [(brd, bld) for bld in |
| 211 | os.listdir(os.path.join(static_dir, brd)) if build in bld] |
| 212 | matches.extend(a) |
| 213 | return matches |
| 214 | |
| 215 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 216 | def GetLatestBuildVersion(static_dir, target, milestone=None): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 217 | """Retrieves the latest build version for a given board. |
| 218 | |
| 219 | Args: |
| 220 | static_dir: Directory where builds are served from. |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 221 | target: The build target, typically a combination of the board and the |
| 222 | type of build e.g. x86-mario-release. |
| 223 | milestone: For latest build set to None, for builds only in a specific |
| 224 | 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] | 225 | |
| 226 | Returns: |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 227 | If latest found, a full build string is returned e.g. R17-1234.0.0-a1-b983. |
| 228 | 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] | 229 | |
| 230 | Raises: |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 231 | DevServerUtilError: If for some reason the latest build cannot be |
| 232 | deteremined, this could be due to the dir not existing or no builds |
| 233 | being present after filtering on milestone. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 234 | """ |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 235 | target_path = os.path.join(static_dir, target) |
| 236 | if not os.path.isdir(target_path): |
| 237 | raise DevServerUtilError('Cannot find path %s' % target_path) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 238 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 239 | builds = [distutils.version.LooseVersion(build) for build in |
| 240 | os.listdir(target_path)] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 241 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 242 | if milestone and builds: |
| 243 | # Check if milestone Rxx is in the string representation of the build. |
| 244 | builds = filter(lambda x: milestone.upper() in str(x), builds) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 245 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 246 | if not builds: |
| 247 | raise DevServerUtilError('Could not determine build for %s' % target) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 248 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 249 | return str(max(builds)) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 250 | |
| 251 | |
| 252 | def CloneBuild(static_dir, board, build, tag, force=False): |
| 253 | """Clone an official build into the developer sandbox. |
| 254 | |
| 255 | Developer sandbox directory must already exist. |
| 256 | |
| 257 | Args: |
| 258 | static_dir: Directory where builds are served from. |
| 259 | board: Fully qualified board name; e.g. x86-generic-release. |
| 260 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 261 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 262 | force: Force re-creation of build_dir even if it already exists. |
| 263 | |
| 264 | Returns: |
| 265 | The path to the new build. |
| 266 | """ |
| 267 | # Create the developer build directory. |
| 268 | dev_static_dir = os.path.join(static_dir, DEV_BUILD_PREFIX) |
| 269 | dev_build_dir = os.path.join(dev_static_dir, tag) |
| 270 | official_build_dir = os.path.join(static_dir, board, build) |
| 271 | cherrypy.log('Cloning %s -> %s' % (official_build_dir, dev_build_dir), |
| 272 | 'DEVSERVER_UTIL') |
| 273 | dev_build_exists = False |
| 274 | try: |
| 275 | AcquireLock(dev_static_dir, tag) |
| 276 | except DevServerUtilError: |
| 277 | dev_build_exists = True |
| 278 | if force: |
| 279 | dev_build_exists = False |
| 280 | ReleaseLock(dev_static_dir, tag) |
| 281 | AcquireLock(dev_static_dir, tag) |
| 282 | |
| 283 | # Make a copy of the official build, only take necessary files. |
| 284 | if not dev_build_exists: |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 285 | copy_list = [downloadable_artifact.TEST_IMAGE, |
| 286 | downloadable_artifact.ROOT_UPDATE, |
| 287 | downloadable_artifact.STATEFUL_UPDATE] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 288 | for f in copy_list: |
| 289 | shutil.copy(os.path.join(official_build_dir, f), dev_build_dir) |
| 290 | |
| 291 | return dev_build_dir |
| 292 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 293 | |
| 294 | def GetControlFile(static_dir, build, control_path): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 295 | """Attempts to pull the requested control file from the Dev Server. |
| 296 | |
| 297 | Args: |
| 298 | static_dir: Directory where builds are served from. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 299 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 300 | control_path: Path to control file on Dev Server relative to Autotest root. |
| 301 | |
| 302 | Raises: |
| 303 | DevServerUtilError: If lock can't be acquired. |
| 304 | |
| 305 | Returns: |
| 306 | Content of the requested control file. |
| 307 | """ |
Scott Zawalski | 1572d15 | 2012-01-16 14:36:02 -0500 | [diff] [blame] | 308 | # Be forgiving if the user passes in the control_path with a leading / |
| 309 | control_path = control_path.lstrip('/') |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 310 | control_path = os.path.join(static_dir, build, 'autotest', |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 311 | control_path) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 312 | if not SafeSandboxAccess(static_dir, control_path): |
| 313 | raise DevServerUtilError('Invaid control file "%s".' % control_path) |
| 314 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 315 | if not os.path.exists(control_path): |
| 316 | # TODO(scottz): Come up with some sort of error mechanism. |
| 317 | # crosbug.com/25040 |
| 318 | return 'Unknown control path %s' % control_path |
| 319 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 320 | with open(control_path, 'r') as control_file: |
| 321 | return control_file.read() |
| 322 | |
| 323 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 324 | def GetControlFileList(static_dir, build): |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 325 | """List all control|control. files in the specified board/build path. |
| 326 | |
| 327 | Args: |
| 328 | static_dir: Directory where builds are served from. |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 329 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 330 | |
| 331 | Raises: |
| 332 | DevServerUtilError: If path is outside of sandbox. |
| 333 | |
| 334 | Returns: |
| 335 | String of each file separated by a newline. |
| 336 | """ |
Scott Zawalski | 1572d15 | 2012-01-16 14:36:02 -0500 | [diff] [blame] | 337 | autotest_dir = os.path.join(static_dir, build, 'autotest/') |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 338 | if not SafeSandboxAccess(static_dir, autotest_dir): |
| 339 | raise DevServerUtilError('Autotest dir not in sandbox "%s".' % autotest_dir) |
| 340 | |
| 341 | control_files = set() |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 342 | if not os.path.exists(autotest_dir): |
| 343 | # TODO(scottz): Come up with some sort of error mechanism. |
| 344 | # crosbug.com/25040 |
| 345 | return 'Unknown build path %s' % autotest_dir |
| 346 | |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 347 | for entry in os.walk(autotest_dir): |
| 348 | dir_path, _, files = entry |
| 349 | for file_entry in files: |
| 350 | if file_entry.startswith('control.') or file_entry == 'control': |
| 351 | control_files.add(os.path.join(dir_path, |
Chris Sosa | ea148d9 | 2012-03-06 16:22:04 -0800 | [diff] [blame] | 352 | file_entry).replace(autotest_dir, '')) |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 353 | |
| 354 | return '\n'.join(control_files) |
| 355 | |
| 356 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 357 | def ListAutoupdateTargets(static_dir, board, build): |
| 358 | """Returns a list of autoupdate test targets for the given board, build. |
| 359 | |
| 360 | Args: |
| 361 | static_dir: Directory where builds are served from. |
| 362 | board: Fully qualified board name; e.g. x86-generic-release. |
| 363 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 364 | |
| 365 | Returns: |
| 366 | List of autoupdate test targets; e.g. ['0.14.747.0-r2bf8859c-b2927_nton'] |
| 367 | """ |
| 368 | return os.listdir(os.path.join(static_dir, board, build, AU_BASE)) |