joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 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 | |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 5 | """Main module for parsing and interpreting XBuddy paths for the devserver.""" |
| 6 | |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Yiming Chen | aab488e | 2014-11-17 14:49:31 -0800 | [diff] [blame] | 9 | import cherrypy |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 10 | import ConfigParser |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 11 | import datetime |
| 12 | import operator |
| 13 | import os |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 14 | import re |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 15 | import shutil |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 16 | import time |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 17 | import threading |
| 18 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 19 | import build_util |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 20 | import artifact_info |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 21 | import common_util |
| 22 | import devserver_constants |
| 23 | import downloader |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 24 | import gsutil_util |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 25 | import log_util |
| 26 | |
| 27 | # Module-local log function. |
| 28 | def _Log(message, *args): |
| 29 | return log_util.LogWithTag('XBUDDY', message, *args) |
| 30 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 31 | # xBuddy config constants |
| 32 | CONFIG_FILE = 'xbuddy_config.ini' |
| 33 | SHADOW_CONFIG_FILE = 'shadow_xbuddy_config.ini' |
| 34 | PATH_REWRITES = 'PATH_REWRITES' |
| 35 | GENERAL = 'GENERAL' |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 36 | LOCATION_SUFFIXES = 'LOCATION_SUFFIXES' |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 37 | |
Chris Sosa | c2abc72 | 2013-08-26 17:11:22 -0700 | [diff] [blame] | 38 | # Path for shadow config in chroot. |
| 39 | CHROOT_SHADOW_DIR = '/mnt/host/source/src/platform/dev' |
| 40 | |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 41 | # XBuddy aliases |
| 42 | TEST = 'test' |
| 43 | BASE = 'base' |
| 44 | DEV = 'dev' |
| 45 | FULL = 'full_payload' |
| 46 | RECOVERY = 'recovery' |
| 47 | STATEFUL = 'stateful' |
| 48 | AUTOTEST = 'autotest' |
| 49 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 50 | # Local build constants |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 51 | ANY = "ANY" |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 52 | LATEST = "latest" |
| 53 | LOCAL = "local" |
| 54 | REMOTE = "remote" |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 55 | |
| 56 | # TODO(sosa): Fix a lot of assumptions about these aliases. There is too much |
| 57 | # implicit logic here that's unnecessary. What should be done: |
| 58 | # 1) Collapse Alias logic to one set of aliases for xbuddy (not local/remote). |
| 59 | # 2) Do not use zip when creating these dicts. Better to not rely on ordering. |
| 60 | # 3) Move alias/artifact mapping to a central module rather than having it here. |
| 61 | # 4) Be explicit when things are missing i.e. no dev images in image.zip. |
| 62 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 63 | LOCAL_ALIASES = [ |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 64 | TEST, |
| 65 | DEV, |
| 66 | BASE, |
| 67 | RECOVERY, |
| 68 | FULL, |
| 69 | STATEFUL, |
| 70 | ANY, |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 71 | ] |
| 72 | |
| 73 | LOCAL_FILE_NAMES = [ |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 74 | devserver_constants.TEST_IMAGE_FILE, |
| 75 | devserver_constants.IMAGE_FILE, |
| 76 | devserver_constants.BASE_IMAGE_FILE, |
| 77 | devserver_constants.RECOVERY_IMAGE_FILE, |
| 78 | devserver_constants.UPDATE_FILE, |
| 79 | devserver_constants.STATEFUL_FILE, |
| 80 | None, # For ANY. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 81 | ] |
| 82 | |
| 83 | LOCAL_ALIAS_TO_FILENAME = dict(zip(LOCAL_ALIASES, LOCAL_FILE_NAMES)) |
| 84 | |
| 85 | # Google Storage constants |
| 86 | GS_ALIASES = [ |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 87 | TEST, |
| 88 | BASE, |
| 89 | RECOVERY, |
| 90 | FULL, |
| 91 | STATEFUL, |
| 92 | AUTOTEST, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 93 | ] |
| 94 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 95 | GS_FILE_NAMES = [ |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 96 | devserver_constants.TEST_IMAGE_FILE, |
| 97 | devserver_constants.BASE_IMAGE_FILE, |
| 98 | devserver_constants.RECOVERY_IMAGE_FILE, |
| 99 | devserver_constants.UPDATE_FILE, |
| 100 | devserver_constants.STATEFUL_FILE, |
| 101 | devserver_constants.AUTOTEST_DIR, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 102 | ] |
| 103 | |
| 104 | ARTIFACTS = [ |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 105 | artifact_info.TEST_IMAGE, |
| 106 | artifact_info.BASE_IMAGE, |
| 107 | artifact_info.RECOVERY_IMAGE, |
| 108 | artifact_info.FULL_PAYLOAD, |
| 109 | artifact_info.STATEFUL_PAYLOAD, |
| 110 | artifact_info.AUTOTEST, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 111 | ] |
| 112 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 113 | GS_ALIAS_TO_FILENAME = dict(zip(GS_ALIASES, GS_FILE_NAMES)) |
| 114 | GS_ALIAS_TO_ARTIFACT = dict(zip(GS_ALIASES, ARTIFACTS)) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 115 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 116 | LATEST_OFFICIAL = "latest-official" |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 117 | |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 118 | RELEASE = "-release" |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 119 | |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 120 | |
| 121 | class XBuddyException(Exception): |
| 122 | """Exception classes used by this module.""" |
| 123 | pass |
| 124 | |
| 125 | |
| 126 | # no __init__ method |
| 127 | #pylint: disable=W0232 |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 128 | class Timestamp(object): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 129 | """Class to translate build path strings and timestamp filenames.""" |
| 130 | |
| 131 | _TIMESTAMP_DELIMITER = 'SLASH' |
| 132 | XBUDDY_TIMESTAMP_DIR = 'xbuddy_UpdateTimestamps' |
| 133 | |
| 134 | @staticmethod |
| 135 | def TimestampToBuild(timestamp_filename): |
| 136 | return timestamp_filename.replace(Timestamp._TIMESTAMP_DELIMITER, '/') |
| 137 | |
| 138 | @staticmethod |
| 139 | def BuildToTimestamp(build_path): |
| 140 | return build_path.replace('/', Timestamp._TIMESTAMP_DELIMITER) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 141 | |
| 142 | @staticmethod |
| 143 | def UpdateTimestamp(timestamp_dir, build_id): |
| 144 | """Update timestamp file of build with build_id.""" |
| 145 | common_util.MkDirP(timestamp_dir) |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 146 | _Log("Updating timestamp for %s", build_id) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 147 | time_file = os.path.join(timestamp_dir, |
| 148 | Timestamp.BuildToTimestamp(build_id)) |
| 149 | with file(time_file, 'a'): |
| 150 | os.utime(time_file, None) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 151 | #pylint: enable=W0232 |
| 152 | |
| 153 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 154 | class XBuddy(build_util.BuildObject): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 155 | """Class that manages image retrieval and caching by the devserver. |
| 156 | |
| 157 | Image retrieval by xBuddy path: |
| 158 | XBuddy accesses images and artifacts that it stores using an xBuddy |
| 159 | path of the form: board/version/alias |
| 160 | The primary xbuddy.Get call retrieves the correct artifact or url to where |
| 161 | the artifacts can be found. |
| 162 | |
| 163 | Image caching: |
| 164 | Images and other artifacts are stored identically to how they would have |
| 165 | been if devserver's stage rpc was called and the xBuddy cache replaces |
| 166 | build versions on a LRU basis. Timestamps are maintained by last accessed |
| 167 | times of representative files in the a directory in the static serve |
| 168 | directory (XBUDDY_TIMESTAMP_DIR). |
| 169 | |
| 170 | Private class members: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 171 | _true_values: used for interpreting boolean values |
| 172 | _staging_thread_count: track download requests |
| 173 | _timestamp_folder: directory with empty files standing in as timestamps |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 174 | for each image currently cached by xBuddy |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 175 | """ |
| 176 | _true_values = ['true', 't', 'yes', 'y'] |
| 177 | |
| 178 | # Number of threads that are staging images. |
| 179 | _staging_thread_count = 0 |
| 180 | # Lock used to lock increasing/decreasing count. |
| 181 | _staging_thread_count_lock = threading.Lock() |
| 182 | |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 183 | def __init__(self, manage_builds=False, board=None, version=None, |
| 184 | images_dir=None, log_screen=True, **kwargs): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 185 | super(XBuddy, self).__init__(**kwargs) |
joychen | b0dfe55 | 2013-07-30 10:02:06 -0700 | [diff] [blame] | 186 | |
Yiming Chen | aab488e | 2014-11-17 14:49:31 -0800 | [diff] [blame] | 187 | if not log_screen: |
| 188 | cherrypy.config.update({'log.screen': False}) |
| 189 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 190 | self.config = self._ReadConfig() |
| 191 | self._manage_builds = manage_builds or self._ManageBuilds() |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 192 | self._board = board |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 193 | self._version = version |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 194 | self._timestamp_folder = os.path.join(self.static_dir, |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 195 | Timestamp.XBUDDY_TIMESTAMP_DIR) |
Chris Sosa | 7cd2320 | 2013-10-15 17:22:57 -0700 | [diff] [blame] | 196 | if images_dir: |
| 197 | self.images_dir = images_dir |
| 198 | else: |
| 199 | self.images_dir = os.path.join(self.GetSourceRoot(), 'src/build/images') |
| 200 | |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 201 | common_util.MkDirP(self._timestamp_folder) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 202 | |
| 203 | @classmethod |
| 204 | def ParseBoolean(cls, boolean_string): |
| 205 | """Evaluate a string to a boolean value""" |
| 206 | if boolean_string: |
| 207 | return boolean_string.lower() in cls._true_values |
| 208 | else: |
| 209 | return False |
| 210 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 211 | def _ReadConfig(self): |
| 212 | """Read xbuddy config from ini files. |
| 213 | |
| 214 | Reads the base config from xbuddy_config.ini, and then merges in the |
| 215 | shadow config from shadow_xbuddy_config.ini |
| 216 | |
| 217 | Returns: |
| 218 | The merged configuration. |
Yu-Ju Hong | c54658c | 2014-01-22 09:18:07 -0800 | [diff] [blame] | 219 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 220 | Raises: |
| 221 | XBuddyException if the config file is missing. |
| 222 | """ |
| 223 | xbuddy_config = ConfigParser.ConfigParser() |
| 224 | config_file = os.path.join(self.devserver_dir, CONFIG_FILE) |
| 225 | if os.path.exists(config_file): |
| 226 | xbuddy_config.read(config_file) |
| 227 | else: |
Yiming Chen | d920214 | 2014-11-07 14:56:52 -0800 | [diff] [blame] | 228 | # Get the directory of xbuddy.py file. |
| 229 | file_dir = os.path.dirname(os.path.realpath(__file__)) |
| 230 | # Read the default xbuddy_config.ini from the directory. |
| 231 | xbuddy_config.read(os.path.join(file_dir, CONFIG_FILE)) |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 232 | |
| 233 | # Read the shadow file if there is one. |
Chris Sosa | c2abc72 | 2013-08-26 17:11:22 -0700 | [diff] [blame] | 234 | if os.path.isdir(CHROOT_SHADOW_DIR): |
| 235 | shadow_config_file = os.path.join(CHROOT_SHADOW_DIR, SHADOW_CONFIG_FILE) |
| 236 | else: |
| 237 | shadow_config_file = os.path.join(self.devserver_dir, SHADOW_CONFIG_FILE) |
| 238 | |
| 239 | _Log('Using shadow config file stored at %s', shadow_config_file) |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 240 | if os.path.exists(shadow_config_file): |
| 241 | shadow_xbuddy_config = ConfigParser.ConfigParser() |
| 242 | shadow_xbuddy_config.read(shadow_config_file) |
| 243 | |
| 244 | # Merge shadow config in. |
| 245 | sections = shadow_xbuddy_config.sections() |
| 246 | for s in sections: |
| 247 | if not xbuddy_config.has_section(s): |
| 248 | xbuddy_config.add_section(s) |
| 249 | options = shadow_xbuddy_config.options(s) |
| 250 | for o in options: |
| 251 | val = shadow_xbuddy_config.get(s, o) |
| 252 | xbuddy_config.set(s, o, val) |
| 253 | |
| 254 | return xbuddy_config |
| 255 | |
| 256 | def _ManageBuilds(self): |
| 257 | """Checks if xBuddy is managing local builds using the current config.""" |
| 258 | try: |
| 259 | return self.ParseBoolean(self.config.get(GENERAL, 'manage_builds')) |
| 260 | except ConfigParser.Error: |
| 261 | return False |
| 262 | |
| 263 | def _Capacity(self): |
| 264 | """Gets the xbuddy capacity from the current config.""" |
| 265 | try: |
| 266 | return int(self.config.get(GENERAL, 'capacity')) |
| 267 | except ConfigParser.Error: |
| 268 | return 5 |
| 269 | |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 270 | def _LookupAlias(self, alias, board, version): |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 271 | """Given the full xbuddy config, look up an alias for path rewrite. |
| 272 | |
| 273 | Args: |
| 274 | alias: The xbuddy path that could be one of the aliases in the |
| 275 | rewrite table. |
| 276 | board: The board to fill in with when paths are rewritten. Can be from |
| 277 | the update request xml or the default board from devserver. |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 278 | version: The version to fill in when rewriting paths. Could be a specific |
| 279 | version number or a version alias like LATEST. |
Yu-Ju Hong | c54658c | 2014-01-22 09:18:07 -0800 | [diff] [blame] | 280 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 281 | Returns: |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 282 | A pair (val, suffix) where val is the rewritten path, or the original |
| 283 | string if no rewrite was found; and suffix is the assigned location |
| 284 | suffix, or the default suffix if none was found. |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 285 | """ |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 286 | try: |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 287 | suffix = self.config.get(LOCATION_SUFFIXES, alias) |
| 288 | except ConfigParser.Error: |
| 289 | suffix = RELEASE |
| 290 | |
| 291 | try: |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 292 | val = self.config.get(PATH_REWRITES, alias) |
| 293 | except ConfigParser.Error: |
| 294 | # No alias lookup found. Return original path. |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 295 | val = None |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 296 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 297 | if not (val and val.strip()): |
| 298 | val = alias |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 299 | else: |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 300 | # The found value is not an empty string. |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 301 | # Fill in the board and version. |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 302 | val = val.replace("BOARD", "%(board)s") |
| 303 | val = val.replace("VERSION", "%(version)s") |
| 304 | val = val % {'board': board, 'version': version} |
| 305 | |
| 306 | _Log("Path is %s, location suffix is %s", val, suffix) |
| 307 | return val, suffix |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 308 | |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 309 | @staticmethod |
| 310 | def _ResolveImageDir(image_dir): |
| 311 | """Clean up and return the image dir to use. |
| 312 | |
| 313 | Args: |
| 314 | image_dir: directory in Google Storage to use. |
| 315 | |
| 316 | Returns: |
| 317 | |image_dir| if |image_dir| is not None. Otherwise, returns |
| 318 | devserver_constants.GS_IMAGE_DIR |
| 319 | """ |
| 320 | image_dir = image_dir or devserver_constants.GS_IMAGE_DIR |
| 321 | # Remove trailing slashes. |
| 322 | return image_dir.rstrip('/') |
| 323 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 324 | def _LookupOfficial(self, board, suffix, image_dir=None): |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 325 | """Check LATEST-master for the version number of interest.""" |
| 326 | _Log("Checking gs for latest %s-%s image", board, suffix) |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 327 | image_dir = XBuddy._ResolveImageDir(image_dir) |
| 328 | latest_addr = (devserver_constants.GS_LATEST_MASTER % |
| 329 | {'image_dir': image_dir, |
| 330 | 'board': board, |
| 331 | 'suffix': suffix}) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 332 | cmd = 'gsutil cat %s' % latest_addr |
| 333 | msg = 'Failed to find build at %s' % latest_addr |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 334 | # Full release + version is in the LATEST file. |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 335 | version = gsutil_util.GSUtilRun(cmd, msg) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 336 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 337 | return devserver_constants.IMAGE_DIR % {'board':board, |
| 338 | 'suffix':suffix, |
| 339 | 'version':version} |
Gilad Arnold | 869e8ab | 2015-02-19 23:34:49 -0800 | [diff] [blame] | 340 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 341 | def _LookupChannel(self, board, suffix, channel='stable', |
| 342 | image_dir=None): |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 343 | """Check the channel folder for the version number of interest.""" |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 344 | # Get all names in channel dir. Get 10 highest directories by version. |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 345 | _Log("Checking channel '%s' for latest '%s' image", channel, board) |
Yu-Ju Hong | c54658c | 2014-01-22 09:18:07 -0800 | [diff] [blame] | 346 | # Due to historical reasons, gs://chromeos-releases uses |
| 347 | # daisy-spring as opposed to the board name daisy_spring. Convert |
| 348 | # the board name for the lookup. |
| 349 | channel_dir = devserver_constants.GS_CHANNEL_DIR % { |
| 350 | 'channel':channel, |
| 351 | 'board':re.sub('_', '-', board)} |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 352 | latest_version = gsutil_util.GetLatestVersionFromGSDir( |
| 353 | channel_dir, with_release=False) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 354 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 355 | # Figure out release number from the version number. |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 356 | image_url = devserver_constants.IMAGE_DIR % { |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 357 | 'board': board, |
| 358 | 'suffix': suffix, |
| 359 | 'version': 'R*' + latest_version} |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 360 | image_dir = XBuddy._ResolveImageDir(image_dir) |
| 361 | gs_url = os.path.join(image_dir, image_url) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 362 | |
| 363 | # There should only be one match on cros-image-archive. |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 364 | full_version = gsutil_util.GetLatestVersionFromGSDir(gs_url) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 365 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 366 | return devserver_constants.IMAGE_DIR % {'board': board, |
| 367 | 'suffix': suffix, |
| 368 | 'version': full_version} |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 369 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 370 | def _LookupVersion(self, board, suffix, version): |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 371 | """Search GS image releases for the highest match to a version prefix.""" |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 372 | # Build the pattern for GS to match. |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 373 | _Log("Checking gs for latest '%s' image with prefix '%s'", board, version) |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 374 | image_url = devserver_constants.IMAGE_DIR % {'board': board, |
| 375 | 'suffix': suffix, |
| 376 | 'version': version + '*'} |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 377 | image_dir = os.path.join(devserver_constants.GS_IMAGE_DIR, image_url) |
| 378 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 379 | # Grab the newest version of the ones matched. |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 380 | full_version = gsutil_util.GetLatestVersionFromGSDir(image_dir) |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 381 | return devserver_constants.IMAGE_DIR % {'board': board, |
| 382 | 'suffix': suffix, |
| 383 | 'version': full_version} |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 384 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 385 | def _RemoteBuildId(self, board, suffix, version): |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 386 | """Returns the remote build_id for the given board and version. |
| 387 | |
| 388 | Raises: |
| 389 | XBuddyException: If we failed to resolve the version to a valid build_id. |
| 390 | """ |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 391 | build_id_as_is = devserver_constants.IMAGE_DIR % {'board': board, |
| 392 | 'suffix': '', |
| 393 | 'version': version} |
| 394 | build_id_suffix = devserver_constants.IMAGE_DIR % {'board': board, |
| 395 | 'suffix': suffix, |
| 396 | 'version': version} |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 397 | # Return the first path that exists. We assume that what the user typed |
| 398 | # is better than with a default suffix added i.e. x86-generic/blah is |
| 399 | # more valuable than x86-generic-release/blah. |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 400 | for build_id in build_id_as_is, build_id_suffix: |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 401 | cmd = 'gsutil ls %s/%s' % (devserver_constants.GS_IMAGE_DIR, build_id) |
| 402 | try: |
| 403 | version = gsutil_util.GSUtilRun(cmd, None) |
| 404 | return build_id |
| 405 | except gsutil_util.GSUtilError: |
| 406 | continue |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 407 | |
| 408 | raise XBuddyException('Could not find remote build_id for %s %s' % ( |
| 409 | board, version)) |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 410 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 411 | def _ResolveBuildVersion(self, board, suffix, base_version): |
Gilad Arnold | 869e8ab | 2015-02-19 23:34:49 -0800 | [diff] [blame] | 412 | """Check LATEST-<base_version> and returns a full build version.""" |
| 413 | _Log('Checking gs for full version for %s of %s', base_version, board) |
| 414 | # TODO(garnold) We might want to accommodate version prefixes and pick the |
| 415 | # most recent found, as done in _LookupVersion(). |
| 416 | latest_addr = (devserver_constants.GS_LATEST_BASE_VERSION % |
| 417 | {'image_dir': devserver_constants.GS_IMAGE_DIR, |
| 418 | 'board': board, |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 419 | 'suffix': suffix, |
Gilad Arnold | 869e8ab | 2015-02-19 23:34:49 -0800 | [diff] [blame] | 420 | 'base_version': base_version}) |
| 421 | cmd = 'gsutil cat %s' % latest_addr |
| 422 | msg = 'Failed to find build at %s' % latest_addr |
| 423 | # Full release + version is in the LATEST file. |
| 424 | return gsutil_util.GSUtilRun(cmd, msg) |
| 425 | |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 426 | def _ResolveVersionToBuildId(self, board, suffix, version, image_dir=None): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 427 | """Handle version aliases for remote payloads in GS. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 428 | |
| 429 | Args: |
| 430 | board: as specified in the original call. (i.e. x86-generic, parrot) |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 431 | suffix: The location suffix, to be added to board name. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 432 | version: as entered in the original call. can be |
| 433 | {TBD, 0. some custom alias as defined in a config file} |
Gilad Arnold | 869e8ab | 2015-02-19 23:34:49 -0800 | [diff] [blame] | 434 | 1. fully qualified build version or base version. |
| 435 | 2. latest |
| 436 | 3. latest-{channel} |
| 437 | 4. latest-official-{board suffix} |
| 438 | 5. version prefix (i.e. RX-Y.X, RX-Y, RX) |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 439 | image_dir: image directory to check in Google Storage. If none, |
| 440 | the default bucket is used. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 441 | |
| 442 | Returns: |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 443 | Location where the image dir is actually found on GS (build_id) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 444 | |
Chris Sosa | ea734d9 | 2013-10-11 11:28:58 -0700 | [diff] [blame] | 445 | Raises: |
| 446 | XBuddyException: If we failed to resolve the version to a valid url. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 447 | """ |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 448 | # Only the last segment of the alias is variable relative to the rest. |
| 449 | version_tuple = version.rsplit('-', 1) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 450 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 451 | if re.match(devserver_constants.VERSION_RE, version): |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 452 | return self._RemoteBuildId(board, suffix, version) |
Gilad Arnold | 869e8ab | 2015-02-19 23:34:49 -0800 | [diff] [blame] | 453 | elif re.match(devserver_constants.VERSION, version): |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 454 | return self._RemoteBuildId( |
| 455 | board, suffix, self._ResolveBuildVersion(board, suffix, version)) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 456 | elif version == LATEST_OFFICIAL: |
| 457 | # latest-official --> LATEST build in board-release |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 458 | return self._LookupOfficial(board, suffix, image_dir=image_dir) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 459 | elif version_tuple[0] == LATEST_OFFICIAL: |
| 460 | # latest-official-{suffix} --> LATEST build in board-{suffix} |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 461 | return self._LookupOfficial(board, version_tuple[1], |
| 462 | image_dir=image_dir) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 463 | elif version == LATEST: |
| 464 | # latest --> latest build on stable channel |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 465 | return self._LookupChannel(board, suffix, image_dir=image_dir) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 466 | elif version_tuple[0] == LATEST: |
| 467 | if re.match(devserver_constants.VERSION_RE, version_tuple[1]): |
| 468 | # latest-R* --> most recent qualifying build |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 469 | return self._LookupVersion(board, suffix, version_tuple[1]) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 470 | else: |
| 471 | # latest-{channel} --> latest build within that channel |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 472 | return self._LookupChannel(board, suffix, channel=version_tuple[1], |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 473 | image_dir=image_dir) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 474 | else: |
| 475 | # The given version doesn't match any known patterns. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 476 | raise XBuddyException("Version %s unknown. Can't find on GS." % version) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 477 | |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 478 | @staticmethod |
| 479 | def _Symlink(link, target): |
| 480 | """Symlinks link to target, and removes whatever link was there before.""" |
| 481 | _Log("Linking to %s from %s", link, target) |
| 482 | if os.path.lexists(link): |
| 483 | os.unlink(link) |
| 484 | os.symlink(target, link) |
| 485 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 486 | def _GetLatestLocalVersion(self, board): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 487 | """Get the version of the latest image built for board by build_image |
| 488 | |
| 489 | Updates the symlink reference within the xBuddy static dir to point to |
| 490 | the real image dir in the local /build/images directory. |
| 491 | |
| 492 | Args: |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 493 | board: board that image was built for. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 494 | |
| 495 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 496 | The discovered version of the image. |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 497 | |
| 498 | Raises: |
| 499 | XBuddyException if neither test nor dev image was found in latest built |
| 500 | directory. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 501 | """ |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 502 | latest_local_dir = self.GetLatestImageDir(board) |
joychen | b0dfe55 | 2013-07-30 10:02:06 -0700 | [diff] [blame] | 503 | if not latest_local_dir or not os.path.exists(latest_local_dir): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 504 | raise XBuddyException('No builds found for %s. Did you run build_image?' % |
| 505 | board) |
| 506 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 507 | # Assume that the version number is the name of the directory. |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 508 | return os.path.basename(latest_local_dir.rstrip('/')) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 509 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 510 | @staticmethod |
| 511 | def _FindAny(local_dir): |
| 512 | """Returns the image_type for ANY given the local_dir.""" |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 513 | test_image = os.path.join(local_dir, devserver_constants.TEST_IMAGE_FILE) |
Yu-Ju Hong | c23c79b | 2014-03-17 12:40:33 -0700 | [diff] [blame] | 514 | dev_image = os.path.join(local_dir, devserver_constants.IMAGE_FILE) |
| 515 | # Prioritize test images over dev images. |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 516 | if os.path.exists(test_image): |
| 517 | return 'test' |
| 518 | |
Yu-Ju Hong | c23c79b | 2014-03-17 12:40:33 -0700 | [diff] [blame] | 519 | if os.path.exists(dev_image): |
| 520 | return 'dev' |
| 521 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 522 | raise XBuddyException('No images found in %s' % local_dir) |
| 523 | |
| 524 | @staticmethod |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 525 | def _InterpretPath(path, default_board=None, default_version=None): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 526 | """Split and return the pieces of an xBuddy path name |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 527 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 528 | Args: |
| 529 | path: the path xBuddy Get was called with. |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 530 | default_board: board to use in case board isn't in path. |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 531 | default_version: Version to use in case version isn't in path. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 532 | |
Yu-Ju Hong | c54658c | 2014-01-22 09:18:07 -0800 | [diff] [blame] | 533 | Returns: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 534 | tuple of (image_type, board, version, whether the path is local) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 535 | |
| 536 | Raises: |
| 537 | XBuddyException: if the path can't be resolved into valid components |
| 538 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 539 | path_list = filter(None, path.split('/')) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 540 | |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 541 | # Do the stuff that is well known first. We know that if paths have a |
| 542 | # image_type, it must be one of the GS/LOCAL aliases and it must be at the |
| 543 | # end. Similarly, local/remote are well-known and must start the path list. |
| 544 | is_local = True |
| 545 | if path_list and path_list[0] in (REMOTE, LOCAL): |
| 546 | is_local = (path_list.pop(0) == LOCAL) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 547 | |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 548 | # Default image type is determined by remote vs. local. |
| 549 | if is_local: |
| 550 | image_type = ANY |
| 551 | else: |
| 552 | image_type = TEST |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 553 | |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 554 | if path_list and path_list[-1] in GS_ALIASES + LOCAL_ALIASES: |
| 555 | image_type = path_list.pop(-1) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 556 | |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 557 | # Now for the tricky part. We don't actually know at this point if the rest |
| 558 | # of the path is just a board | version (like R33-2341.0.0) or just a board |
| 559 | # or just a version. So we do our best to do the right thing. |
| 560 | board = default_board |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 561 | version = default_version or LATEST |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 562 | if len(path_list) == 1: |
| 563 | path = path_list.pop(0) |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 564 | # Treat this as a version if it's one we know (contains default or |
| 565 | # latest), or we were given an actual default board. |
| 566 | if default_version in path or LATEST in path or default_board is not None: |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 567 | version = path |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 568 | else: |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 569 | board = path |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 570 | |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 571 | elif len(path_list) == 2: |
| 572 | # Assumes board/version. |
| 573 | board = path_list.pop(0) |
| 574 | version = path_list.pop(0) |
| 575 | |
| 576 | if path_list: |
| 577 | raise XBuddyException("Path isn't valid. Could not figure out how to " |
| 578 | "parse remaining components: %s." % path_list) |
| 579 | |
| 580 | _Log("Get artifact '%s' with board %s and version %s'. Locally? %s", |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 581 | image_type, board, version, is_local) |
| 582 | |
| 583 | return image_type, board, version, is_local |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 584 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 585 | def _SyncRegistryWithBuildImages(self): |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 586 | """Crawl images_dir for build_ids of images generated from build_image. |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 587 | |
| 588 | This will find images and symlink them in xBuddy's static dir so that |
| 589 | xBuddy's cache can serve them. |
| 590 | If xBuddy's _manage_builds option is on, then a timestamp will also be |
| 591 | generated, and xBuddy will clear them from the directory they are in, as |
| 592 | necessary. |
| 593 | """ |
Yu-Ju Hong | 235d1b5 | 2014-04-16 11:01:47 -0700 | [diff] [blame] | 594 | if not os.path.isdir(self.images_dir): |
| 595 | # Skip syncing if images_dir does not exist. |
| 596 | _Log('Cannot find %s; skip syncing image registry.', self.images_dir) |
| 597 | return |
| 598 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 599 | build_ids = [] |
| 600 | for b in os.listdir(self.images_dir): |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 601 | # Ensure we have directories to track all boards in build/images |
| 602 | common_util.MkDirP(os.path.join(self.static_dir, b)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 603 | board_dir = os.path.join(self.images_dir, b) |
| 604 | build_ids.extend(['/'.join([b, v]) for v |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 605 | in os.listdir(board_dir) if not v == LATEST]) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 606 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 607 | # Symlink undiscovered images, and update timestamps if manage_builds is on. |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 608 | for build_id in build_ids: |
| 609 | link = os.path.join(self.static_dir, build_id) |
| 610 | target = os.path.join(self.images_dir, build_id) |
| 611 | XBuddy._Symlink(link, target) |
| 612 | if self._manage_builds: |
| 613 | Timestamp.UpdateTimestamp(self._timestamp_folder, build_id) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 614 | |
| 615 | def _ListBuildTimes(self): |
Gilad Arnold | 5f46d8e | 2015-02-19 12:17:55 -0800 | [diff] [blame] | 616 | """Returns the currently cached builds and their last access timestamp. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 617 | |
| 618 | Returns: |
| 619 | list of tuples that matches xBuddy build/version to timestamps in long |
| 620 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 621 | # Update currently cached builds. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 622 | build_dict = {} |
| 623 | |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 624 | for f in os.listdir(self._timestamp_folder): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 625 | last_accessed = os.path.getmtime(os.path.join(self._timestamp_folder, f)) |
| 626 | build_id = Timestamp.TimestampToBuild(f) |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 627 | stale_time = datetime.timedelta(seconds=(time.time() - last_accessed)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 628 | build_dict[build_id] = stale_time |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 629 | return_tup = sorted(build_dict.iteritems(), key=operator.itemgetter(1)) |
| 630 | return return_tup |
| 631 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 632 | def _Download(self, gs_url, artifacts): |
| 633 | """Download the artifacts from the given gs_url. |
| 634 | |
| 635 | Raises: |
| 636 | build_artifact.ArtifactDownloadError: If we failed to download the |
| 637 | artifact. |
| 638 | """ |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 639 | with XBuddy._staging_thread_count_lock: |
| 640 | XBuddy._staging_thread_count += 1 |
| 641 | try: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 642 | _Log("Downloading %s from %s", artifacts, gs_url) |
| 643 | downloader.Downloader(self.static_dir, gs_url).Download(artifacts, []) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 644 | finally: |
| 645 | with XBuddy._staging_thread_count_lock: |
| 646 | XBuddy._staging_thread_count -= 1 |
| 647 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 648 | def CleanCache(self): |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 649 | """Delete all builds besides the newest N builds""" |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 650 | if not self._manage_builds: |
| 651 | return |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 652 | cached_builds = [e[0] for e in self._ListBuildTimes()] |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 653 | _Log('In cache now: %s', cached_builds) |
| 654 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 655 | for b in range(self._Capacity(), len(cached_builds)): |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 656 | b_path = cached_builds[b] |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 657 | _Log("Clearing '%s' from cache", b_path) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 658 | |
| 659 | time_file = os.path.join(self._timestamp_folder, |
| 660 | Timestamp.BuildToTimestamp(b_path)) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 661 | os.unlink(time_file) |
| 662 | clear_dir = os.path.join(self.static_dir, b_path) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 663 | try: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 664 | # Handle symlinks, in the case of links to local builds if enabled. |
| 665 | if os.path.islink(clear_dir): |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 666 | target = os.readlink(clear_dir) |
| 667 | _Log('Deleting locally built image at %s', target) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 668 | |
| 669 | os.unlink(clear_dir) |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 670 | if os.path.exists(target): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 671 | shutil.rmtree(target) |
| 672 | elif os.path.exists(clear_dir): |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 673 | _Log('Deleting downloaded image at %s', clear_dir) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 674 | shutil.rmtree(clear_dir) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 675 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 676 | except Exception as err: |
| 677 | raise XBuddyException('Failed to clear %s: %s' % (clear_dir, err)) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 678 | |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 679 | def _GetFromGS(self, build_id, image_type, image_dir=None): |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 680 | """Check if the artifact is available locally. Download from GS if not. |
| 681 | |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 682 | Args: |
| 683 | build_id: Path to the image or update directory on the devserver or |
| 684 | in Google Storage. e.g. 'x86-generic/R26-4000.0.0' |
| 685 | image_type: Image type to download. Look at aliases at top of file for |
| 686 | options. |
| 687 | image_dir: Google Storage image archive to search in if requesting a |
| 688 | remote artifact. If none uses the default bucket. |
| 689 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 690 | Raises: |
| 691 | build_artifact.ArtifactDownloadError: If we failed to download the |
| 692 | artifact. |
| 693 | """ |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 694 | image_dir = XBuddy._ResolveImageDir(image_dir) |
| 695 | gs_url = os.path.join(image_dir, build_id) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 696 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 697 | # Stage image if not found in cache. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 698 | file_name = GS_ALIAS_TO_FILENAME[image_type] |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 699 | file_loc = os.path.join(self.static_dir, build_id, file_name) |
| 700 | cached = os.path.exists(file_loc) |
| 701 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 702 | if not cached: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 703 | artifact = GS_ALIAS_TO_ARTIFACT[image_type] |
| 704 | self._Download(gs_url, [artifact]) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 705 | else: |
| 706 | _Log('Image already cached.') |
| 707 | |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 708 | def _GetArtifact(self, path_list, board=None, version=None, |
| 709 | lookup_only=False, image_dir=None): |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 710 | """Interpret an xBuddy path and return directory/file_name to resource. |
| 711 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 712 | Note board can be passed that in but by default if self._board is set, |
| 713 | that is used rather than board. |
| 714 | |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 715 | Args: |
| 716 | path_list: [board, version, alias] as split from the xbuddy call url. |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 717 | board: Board whos artifacts we are looking for. Only used if no board was |
| 718 | given during XBuddy initialization. |
| 719 | version: Version whose artifacts we are looking for. Used if no version |
| 720 | was given during XBuddy initialization. If None, defers to LATEST. |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 721 | lookup_only: If true just look up the artifact, if False stage it on |
| 722 | the devserver as well. |
| 723 | image_dir: Google Storage image archive to search in if requesting a |
| 724 | remote artifact. If none uses the default bucket. |
| 725 | |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 726 | Returns: |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 727 | build_id: Path to the image or update directory on the devserver or |
| 728 | in Google Storage. e.g. 'x86-generic/R26-4000.0.0' |
| 729 | file_name: of the artifact in the build_id directory. |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 730 | |
| 731 | Raises: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 732 | XBuddyException: if the path could not be translated |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 733 | build_artifact.ArtifactDownloadError: if we failed to download the |
| 734 | artifact. |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 735 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 736 | path = '/'.join(path_list) |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 737 | default_board = self._board if self._board else board |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 738 | default_version = self._version or version or LATEST |
joychen | b0dfe55 | 2013-07-30 10:02:06 -0700 | [diff] [blame] | 739 | # Rewrite the path if there is an appropriate default. |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 740 | path, suffix = self._LookupAlias(path, default_board, default_version) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 741 | # Parse the path. |
Chris Sosa | 0eecf96 | 2014-02-03 14:14:39 -0800 | [diff] [blame] | 742 | image_type, board, version, is_local = self._InterpretPath( |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 743 | path, default_board, default_version) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 744 | if is_local: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 745 | # Get a local image. |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 746 | if version == LATEST: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 747 | # Get the latest local image for the given board. |
| 748 | version = self._GetLatestLocalVersion(board) |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 749 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 750 | build_id = os.path.join(board, version) |
| 751 | artifact_dir = os.path.join(self.static_dir, build_id) |
| 752 | if image_type == ANY: |
| 753 | image_type = self._FindAny(artifact_dir) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 754 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 755 | file_name = LOCAL_ALIAS_TO_FILENAME[image_type] |
| 756 | artifact_path = os.path.join(artifact_dir, file_name) |
| 757 | if not os.path.exists(artifact_path): |
| 758 | raise XBuddyException('Local %s artifact not in static_dir at %s' % |
| 759 | (image_type, artifact_path)) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 760 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 761 | else: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 762 | # Get a remote image. |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 763 | if image_type not in GS_ALIASES: |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 764 | raise XBuddyException('Bad remote image type: %s. Use one of: %s' % |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 765 | (image_type, GS_ALIASES)) |
Gilad Arnold | 896c6d8 | 2015-03-13 16:20:29 -0700 | [diff] [blame^] | 766 | build_id = self._ResolveVersionToBuildId(board, suffix, version, |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 767 | image_dir=image_dir) |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 768 | _Log('Resolved version %s to %s.', version, build_id) |
| 769 | file_name = GS_ALIAS_TO_FILENAME[image_type] |
| 770 | if not lookup_only: |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 771 | self._GetFromGS(build_id, image_type, image_dir=image_dir) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 772 | |
joychen | c3944cb | 2013-08-19 10:42:07 -0700 | [diff] [blame] | 773 | return build_id, file_name |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 774 | |
| 775 | ############################ BEGIN PUBLIC METHODS |
| 776 | |
| 777 | def List(self): |
| 778 | """Lists the currently available images & time since last access.""" |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 779 | self._SyncRegistryWithBuildImages() |
| 780 | builds = self._ListBuildTimes() |
| 781 | return_string = '' |
| 782 | for build, timestamp in builds: |
| 783 | return_string += '<b>' + build + '</b> ' |
| 784 | return_string += '(time since last access: ' + str(timestamp) + ')<br>' |
| 785 | return return_string |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 786 | |
| 787 | def Capacity(self): |
| 788 | """Returns the number of images cached by xBuddy.""" |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 789 | return str(self._Capacity()) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 790 | |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 791 | def Translate(self, path_list, board=None, version=None, image_dir=None): |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 792 | """Translates an xBuddy path to a real path to artifact if it exists. |
| 793 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 794 | Equivalent to the Get call, minus downloading and updating timestamps, |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 795 | |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 796 | Args: |
| 797 | path_list: [board, version, alias] as split from the xbuddy call url. |
| 798 | board: Board whos artifacts we are looking for. If None, use the board |
| 799 | XBuddy was initialized to use. |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 800 | version: Version whose artifacts we are looking for. If None, use the |
| 801 | version XBuddy was initialized with, or LATEST. |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 802 | image_dir: image directory to check in Google Storage. If none, |
| 803 | the default bucket is used. |
| 804 | |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 805 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 806 | build_id: Path to the image or update directory on the devserver. |
| 807 | e.g. 'x86-generic/R26-4000.0.0' |
| 808 | The returned path is always the path to the directory within |
| 809 | static_dir, so it is always the build_id of the image. |
| 810 | file_name: The file name of the artifact. Can take any of the file |
| 811 | values in devserver_constants. |
| 812 | e.g. 'chromiumos_test_image.bin' or 'update.gz' if the path list |
| 813 | specified 'test' or 'full_payload' artifacts, respectively. |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 814 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 815 | Raises: |
| 816 | XBuddyException: if the path couldn't be translated |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 817 | """ |
| 818 | self._SyncRegistryWithBuildImages() |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 819 | build_id, file_name = self._GetArtifact(path_list, board=board, |
Gilad Arnold | d04fcab | 2015-02-19 12:00:45 -0800 | [diff] [blame] | 820 | version=version, |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 821 | lookup_only=True, |
| 822 | image_dir=image_dir) |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 823 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 824 | _Log('Returning path to payload: %s/%s', build_id, file_name) |
| 825 | return build_id, file_name |
joychen | 346531c | 2013-07-24 16:55:56 -0700 | [diff] [blame] | 826 | |
Yu-Ju Hong | 1bdb7a9 | 2014-04-10 16:02:11 -0700 | [diff] [blame] | 827 | def StageTestArtifactsForUpdate(self, path_list): |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 828 | """Stages test artifacts for update and returns build_id. |
| 829 | |
| 830 | Raises: |
| 831 | XBuddyException: if the path could not be translated |
| 832 | build_artifact.ArtifactDownloadError: if we failed to download the test |
| 833 | artifacts. |
| 834 | """ |
| 835 | build_id, file_name = self.Translate(path_list) |
| 836 | if file_name == devserver_constants.TEST_IMAGE_FILE: |
| 837 | gs_url = os.path.join(devserver_constants.GS_IMAGE_DIR, |
| 838 | build_id) |
| 839 | artifacts = [FULL, STATEFUL] |
| 840 | self._Download(gs_url, artifacts) |
| 841 | return build_id |
| 842 | |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 843 | def Get(self, path_list, image_dir=None): |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 844 | """The full xBuddy call, returns resource specified by path_list. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 845 | |
| 846 | Please see devserver.py:xbuddy for full documentation. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 847 | |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 848 | Args: |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 849 | path_list: [board, version, alias] as split from the xbuddy call url. |
| 850 | image_dir: image directory to check in Google Storage. If none, |
| 851 | the default bucket is used. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 852 | |
| 853 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 854 | build_id: Path to the image or update directory on the devserver. |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 855 | e.g. 'x86-generic/R26-4000.0.0' |
| 856 | The returned path is always the path to the directory within |
| 857 | static_dir, so it is always the build_id of the image. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 858 | file_name: The file name of the artifact. Can take any of the file |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 859 | values in devserver_constants. |
| 860 | e.g. 'chromiumos_test_image.bin' or 'update.gz' if the path list |
| 861 | specified 'test' or 'full_payload' artifacts, respectively. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 862 | |
| 863 | Raises: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 864 | XBuddyException: if the path could not be translated |
| 865 | build_artifact.ArtifactDownloadError: if we failed to download the |
| 866 | artifact. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 867 | """ |
joychen | 7df67f7 | 2013-07-18 14:21:12 -0700 | [diff] [blame] | 868 | self._SyncRegistryWithBuildImages() |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 869 | build_id, file_name = self._GetArtifact(path_list, image_dir=image_dir) |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 870 | Timestamp.UpdateTimestamp(self._timestamp_folder, build_id) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 871 | #TODO (joyc): run in sep thread |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 872 | self.CleanCache() |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 873 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 874 | _Log('Returning path to payload: %s/%s', build_id, file_name) |
| 875 | return build_id, file_name |