Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 |
| 8 | import shutil |
| 9 | import tempfile |
| 10 | |
| 11 | import devserver_util |
| 12 | |
| 13 | |
| 14 | class Downloader(object): |
| 15 | """Download images to the devsever. |
| 16 | |
| 17 | Given a URL to a build on the archive server: |
| 18 | |
| 19 | - Determine if the build already exists. |
| 20 | - Download and extract the build to a staging directory. |
| 21 | - Package autotest tests. |
| 22 | - Install components to static dir. |
| 23 | """ |
| 24 | |
| 25 | def __init__(self, static_dir): |
| 26 | self._static_dir = static_dir |
| 27 | |
| 28 | def Download(self, archive_url): |
| 29 | # Parse archive_url into board and build. |
| 30 | # e.g. gs://chromeos-image-archive/{board}/{build} |
| 31 | archive_url = archive_url.strip('/') |
| 32 | board, build = archive_url.rsplit('/', 2)[-2:] |
| 33 | |
| 34 | # Bind build_dir and staging_dir here so we can tell if we need to do any |
| 35 | # cleanup after an exception occurs before build_dir is set. |
| 36 | build_dir = staging_dir = None |
| 37 | lock_tag = '/'.join([board, build]) |
| 38 | try: |
| 39 | # Create Dev Server directory for this build and tell other Downloader |
| 40 | # instances we have processed this build. |
| 41 | try: |
| 42 | build_dir = devserver_util.AcquireLock(static_dir=self._static_dir, |
| 43 | tag=lock_tag) |
| 44 | except devserver_util.DevServerUtilError, e: |
| 45 | cherrypy.log('Refused lock "%s". Assuming build has already been' |
| 46 | 'processed: %s' % (lock_tag, str(e)), 'DOWNLOAD') |
| 47 | return 'Success' |
| 48 | |
| 49 | cherrypy.log('Downloading build from %s' % archive_url, 'DOWNLOAD') |
Frank Farzan | bcb571e | 2012-01-03 11:48:17 -0800 | [diff] [blame^] | 50 | staging_dir = tempfile.mkdtemp(suffix='_'.join([board, build])) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 51 | devserver_util.DownloadBuildFromGS( |
| 52 | staging_dir=staging_dir, archive_url=archive_url, build=build) |
| 53 | |
| 54 | cherrypy.log('Packaging autotest tests.', 'DOWNLOAD') |
| 55 | devserver_util.PrepareAutotestPkgs(staging_dir) |
| 56 | |
| 57 | cherrypy.log('Installing build components.', 'DOWNLOAD') |
| 58 | devserver_util.InstallBuild( |
| 59 | staging_dir=staging_dir, build_dir=build_dir) |
| 60 | except Exception: |
| 61 | # Release processing lock, which will remove build components directory |
| 62 | # so future runs can retry. |
| 63 | if build_dir: |
| 64 | devserver_util.ReleaseLock(static_dir=self._static_dir, tag=lock_tag) |
| 65 | raise |
| 66 | finally: |
| 67 | # Always cleanup after ourselves. |
| 68 | if staging_dir: |
| 69 | cherrypy.log('Cleaning up staging directory %s' % staging_dir, |
| 70 | 'DOWNLOAD') |
| 71 | shutil.rmtree(staging_dir) |
| 72 | |
| 73 | return 'Success' |