Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 1 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 5 | import os |
Gilad Arnold | 0b8c3f3 | 2012-09-19 14:35:44 -0700 | [diff] [blame] | 6 | import threading |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 7 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 8 | import build_artifact |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 9 | import common_util |
| 10 | import log_util |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 11 | |
| 12 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 13 | class Downloader(log_util.Loggable): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 14 | """Downloader of images to the devsever. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 15 | |
| 16 | Given a URL to a build on the archive server: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 17 | - Caches that build and the given artifacts onto the devserver. |
| 18 | - May also initiate caching of related artifacts in the background. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 19 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 20 | Private class members: |
| 21 | archive_url: a URL where to download build artifacts from. |
| 22 | static_dir: local filesystem directory to store all artifacts. |
| 23 | build_dir: the local filesystem directory to store artifacts for the given |
| 24 | build defined by the archive_url. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 25 | """ |
| 26 | |
Alex Miller | a44d502 | 2012-07-27 11:34:16 -0700 | [diff] [blame] | 27 | # This filename must be kept in sync with clean_staged_images.py |
| 28 | _TIMESTAMP_FILENAME = 'staged.timestamp' |
Chris Masone | a22d938 | 2012-05-18 12:38:51 -0700 | [diff] [blame] | 29 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 30 | def __init__(self, static_dir, archive_url): |
| 31 | super(Downloader, self).__init__() |
| 32 | self._archive_url = archive_url |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 33 | self._static_dir = static_dir |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 34 | self._build_dir = Downloader.GetBuildDir(static_dir, archive_url) |
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): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 38 | """Parses archive_url into rel_path and build. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 39 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 40 | Parses archive_url into rel_path and build e.g. |
| 41 | gs://chromeos-image-archive/{rel_path}/{build}. |
| 42 | |
| 43 | Args: |
| 44 | archive_url: a URL at which build artifacts are archived. |
| 45 | |
| 46 | Returns: |
| 47 | A tuple of (build relative path, short build name) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 48 | """ |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 49 | # The archive_url is of the form gs://server/[some_path/target]/...]/build |
| 50 | # This function discards 'gs://server/' and extracts the [some_path/target] |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 51 | # as rel_path and the build as build. |
Yu-Ju Hong | d49d7f4 | 2012-06-25 12:23:11 -0700 | [diff] [blame] | 52 | sub_url = archive_url.partition('://')[2] |
| 53 | split_sub_url = sub_url.split('/') |
| 54 | rel_path = '/'.join(split_sub_url[1:-1]) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 55 | build = split_sub_url[-1] |
| 56 | return rel_path, build |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 57 | |
| 58 | @staticmethod |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 59 | def GetBuildDir(static_dir, archive_url): |
| 60 | """Returns the path to where the artifacts will be staged. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 61 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 62 | Args: |
| 63 | static_dir: The base static dir that will be used. |
| 64 | archive_url: The gs path to the archive url. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 65 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 66 | # Parse archive_url into rel_path (contains the build target) and |
| 67 | # build e.g. gs://chromeos-image-archive/{rel_path}/{build}. |
| 68 | rel_path, build = Downloader.ParseUrl(archive_url) |
| 69 | return os.path.join(static_dir, rel_path, build) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 70 | |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 71 | @staticmethod |
Alex Miller | a44d502 | 2012-07-27 11:34:16 -0700 | [diff] [blame] | 72 | def _TouchTimestampForStaged(directory_path): |
| 73 | file_name = os.path.join(directory_path, Downloader._TIMESTAMP_FILENAME) |
| 74 | # Easiest python version of |touch file_name| |
| 75 | with file(file_name, 'a'): |
| 76 | os.utime(file_name, None) |
| 77 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 78 | def Download(self, artifacts): |
| 79 | """Downloads and caches the |artifacts|. |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 80 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 81 | Downloads and caches the |artifacts|. Returns once these |
| 82 | are present on the devserver. A call to this will attempt to cache |
| 83 | non-specified artifacts in the background following the principle of |
| 84 | spatial locality. |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 85 | |
| 86 | Args: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 87 | artifacts: a list of artifact names that correspond to artifacts to stage. |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 88 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 89 | common_util.MkDirP(self._build_dir) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 90 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 91 | # We are doing some work on this build -- let's touch it to indicate that |
| 92 | # we shouldn't be cleaning it up anytime soon. |
| 93 | Downloader._TouchTimestampForStaged(self._build_dir) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 94 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 95 | # Create factory to create build_artifacts from artifact names. |
| 96 | build = self.ParseUrl(self._archive_url)[1] |
| 97 | factory = build_artifact.ArtifactFactory(self._build_dir, self._archive_url, |
| 98 | artifacts, build) |
| 99 | background_artifacts = factory.OptionalArtifacts() |
| 100 | if background_artifacts: |
| 101 | self._DownloadArtifactsInBackground(background_artifacts) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 102 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 103 | required_artifacts = factory.RequiredArtifacts() |
| 104 | str_repr = [str(a) for a in required_artifacts] |
| 105 | self._Log('Downloading artifacts %s.', ' '.join(str_repr)) |
| 106 | self._DownloadArtifactsSerially(required_artifacts, no_wait=True) |
| 107 | |
| 108 | def _DownloadArtifactsSerially(self, artifacts, no_wait): |
| 109 | """Simple function to download all the given artifacts serially. |
| 110 | |
| 111 | Args: |
| 112 | artifacts: List of build_artifact.BuildArtifact instances to download. |
| 113 | no_wait: If True, don't block waiting for artifact to exist if we fail to |
| 114 | immediately find it. |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 115 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 116 | for artifact in artifacts: |
| 117 | artifact.Process(no_wait) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 118 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 119 | def _DownloadArtifactsInBackground(self, artifacts): |
| 120 | """Downloads |artifacts| in the background. |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 121 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 122 | Downloads |artifacts| in the background. As these are backgrounded |
| 123 | artifacts, they are done best effort and may not exist. |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 124 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 125 | Args: |
| 126 | artifacts: List of build_artifact.BuildArtifact instances to download. |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 127 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame^] | 128 | self._Log('Invoking background download of artifacts for %r', artifacts) |
| 129 | thread = threading.Thread(target=self._DownloadArtifactsSerially, |
| 130 | args=(artifacts, False)) |
| 131 | thread.start() |