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