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