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