Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 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 | |
| 5 | """Module containing gsutil helper methods.""" |
| 6 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 7 | import distutils.version |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 8 | import logging |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 9 | import random |
| 10 | import re |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 11 | import subprocess |
Chris Sosa | 101fd86 | 2012-06-12 17:44:53 -0700 | [diff] [blame] | 12 | import time |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 13 | |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 14 | import devserver_constants |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 15 | import log_util |
| 16 | |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 17 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 18 | GSUTIL_ATTEMPTS = 5 |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 19 | UPLOADED_LIST = 'UPLOADED' |
| 20 | |
| 21 | |
| 22 | # Module-local log function. |
| 23 | def _Log(message, *args): |
| 24 | return log_util.LogWithTag('GSUTIL_UTIL', message, *args) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class GSUtilError(Exception): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 28 | """Exception raised when we run into an error running gsutil.""" |
| 29 | pass |
| 30 | |
| 31 | |
| 32 | class PatternNotSpecific(Exception): |
| 33 | """Raised when unexpectedly more than one item is returned for a pattern.""" |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 34 | pass |
| 35 | |
| 36 | |
| 37 | def GSUtilRun(cmd, err_msg): |
| 38 | """Runs a GSUTIL command up to GSUTIL_ATTEMPTS number of times. |
| 39 | |
Chris Sosa | 101fd86 | 2012-06-12 17:44:53 -0700 | [diff] [blame] | 40 | Attempts are tried with exponential backoff. |
| 41 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 42 | Returns: |
| 43 | stdout of the called gsutil command. |
| 44 | Raises: |
| 45 | subprocess.CalledProcessError if all attempt to run gsutil cmd fails. |
| 46 | """ |
| 47 | proc = None |
Chris Sosa | 101fd86 | 2012-06-12 17:44:53 -0700 | [diff] [blame] | 48 | sleep_timeout = 1 |
Chris Sosa | 407e8c5 | 2013-02-27 15:33:35 -0800 | [diff] [blame] | 49 | stderr = None |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 50 | for _attempt in range(GSUTIL_ATTEMPTS): |
Chris Sosa | 407e8c5 | 2013-02-27 15:33:35 -0800 | [diff] [blame] | 51 | proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, |
| 52 | stderr=subprocess.PIPE) |
| 53 | stdout, stderr = proc.communicate() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 54 | if proc.returncode == 0: |
| 55 | return stdout |
Chris Sosa | 6de2930 | 2013-03-14 15:27:36 -0700 | [diff] [blame] | 56 | elif stderr and ('matched no objects' in stderr or |
| 57 | 'non-existent object' in stderr): |
Chris Sosa | 407e8c5 | 2013-02-27 15:33:35 -0800 | [diff] [blame] | 58 | # TODO(sosa): Note this is a heuristic that makes us not re-attempt |
| 59 | # unnecessarily. However, if it fails, the worst that can happen is just |
| 60 | # waiting longer than necessary. |
| 61 | break |
Chris Sosa | 6de2930 | 2013-03-14 15:27:36 -0700 | [diff] [blame] | 62 | elif proc.returncode == 127: |
| 63 | raise GSUtilError('gsutil tool not found in your path.') |
Chris Sosa | 101fd86 | 2012-06-12 17:44:53 -0700 | [diff] [blame] | 64 | |
| 65 | time.sleep(sleep_timeout) |
| 66 | sleep_timeout *= 2 |
| 67 | |
Chris Sosa | 407e8c5 | 2013-02-27 15:33:35 -0800 | [diff] [blame] | 68 | raise GSUtilError('%s GSUTIL cmd %s failed with return code %d:\n\n%s' % ( |
| 69 | err_msg, cmd, proc.returncode, stderr)) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 70 | |
| 71 | |
| 72 | def DownloadFromGS(src, dst): |
| 73 | """Downloads object from gs_url |src| to |dst|. |
| 74 | |
| 75 | Raises: |
| 76 | GSUtilError: if an error occurs during the download. |
| 77 | """ |
| 78 | cmd = 'gsutil cp %s %s' % (src, dst) |
| 79 | msg = 'Failed to download "%s".' % src |
| 80 | GSUtilRun(cmd, msg) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 81 | |
| 82 | |
| 83 | def _GetGSNamesFromList(filename_list, pattern): |
| 84 | """Given a list of filenames, returns the filenames that match pattern.""" |
| 85 | matches = [] |
| 86 | re_pattern = re.compile(pattern) |
| 87 | for filename in filename_list: |
| 88 | if re_pattern.match(filename): |
| 89 | matches.append(filename) |
| 90 | |
| 91 | return matches |
| 92 | |
| 93 | |
| 94 | def GetGSNamesWithWait(pattern, archive_url, err_str, single_item=True, |
| 95 | timeout=600, delay=10): |
| 96 | """Returns the google storage names specified by the given pattern. |
| 97 | |
| 98 | This method polls Google Storage until the target artifacts specified by the |
| 99 | pattern is available or until the timeout occurs. Because we may not know the |
| 100 | exact name of the target artifacts, the method accepts a filename pattern, |
| 101 | to identify whether an artifact whose name matches the pattern exists (e.g. |
| 102 | use pattern '_full_' to search for the full payload |
| 103 | 'chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin'). Returns the name only if |
| 104 | found before the timeout. |
| 105 | |
| 106 | Args: |
| 107 | pattern: Regular expression pattern to identify the target artifact. |
| 108 | archive_url: URL of the Google Storage bucket. |
| 109 | err_str: String to display in the error message on error. |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 110 | single_item: Only a single item should be returned. If more than one item |
| 111 | matches the pattern errors out unless pattern matches one |
| 112 | exactly. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 113 | timeout/delay: optional and self-explanatory. |
| 114 | |
| 115 | Returns: |
| 116 | The list of artifacts matching the pattern in Google Storage bucket or None |
| 117 | if not found. |
| 118 | |
| 119 | Raises: |
| 120 | PatternNotSpecific: If caller sets single_item but multiple items match. |
| 121 | """ |
| 122 | deadline = time.time() + timeout |
Chris Sosa | 6de2930 | 2013-03-14 15:27:36 -0700 | [diff] [blame] | 123 | while True: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 124 | uploaded_list = [] |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 125 | try: |
| 126 | cmd = 'gsutil cat %s/%s' % (archive_url, UPLOADED_LIST) |
| 127 | msg = 'Failed to get a list of uploaded files.' |
| 128 | uploaded_list = GSUtilRun(cmd, msg).splitlines() |
| 129 | except GSUtilError: |
| 130 | # For backward compatibility, falling back to use "gsutil ls" |
| 131 | # when the manifest file is not present. |
| 132 | cmd = 'gsutil ls %s/*' % archive_url |
| 133 | msg = 'Failed to list payloads.' |
| 134 | returned_list = GSUtilRun(cmd, msg).splitlines() |
| 135 | for item in returned_list: |
beeps | c3d0f87 | 2013-07-31 21:50:40 -0700 | [diff] [blame] | 136 | try: |
| 137 | uploaded_list.append(item.rsplit('/', 1)[1]) |
| 138 | except IndexError: |
| 139 | pass |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 140 | |
| 141 | # Check if all target artifacts are available. |
| 142 | found_names = _GetGSNamesFromList(uploaded_list, pattern) |
| 143 | if found_names: |
| 144 | if single_item and len(found_names) > 1: |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 145 | found_names_exact = _GetGSNamesFromList(uploaded_list, '^%s$' % pattern) |
| 146 | if not found_names_exact: |
| 147 | raise PatternNotSpecific( |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 148 | 'Too many items %s returned by pattern %s in %s' % ( |
| 149 | str(found_names), pattern, archive_url)) |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 150 | else: |
| 151 | logging.info('More than one item returned but one file matched' |
| 152 | ' exactly so returning that: %s.', found_names_exact) |
| 153 | found_names = found_names_exact |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 154 | |
| 155 | return found_names |
| 156 | |
Chris Sosa | 6de2930 | 2013-03-14 15:27:36 -0700 | [diff] [blame] | 157 | # Don't delay past deadline. |
| 158 | to_delay = random.uniform(1.5 * delay, 2.5 * delay) |
| 159 | if to_delay < (deadline - time.time()): |
| 160 | _Log('Retrying in %f seconds...%s', to_delay, err_str) |
| 161 | time.sleep(to_delay) |
| 162 | else: |
| 163 | return None |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 164 | |
| 165 | |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 166 | def GetLatestVersionFromGSDir(gsutil_dir, with_release=True): |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 167 | """Returns most recent version number found in a GS directory. |
| 168 | |
| 169 | This lists out the contents of the given GS bucket or regex to GS buckets, |
| 170 | and tries to grab the newest version found in the directory names. |
| 171 | """ |
| 172 | cmd = 'gsutil ls %s' % gsutil_dir |
| 173 | msg = 'Failed to find most recent builds at %s' % gsutil_dir |
| 174 | dir_names = [p.split('/')[-2] for p in GSUtilRun(cmd, msg).splitlines()] |
| 175 | try: |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 176 | if with_release: |
| 177 | versions = filter(lambda x: re.match(devserver_constants.VERSION_RE, x), |
| 178 | dir_names) |
| 179 | else: |
| 180 | versions = filter(lambda x: re.match(devserver_constants.VERSION, x), |
| 181 | dir_names) |
joychen | f8f07e2 | 2013-07-12 17:45:51 -0700 | [diff] [blame] | 182 | latest_version = max(versions, key=distutils.version.LooseVersion) |
| 183 | except ValueError: |
| 184 | raise GSUtilError(msg) |
| 185 | |
| 186 | return latest_version |