Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 5 | import os |
Gilad Arnold | 0b8c3f3 | 2012-09-19 21:35:44 | [diff] [blame] | 6 | import threading |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 7 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 8 | import build_artifact |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 9 | import common_util |
Dan Shi | ba0e674 | 2013-06-27 00:39:05 | [diff] [blame] | 10 | import gsutil_util |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 11 | import log_util |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 12 | |
| 13 | |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 14 | class Downloader(log_util.Loggable): |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 15 | """Downloader of images to the devsever. |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 16 | |
| 17 | Given a URL to a build on the archive server: |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 18 | - Caches that build and the given artifacts onto the devserver. |
| 19 | - May also initiate caching of related artifacts in the background. |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 20 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 21 | Private class members: |
| 22 | archive_url: a URL where to download build artifacts from. |
| 23 | static_dir: local filesystem directory to store all artifacts. |
| 24 | build_dir: the local filesystem directory to store artifacts for the given |
| 25 | build defined by the archive_url. |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 26 | """ |
| 27 | |
Alex Miller | a44d502 | 2012-07-27 18:34:16 | [diff] [blame] | 28 | # This filename must be kept in sync with clean_staged_images.py |
| 29 | _TIMESTAMP_FILENAME = 'staged.timestamp' |
Chris Masone | a22d938 | 2012-05-18 19:38:51 | [diff] [blame] | 30 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 31 | def __init__(self, static_dir, archive_url): |
| 32 | super(Downloader, self).__init__() |
| 33 | self._archive_url = archive_url |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 34 | self._static_dir = static_dir |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 35 | self._build_dir = Downloader.GetBuildDir(static_dir, archive_url) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 36 | |
| 37 | @staticmethod |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 38 | def ParseUrl(archive_url): |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 39 | """Parses archive_url into rel_path and build. |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 40 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 41 | Parses archive_url into rel_path and build e.g. |
| 42 | gs://chromeos-image-archive/{rel_path}/{build}. |
| 43 | |
| 44 | Args: |
| 45 | archive_url: a URL at which build artifacts are archived. |
| 46 | |
| 47 | Returns: |
| 48 | A tuple of (build relative path, short build name) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 49 | """ |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 50 | # The archive_url is of the form gs://server/[some_path/target]/...]/build |
| 51 | # This function discards 'gs://server/' and extracts the [some_path/target] |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 52 | # as rel_path and the build as build. |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 53 | sub_url = archive_url.partition('://')[2] |
| 54 | split_sub_url = sub_url.split('/') |
| 55 | rel_path = '/'.join(split_sub_url[1:-1]) |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 56 | build = split_sub_url[-1] |
| 57 | return rel_path, build |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 58 | |
| 59 | @staticmethod |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 60 | def GetBuildDir(static_dir, archive_url): |
| 61 | """Returns the path to where the artifacts will be staged. |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 62 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 63 | Args: |
| 64 | static_dir: The base static dir that will be used. |
| 65 | archive_url: The gs path to the archive url. |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 66 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 67 | # Parse archive_url into rel_path (contains the build target) and |
| 68 | # build e.g. gs://chromeos-image-archive/{rel_path}/{build}. |
| 69 | rel_path, build = Downloader.ParseUrl(archive_url) |
| 70 | return os.path.join(static_dir, rel_path, build) |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 71 | |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 72 | @staticmethod |
Alex Miller | a44d502 | 2012-07-27 18:34:16 | [diff] [blame] | 73 | def _TouchTimestampForStaged(directory_path): |
| 74 | file_name = os.path.join(directory_path, Downloader._TIMESTAMP_FILENAME) |
| 75 | # Easiest python version of |touch file_name| |
| 76 | with file(file_name, 'a'): |
| 77 | os.utime(file_name, None) |
| 78 | |
Dan Shi | ba0e674 | 2013-06-27 00:39:05 | [diff] [blame] | 79 | @staticmethod |
| 80 | def _TryRemoveStageDir(directory_path): |
| 81 | """If download failed with GSUtilError, try to remove the stage dir. |
| 82 | |
| 83 | If the download attempt failed with GSUtilError and staged.timestamp is the |
| 84 | only file in that directory. The build could be non-existing, and the |
| 85 | directory should be removed. |
| 86 | |
| 87 | @param directory_path: directory used to stage the image. |
| 88 | |
| 89 | """ |
| 90 | file_name = os.path.join(directory_path, Downloader._TIMESTAMP_FILENAME) |
| 91 | if os.path.exists(file_name) and len(os.listdir(directory_path)) == 1: |
| 92 | os.remove(file_name) |
| 93 | os.rmdir(directory_path) |
| 94 | |
Chris Sosa | 6b0c617 | 2013-08-06 00:01:33 | [diff] [blame] | 95 | def Download(self, artifacts, files, async=False): |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 96 | """Downloads and caches the |artifacts|. |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 97 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 98 | Downloads and caches the |artifacts|. Returns once these |
| 99 | are present on the devserver. A call to this will attempt to cache |
| 100 | non-specified artifacts in the background following the principle of |
| 101 | spatial locality. |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 102 | |
Chris Sosa | 6b0c617 | 2013-08-06 00:01:33 | [diff] [blame] | 103 | artifacts: A list of artifact names that correspond to |
| 104 | artifacts defined in artifact_info.py to stage. |
| 105 | files: A list of filenames to stage from an archive_url. |
| 106 | async: If True, return without waiting for download to complete. |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 107 | |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 108 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 109 | common_util.MkDirP(self._build_dir) |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 110 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 111 | # We are doing some work on this build -- let's touch it to indicate that |
| 112 | # we shouldn't be cleaning it up anytime soon. |
| 113 | Downloader._TouchTimestampForStaged(self._build_dir) |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 114 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 115 | # Create factory to create build_artifacts from artifact names. |
| 116 | build = self.ParseUrl(self._archive_url)[1] |
Chris Sosa | 6b0c617 | 2013-08-06 00:01:33 | [diff] [blame] | 117 | factory = build_artifact.ArtifactFactory( |
| 118 | self._build_dir, self._archive_url, artifacts, files, |
| 119 | build) |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 120 | background_artifacts = factory.OptionalArtifacts() |
| 121 | if background_artifacts: |
| 122 | self._DownloadArtifactsInBackground(background_artifacts) |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 123 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 124 | required_artifacts = factory.RequiredArtifacts() |
| 125 | str_repr = [str(a) for a in required_artifacts] |
| 126 | self._Log('Downloading artifacts %s.', ' '.join(str_repr)) |
Dan Shi | e37f8fe | 2013-08-09 23:10:29 | [diff] [blame] | 127 | |
Dan Shi | ba0e674 | 2013-06-27 00:39:05 | [diff] [blame] | 128 | try: |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 129 | if async: |
Dan Shi | e37f8fe | 2013-08-09 23:10:29 | [diff] [blame] | 130 | # Make sure all artifacts exist before starting downloading in a new |
| 131 | # thread. This prevents caller from waiting indefinitely for any |
| 132 | # nonexistent artifact. |
| 133 | for artifact in required_artifacts: |
Chris Sosa | c4e8784 | 2013-08-17 01:04:14 | [diff] [blame] | 134 | artifact.WaitForArtifactToExist(timeout=10, update_name=False) |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 135 | self._DownloadArtifactsInBackground(required_artifacts) |
| 136 | else: |
| 137 | self._DownloadArtifactsSerially(required_artifacts, no_wait=True) |
Dan Shi | ba0e674 | 2013-06-27 00:39:05 | [diff] [blame] | 138 | except gsutil_util.GSUtilError: |
| 139 | Downloader._TryRemoveStageDir(self._build_dir) |
| 140 | raise |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 141 | |
Chris Sosa | 6b0c617 | 2013-08-06 00:01:33 | [diff] [blame] | 142 | def IsStaged(self, artifacts, files): |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 143 | """Check if all artifacts have been downloaded. |
| 144 | |
Chris Sosa | 6b0c617 | 2013-08-06 00:01:33 | [diff] [blame] | 145 | artifacts: A list of artifact names that correspond to |
| 146 | artifacts defined in artifact_info.py to stage. |
| 147 | files: A list of filenames to stage from an archive_url. |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 148 | @returns: True if all artifacts are staged. |
| 149 | |
| 150 | """ |
| 151 | # Create factory to create build_artifacts from artifact names. |
| 152 | build = self.ParseUrl(self._archive_url)[1] |
Chris Sosa | 6b0c617 | 2013-08-06 00:01:33 | [diff] [blame] | 153 | factory = build_artifact.ArtifactFactory( |
| 154 | self._build_dir, self._archive_url, artifacts, files, build) |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 155 | required_artifacts = factory.RequiredArtifacts() |
| 156 | return all([artifact.ArtifactStaged() for artifact in required_artifacts]) |
| 157 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 158 | def _DownloadArtifactsSerially(self, artifacts, no_wait): |
| 159 | """Simple function to download all the given artifacts serially. |
| 160 | |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 161 | @param artifacts: A list of build_artifact.BuildArtifact instances to |
| 162 | download. |
| 163 | @param no_wait: If True, don't block waiting for artifact to exist if we |
| 164 | fail to immediately find it. |
| 165 | |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 166 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 167 | for artifact in artifacts: |
| 168 | artifact.Process(no_wait) |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 169 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 170 | def _DownloadArtifactsInBackground(self, artifacts): |
| 171 | """Downloads |artifacts| in the background. |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 172 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 173 | Downloads |artifacts| in the background. As these are backgrounded |
| 174 | artifacts, they are done best effort and may not exist. |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 175 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 176 | Args: |
| 177 | artifacts: List of build_artifact.BuildArtifact instances to download. |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 178 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 179 | self._Log('Invoking background download of artifacts for %r', artifacts) |
| 180 | thread = threading.Thread(target=self._DownloadArtifactsSerially, |
| 181 | args=(artifacts, False)) |
| 182 | thread.start() |