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