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