Darin Petkov | c3fd90c | 2011-05-11 14:23:00 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [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 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 5 | import json |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 6 | import os |
Chris Sosa | 05491b1 | 2010-11-08 17:14:16 -0800 | [diff] [blame] | 7 | import subprocess |
Gilad Arnold | e74b381 | 2013-04-22 11:27:38 -0700 | [diff] [blame] | 8 | import sys |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 9 | import time |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 10 | import urllib2 |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 11 | import urlparse |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 12 | |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 13 | import cherrypy |
| 14 | |
Gilad Arnold | e74b381 | 2013-04-22 11:27:38 -0700 | [diff] [blame] | 15 | # Allow importing from dev/host/lib when running from source tree. |
| 16 | lib_dir = os.path.join(os.path.dirname(__file__), 'host', 'lib') |
| 17 | if os.path.exists(lib_dir) and os.path.isdir(lib_dir): |
| 18 | sys.path.insert(1, lib_dir) |
| 19 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 20 | import build_util |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 21 | import autoupdate_lib |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 22 | import common_util |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 23 | import devserver_constants as constants |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 24 | import log_util |
Gilad Arnold | e74b381 | 2013-04-22 11:27:38 -0700 | [diff] [blame] | 25 | # pylint: disable=F0401 |
| 26 | import update_payload |
Chris Sosa | 05491b1 | 2010-11-08 17:14:16 -0800 | [diff] [blame] | 27 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 28 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 29 | # If used by client in place of an pre-update version string, forces an update |
| 30 | # to the client regardless of the relative versions of the payload and client. |
| 31 | FORCED_UPDATE = 'ForcedUpdate' |
| 32 | |
| 33 | # Files needed to serve an update. |
| 34 | UPDATE_FILES = ( |
| 35 | constants.UPDATE_FILE, |
| 36 | constants.STATEFUL_FILE, |
| 37 | constants.METADATA_FILE |
| 38 | ) |
| 39 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 40 | # Module-local log function. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 41 | def _Log(message, *args): |
| 42 | return log_util.LogWithTag('UPDATE', message, *args) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 43 | |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 44 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 45 | class AutoupdateError(Exception): |
| 46 | """Exception classes used by this module.""" |
| 47 | pass |
| 48 | |
| 49 | |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 50 | def _ChangeUrlPort(url, new_port): |
| 51 | """Return the URL passed in with a different port""" |
| 52 | scheme, netloc, path, query, fragment = urlparse.urlsplit(url) |
| 53 | host_port = netloc.split(':') |
| 54 | |
| 55 | if len(host_port) == 1: |
| 56 | host_port.append(new_port) |
| 57 | else: |
| 58 | host_port[1] = new_port |
| 59 | |
| 60 | print host_port |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 61 | netloc = '%s:%s' % tuple(host_port) |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 62 | |
| 63 | return urlparse.urlunsplit((scheme, netloc, path, query, fragment)) |
| 64 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 65 | def _NonePathJoin(*args): |
| 66 | """os.path.join that filters None's from the argument list.""" |
| 67 | return os.path.join(*filter(None, args)) |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 68 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 69 | |
| 70 | class HostInfo(object): |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 71 | """Records information about an individual host. |
| 72 | |
| 73 | Members: |
| 74 | attrs: Static attributes (legacy) |
| 75 | log: Complete log of recorded client entries |
| 76 | """ |
| 77 | |
| 78 | def __init__(self): |
| 79 | # A dictionary of current attributes pertaining to the host. |
| 80 | self.attrs = {} |
| 81 | |
| 82 | # A list of pairs consisting of a timestamp and a dictionary of recorded |
| 83 | # attributes. |
| 84 | self.log = [] |
| 85 | |
| 86 | def __repr__(self): |
| 87 | return 'attrs=%s, log=%s' % (self.attrs, self.log) |
| 88 | |
| 89 | def AddLogEntry(self, entry): |
| 90 | """Append a new log entry.""" |
| 91 | # Append a timestamp. |
| 92 | assert not 'timestamp' in entry, 'Oops, timestamp field already in use' |
| 93 | entry['timestamp'] = time.strftime('%Y-%m-%d %H:%M:%S') |
| 94 | # Add entry to hosts' message log. |
| 95 | self.log.append(entry) |
| 96 | |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 97 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 98 | class HostInfoTable(object): |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 99 | """Records information about a set of hosts who engage in update activity. |
| 100 | |
| 101 | Members: |
| 102 | table: Table of information on hosts. |
| 103 | """ |
| 104 | |
| 105 | def __init__(self): |
| 106 | # A dictionary of host information. Keys are normally IP addresses. |
| 107 | self.table = {} |
| 108 | |
| 109 | def __repr__(self): |
| 110 | return '%s' % self.table |
| 111 | |
| 112 | def GetInitHostInfo(self, host_id): |
| 113 | """Return a host's info object, or create a new one if none exists.""" |
| 114 | return self.table.setdefault(host_id, HostInfo()) |
| 115 | |
| 116 | def GetHostInfo(self, host_id): |
| 117 | """Return an info object for given host, if such exists.""" |
Chris Sosa | 1885d03 | 2012-11-29 17:07:27 -0800 | [diff] [blame] | 118 | return self.table.get(host_id) |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 119 | |
| 120 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 121 | class UpdateMetadata(object): |
| 122 | """Object containing metadata about an update payload.""" |
| 123 | |
| 124 | def __init__(self, sha1, sha256, size, is_delta_format): |
| 125 | self.sha1 = sha1 |
| 126 | self.sha256 = sha256 |
| 127 | self.size = size |
| 128 | self.is_delta_format = is_delta_format |
| 129 | |
| 130 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 131 | class Autoupdate(build_util.BuildObject): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 132 | """Class that contains functionality that handles Chrome OS update pings. |
| 133 | |
| 134 | Members: |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 135 | urlbase: base URL, other than devserver, for update images. |
| 136 | forced_image: path to an image to use for all updates. |
| 137 | payload_path: path to pre-generated payload to serve. |
| 138 | src_image: if specified, creates a delta payload from this image. |
| 139 | proxy_port: port of local proxy to tell client to connect to you |
| 140 | through. |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 141 | patch_kernel: Patch the kernel when generating updates |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 142 | board: board for the image. Needed for pre-generating of updates. |
| 143 | copy_to_static_root: copies images generated from the cache to ~/static. |
| 144 | private_key: path to private key in PEM format. |
Gilad Arnold | 8318eac | 2012-10-04 12:52:23 -0700 | [diff] [blame] | 145 | critical_update: whether provisioned payload is critical. |
| 146 | remote_payload: whether provisioned payload is remotely staged. |
| 147 | max_updates: maximum number of updates we'll try to provision. |
| 148 | host_log: record full history of host update events. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 149 | """ |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 150 | |
joychen | ed64b22 | 2013-06-21 16:39:34 -0700 | [diff] [blame] | 151 | _OLD_PAYLOAD_URL_PREFIX = '/static/archive' |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 152 | _PAYLOAD_URL_PREFIX = '/static/' |
| 153 | _FILEINFO_URL_PREFIX = '/api/fileinfo/' |
| 154 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 155 | SHA1_ATTR = 'sha1' |
| 156 | SHA256_ATTR = 'sha256' |
| 157 | SIZE_ATTR = 'size' |
| 158 | ISDELTA_ATTR = 'is_delta' |
| 159 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 160 | def __init__(self, xbuddy, urlbase=None, forced_image=None, payload_path=None, |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 161 | proxy_port=None, src_image='', patch_kernel=True, board=None, |
Chris Sosa | 0f1ec84 | 2011-02-14 16:33:22 -0800 | [diff] [blame] | 162 | copy_to_static_root=True, private_key=None, |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 163 | critical_update=False, remote_payload=False, max_updates= -1, |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 164 | host_log=False, *args, **kwargs): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 165 | super(Autoupdate, self).__init__(*args, **kwargs) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 166 | self.xbuddy = xbuddy |
| 167 | self.urlbase = urlbase or None |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 168 | self.forced_image = forced_image |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 169 | self.payload_path = payload_path |
Chris Sosa | 62f720b | 2010-10-26 21:39:48 -0700 | [diff] [blame] | 170 | self.src_image = src_image |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 171 | self.proxy_port = proxy_port |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 172 | self.patch_kernel = patch_kernel |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 173 | self.board = board or self.GetDefaultBoardID() |
Chris Sosa | 08d55a2 | 2011-01-19 16:08:02 -0800 | [diff] [blame] | 174 | self.copy_to_static_root = copy_to_static_root |
Chris Sosa | 0f1ec84 | 2011-02-14 16:33:22 -0800 | [diff] [blame] | 175 | self.private_key = private_key |
Satoru Takabayashi | d733cbe | 2011-11-15 09:36:32 -0800 | [diff] [blame] | 176 | self.critical_update = critical_update |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 177 | self.remote_payload = remote_payload |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 178 | self.max_updates = max_updates |
Gilad Arnold | 8318eac | 2012-10-04 12:52:23 -0700 | [diff] [blame] | 179 | self.host_log = host_log |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 180 | |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 181 | self.pregenerated_path = None |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 182 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 183 | # Initialize empty host info cache. Used to keep track of various bits of |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 184 | # information about a given host. A host is identified by its IP address. |
| 185 | # The info stored for each host includes a complete log of events for this |
| 186 | # host, as well as a dictionary of current attributes derived from events. |
| 187 | self.host_infos = HostInfoTable() |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 188 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 189 | @classmethod |
| 190 | def _ReadMetadataFromStream(cls, stream): |
| 191 | """Returns metadata obj from input json stream that implements .read().""" |
| 192 | file_attr_dict = {} |
| 193 | try: |
| 194 | file_attr_dict = json.loads(stream.read()) |
| 195 | except IOError: |
| 196 | return None |
| 197 | |
| 198 | sha1 = file_attr_dict.get(cls.SHA1_ATTR) |
| 199 | sha256 = file_attr_dict.get(cls.SHA256_ATTR) |
| 200 | size = file_attr_dict.get(cls.SIZE_ATTR) |
| 201 | is_delta = file_attr_dict.get(cls.ISDELTA_ATTR) |
| 202 | return UpdateMetadata(sha1, sha256, size, is_delta) |
| 203 | |
| 204 | @staticmethod |
| 205 | def _ReadMetadataFromFile(payload_dir): |
| 206 | """Returns metadata object from the metadata_file in the payload_dir""" |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 207 | metadata_file = os.path.join(payload_dir, constants.METADATA_FILE) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 208 | if os.path.exists(metadata_file): |
| 209 | with open(metadata_file, 'r') as metadata_stream: |
| 210 | return Autoupdate._ReadMetadataFromStream(metadata_stream) |
| 211 | |
| 212 | @classmethod |
| 213 | def _StoreMetadataToFile(cls, payload_dir, metadata_obj): |
| 214 | """Stores metadata object into the metadata_file of the payload_dir""" |
| 215 | file_dict = {cls.SHA1_ATTR: metadata_obj.sha1, |
| 216 | cls.SHA256_ATTR: metadata_obj.sha256, |
| 217 | cls.SIZE_ATTR: metadata_obj.size, |
| 218 | cls.ISDELTA_ATTR: metadata_obj.is_delta_format} |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 219 | metadata_file = os.path.join(payload_dir, constants.METADATA_FILE) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 220 | with open(metadata_file, 'w') as file_handle: |
| 221 | json.dump(file_dict, file_handle) |
| 222 | |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 223 | @staticmethod |
| 224 | def _GetVersionFromDir(image_dir): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 225 | """Returns the version of the image based on the name of the directory.""" |
| 226 | latest_version = os.path.basename(image_dir) |
Daniel Erat | 8a0bc4a | 2011-09-30 08:52:52 -0700 | [diff] [blame] | 227 | parts = latest_version.split('-') |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 228 | # If we can't get a version number from the directory, default to a high |
| 229 | # number to allow the update to happen |
| 230 | return parts[1] if len(parts) == 3 else "9999.0.0" |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 231 | |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 232 | @staticmethod |
| 233 | def _CanUpdate(client_version, latest_version): |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 234 | """Returns true if the latest_version is greater than the client_version. |
| 235 | """ |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 236 | _Log('client version %s latest version %s', client_version, latest_version) |
Daniel Erat | 8a0bc4a | 2011-09-30 08:52:52 -0700 | [diff] [blame] | 237 | |
| 238 | client_tokens = client_version.replace('_', '').split('.') |
Daniel Erat | 8a0bc4a | 2011-09-30 08:52:52 -0700 | [diff] [blame] | 239 | latest_tokens = latest_version.replace('_', '').split('.') |
Daniel Erat | 8a0bc4a | 2011-09-30 08:52:52 -0700 | [diff] [blame] | 240 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 241 | if len(latest_tokens) == len(client_tokens) == 3: |
| 242 | return latest_tokens > client_tokens |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 243 | else: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 244 | # If the directory name isn't a version number, let it pass. |
| 245 | return True |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 246 | |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 247 | @staticmethod |
Gilad Arnold | e74b381 | 2013-04-22 11:27:38 -0700 | [diff] [blame] | 248 | def IsDeltaFormatFile(filename): |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 249 | try: |
Gilad Arnold | e74b381 | 2013-04-22 11:27:38 -0700 | [diff] [blame] | 250 | with open(filename) as payload_file: |
| 251 | payload = update_payload.Payload(payload_file) |
| 252 | payload.Init() |
| 253 | return payload.IsDelta() |
| 254 | except (IOError, update_payload.PayloadError): |
| 255 | # For unit tests we may not have real files, so it's ok to ignore these |
| 256 | # errors. |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 257 | return False |
| 258 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 259 | def GenerateUpdateFile(self, src_image, image_path, output_dir): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 260 | """Generates an update gz given a full path to an image. |
| 261 | |
| 262 | Args: |
| 263 | image_path: Full path to image. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 264 | Raises: |
| 265 | subprocess.CalledProcessError if the update generator fails to generate a |
| 266 | stateful payload. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 267 | """ |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 268 | update_path = os.path.join(output_dir, constants.UPDATE_FILE) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 269 | _Log('Generating update image %s', update_path) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 270 | |
Chris Sosa | 0f1ec84 | 2011-02-14 16:33:22 -0800 | [diff] [blame] | 271 | update_command = [ |
Chris Sosa | 5b8b5eb | 2012-03-27 11:15:27 -0700 | [diff] [blame] | 272 | 'cros_generate_update_payload', |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 273 | '--image', image_path, |
| 274 | '--output', update_path, |
Chris Sosa | 0f1ec84 | 2011-02-14 16:33:22 -0800 | [diff] [blame] | 275 | ] |
Chris Sosa | 4136e69 | 2010-10-28 23:42:37 -0700 | [diff] [blame] | 276 | |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 277 | if src_image: |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 278 | update_command.extend(['--src_image', src_image]) |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 279 | |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 280 | if self.patch_kernel: |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 281 | update_command.append('--patch_kernel') |
| 282 | |
| 283 | if self.private_key: |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 284 | update_command.extend(['--private_key', self.private_key]) |
Chris Sosa | 0f1ec84 | 2011-02-14 16:33:22 -0800 | [diff] [blame] | 285 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 286 | _Log('Running %s', ' '.join(update_command)) |
| 287 | subprocess.check_call(update_command) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 288 | |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 289 | @staticmethod |
| 290 | def GenerateStatefulFile(image_path, output_dir): |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 291 | """Generates a stateful update payload given a full path to an image. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 292 | |
| 293 | Args: |
| 294 | image_path: Full path to image. |
Chris Sosa | 908fd6f | 2010-11-10 17:31:18 -0800 | [diff] [blame] | 295 | Raises: |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 296 | subprocess.CalledProcessError if the update generator fails to generate a |
Chris Sosa | 908fd6f | 2010-11-10 17:31:18 -0800 | [diff] [blame] | 297 | stateful payload. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 298 | """ |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 299 | update_command = [ |
| 300 | 'cros_generate_stateful_update_payload', |
| 301 | '--image', image_path, |
| 302 | '--output_dir', output_dir, |
| 303 | ] |
| 304 | _Log('Running %s', ' '.join(update_command)) |
| 305 | subprocess.check_call(update_command) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 306 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 307 | def FindCachedUpdateImageSubDir(self, src_image, dest_image): |
| 308 | """Find directory to store a cached update. |
| 309 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 310 | Given one, or two images for an update, this finds which cache directory |
| 311 | should hold the update files, even if they don't exist yet. |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 312 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 313 | Returns: |
| 314 | A directory path for storing a cached update, of the following form: |
| 315 | Non-delta updates: |
| 316 | CACHE_DIR/<dest_hash> |
| 317 | Delta updates: |
| 318 | CACHE_DIR/<src_hash>_<dest_hash> |
| 319 | Signed updates (self.private_key): |
| 320 | CACHE_DIR/<src_hash>_<dest_hash>+<private_key_hash> |
Chris Sosa | 744e147 | 2011-09-07 19:32:50 -0700 | [diff] [blame] | 321 | """ |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 322 | update_dir = '' |
Chris Sosa | 744e147 | 2011-09-07 19:32:50 -0700 | [diff] [blame] | 323 | if src_image: |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 324 | update_dir += common_util.GetFileMd5(src_image) + '_' |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 325 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 326 | update_dir += common_util.GetFileMd5(dest_image) |
Chris Sosa | 744e147 | 2011-09-07 19:32:50 -0700 | [diff] [blame] | 327 | if self.private_key: |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 328 | update_dir += '+' + common_util.GetFileMd5(self.private_key) |
Chris Sosa | 744e147 | 2011-09-07 19:32:50 -0700 | [diff] [blame] | 329 | |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 330 | if self.patch_kernel: |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 331 | update_dir += '+patched_kernel' |
Chris Sosa | 9fba756 | 2012-01-31 10:15:47 -0800 | [diff] [blame] | 332 | |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 333 | return os.path.join(constants.CACHE_DIR, update_dir) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 334 | |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 335 | def GenerateUpdateImage(self, image_path, output_dir): |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 336 | """Force generates an update payload based on the given image_path. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 337 | |
Chris Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame] | 338 | Args: |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 339 | image_path: full path to the image. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 340 | output_dir: the directory to write the update payloads to |
| 341 | Raises: |
| 342 | AutoupdateError if it failed to generate either update or stateful |
| 343 | payload. |
Chris Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame] | 344 | """ |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 345 | _Log('Generating update for image %s', image_path) |
Andrew de los Reyes | 9a52871 | 2010-06-30 10:29:43 -0700 | [diff] [blame] | 346 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 347 | # Delete any previous state in this directory. |
| 348 | os.system('rm -rf "%s"' % output_dir) |
| 349 | os.makedirs(output_dir) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 350 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 351 | try: |
| 352 | self.GenerateUpdateFile(self.src_image, image_path, output_dir) |
| 353 | self.GenerateStatefulFile(image_path, output_dir) |
| 354 | except subprocess.CalledProcessError: |
| 355 | os.system('rm -rf "%s"' % output_dir) |
| 356 | raise AutoupdateError('Failed to generate update in %s' % output_dir) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 357 | |
| 358 | def GenerateUpdateImageWithCache(self, image_path, static_image_dir): |
| 359 | """Force generates an update payload based on the given image_path. |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 360 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 361 | Args: |
| 362 | image_path: full path to the image. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 363 | static_image_dir: the directory to move images to after generating. |
| 364 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 365 | update directory relative to static_image_dir. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 366 | Raises: |
| 367 | AutoupdateError if it we need to generate a payload and fail to do so. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 368 | """ |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 369 | _Log('Generating update for src %s image %s', self.src_image, image_path) |
Chris Sosa | e67b78f | 2010-11-04 17:33:16 -0700 | [diff] [blame] | 370 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 371 | # If it was pregenerated, don't regenerate. |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 372 | if self.pregenerated_path: |
| 373 | return self.pregenerated_path |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 374 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 375 | # Which sub_dir of static_image_dir should hold our cached update image. |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 376 | cache_sub_dir = self.FindCachedUpdateImageSubDir(self.src_image, |
| 377 | image_path) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 378 | _Log('Caching in sub_dir "%s"', cache_sub_dir) |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 379 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 380 | # The cached payloads exist in a cache dir. |
| 381 | cache_dir = os.path.join(static_image_dir, cache_sub_dir) |
| 382 | |
| 383 | cache_update_payload = os.path.join(cache_dir, |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 384 | constants.UPDATE_FILE) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 385 | cache_stateful_payload = os.path.join(cache_dir, |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 386 | constants.STATEFUL_FILE) |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 387 | # Check to see if this cache directory is valid. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 388 | if not (os.path.exists(cache_update_payload) and |
| 389 | os.path.exists(cache_stateful_payload)): |
| 390 | self.GenerateUpdateImage(image_path, cache_dir) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 391 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 392 | # Don't regenerate the image for this devserver instance. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 393 | self.pregenerated_path = cache_sub_dir |
Chris Sosa | 65d339b | 2013-01-21 18:59:21 -0800 | [diff] [blame] | 394 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 395 | # Generate the cache file. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 396 | self.GetLocalPayloadAttrs(cache_dir) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 397 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 398 | return cache_sub_dir |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 399 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 400 | def _Symlink(self, src_path, dest_path): |
| 401 | if os.path.exists(src_path): |
| 402 | if os.path.lexists(dest_path): |
| 403 | os.unlink(dest_path) |
| 404 | _Log('Creating symlink to: %s --> %s', dest_path, src_path) |
| 405 | os.symlink(src_path, dest_path) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 406 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 407 | def _SymlinkUpdateFiles(self, image_dir): |
| 408 | """Set files in the base static_dir to link to most recent update files. |
| 409 | |
| 410 | Every time an update is called, clear existing files/symlinks in the |
| 411 | devserver's static_dir, and replace them with symlinks. |
| 412 | This allows the base of archive_dir to serve the most recent update. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 413 | |
| 414 | Args: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 415 | image_dir: Where update files are staged. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 416 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 417 | if self.static_dir == image_dir: |
| 418 | _Log("Serving from static directory.") |
| 419 | return |
| 420 | for f in UPDATE_FILES: |
| 421 | link = os.path.join(self.static_dir, f) |
| 422 | target = os.path.join(image_dir, f) |
| 423 | self._Symlink(target, link) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 424 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 425 | def GetUpdateForLabel(self, client_version, label, |
| 426 | image_name=constants.TEST_IMAGE_FILE): |
| 427 | """Given a label, get an update from the directory. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 428 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 429 | Args: |
| 430 | client_version: Current version of the client or FORCED_UPDATE |
| 431 | label: the relative directory inside the static dir |
| 432 | image_name: If the image type was specified by the update rpc, we try to |
| 433 | find an image with this file name first. This is by default |
| 434 | "chromiumos_test_image.bin" but can also take any of the values in |
| 435 | devserver_constants.ALL_IMAGES |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 436 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 437 | A relative path to the directory with the update payload. |
| 438 | This is the label if an update did not need to be generated, but can |
| 439 | be label/cache/hashed_dir_for_update. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 440 | Raises: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 441 | AutoupdateError: If client version is higher than available update found |
| 442 | at the directory given by the label. |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 443 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 444 | _Log('Update label/file: %s/%s', label, image_name) |
| 445 | static_image_dir = _NonePathJoin(self.static_dir, label) |
| 446 | static_update_path = _NonePathJoin(static_image_dir, constants.UPDATE_FILE) |
| 447 | static_image_path = _NonePathJoin(static_image_dir, image_name) |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 448 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 449 | # Update the client only if client version is older than available update. |
| 450 | latest_version = self._GetVersionFromDir(static_image_dir) |
| 451 | if not (client_version == FORCED_UPDATE or |
| 452 | self._CanUpdate(client_version, latest_version)): |
| 453 | raise AutoupdateError( |
| 454 | 'Update check received but no update available for client') |
Don Garrett | ee25e55 | 2010-11-23 12:09:35 -0800 | [diff] [blame] | 455 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 456 | if label and os.path.exists(static_update_path): |
| 457 | # An update payload was found for the given label, return it. |
| 458 | return label |
| 459 | elif os.path.exists(static_image_path) and common_util.IsInsideChroot(): |
| 460 | # Image was found for the given label. Generate update if we can. |
| 461 | rel_path = self.GenerateUpdateImageWithCache( |
| 462 | static_image_path, static_image_dir=static_image_dir) |
| 463 | return _NonePathJoin(label, rel_path) |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 464 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 465 | # The label didn't resolve. |
| 466 | return None |
Chris Sosa | 2c048f1 | 2010-10-27 16:05:27 -0700 | [diff] [blame] | 467 | |
| 468 | def PreGenerateUpdate(self): |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 469 | """Pre-generates an update and prints out the relative path it. |
| 470 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 471 | Returns relative path of the update. |
Chris Sosa | 65d339b | 2013-01-21 18:59:21 -0800 | [diff] [blame] | 472 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 473 | Raises: |
| 474 | AutoupdateError if it failed to generate the payload. |
| 475 | """ |
| 476 | _Log('Pre-generating the update payload') |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 477 | # Does not work with labels so just use static dir. (empty label) |
| 478 | pregenerated_update = self.GetPathToPayload('', FORCED_UPDATE, self.board) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 479 | print 'PREGENERATED_UPDATE=%s' % _NonePathJoin(pregenerated_update, |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 480 | constants.UPDATE_FILE) |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 481 | return pregenerated_update |
Chris Sosa | 2c048f1 | 2010-10-27 16:05:27 -0700 | [diff] [blame] | 482 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 483 | def _GetRemotePayloadAttrs(self, url): |
| 484 | """Returns hashes, size and delta flag of a remote update payload. |
| 485 | |
| 486 | Obtain attributes of a payload file available on a remote devserver. This |
| 487 | is based on the assumption that the payload URL uses the /static prefix. We |
| 488 | need to make sure that both clients (requests) and remote devserver |
| 489 | (provisioning) preserve this invariant. |
| 490 | |
| 491 | Args: |
| 492 | url: URL of statically staged remote file (http://host:port/static/...) |
| 493 | Returns: |
| 494 | A tuple containing the SHA1, SHA256, file size and whether or not it's a |
| 495 | delta payload (Boolean). |
| 496 | """ |
| 497 | if self._PAYLOAD_URL_PREFIX not in url: |
| 498 | raise AutoupdateError( |
| 499 | 'Payload URL does not have the expected prefix (%s)' % |
| 500 | self._PAYLOAD_URL_PREFIX) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 501 | |
joychen | ed64b22 | 2013-06-21 16:39:34 -0700 | [diff] [blame] | 502 | if self._OLD_PAYLOAD_URL_PREFIX in url: |
| 503 | fileinfo_url = url.replace(self._OLD_PAYLOAD_URL_PREFIX, |
| 504 | self._FILEINFO_URL_PREFIX) |
| 505 | else: |
| 506 | fileinfo_url = url.replace(self._PAYLOAD_URL_PREFIX, |
| 507 | self._FILEINFO_URL_PREFIX) |
| 508 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 509 | _Log('Retrieving file info for remote payload via %s', fileinfo_url) |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 510 | try: |
| 511 | conn = urllib2.urlopen(fileinfo_url) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 512 | metadata_obj = Autoupdate._ReadMetadataFromStream(conn) |
| 513 | # These fields are required for remote calls. |
| 514 | if not metadata_obj: |
| 515 | raise AutoupdateError('Failed to obtain remote payload info') |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 516 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 517 | return metadata_obj |
| 518 | except IOError as e: |
| 519 | raise AutoupdateError('Failed to obtain remote payload info: %s', e) |
| 520 | |
| 521 | def GetLocalPayloadAttrs(self, payload_dir): |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 522 | """Returns hashes, size and delta flag of a local update payload. |
| 523 | |
| 524 | Args: |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 525 | payload_dir: Path to the directory the payload is in. |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 526 | Returns: |
| 527 | A tuple containing the SHA1, SHA256, file size and whether or not it's a |
| 528 | delta payload (Boolean). |
| 529 | """ |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 530 | filename = os.path.join(payload_dir, constants.UPDATE_FILE) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 531 | if not os.path.exists(filename): |
| 532 | raise AutoupdateError('update.gz not present in payload dir %s' % |
| 533 | payload_dir) |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 534 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 535 | metadata_obj = Autoupdate._ReadMetadataFromFile(payload_dir) |
| 536 | if not metadata_obj or not (metadata_obj.sha1 and |
| 537 | metadata_obj.sha256 and |
| 538 | metadata_obj.size): |
| 539 | sha1 = common_util.GetFileSha1(filename) |
| 540 | sha256 = common_util.GetFileSha256(filename) |
| 541 | size = common_util.GetFileSize(filename) |
Gilad Arnold | e74b381 | 2013-04-22 11:27:38 -0700 | [diff] [blame] | 542 | is_delta_format = self.IsDeltaFormatFile(filename) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 543 | metadata_obj = UpdateMetadata(sha1, sha256, size, is_delta_format) |
| 544 | Autoupdate._StoreMetadataToFile(payload_dir, metadata_obj) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 545 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 546 | return metadata_obj |
| 547 | |
| 548 | def _ProcessUpdateComponents(self, app, event): |
| 549 | """Processes the app and event components of an update request. |
| 550 | |
| 551 | Returns tuple containing forced_update_label, client_version, and board. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 552 | """ |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 553 | # Initialize an empty dictionary for event attributes to log. |
| 554 | log_message = {} |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 555 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 556 | # Determine request IP, strip any IPv6 data for simplicity. |
| 557 | client_ip = cherrypy.request.remote.ip.split(':')[-1] |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 558 | # Obtain (or init) info object for this client. |
| 559 | curr_host_info = self.host_infos.GetInitHostInfo(client_ip) |
| 560 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 561 | client_version = FORCED_UPDATE |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 562 | board = None |
| 563 | if app: |
| 564 | client_version = app.getAttribute('version') |
| 565 | channel = app.getAttribute('track') |
| 566 | board = (app.hasAttribute('board') and app.getAttribute('board') |
joychen | b0dfe55 | 2013-07-30 10:02:06 -0700 | [diff] [blame] | 567 | or self.GetDefaultBoardID()) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 568 | # Add attributes to log message |
| 569 | log_message['version'] = client_version |
| 570 | log_message['track'] = channel |
| 571 | log_message['board'] = board |
| 572 | curr_host_info.attrs['last_known_version'] = client_version |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 573 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 574 | if event: |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 575 | event_result = int(event[0].getAttribute('eventresult')) |
| 576 | event_type = int(event[0].getAttribute('eventtype')) |
Gilad Arnold | b11a894 | 2012-03-13 15:33:21 -0700 | [diff] [blame] | 577 | client_previous_version = (event[0].getAttribute('previousversion') |
| 578 | if event[0].hasAttribute('previousversion') |
| 579 | else None) |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 580 | # Store attributes to legacy host info structure |
| 581 | curr_host_info.attrs['last_event_status'] = event_result |
| 582 | curr_host_info.attrs['last_event_type'] = event_type |
| 583 | # Add attributes to log message |
| 584 | log_message['event_result'] = event_result |
| 585 | log_message['event_type'] = event_type |
Gilad Arnold | b11a894 | 2012-03-13 15:33:21 -0700 | [diff] [blame] | 586 | if client_previous_version is not None: |
| 587 | log_message['previous_version'] = client_previous_version |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 588 | |
Gilad Arnold | 8318eac | 2012-10-04 12:52:23 -0700 | [diff] [blame] | 589 | # Log host event, if so instructed. |
| 590 | if self.host_log: |
| 591 | curr_host_info.AddLogEntry(log_message) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 592 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 593 | return (curr_host_info.attrs.pop('forced_update_label', None), |
| 594 | client_version, board) |
| 595 | |
| 596 | def _GetStaticUrl(self): |
| 597 | """Returns the static url base that should prefix all payload responses.""" |
| 598 | x_forwarded_host = cherrypy.request.headers.get('X-Forwarded-Host') |
| 599 | if x_forwarded_host: |
| 600 | hostname = 'http://' + x_forwarded_host |
| 601 | else: |
| 602 | hostname = cherrypy.request.base |
| 603 | |
| 604 | if self.urlbase: |
| 605 | static_urlbase = self.urlbase |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 606 | else: |
| 607 | static_urlbase = '%s/static' % hostname |
| 608 | |
| 609 | # If we have a proxy port, adjust the URL we instruct the client to |
| 610 | # use to go through the proxy. |
| 611 | if self.proxy_port: |
| 612 | static_urlbase = _ChangeUrlPort(static_urlbase, self.proxy_port) |
| 613 | |
| 614 | _Log('Using static url base %s', static_urlbase) |
| 615 | _Log('Handling update ping as %s', hostname) |
| 616 | return static_urlbase |
| 617 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 618 | def GetPathToPayload(self, label, client_version, board): |
| 619 | """Find a payload locally. |
| 620 | |
| 621 | See devserver's update rpc for documentation. |
| 622 | |
| 623 | Args: |
| 624 | label: from update request |
| 625 | client_version: from update request |
| 626 | board: from update request |
| 627 | Return: |
| 628 | The relative path to an update from the static_dir |
| 629 | Raises: |
| 630 | AutoupdateError: If the update could not be found. |
| 631 | """ |
| 632 | path_to_payload = None |
| 633 | #TODO(joychen): deprecate --payload flag |
| 634 | if self.payload_path: |
| 635 | # Copy the image from the path to '/forced_payload' |
| 636 | label = 'forced_payload' |
| 637 | dest_path = os.path.join(self.static_dir, label, constants.UPDATE_FILE) |
| 638 | dest_stateful = os.path.join(self.static_dir, label, |
| 639 | constants.STATEFUL_FILE) |
| 640 | |
| 641 | src_path = os.path.abspath(self.payload_path) |
| 642 | src_stateful = os.path.join(os.path.dirname(src_path), |
| 643 | constants.STATEFUL_FILE) |
| 644 | common_util.MkDirP(os.path.join(self.static_dir, label)) |
| 645 | self._Symlink(src_path, dest_path) |
| 646 | if os.path.exists(src_stateful): |
| 647 | # The stateful payload is optional. |
| 648 | self._Symlink(src_stateful, dest_stateful) |
| 649 | else: |
| 650 | _Log('WARN: %s not found. Expected for dev and test builds', |
| 651 | constants.STATEFUL_FILE) |
| 652 | if os.path.exists(dest_stateful): |
| 653 | os.remove(dest_stateful) |
| 654 | path_to_payload = self.GetUpdateForLabel(client_version, label) |
| 655 | #TODO(joychen): deprecate --image flag |
| 656 | elif self.forced_image: |
joychen | dbfe6c9 | 2013-08-16 20:03:49 -0700 | [diff] [blame] | 657 | if self.forced_image.startswith('xbuddy:'): |
| 658 | # This is trying to use an xbuddy path in place of a path to an image. |
joychen | dbfe6c9 | 2013-08-16 20:03:49 -0700 | [diff] [blame] | 659 | xbuddy_label = self.forced_image.split(':')[1] |
| 660 | self.forced_image = None |
joychen | 365a574 | 2013-08-21 10:41:18 -0700 | [diff] [blame^] | 661 | # Make sure the xbuddy path target is in the directory. |
| 662 | path_to_payload, _image_name = self.xbuddy.Get(xbuddy_label.split('/')) |
| 663 | # Pretend to have called update with this update path to payload. |
joychen | dbfe6c9 | 2013-08-16 20:03:49 -0700 | [diff] [blame] | 664 | return self.GetPathToPayload(xbuddy_label, client_version, board) |
| 665 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 666 | src_path = os.path.abspath(self.forced_image) |
| 667 | if os.path.exists(src_path) and common_util.IsInsideChroot(): |
| 668 | # Image was found for the given label. Generate update if we can. |
| 669 | return self.GenerateUpdateImageWithCache( |
| 670 | src_path, static_image_dir=self.static_dir) |
| 671 | else: |
| 672 | label = label or '' |
| 673 | label_list = label.split('/') |
| 674 | # Suppose that the path follows old protocol of indexing straight |
| 675 | # into static_dir with board/version label. |
| 676 | # Attempt to get the update in that directory, generating if necc. |
| 677 | path_to_payload = self.GetUpdateForLabel(client_version, label) |
| 678 | if path_to_payload is None: |
| 679 | # There was no update or image found in the directory. |
| 680 | # Let XBuddy find an image, and then generate an update to it. |
| 681 | if label_list[0] == 'xbuddy': |
| 682 | # If path explicitly calls xbuddy, pop off the tag. |
| 683 | label_list.pop() |
| 684 | x_label, image_name = self.xbuddy.Translate(label_list, board) |
| 685 | if image_name not in constants.ALL_IMAGES: |
| 686 | raise AutoupdateError( |
| 687 | "Use an image alias: dev, base, test, or recovery.") |
| 688 | # Path has been resolved, try to get the image. |
| 689 | path_to_payload = self.GetUpdateForLabel(client_version, x_label, |
| 690 | image_name) |
| 691 | if path_to_payload is None: |
| 692 | # Neither image nor update payload found after translation. |
| 693 | # Try to get an update to a test image from GS using the label. |
| 694 | path_to_payload, _image_name = self.xbuddy.Get( |
| 695 | ['remote', label, 'full_payload']) |
| 696 | |
| 697 | # One of the above options should have gotten us a relative path. |
| 698 | if path_to_payload is None: |
| 699 | raise AutoupdateError('Failed to get an update for: %s' % label) |
| 700 | else: |
| 701 | # Add links from the static directory to the update. |
| 702 | self._SymlinkUpdateFiles( |
| 703 | _NonePathJoin(self.static_dir, path_to_payload)) |
| 704 | |
| 705 | return path_to_payload |
| 706 | |
| 707 | def HandleUpdatePing(self, data, label=''): |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 708 | """Handles an update ping from an update client. |
| 709 | |
| 710 | Args: |
| 711 | data: XML blob from client. |
| 712 | label: optional label for the update. |
| 713 | Returns: |
| 714 | Update payload message for client. |
| 715 | """ |
| 716 | # Get the static url base that will form that base of our update url e.g. |
| 717 | # http://hostname:8080/static/update.gz. |
| 718 | static_urlbase = self._GetStaticUrl() |
| 719 | |
| 720 | # Parse the XML we got into the components we care about. |
| 721 | protocol, app, event, update_check = autoupdate_lib.ParseUpdateRequest(data) |
| 722 | |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 723 | # Process attributes of the update check. |
| 724 | forced_update_label, client_version, board = self._ProcessUpdateComponents( |
| 725 | app, event) |
| 726 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 727 | if not update_check: |
| 728 | # TODO(sosa): Generate correct non-updatecheck payload to better test |
| 729 | # update clients. |
| 730 | _Log('Non-update check received. Returning blank payload') |
| 731 | return autoupdate_lib.GetNoUpdateResponse(protocol) |
| 732 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 733 | if forced_update_label: |
| 734 | if label: |
| 735 | _Log('Label: %s set but being overwritten to %s by request', label, |
| 736 | forced_update_label) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 737 | label = forced_update_label |
| 738 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 739 | if self.max_updates == 0: |
| 740 | # In case max_updates is used, return no response if max reached. |
| 741 | _Log('Request received but max number of updates handled') |
| 742 | return autoupdate_lib.GetNoUpdateResponse(protocol) |
| 743 | |
| 744 | _Log('Update Check Received. Client is using protocol version: %s', |
| 745 | protocol) |
| 746 | self.max_updates -= 1 |
| 747 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 748 | # Finally its time to generate the omaha response to give to client that |
| 749 | # lets them know where to find the payload and its associated metadata. |
| 750 | metadata_obj = None |
| 751 | |
| 752 | try: |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 753 | # Are we provisioning a remote or local payload? |
| 754 | if self.remote_payload: |
| 755 | # If no explicit label was provided, use the value of --payload. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 756 | if not label: |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 757 | label = self.payload_path |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 758 | |
Chris Sosa | 52f15bc | 2013-08-13 17:14:15 -0700 | [diff] [blame] | 759 | # TODO(sosa): Remove backwards-compatible hack. |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 760 | if not '.bin' in label: |
Chris Sosa | 52f15bc | 2013-08-13 17:14:15 -0700 | [diff] [blame] | 761 | url = _NonePathJoin(static_urlbase, label, 'update.gz') |
| 762 | else: |
| 763 | url = _NonePathJoin(static_urlbase, label) |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 764 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 765 | # Get remote payload attributes. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 766 | metadata_obj = self._GetRemotePayloadAttrs(url) |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 767 | else: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 768 | path_to_payload = self.GetPathToPayload(label, client_version, board) |
| 769 | url = _NonePathJoin(static_urlbase, path_to_payload, |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 770 | constants.UPDATE_FILE) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 771 | local_payload_dir = _NonePathJoin(self.static_dir, path_to_payload) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 772 | metadata_obj = self.GetLocalPayloadAttrs(local_payload_dir) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 773 | except AutoupdateError as e: |
| 774 | # Raised if we fail to generate an update payload. |
| 775 | _Log('Failed to process an update: %r', e) |
| 776 | return autoupdate_lib.GetNoUpdateResponse(protocol) |
| 777 | |
| 778 | _Log('Responding to client to use url %s to get image', url) |
| 779 | return autoupdate_lib.GetUpdateResponse( |
| 780 | metadata_obj.sha1, metadata_obj.sha256, metadata_obj.size, url, |
| 781 | metadata_obj.is_delta_format, protocol, self.critical_update) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 782 | |
| 783 | def HandleHostInfoPing(self, ip): |
| 784 | """Returns host info dictionary for the given IP in JSON format.""" |
| 785 | assert ip, 'No ip provided.' |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 786 | if ip in self.host_infos.table: |
| 787 | return json.dumps(self.host_infos.GetHostInfo(ip).attrs) |
| 788 | |
| 789 | def HandleHostLogPing(self, ip): |
| 790 | """Returns a complete log of events for host in JSON format.""" |
Gilad Arnold | 4ba437d | 2012-10-05 15:28:27 -0700 | [diff] [blame] | 791 | # If all events requested, return a dictionary of logs keyed by IP address. |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 792 | if ip == 'all': |
| 793 | return json.dumps( |
| 794 | dict([(key, self.host_infos.table[key].log) |
| 795 | for key in self.host_infos.table])) |
Gilad Arnold | 4ba437d | 2012-10-05 15:28:27 -0700 | [diff] [blame] | 796 | |
| 797 | # Otherwise we're looking for a specific IP address, so find its log. |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 798 | if ip in self.host_infos.table: |
| 799 | return json.dumps(self.host_infos.GetHostInfo(ip).log) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 800 | |
Gilad Arnold | 4ba437d | 2012-10-05 15:28:27 -0700 | [diff] [blame] | 801 | # If no events were logged for this IP, return an empty log. |
| 802 | return json.dumps([]) |
| 803 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 804 | def HandleSetUpdatePing(self, ip, label): |
| 805 | """Sets forced_update_label for a given host.""" |
| 806 | assert ip, 'No ip provided.' |
| 807 | assert label, 'No label provided.' |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 808 | self.host_infos.GetInitHostInfo(ip).attrs['forced_update_label'] = label |