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