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