Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [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 | |
| 7 | import cherrypy |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 8 | import multiprocessing |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 9 | import os |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 10 | import shutil |
| 11 | import tempfile |
| 12 | |
| 13 | import devserver_util |
| 14 | |
| 15 | |
| 16 | class Downloader(object): |
| 17 | """Download images to the devsever. |
| 18 | |
| 19 | Given a URL to a build on the archive server: |
| 20 | |
| 21 | - Determine if the build already exists. |
| 22 | - Download and extract the build to a staging directory. |
| 23 | - Package autotest tests. |
| 24 | - Install components to static dir. |
| 25 | """ |
| 26 | |
Chris Masone | a22d938 | 2012-05-18 19:38:51 | [diff] [blame] | 27 | _LOG_TAG = 'DOWNLOAD' |
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 | |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 31 | def __init__(self, static_dir): |
| 32 | self._static_dir = static_dir |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 33 | self._build_dir = None |
| 34 | self._staging_dir = None |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 35 | self._status_queue = multiprocessing.Queue(maxsize=1) |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 36 | self._lock_tag = None |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 37 | |
| 38 | @staticmethod |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 39 | def ParseUrl(archive_url): |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 40 | """Parse archive_url into rel_path and short_build |
| 41 | e.g. gs://chromeos-image-archive/{rel_path}/{short_build} |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 42 | |
| 43 | @param archive_url: a URL at which build artifacts are archived. |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 44 | @return a tuple of (build relative path, short build name) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 45 | """ |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 46 | # The archive_url is of the form gs://server/[some_path/target]/...]/build |
| 47 | # This function discards 'gs://server/' and extracts the [some_path/target] |
| 48 | # as rel_path and the build as short_build. |
| 49 | sub_url = archive_url.partition('://')[2] |
| 50 | split_sub_url = sub_url.split('/') |
| 51 | rel_path = '/'.join(split_sub_url[1:-1]) |
| 52 | short_build = split_sub_url[-1] |
| 53 | return rel_path, short_build |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 54 | |
| 55 | @staticmethod |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 56 | def GenerateLockTag(rel_path, short_build): |
| 57 | """Generate a name for a lock scoped to this rel_path/build pair. |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 58 | |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 59 | @param rel_path: the relative path for the build. |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 60 | @param short_build: short build name |
| 61 | @return a name to use with AcquireLock that will scope the lock. |
| 62 | """ |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 63 | return '/'.join([rel_path, short_build]) |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 64 | |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 65 | @staticmethod |
Alex Miller | a44d502 | 2012-07-27 18:34:16 | [diff] [blame] | 66 | def _TouchTimestampForStaged(directory_path): |
| 67 | file_name = os.path.join(directory_path, Downloader._TIMESTAMP_FILENAME) |
| 68 | # Easiest python version of |touch file_name| |
| 69 | with file(file_name, 'a'): |
| 70 | os.utime(file_name, None) |
| 71 | |
| 72 | @staticmethod |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 73 | def BuildStaged(archive_url, static_dir): |
| 74 | """Returns True if the build is already staged.""" |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 75 | rel_path, short_build = Downloader.ParseUrl(archive_url) |
| 76 | sub_directory = Downloader.GenerateLockTag(rel_path, short_build) |
Alex Miller | a44d502 | 2012-07-27 18:34:16 | [diff] [blame] | 77 | directory_path = os.path.join(static_dir, sub_directory) |
| 78 | exists = os.path.isdir(directory_path) |
| 79 | # If the build exists, then touch the timestamp to tell |
| 80 | # clean_stages_images.py that we're using this build. |
| 81 | if exists: |
| 82 | Downloader._TouchTimestampForStaged(directory_path) |
| 83 | return exists |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 84 | |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 85 | def Download(self, archive_url, background=False): |
| 86 | """Downloads the given build artifacts defined by the |archive_url|. |
| 87 | |
| 88 | If background is set to True, will return back early before all artifacts |
| 89 | have been downloaded. The artifacts that can be backgrounded are all those |
| 90 | that are not set as synchronous. |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 91 | |
| 92 | TODO: refactor this into a common Download method, once unit tests are |
| 93 | fixed up to make iterating on the code easier. |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 94 | """ |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 95 | # Parse archive_url into rel_path (contains the build target) and |
| 96 | # short_build. |
| 97 | # e.g. gs://chromeos-image-archive/{rel_path}/{short_build} |
| 98 | rel_path, short_build = self.ParseUrl(archive_url) |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 99 | # This should never happen. The Devserver should only try to call this |
| 100 | # method if no previous downloads have been staged for this archive_url. |
| 101 | assert not Downloader.BuildStaged(archive_url, self._static_dir) |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 102 | # Bind build_dir and staging_dir here so we can tell if we need to do any |
| 103 | # cleanup after an exception occurs before build_dir is set. |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 104 | self._lock_tag = self.GenerateLockTag(rel_path, short_build) |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 105 | try: |
| 106 | # Create Dev Server directory for this build and tell other Downloader |
| 107 | # instances we have processed this build. |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 108 | self._build_dir = devserver_util.AcquireLock( |
| 109 | static_dir=self._static_dir, tag=self._lock_tag) |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 110 | |
Yu-Ju Hong | 1a83a71 | 2012-06-27 16:11:34 | [diff] [blame] | 111 | # Replace '/' with '_' in rel_path because it may contain multiple levels |
| 112 | # which would not be qualified as part of the suffix. |
| 113 | self._staging_dir = tempfile.mkdtemp(suffix='_'.join( |
Chris Sosa | f097564 | 2012-06-29 20:53:42 | [diff] [blame] | 114 | [rel_path.replace('/', '_'), short_build])) |
Alex Miller | a44d502 | 2012-07-27 18:34:16 | [diff] [blame] | 115 | Downloader._TouchTimestampForStaged(self._staging_dir) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 116 | cherrypy.log('Gathering download requirements %s' % archive_url, |
Chris Masone | a22d938 | 2012-05-18 19:38:51 | [diff] [blame] | 117 | self._LOG_TAG) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 118 | artifacts = self.GatherArtifactDownloads( |
| 119 | self._staging_dir, archive_url, short_build, self._build_dir) |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 120 | devserver_util.PrepareBuildDirectory(self._build_dir) |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 121 | |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 122 | cherrypy.log('Downloading foreground artifacts from %s' % archive_url, |
Chris Masone | a22d938 | 2012-05-18 19:38:51 | [diff] [blame] | 123 | self._LOG_TAG) |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 124 | background_artifacts = [] |
| 125 | for artifact in artifacts: |
| 126 | if artifact.Synchronous(): |
| 127 | artifact.Download() |
| 128 | artifact.Stage() |
| 129 | else: |
| 130 | background_artifacts.append(artifact) |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 131 | |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 132 | if background: |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 133 | self._DownloadArtifactsInBackground(background_artifacts) |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 134 | else: |
| 135 | self._DownloadArtifactsSerially(background_artifacts) |
| 136 | |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 137 | except Exception, e: |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 138 | # Release processing lock, which will remove build components directory |
| 139 | # so future runs can retry. |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 140 | if self._build_dir: |
| 141 | devserver_util.ReleaseLock(static_dir=self._static_dir, |
| 142 | tag=self._lock_tag) |
| 143 | |
| 144 | self._status_queue.put(e) |
| 145 | self._Cleanup() |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 146 | raise |
Frank Farzan | 37761d1 | 2011-12-01 22:29:08 | [diff] [blame] | 147 | |
| 148 | return 'Success' |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 149 | |
| 150 | def _Cleanup(self): |
| 151 | """Cleans up the staging dir for this downloader instanfce.""" |
| 152 | if self._staging_dir: |
| 153 | cherrypy.log('Cleaning up staging directory %s' % self._staging_dir, |
Chris Masone | a22d938 | 2012-05-18 19:38:51 | [diff] [blame] | 154 | self._LOG_TAG) |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [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.""" |
Chris Masone | a22d938 | 2012-05-18 19:38:51 | [diff] [blame] | 161 | cherrypy.log('Downloading background artifacts serially.', self._LOG_TAG) |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [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: |
| 172 | devserver_util.ReleaseLock(static_dir=self._static_dir, |
| 173 | tag=self._lock_tag) |
| 174 | else: |
| 175 | self._status_queue.put('Success') |
| 176 | finally: |
| 177 | self._Cleanup() |
| 178 | |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 179 | def _DownloadArtifactsInBackground(self, artifacts): |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 180 | """Downloads |artifacts| in the background and signals when complete.""" |
| 181 | proc = multiprocessing.Process(target=self._DownloadArtifactsSerially, |
| 182 | args=(artifacts,)) |
Chris Sosa | b65973e | 2012-03-30 01:31:02 | [diff] [blame] | 183 | proc.start() |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 184 | |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 185 | def GatherArtifactDownloads(self, main_staging_dir, archive_url, short_build, |
| 186 | build_dir): |
| 187 | """Wrapper around devserver_util.GatherArtifactDownloads(). |
| 188 | |
| 189 | The wrapper allows mocking and overriding in derived classes. |
| 190 | """ |
| 191 | return devserver_util.GatherArtifactDownloads(main_staging_dir, archive_url, |
| 192 | short_build, build_dir) |
| 193 | |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 194 | def GetStatusOfBackgroundDownloads(self): |
| 195 | """Returns the status of the background downloads. |
| 196 | |
| 197 | This commands returns the status of the background downloads and blocks |
| 198 | until a status is returned. |
| 199 | """ |
| 200 | status = self._status_queue.get() |
| 201 | # In case anyone else is calling. |
| 202 | self._status_queue.put(status) |
| 203 | # It's possible we received an exception, if so, re-raise it here. |
| 204 | if isinstance(status, Exception): |
| 205 | raise status |
| 206 | |
| 207 | return status |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 208 | |
| 209 | |
| 210 | class SymbolDownloader(Downloader): |
| 211 | """Download and stage debug symbols for a build on the devsever. |
| 212 | |
| 213 | Given a URL to a build on the archive server: |
| 214 | |
| 215 | - Determine if the build already exists. |
| 216 | - Download and extract the debug symbols to a staging directory. |
| 217 | - Install symbols to static dir. |
| 218 | """ |
| 219 | |
| 220 | _DONE_FLAG = 'done' |
Chris Masone | a22d938 | 2012-05-18 19:38:51 | [diff] [blame] | 221 | _LOG_TAG = 'SYMBOL_DOWNLOAD' |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 222 | |
| 223 | @staticmethod |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 224 | def GenerateLockTag(rel_path, short_build): |
| 225 | return '/'.join([rel_path, short_build, 'symbols']) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 226 | |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 227 | def Download(self, archive_url, _background=False): |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 228 | """Downloads debug symbols for the build defined by the |archive_url|. |
| 229 | |
| 230 | The symbols will be downloaded synchronously |
| 231 | """ |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 232 | # Parse archive_url into rel_path (contains the build target) and |
| 233 | # short_build. |
| 234 | # e.g. gs://chromeos-image-archive/{rel_path}/{short_build} |
| 235 | rel_path, short_build = self.ParseUrl(archive_url) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 236 | |
| 237 | # Bind build_dir and staging_dir here so we can tell if we need to do any |
| 238 | # cleanup after an exception occurs before build_dir is set. |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 239 | self._lock_tag = self.GenerateLockTag(rel_path, short_build) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 240 | if self.SymbolsStaged(archive_url, self._static_dir): |
| 241 | cherrypy.log( |
| 242 | 'Symbols for build %s have already been staged.' % self._lock_tag, |
Chris Masone | a22d938 | 2012-05-18 19:38:51 | [diff] [blame] | 243 | self._LOG_TAG) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 244 | return 'Success' |
| 245 | |
| 246 | try: |
| 247 | # Create Dev Server directory for this build and tell other Downloader |
| 248 | # instances we have processed this build. |
| 249 | self._build_dir = devserver_util.AcquireLock( |
| 250 | static_dir=self._static_dir, tag=self._lock_tag) |
| 251 | |
Yu-Ju Hong | 1a83a71 | 2012-06-27 16:11:34 | [diff] [blame] | 252 | # Replace '/' with '_' in rel_path because it may contain multiple levels |
| 253 | # which would not be qualified as part of the suffix. |
| 254 | self._staging_dir = tempfile.mkdtemp(suffix='_'.join( |
Chris Sosa | f097564 | 2012-06-29 20:53:42 | [diff] [blame] | 255 | [rel_path.replace('/', '_'), short_build])) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 256 | cherrypy.log('Downloading debug symbols from %s' % archive_url, |
Chris Masone | a22d938 | 2012-05-18 19:38:51 | [diff] [blame] | 257 | self._LOG_TAG) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 258 | |
| 259 | [symbol_artifact] = self.GatherArtifactDownloads( |
| 260 | self._staging_dir, archive_url, '', self._static_dir) |
| 261 | symbol_artifact.Download() |
| 262 | symbol_artifact.Stage() |
Chris Masone | a22d938 | 2012-05-18 19:38:51 | [diff] [blame] | 263 | self.MarkSymbolsStaged() |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 264 | |
| 265 | except Exception: |
| 266 | # Release processing "lock", which will indicate to future runs that we |
| 267 | # did not succeed, and so they should try again. |
| 268 | if self._build_dir: |
| 269 | devserver_util.ReleaseLock(static_dir=self._static_dir, |
| 270 | tag=self._lock_tag) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 271 | raise |
Chris Masone | a22d938 | 2012-05-18 19:38:51 | [diff] [blame] | 272 | finally: |
| 273 | self._Cleanup() |
Chris Sosa | 4d9c4d4 | 2012-06-29 22:23:23 | [diff] [blame] | 274 | |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 275 | return 'Success' |
| 276 | |
| 277 | def GatherArtifactDownloads(self, temp_download_dir, archive_url, short_build, |
| 278 | static_dir): |
| 279 | """Call SymbolDownloader-appropriate artifact gathering method. |
| 280 | |
| 281 | @param temp_download_dir: the tempdir into which we're downloading artifacts |
| 282 | prior to staging them. |
| 283 | @param archive_url: the google storage url of the bucket where the debug |
| 284 | symbols for the desired build are stored. |
| 285 | @param short_build: IGNORED |
| 286 | @param staging_dir: the dir into which to stage the symbols |
| 287 | |
| 288 | @return an iterable of one DebugTarball pointing to the right debug symbols. |
| 289 | This is an iterable so that it's similar to GatherArtifactDownloads. |
| 290 | Also, it's possible that someday we might have more than one. |
| 291 | """ |
| 292 | return devserver_util.GatherSymbolArtifactDownloads(temp_download_dir, |
| 293 | archive_url, |
| 294 | static_dir) |
| 295 | |
| 296 | def MarkSymbolsStaged(self): |
| 297 | """Puts a flag file on disk to signal that symbols are staged.""" |
| 298 | with open(os.path.join(self._build_dir, self._DONE_FLAG), 'w') as flag: |
| 299 | flag.write(self._DONE_FLAG) |
| 300 | |
| 301 | def SymbolsStaged(self, archive_url, static_dir): |
| 302 | """Returns True if the build is already staged.""" |
Yu-Ju Hong | d49d7f4 | 2012-06-25 19:23:11 | [diff] [blame] | 303 | rel_path, short_build = self.ParseUrl(archive_url) |
| 304 | sub_directory = self.GenerateLockTag(rel_path, short_build) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 305 | return os.path.isfile(os.path.join(static_dir, |
| 306 | sub_directory, |
| 307 | self._DONE_FLAG)) |