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