Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
Gilad Arnold | 0b8c3f3 | 2012-09-19 14:35:44 -0700 | [diff] [blame] | 7 | import Queue |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 8 | import cherrypy |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 9 | import os |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 10 | import shutil |
| 11 | import tempfile |
Gilad Arnold | 0b8c3f3 | 2012-09-19 14:35:44 -0700 | [diff] [blame] | 12 | import threading |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 13 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 14 | import build_artifact |
| 15 | import common_util |
| 16 | import log_util |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 17 | |
| 18 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 19 | class Downloader(log_util.Loggable): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 20 | """Download images to the devsever. |
| 21 | |
| 22 | Given a URL to a build on the archive server: |
| 23 | |
| 24 | - Determine if the build already exists. |
| 25 | - Download and extract the build to a staging directory. |
| 26 | - Package autotest tests. |
| 27 | - Install components to static dir. |
| 28 | """ |
| 29 | |
Alex Miller | a44d502 | 2012-07-27 11:34:16 -0700 | [diff] [blame] | 30 | # This filename must be kept in sync with clean_staged_images.py |
| 31 | _TIMESTAMP_FILENAME = 'staged.timestamp' |
Chris Masone | a22d938 | 2012-05-18 12:38:51 -0700 | [diff] [blame] | 32 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 33 | def __init__(self, static_dir): |
| 34 | self._static_dir = static_dir |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 35 | self._build_dir = None |
| 36 | self._staging_dir = None |
Gilad Arnold | 0b8c3f3 | 2012-09-19 14:35:44 -0700 | [diff] [blame] | 37 | self._status_queue = Queue.Queue(maxsize=1) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 38 | self._lock_tag = None |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 39 | |
| 40 | @staticmethod |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 41 | def ParseUrl(archive_url): |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 42 | """Parse archive_url into rel_path and short_build |
| 43 | e.g. gs://chromeos-image-archive/{rel_path}/{short_build} |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 44 | |
| 45 | @param archive_url: a URL at which build artifacts are archived. |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 46 | @return a tuple of (build relative path, short build name) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 47 | """ |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 48 | # The archive_url is of the form gs://server/[some_path/target]/...]/build |
| 49 | # This function discards 'gs://server/' and extracts the [some_path/target] |
| 50 | # as rel_path and the build as short_build. |
| 51 | sub_url = archive_url.partition('://')[2] |
| 52 | split_sub_url = sub_url.split('/') |
| 53 | rel_path = '/'.join(split_sub_url[1:-1]) |
| 54 | short_build = split_sub_url[-1] |
| 55 | return rel_path, short_build |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 56 | |
| 57 | @staticmethod |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 58 | def GenerateLockTag(rel_path, short_build): |
| 59 | """Generate a name for a lock scoped to this rel_path/build pair. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 60 | |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 61 | @param rel_path: the relative path for the build. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 62 | @param short_build: short build name |
| 63 | @return a name to use with AcquireLock that will scope the lock. |
| 64 | """ |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 65 | return '/'.join([rel_path, short_build]) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 66 | |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 67 | @staticmethod |
Alex Miller | a44d502 | 2012-07-27 11:34:16 -0700 | [diff] [blame] | 68 | def _TouchTimestampForStaged(directory_path): |
| 69 | file_name = os.path.join(directory_path, Downloader._TIMESTAMP_FILENAME) |
| 70 | # Easiest python version of |touch file_name| |
| 71 | with file(file_name, 'a'): |
| 72 | os.utime(file_name, None) |
| 73 | |
| 74 | @staticmethod |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 75 | def BuildStaged(archive_url, static_dir): |
| 76 | """Returns True if the build is already staged.""" |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 77 | rel_path, short_build = Downloader.ParseUrl(archive_url) |
| 78 | sub_directory = Downloader.GenerateLockTag(rel_path, short_build) |
Alex Miller | a44d502 | 2012-07-27 11:34:16 -0700 | [diff] [blame] | 79 | directory_path = os.path.join(static_dir, sub_directory) |
| 80 | exists = os.path.isdir(directory_path) |
| 81 | # If the build exists, then touch the timestamp to tell |
| 82 | # clean_stages_images.py that we're using this build. |
| 83 | if exists: |
| 84 | Downloader._TouchTimestampForStaged(directory_path) |
| 85 | return exists |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 86 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 87 | def Download(self, archive_url, background=False): |
| 88 | """Downloads the given build artifacts defined by the |archive_url|. |
| 89 | |
| 90 | If background is set to True, will return back early before all artifacts |
| 91 | have been downloaded. The artifacts that can be backgrounded are all those |
| 92 | that are not set as synchronous. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 93 | |
| 94 | TODO: refactor this into a common Download method, once unit tests are |
| 95 | fixed up to make iterating on the code easier. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 96 | """ |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 97 | # Parse archive_url into rel_path (contains the build target) and |
| 98 | # short_build. |
| 99 | # e.g. gs://chromeos-image-archive/{rel_path}/{short_build} |
| 100 | rel_path, short_build = self.ParseUrl(archive_url) |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 101 | # This should never happen. The Devserver should only try to call this |
| 102 | # method if no previous downloads have been staged for this archive_url. |
| 103 | assert not Downloader.BuildStaged(archive_url, self._static_dir) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 104 | # Bind build_dir and staging_dir here so we can tell if we need to do any |
| 105 | # cleanup after an exception occurs before build_dir is set. |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 106 | self._lock_tag = self.GenerateLockTag(rel_path, short_build) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 107 | try: |
| 108 | # Create Dev Server directory for this build and tell other Downloader |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 109 | # instances we have processed this build. Note that during normal |
| 110 | # execution, this lock is only released in the actual downloading |
| 111 | # procedure called below. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 112 | self._build_dir = common_util.AcquireLock( |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 113 | static_dir=self._static_dir, tag=self._lock_tag) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 114 | |
Yu-Ju Hong | 1a83a71 | 2012-06-27 09:11:34 -0700 | [diff] [blame] | 115 | # Replace '/' with '_' in rel_path because it may contain multiple levels |
| 116 | # which would not be qualified as part of the suffix. |
| 117 | self._staging_dir = tempfile.mkdtemp(suffix='_'.join( |
Chris Sosa | f097564 | 2012-06-29 13:53:42 -0700 | [diff] [blame] | 118 | [rel_path.replace('/', '_'), short_build])) |
Alex Miller | a44d502 | 2012-07-27 11:34:16 -0700 | [diff] [blame] | 119 | Downloader._TouchTimestampForStaged(self._staging_dir) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 120 | self._Log('Gathering download requirements %s' % archive_url) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 121 | artifacts = self.GatherArtifactDownloads( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 122 | self._staging_dir, archive_url, self._build_dir, short_build) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 123 | common_util.PrepareBuildDirectory(self._build_dir) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 124 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 125 | self._Log('Downloading foreground artifacts from %s' % archive_url) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 126 | background_artifacts = [] |
| 127 | for artifact in artifacts: |
| 128 | if artifact.Synchronous(): |
| 129 | artifact.Download() |
| 130 | artifact.Stage() |
| 131 | else: |
| 132 | background_artifacts.append(artifact) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 133 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 134 | if background: |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 135 | self._DownloadArtifactsInBackground(background_artifacts) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 136 | else: |
| 137 | self._DownloadArtifactsSerially(background_artifacts) |
| 138 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 139 | except Exception, e: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 140 | # Release processing lock, which will remove build components directory |
| 141 | # so future runs can retry. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 142 | if self._build_dir: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 143 | common_util.ReleaseLock(static_dir=self._static_dir, tag=self._lock_tag, |
| 144 | destroy=True) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 145 | |
| 146 | self._status_queue.put(e) |
| 147 | self._Cleanup() |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 148 | raise |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 149 | return 'Success' |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 150 | |
| 151 | def _Cleanup(self): |
| 152 | """Cleans up the staging dir for this downloader instanfce.""" |
| 153 | if self._staging_dir: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 154 | self._Log('Cleaning up staging directory %s' % self._staging_dir) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 155 | shutil.rmtree(self._staging_dir) |
| 156 | |
| 157 | self._staging_dir = None |
| 158 | |
| 159 | def _DownloadArtifactsSerially(self, artifacts): |
| 160 | """Simple function to download all the given artifacts serially.""" |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 161 | self._Log('Downloading artifacts serially.') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 162 | try: |
| 163 | for artifact in artifacts: |
| 164 | artifact.Download() |
| 165 | artifact.Stage() |
| 166 | except Exception, e: |
| 167 | self._status_queue.put(e) |
| 168 | |
| 169 | # Release processing lock, which will remove build components directory |
| 170 | # so future runs can retry. |
| 171 | if self._build_dir: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 172 | common_util.ReleaseLock(static_dir=self._static_dir, tag=self._lock_tag, |
| 173 | destroy=True) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 174 | else: |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 175 | # Release processing lock, keeping directory intact. |
| 176 | if self._build_dir: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 177 | common_util.ReleaseLock(static_dir=self._static_dir, tag=self._lock_tag) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 178 | self._status_queue.put('Success') |
| 179 | finally: |
| 180 | self._Cleanup() |
| 181 | |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 182 | def _DownloadArtifactsInBackground(self, artifacts): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 183 | """Downloads |artifacts| in the background and signals when complete.""" |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 184 | self._Log('Invoking background download of artifacts') |
Gilad Arnold | 0b8c3f3 | 2012-09-19 14:35:44 -0700 | [diff] [blame] | 185 | thread = threading.Thread(target=self._DownloadArtifactsSerially, |
| 186 | args=(artifacts,)) |
| 187 | thread.start() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 188 | |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 189 | def GatherArtifactDownloads(self, main_staging_dir, archive_url, build_dir, |
| 190 | short_build): |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 191 | """Wrapper around common_util.GatherArtifactDownloads(). |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 192 | |
| 193 | The wrapper allows mocking and overriding in derived classes. |
| 194 | """ |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 195 | return common_util.GatherArtifactDownloads( |
| 196 | main_staging_dir, archive_url, build_dir, short_build) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 197 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 198 | def GetStatusOfBackgroundDownloads(self): |
| 199 | """Returns the status of the background downloads. |
| 200 | |
| 201 | This commands returns the status of the background downloads and blocks |
| 202 | until a status is returned. |
| 203 | """ |
| 204 | status = self._status_queue.get() |
| 205 | # In case anyone else is calling. |
| 206 | self._status_queue.put(status) |
Alex Miller | 92ed359 | 2012-08-15 16:27:46 -0700 | [diff] [blame] | 207 | # If someone is curious about the status of a build, then we should |
| 208 | # probably keep it around for a bit longer. |
| 209 | if os.path.exists(self._staging_dir): |
| 210 | Downloader._TouchTimestampForStaged(self._staging_dir) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 211 | # It's possible we received an exception, if so, re-raise it here. |
| 212 | if isinstance(status, Exception): |
| 213 | raise status |
| 214 | |
| 215 | return status |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 216 | |
| 217 | |
| 218 | class SymbolDownloader(Downloader): |
| 219 | """Download and stage debug symbols for a build on the devsever. |
| 220 | |
| 221 | Given a URL to a build on the archive server: |
| 222 | |
| 223 | - Determine if the build already exists. |
| 224 | - Download and extract the debug symbols to a staging directory. |
| 225 | - Install symbols to static dir. |
| 226 | """ |
| 227 | |
| 228 | _DONE_FLAG = 'done' |
| 229 | |
| 230 | @staticmethod |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 231 | def GenerateLockTag(rel_path, short_build): |
| 232 | return '/'.join([rel_path, short_build, 'symbols']) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 233 | |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 234 | def Download(self, archive_url, _background=False): |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 235 | """Downloads debug symbols for the build defined by the |archive_url|. |
| 236 | |
| 237 | The symbols will be downloaded synchronously |
| 238 | """ |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 239 | # Parse archive_url into rel_path (contains the build target) and |
| 240 | # short_build. |
| 241 | # e.g. gs://chromeos-image-archive/{rel_path}/{short_build} |
| 242 | rel_path, short_build = self.ParseUrl(archive_url) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 243 | |
| 244 | # Bind build_dir and staging_dir here so we can tell if we need to do any |
| 245 | # cleanup after an exception occurs before build_dir is set. |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 246 | self._lock_tag = self.GenerateLockTag(rel_path, short_build) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 247 | if self.SymbolsStaged(archive_url, self._static_dir): |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 248 | self._Log('Symbols for build %s have already been staged.' % |
| 249 | self._lock_tag) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 250 | return 'Success' |
| 251 | |
| 252 | try: |
| 253 | # Create Dev Server directory for this build and tell other Downloader |
| 254 | # instances we have processed this build. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 255 | self._build_dir = common_util.AcquireLock( |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 256 | static_dir=self._static_dir, tag=self._lock_tag) |
| 257 | |
Yu-Ju Hong | 1a83a71 | 2012-06-27 09:11:34 -0700 | [diff] [blame] | 258 | # Replace '/' with '_' in rel_path because it may contain multiple levels |
| 259 | # which would not be qualified as part of the suffix. |
| 260 | self._staging_dir = tempfile.mkdtemp(suffix='_'.join( |
Chris Sosa | f097564 | 2012-06-29 13:53:42 -0700 | [diff] [blame] | 261 | [rel_path.replace('/', '_'), short_build])) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 262 | self._Log('Downloading debug symbols from %s' % archive_url) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 263 | |
| 264 | [symbol_artifact] = self.GatherArtifactDownloads( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 265 | self._staging_dir, archive_url, self._static_dir) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 266 | symbol_artifact.Download() |
| 267 | symbol_artifact.Stage() |
Chris Masone | a22d938 | 2012-05-18 12:38:51 -0700 | [diff] [blame] | 268 | self.MarkSymbolsStaged() |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 269 | |
| 270 | except Exception: |
| 271 | # Release processing "lock", which will indicate to future runs that we |
| 272 | # did not succeed, and so they should try again. |
| 273 | if self._build_dir: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 274 | common_util.ReleaseLock(static_dir=self._static_dir, tag=self._lock_tag, |
| 275 | destroy=True) |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 276 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 277 | raise |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 278 | else: |
| 279 | # Release processing "lock", keeping directory intact. |
| 280 | if self._build_dir: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 281 | common_util.ReleaseLock(static_dir=self._static_dir, tag=self._lock_tag) |
Chris Masone | a22d938 | 2012-05-18 12:38:51 -0700 | [diff] [blame] | 282 | finally: |
| 283 | self._Cleanup() |
Chris Sosa | 4d9c4d4 | 2012-06-29 15:23:23 -0700 | [diff] [blame] | 284 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 285 | return 'Success' |
| 286 | |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 287 | def GatherArtifactDownloads(self, temp_download_dir, archive_url, static_dir, |
| 288 | short_build=None): |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 289 | """Call SymbolDownloader-appropriate artifact gathering method. |
| 290 | |
| 291 | @param temp_download_dir: the tempdir into which we're downloading artifacts |
| 292 | prior to staging them. |
| 293 | @param archive_url: the google storage url of the bucket where the debug |
| 294 | symbols for the desired build are stored. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 295 | @param staging_dir: the dir into which to stage the symbols |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 296 | @param short_build: (ignored) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 297 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 298 | @return an iterable of one DebugTarballBuildArtifact pointing to the right |
| 299 | debug symbols. This is an iterable so that it's similar to |
| 300 | GatherArtifactDownloads. Also, it's possible that someday we might |
| 301 | have more than one. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 302 | """ |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 303 | return common_util.GatherSymbolArtifactDownloads( |
| 304 | temp_download_dir, archive_url, static_dir) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 305 | |
| 306 | def MarkSymbolsStaged(self): |
| 307 | """Puts a flag file on disk to signal that symbols are staged.""" |
| 308 | with open(os.path.join(self._build_dir, self._DONE_FLAG), 'w') as flag: |
| 309 | flag.write(self._DONE_FLAG) |
| 310 | |
| 311 | def SymbolsStaged(self, archive_url, static_dir): |
| 312 | """Returns True if the build is already staged.""" |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 313 | rel_path, short_build = self.ParseUrl(archive_url) |
| 314 | sub_directory = self.GenerateLockTag(rel_path, short_build) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 315 | return os.path.isfile(os.path.join(static_dir, |
| 316 | sub_directory, |
| 317 | self._DONE_FLAG)) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 318 | |
| 319 | |
| 320 | class ImagesDownloader(Downloader): |
| 321 | """Download and stage prebuilt images for a given build. |
| 322 | |
| 323 | Given a URL to a build on the archive server and a list of images: |
| 324 | - Determine which images have not been staged yet. |
| 325 | - Download the image archive. |
| 326 | - Extract missing images to the staging directory. |
| 327 | |
| 328 | """ |
| 329 | _DONE_FLAG = 'staged' |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 330 | |
| 331 | # List of images to be staged; empty (default) means all. |
| 332 | _image_list = [] |
| 333 | |
| 334 | # A mapping from test image types to their archived file names. |
| 335 | _IMAGE_TO_FNAME = { |
| 336 | 'test': 'chromiumos_test_image.bin', |
| 337 | 'base': 'chromiumos_base_image.bin', |
| 338 | 'recovery': 'recovery_image.bin', |
| 339 | } |
| 340 | |
| 341 | @staticmethod |
| 342 | def GenerateLockTag(rel_path, short_build): |
| 343 | return os.path.join('images', rel_path, short_build) |
| 344 | |
| 345 | def Download(self, archive_url, image_list, _background=False): |
| 346 | """Downloads images in |image_list| from the build defined by |archive_url|. |
| 347 | |
| 348 | Download happens synchronously. |images| may include any of those in |
| 349 | self._IMAGE_TO_FNAME.keys(). |
| 350 | |
| 351 | """ |
| 352 | # Check correctness of image list, remove duplicates. |
| 353 | if not image_list: |
| 354 | raise DevServerError('empty list of image types') |
| 355 | invalid_images = list(set(image_list) - set(self._IMAGE_TO_FNAME.keys())) |
| 356 | if invalid_images: |
| 357 | raise DevServerError('invalid images requested: %s' % invalid_images) |
| 358 | image_list = list(set(image_list)) |
| 359 | |
| 360 | # Parse archive_url into rel_path (contains the build target) and |
| 361 | # short_build. |
| 362 | # e.g. gs://chromeos-image-archive/{rel_path}/{short_build} |
| 363 | rel_path, short_build = self.ParseUrl(archive_url) |
| 364 | |
| 365 | # Bind build_dir and staging_dir here so we can tell if we need to do any |
| 366 | # cleanup after an exception occurs before build_dir is set. |
| 367 | self._lock_tag = self.GenerateLockTag(rel_path, short_build) |
| 368 | staged_image_list = self._CheckStagedImages(archive_url, self._static_dir) |
| 369 | unstaged_image_list = [image for image in image_list |
| 370 | if image not in staged_image_list] |
| 371 | if not unstaged_image_list: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 372 | self._Log( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 373 | 'All requested images (%s) for build %s have already been staged.' % |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 374 | (common_util.CommaSeparatedList(image_list, is_quoted=True) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 375 | if image_list else 'none', |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 376 | self._lock_tag)) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 377 | return 'Success' |
| 378 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 379 | self._Log( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 380 | 'Image(s) %s for build %s will be staged' % |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 381 | (common_util.CommaSeparatedList(unstaged_image_list, is_quoted=True), |
| 382 | self._lock_tag)) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 383 | self._image_list = unstaged_image_list |
| 384 | |
| 385 | try: |
| 386 | # Create a static target directory and lock it for processing. We permit |
| 387 | # the directory to preexist, as different images might be downloaded and |
| 388 | # extracted at different times. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 389 | self._build_dir = common_util.AcquireLock( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 390 | static_dir=self._static_dir, tag=self._lock_tag, |
| 391 | create_once=False) |
| 392 | |
| 393 | # Replace '/' with '_' in rel_path because it may contain multiple levels |
| 394 | # which would not be qualified as part of the suffix. |
| 395 | self._staging_dir = tempfile.mkdtemp(suffix='_'.join( |
| 396 | [rel_path.replace('/', '_'), short_build])) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 397 | self._Log('Downloading image archive from %s' % archive_url) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 398 | dest_static_dir = os.path.join(self._static_dir, self._lock_tag) |
| 399 | [image_archive_artifact] = self.GatherArtifactDownloads( |
| 400 | self._staging_dir, archive_url, dest_static_dir) |
| 401 | image_archive_artifact.Download() |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 402 | self._Log('Staging images to %s' % dest_static_dir) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 403 | image_archive_artifact.Stage() |
| 404 | self._MarkStagedImages(unstaged_image_list) |
| 405 | |
| 406 | except Exception: |
| 407 | # Release processing "lock", which will indicate to future runs that we |
| 408 | # did not succeed, and so they should try again. |
| 409 | if self._build_dir: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 410 | common_util.ReleaseLock(static_dir=self._static_dir, tag=self._lock_tag, |
| 411 | destroy=True) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 412 | raise |
| 413 | else: |
| 414 | # Release processing "lock", keeping directory intact. |
| 415 | if self._build_dir: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 416 | common_util.ReleaseLock(static_dir=self._static_dir, tag=self._lock_tag) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 417 | finally: |
| 418 | self._Cleanup() |
| 419 | |
| 420 | return 'Success' |
| 421 | |
| 422 | def GatherArtifactDownloads(self, temp_download_dir, archive_url, static_dir, |
| 423 | short_build=None): |
| 424 | """Call appropriate artifact gathering method. |
| 425 | |
| 426 | Args: |
| 427 | temp_download_dir: temporary directory for downloading artifacts to |
| 428 | archive_url: URI to the bucket where image archive is stored |
| 429 | staging_dir: directory into which to stage extracted images |
| 430 | short_build: (ignored) |
| 431 | Returns: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 432 | list of downloadable artifacts (of type ZipfileBuildArtifact), currently |
| 433 | containing a single object, configured for extracting a predetermined |
| 434 | list of images |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 435 | """ |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame^] | 436 | return common_util.GatherImageArchiveArtifactDownloads( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 437 | temp_download_dir, archive_url, static_dir, |
| 438 | [self._IMAGE_TO_FNAME[image] for image in self._image_list]) |
| 439 | |
| 440 | def _MarkStagedImages(self, image_list): |
| 441 | """Update the on-disk flag file with the list of newly staged images. |
| 442 | |
| 443 | This does not check for duplicates against already listed images, and will |
| 444 | add any listed images regardless. |
| 445 | |
| 446 | """ |
| 447 | flag_fname = os.path.join(self._build_dir, self._DONE_FLAG) |
| 448 | with open(flag_fname, 'a') as flag_file: |
| 449 | flag_file.writelines([image + '\n' for image in image_list]) |
| 450 | |
| 451 | def _CheckStagedImages(self, archive_url, static_dir): |
| 452 | """Returns a list of images that were already staged. |
| 453 | |
| 454 | Reads the list of images from a flag file, if one is present, and returns |
| 455 | after removing duplicates. |
| 456 | |
| 457 | """ |
| 458 | rel_path, short_build = self.ParseUrl(archive_url) |
| 459 | sub_directory = self.GenerateLockTag(rel_path, short_build) |
| 460 | flag_fname = os.path.join(static_dir, sub_directory, self._DONE_FLAG) |
| 461 | staged_image_list = [] |
| 462 | # TODO(garnold) make this code immune to race conditions, probably by |
| 463 | # acquiring a lock around the file access code. |
| 464 | if os.path.isfile(flag_fname): |
| 465 | with open(flag_fname) as flag_file: |
| 466 | staged_image_list = [image.strip() for image in flag_file.readlines()] |
| 467 | return list(set(staged_image_list)) |