Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 1 | # Copyright (c) 2009-2010 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 | |
rtc@google.com | 6424466 | 2009-11-12 00:52:08 +0000 | [diff] [blame] | 5 | from buildutil import BuildObject |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 6 | from xml.dom import minidom |
| 7 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 8 | import cherrypy |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 9 | import os |
Darin Petkov | 798fe7d | 2010-03-22 15:18:13 -0700 | [diff] [blame] | 10 | import shutil |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 11 | import socket |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 12 | import time |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 13 | |
| 14 | def _LogMessage(message): |
| 15 | cherrypy.log(message, 'UPDATE') |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 16 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 17 | |
rtc@google.com | 6424466 | 2009-11-12 00:52:08 +0000 | [diff] [blame] | 18 | class Autoupdate(BuildObject): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 19 | """Class that contains functionality that handles Chrome OS update pings. |
| 20 | |
| 21 | Members: |
| 22 | serve_only: Serve images from a pre-built image.zip file. static_dir |
| 23 | must be set to the location of the image.zip. |
| 24 | factory_config: Path to the factory config file if handling factory |
| 25 | requests. |
| 26 | use_test_image: Use chromiumos_test_image.bin rather than the standard. |
| 27 | static_url_base: base URL, other than devserver, for update images. |
| 28 | client_prefix: The prefix for the update engine client. |
| 29 | forced_image: Path to an image to use for all updates. |
| 30 | """ |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 31 | |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 32 | def __init__(self, serve_only=None, test_image=False, urlbase=None, |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 33 | factory_config_path=None, client_prefix=None, forced_image=None, |
Chris Sosa | 62f720b | 2010-10-26 21:39:48 -0700 | [diff] [blame] | 34 | use_cached=False, port=8080, src_image='', *args, **kwargs): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 35 | super(Autoupdate, self).__init__(*args, **kwargs) |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 36 | self.serve_only = serve_only |
Sean O'Connor | 1b4b076 | 2010-06-02 17:37:32 -0700 | [diff] [blame] | 37 | self.factory_config = factory_config_path |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 38 | self.use_test_image = test_image |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 39 | if urlbase: |
Chris Sosa | 9841e1c | 2010-10-14 10:51:45 -0700 | [diff] [blame] | 40 | self.urlbase = urlbase |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 41 | else: |
Chris Sosa | 9841e1c | 2010-10-14 10:51:45 -0700 | [diff] [blame] | 42 | self.urlbase = None |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 43 | |
Chris Sosa | b63a928 | 2010-09-02 10:43:23 -0700 | [diff] [blame] | 44 | self.client_prefix = client_prefix |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 45 | self.forced_image = forced_image |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 46 | self.use_cached = use_cached |
Chris Sosa | 62f720b | 2010-10-26 21:39:48 -0700 | [diff] [blame] | 47 | self.src_image = src_image |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 48 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 49 | def _GetSecondsSinceMidnight(self): |
| 50 | """Returns the seconds since midnight as a decimal value.""" |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 51 | now = time.localtime() |
| 52 | return now[3] * 3600 + now[4] * 60 + now[5] |
| 53 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 54 | def _GetDefaultBoardID(self): |
| 55 | """Returns the default board id stored in .default_board.""" |
| 56 | board_file = '%s/.default_board' % (self.scripts_dir) |
| 57 | try: |
| 58 | return open(board_file).read() |
| 59 | except IOError: |
| 60 | return 'x86-generic' |
| 61 | |
| 62 | def _GetLatestImageDir(self, board_id): |
| 63 | """Returns the latest image dir based on shell script.""" |
| 64 | cmd = '%s/get_latest_image.sh --board %s' % (self.scripts_dir, board_id) |
| 65 | return os.popen(cmd).read().strip() |
| 66 | |
| 67 | def _GetVersionFromDir(self, image_dir): |
| 68 | """Returns the version of the image based on the name of the directory.""" |
| 69 | latest_version = os.path.basename(image_dir) |
| 70 | return latest_version.split('-')[0] |
| 71 | |
| 72 | def _CanUpdate(self, client_version, latest_version): |
| 73 | """Returns true if the latest_version is greater than the client_version.""" |
| 74 | client_tokens = client_version.replace('_', '').split('.') |
| 75 | latest_tokens = latest_version.replace('_', '').split('.') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 76 | _LogMessage('client version %s latest version %s' |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 77 | % (client_version, latest_version)) |
| 78 | for i in range(4): |
| 79 | if int(latest_tokens[i]) == int(client_tokens[i]): |
| 80 | continue |
| 81 | return int(latest_tokens[i]) > int(client_tokens[i]) |
| 82 | return False |
| 83 | |
| 84 | def _UnpackStatefulPartition(self, image_path, stateful_file): |
| 85 | """Given an image, unpacks its stateful partition to stateful_file.""" |
| 86 | image_dir = os.path.dirname(image_path) |
| 87 | image_file = os.path.basename(image_path) |
| 88 | |
| 89 | get_offset = '$(cgpt show -b -i 1 %s)' % image_file |
| 90 | get_size = '$(cgpt show -s -i 1 %s)' % image_file |
| 91 | unpack_command = ( |
| 92 | 'cd %s && ' |
| 93 | 'dd if=%s of=%s bs=512 skip=%s count=%s' % (image_dir, image_file, |
| 94 | stateful_file, get_offset, |
| 95 | get_size)) |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 96 | _LogMessage(unpack_command) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 97 | return os.system(unpack_command) == 0 |
| 98 | |
| 99 | def _UnpackZip(self, image_dir): |
| 100 | """Unpacks an image.zip into a given directory.""" |
| 101 | image = os.path.join(image_dir, self._GetImageName()) |
| 102 | if os.path.exists(image): |
| 103 | return True |
| 104 | else: |
| 105 | # -n, never clobber an existing file, in case we get invoked |
| 106 | # simultaneously by multiple request handlers. This means that |
| 107 | # we're assuming each image.zip file lives in a versioned |
| 108 | # directory (a la Buildbot). |
| 109 | return os.system('cd %s && unzip -n image.zip' % image_dir) == 0 |
| 110 | |
| 111 | def _GetImageName(self): |
| 112 | """Returns the name of the image that should be used.""" |
| 113 | if self.use_test_image: |
| 114 | image_name = 'chromiumos_test_image.bin' |
| 115 | else: |
| 116 | image_name = 'chromiumos_image.bin' |
| 117 | return image_name |
| 118 | |
| 119 | def _IsImageNewerThanCached(self, image_path, cached_file_path): |
| 120 | """Returns true if the image is newer than the cached image.""" |
| 121 | if os.path.exists(cached_file_path) and os.path.exists(image_path): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 122 | _LogMessage('Usable cached image found at %s.' % cached_file_path) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 123 | return os.path.getmtime(image_path) > os.path.getmtime(cached_file_path) |
| 124 | elif not os.path.exists(cached_file_path) and not os.path.exists(image_path): |
| 125 | raise Exception('Image does not exist and cached image missing') |
| 126 | else: |
| 127 | # Only one is missing, figure out which one. |
| 128 | if os.path.exists(image_path): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 129 | _LogMessage('No cached image found - image generation required.') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 130 | return True |
| 131 | else: |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 132 | _LogMessage('Cached image found to serve at %s.' % cached_file_path) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 133 | return False |
| 134 | |
| 135 | def _GetSize(self, update_path): |
| 136 | """Returns the size of the file given.""" |
| 137 | return os.path.getsize(update_path) |
| 138 | |
| 139 | def _GetHash(self, update_path): |
| 140 | """Returns the sha1 of the file given.""" |
| 141 | cmd = ('cat %s | openssl sha1 -binary | openssl base64 | tr \'\\n\' \' \';' |
| 142 | % update_path) |
| 143 | return os.popen(cmd).read().rstrip() |
| 144 | |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 145 | def _IsDeltaFormatFile(self, filename): |
| 146 | try: |
| 147 | file_handle = open(filename, 'r') |
| 148 | delta_magic = 'CrAU' |
| 149 | magic = file_handle.read(len(delta_magic)) |
| 150 | return magic == delta_magic |
| 151 | except Exception: |
| 152 | return False |
| 153 | |
Darin Petkov | 91436cb | 2010-09-28 08:52:17 -0700 | [diff] [blame] | 154 | # TODO(petkov): Consider optimizing getting both SHA-1 and SHA-256 so that |
| 155 | # it takes advantage of reduced I/O and multiple processors. Something like: |
| 156 | # % tee < FILE > /dev/null \ |
| 157 | # >( openssl dgst -sha256 -binary | openssl base64 ) \ |
| 158 | # >( openssl sha1 -binary | openssl base64 ) |
| 159 | def _GetSHA256(self, update_path): |
| 160 | """Returns the sha256 of the file given.""" |
| 161 | cmd = ('cat %s | openssl dgst -sha256 -binary | openssl base64' % |
| 162 | update_path) |
| 163 | return os.popen(cmd).read().rstrip() |
| 164 | |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 165 | def GetUpdatePayload(self, hash, sha256, size, url, is_delta_format): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 166 | """Returns a payload to the client corresponding to a new update. |
| 167 | |
| 168 | Args: |
| 169 | hash: hash of update blob |
Darin Petkov | 91436cb | 2010-09-28 08:52:17 -0700 | [diff] [blame] | 170 | sha256: SHA-256 hash of update blob |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 171 | size: size of update blob |
| 172 | url: where to find update blob |
| 173 | Returns: |
| 174 | Xml string to be passed back to client. |
| 175 | """ |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 176 | delta = 'false' |
| 177 | if is_delta_format: |
| 178 | delta = 'true' |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 179 | payload = """<?xml version="1.0" encoding="UTF-8"?> |
| 180 | <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0"> |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 181 | <daystart elapsed_seconds="%s"/> |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 182 | <app appid="{%s}" status="ok"> |
| 183 | <ping status="ok"/> |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 184 | <updatecheck |
| 185 | codebase="%s" |
| 186 | hash="%s" |
Darin Petkov | 91436cb | 2010-09-28 08:52:17 -0700 | [diff] [blame] | 187 | sha256="%s" |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 188 | needsadmin="false" |
| 189 | size="%s" |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 190 | IsDelta="%s" |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 191 | status="ok"/> |
| 192 | </app> |
| 193 | </gupdate> |
| 194 | """ |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 195 | return payload % (self._GetSecondsSinceMidnight(), |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 196 | self.app_id, url, hash, sha256, size, delta) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 197 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 198 | def GetNoUpdatePayload(self): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 199 | """Returns a payload to the client corresponding to no update.""" |
| 200 | payload = """ < ?xml version = "1.0" encoding = "UTF-8"? > |
| 201 | < gupdate xmlns = "http://www.google.com/update2/response" protocol = "2.0" > |
| 202 | < daystart elapsed_seconds = "%s" /> |
| 203 | < app appid = "{%s}" status = "ok" > |
| 204 | < ping status = "ok" /> |
| 205 | < updatecheck status = "noupdate" /> |
| 206 | </ app > |
| 207 | </ gupdate > |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 208 | """ |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 209 | return payload % (self._GetSecondsSinceMidnight(), self.app_id) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 210 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 211 | def GenerateUpdateFile(self, image_path): |
| 212 | """Generates an update gz given a full path to an image. |
| 213 | |
| 214 | Args: |
| 215 | image_path: Full path to image. |
| 216 | Returns: |
| 217 | Path to created update_payload or None on error. |
| 218 | """ |
| 219 | image_dir = os.path.dirname(image_path) |
| 220 | update_path = os.path.join(image_dir, 'update.gz') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 221 | _LogMessage('Generating update image %s' % update_path) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 222 | |
| 223 | mkupdate_command = ( |
Chris Sosa | 62f720b | 2010-10-26 21:39:48 -0700 | [diff] [blame] | 224 | '%s/cros_generate_update_payload --image="%s" --output="%s" ' |
| 225 | '--patch_kernel --noold_style --src_image="%s"' % ( |
| 226 | self.scripts_dir, image_path, |
| 227 | update_path, self.src_image)) |
| 228 | _LogMessage(mkupdate_command) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 229 | if os.system(mkupdate_command) != 0: |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 230 | _LogMessage('Failed to create base update file') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 231 | return None |
| 232 | |
| 233 | return update_path |
| 234 | |
| 235 | def GenerateStatefulFile(self, image_path): |
| 236 | """Generates a stateful update gz given a full path to an image. |
| 237 | |
| 238 | Args: |
| 239 | image_path: Full path to image. |
| 240 | Returns: |
| 241 | Path to created stateful update_payload or None on error. |
| 242 | """ |
| 243 | stateful_partition_path = '%s/stateful.image' % os.path.dirname(image_path) |
| 244 | |
| 245 | # Unpack to get stateful partition. |
| 246 | if self._UnpackStatefulPartition(image_path, stateful_partition_path): |
| 247 | mkstatefulupdate_command = 'gzip -f %s' % stateful_partition_path |
| 248 | if os.system(mkstatefulupdate_command) == 0: |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 249 | _LogMessage('Successfully generated %s.gz' % stateful_partition_path) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 250 | return '%s.gz' % stateful_partition_path |
| 251 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 252 | _LogMessage('Failed to create stateful update file') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 253 | return None |
| 254 | |
| 255 | def MoveImagesToStaticDir(self, update_path, stateful_update_path, |
| 256 | static_image_dir): |
| 257 | """Moves gz files from their directories to serving directories. |
| 258 | |
| 259 | Args: |
| 260 | update_path: full path to main update gz. |
| 261 | stateful_update_path: full path to stateful partition gz. |
| 262 | static_image_dir: where to put files. |
| 263 | Returns: |
| 264 | Returns True if the files were moved over successfully. |
| 265 | """ |
Andrew de los Reyes | 9a52871 | 2010-06-30 10:29:43 -0700 | [diff] [blame] | 266 | try: |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 267 | shutil.copy(update_path, static_image_dir) |
| 268 | shutil.copy(stateful_update_path, static_image_dir) |
| 269 | os.remove(update_path) |
| 270 | os.remove(stateful_update_path) |
| 271 | except Exception: |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 272 | _LogMessage('Failed to move %s and %s to %s' % (update_path, |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 273 | stateful_update_path, |
| 274 | static_image_dir)) |
| 275 | return False |
Andrew de los Reyes | 9a52871 | 2010-06-30 10:29:43 -0700 | [diff] [blame] | 276 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 277 | return True |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 278 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 279 | def GenerateUpdateImage(self, image_path, move_to_static_dir=False, |
| 280 | static_image_dir=None): |
| 281 | """Force generates an update payload based on the given image_path. |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 282 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 283 | Args: |
| 284 | image_path: full path to the image. |
| 285 | move_to_static_dir: Moves the files from their dir to the static dir. |
| 286 | static_image_dir: the directory to move images to after generating. |
| 287 | Returns: |
| 288 | True if the update payload was created successfully. |
| 289 | """ |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 290 | _LogMessage('Generating update for image %s' % image_path) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 291 | update_path = self.GenerateUpdateFile(image_path) |
| 292 | stateful_update_path = self.GenerateStatefulFile(image_path) |
| 293 | if not update_path or not stateful_update_path: |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 294 | _LogMessage('Failed to generate update') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 295 | return False |
| 296 | |
| 297 | if move_to_static_dir: |
| 298 | return self.MoveImagesToStaticDir(update_path, stateful_update_path, |
| 299 | static_image_dir) |
| 300 | else: |
| 301 | return True |
| 302 | |
| 303 | def GenerateLatestUpdateImage(self, board_id, client_version, |
| 304 | static_image_dir=None): |
| 305 | """Generates an update using the latest image that has been built. |
| 306 | |
| 307 | This will only generate an update if the newest update is newer than that |
| 308 | on the client or client_version is 'ForcedUpdate'. |
| 309 | |
| 310 | Args: |
| 311 | board_id: Name of the board. |
| 312 | client_version: Current version of the client or 'ForcedUpdate' |
| 313 | static_image_dir: the directory to move images to after generating. |
| 314 | Returns: |
| 315 | True if the update payload was created successfully. |
| 316 | """ |
| 317 | latest_image_dir = self._GetLatestImageDir(board_id) |
| 318 | latest_version = self._GetVersionFromDir(latest_image_dir) |
| 319 | latest_image_path = os.path.join(latest_image_dir, self._GetImageName()) |
| 320 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 321 | _LogMessage('Preparing to generate update from latest built image %s.' % |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 322 | latest_image_path) |
| 323 | |
| 324 | # Check to see whether or not we should update. |
| 325 | if client_version != 'ForcedUpdate' and not self._CanUpdate( |
| 326 | client_version, latest_version): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 327 | _LogMessage('no update') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 328 | return False |
| 329 | |
| 330 | cached_file_path = os.path.join(static_image_dir, 'update.gz') |
| 331 | if (os.path.exists(cached_file_path) and |
| 332 | not self._IsImageNewerThanCached(latest_image_path, cached_file_path)): |
| 333 | return True |
| 334 | |
| 335 | return self.GenerateUpdateImage(latest_image_path, move_to_static_dir=True, |
| 336 | static_image_dir=static_image_dir) |
| 337 | |
| 338 | def GenerateImageFromZip(self, static_image_dir): |
| 339 | """Generates an update from an image zip file. |
| 340 | |
| 341 | This method assumes you have an image.zip in directory you are serving |
| 342 | from. If this file is newer than a previously cached file, it will unzip |
| 343 | this file, create a payload and serve it. |
| 344 | |
| 345 | Args: |
| 346 | static_image_dir: Directory where the zip file exists. |
| 347 | Returns: |
| 348 | True if the update payload was created successfully. |
| 349 | """ |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 350 | _LogMessage('Preparing to generate update from zip in %s.' % static_image_dir) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 351 | image_path = os.path.join(static_image_dir, self._GetImageName()) |
| 352 | cached_file_path = os.path.join(static_image_dir, 'update.gz') |
| 353 | zip_file_path = os.path.join(static_image_dir, 'image.zip') |
| 354 | if not self._IsImageNewerThanCached(zip_file_path, cached_file_path): |
| 355 | return True |
| 356 | |
| 357 | if not self._UnpackZip(static_image_dir): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 358 | _LogMessage('unzip image.zip failed.') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 359 | return False |
| 360 | |
| 361 | return self.GenerateUpdateImage(image_path, move_to_static_dir=False, |
| 362 | static_image_dir=None) |
Darin Petkov | 8ef8345 | 2010-03-23 16:52:29 -0700 | [diff] [blame] | 363 | |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 364 | def ImportFactoryConfigFile(self, filename, validate_checksums=False): |
| 365 | """Imports a factory-floor server configuration file. The file should |
| 366 | be in this format: |
| 367 | config = [ |
| 368 | { |
| 369 | 'qual_ids': set([1, 2, 3, "x86-generic"]), |
| 370 | 'factory_image': 'generic-factory.gz', |
| 371 | 'factory_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 372 | 'release_image': 'generic-release.gz', |
| 373 | 'release_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 374 | 'oempartitionimg_image': 'generic-oem.gz', |
| 375 | 'oempartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Nick Sanders | e1eea92 | 2010-05-19 22:17:08 -0700 | [diff] [blame] | 376 | 'efipartitionimg_image': 'generic-efi.gz', |
| 377 | 'efipartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 378 | 'stateimg_image': 'generic-state.gz', |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 379 | 'stateimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Tom Wai-Hong Tam | dac3df1 | 2010-06-14 09:56:15 +0800 | [diff] [blame] | 380 | 'firmware_image': 'generic-firmware.gz', |
| 381 | 'firmware_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 382 | }, |
| 383 | { |
| 384 | 'qual_ids': set([6]), |
| 385 | 'factory_image': '6-factory.gz', |
| 386 | 'factory_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 387 | 'release_image': '6-release.gz', |
| 388 | 'release_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 389 | 'oempartitionimg_image': '6-oem.gz', |
| 390 | 'oempartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Nick Sanders | e1eea92 | 2010-05-19 22:17:08 -0700 | [diff] [blame] | 391 | 'efipartitionimg_image': '6-efi.gz', |
| 392 | 'efipartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 393 | 'stateimg_image': '6-state.gz', |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 394 | 'stateimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Tom Wai-Hong Tam | dac3df1 | 2010-06-14 09:56:15 +0800 | [diff] [blame] | 395 | 'firmware_image': '6-firmware.gz', |
| 396 | 'firmware_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 397 | }, |
| 398 | ] |
| 399 | The server will look for the files by name in the static files |
| 400 | directory. |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 401 | |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 402 | If validate_checksums is True, validates checksums and exits. If |
| 403 | a checksum mismatch is found, it's printed to the screen. |
| 404 | """ |
| 405 | f = open(filename, 'r') |
| 406 | output = {} |
| 407 | exec(f.read(), output) |
| 408 | self.factory_config = output['config'] |
| 409 | success = True |
| 410 | for stanza in self.factory_config: |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 411 | for key in stanza.copy().iterkeys(): |
| 412 | suffix = '_image' |
| 413 | if key.endswith(suffix): |
| 414 | kind = key[:-len(suffix)] |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 415 | stanza[kind + '_size'] = self._GetSize(os.path.join( |
| 416 | self.static_dir, stanza[kind + '_image'])) |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 417 | if validate_checksums: |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 418 | factory_checksum = self._GetHash(os.path.join(self.static_dir, |
| 419 | stanza[kind + '_image'])) |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 420 | if factory_checksum != stanza[kind + '_checksum']: |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 421 | print ('Error: checksum mismatch for %s. Expected "%s" but file ' |
| 422 | 'has checksum "%s".' % (stanza[kind + '_image'], |
| 423 | stanza[kind + '_checksum'], |
| 424 | factory_checksum)) |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 425 | success = False |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 426 | |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 427 | if validate_checksums: |
| 428 | if success is False: |
| 429 | raise Exception('Checksum mismatch in conf file.') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 430 | |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 431 | print 'Config file looks good.' |
| 432 | |
| 433 | def GetFactoryImage(self, board_id, channel): |
Nick Sanders | 723f326 | 2010-09-16 05:18:41 -0700 | [diff] [blame] | 434 | kind = channel.rsplit('-', 1)[0] |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 435 | for stanza in self.factory_config: |
| 436 | if board_id not in stanza['qual_ids']: |
| 437 | continue |
Nick Sanders | 15cd6ae | 2010-06-30 12:30:56 -0700 | [diff] [blame] | 438 | if kind + '_image' not in stanza: |
| 439 | break |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 440 | return (stanza[kind + '_image'], |
| 441 | stanza[kind + '_checksum'], |
| 442 | stanza[kind + '_size']) |
Nick Sanders | 15cd6ae | 2010-06-30 12:30:56 -0700 | [diff] [blame] | 443 | return (None, None, None) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 444 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 445 | def HandleFactoryRequest(self, board_id, channel): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 446 | (filename, checksum, size) = self.GetFactoryImage(board_id, channel) |
| 447 | if filename is None: |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 448 | _LogMessage('unable to find image for board %s' % board_id) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 449 | return self.GetNoUpdatePayload() |
Chris Sosa | 05f9516 | 2010-10-14 18:01:52 -0700 | [diff] [blame] | 450 | url = '%s/static/%s' % (self.hostname, filename) |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 451 | is_delta_format = self._IsDeltaFormatFile(filename) |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 452 | _LogMessage('returning update payload ' + url) |
Darin Petkov | 91436cb | 2010-09-28 08:52:17 -0700 | [diff] [blame] | 453 | # Factory install is using memento updater which is using the sha-1 hash so |
| 454 | # setting sha-256 to an empty string. |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 455 | return self.GetUpdatePayload(checksum, '', size, url, is_delta_format) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 456 | |
Chris Sosa | 2c048f1 | 2010-10-27 16:05:27 -0700 | [diff] [blame^] | 457 | def GenerateUpdatePayloadForNonFactory(self, static_image_dir): |
| 458 | """Generates an update for non-factory and returns True on success.""" |
| 459 | if self.use_cached and os.path.exists(os.path.join(static_image_dir, |
| 460 | 'update.gz')): |
| 461 | _LogMessage('Using cached image regardless of timestamps.') |
| 462 | return True |
| 463 | else: |
| 464 | if self.forced_image: |
| 465 | has_built_image = self.GenerateUpdateImage( |
| 466 | self.forced_image, move_to_static_dir=True, |
| 467 | static_image_dir=static_image_dir) |
| 468 | # Now that we've generated it, force devserver to use it. |
| 469 | self.use_cached = True |
| 470 | elif self.serve_only: |
| 471 | return self.GenerateImageFromZip(static_image_dir) |
| 472 | else: |
| 473 | return self.GenerateLatestUpdateImage(board_id, |
| 474 | client_version, |
| 475 | static_image_dir) |
| 476 | |
| 477 | def PreGenerateUpdate(self): |
| 478 | """Pre-generates an update. Does not work for factory or label updates.""" |
| 479 | # Does not work with factory config. |
| 480 | assert(not self.factory_config) |
| 481 | _LogMessage('Pre-generating the update payload.') |
| 482 | # Does not work with labels so just use static dir. |
| 483 | if self.GenerateUpdatePayloadForNonFactory(self.static_dir): |
| 484 | # Force the devserver to use the pre-generated payload. |
| 485 | self.use_cached = True |
| 486 | else: |
| 487 | _LogMessage('Failed to pre-generate update.') |
| 488 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 489 | def HandleUpdatePing(self, data, label=None): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 490 | """Handles an update ping from an update client. |
| 491 | |
| 492 | Args: |
| 493 | data: xml blob from client. |
| 494 | label: optional label for the update. |
| 495 | Returns: |
| 496 | Update payload message for client. |
| 497 | """ |
Chris Sosa | 9841e1c | 2010-10-14 10:51:45 -0700 | [diff] [blame] | 498 | # Set hostname as the hostname that the client is calling to and set up |
| 499 | # the url base. |
| 500 | self.hostname = cherrypy.request.base |
| 501 | if self.urlbase: |
| 502 | static_urlbase = self.urlbase |
| 503 | elif self.serve_only: |
| 504 | static_urlbase = '%s/static/archive' % self.hostname |
| 505 | else: |
| 506 | static_urlbase = '%s/static' % self.hostname |
| 507 | |
| 508 | _LogMessage('Using static url base %s' % static_urlbase) |
| 509 | _LogMessage('Handling update ping as %s: %s' % (self.hostname, data)) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 510 | |
| 511 | # Check the client prefix to make sure you can support this type of update. |
Chris Sosa | 9841e1c | 2010-10-14 10:51:45 -0700 | [diff] [blame] | 512 | update_dom = minidom.parseString(data) |
| 513 | root = update_dom.firstChild |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 514 | if (root.hasAttribute('updaterversion') and |
| 515 | not root.getAttribute('updaterversion').startswith(self.client_prefix)): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 516 | _LogMessage('Got update from unsupported updater:' + |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 517 | root.getAttribute('updaterversion')) |
Andrew de los Reyes | 9223f13 | 2010-05-07 17:08:17 -0700 | [diff] [blame] | 518 | return self.GetNoUpdatePayload() |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 519 | |
| 520 | # We only generate update payloads for updatecheck requests. |
| 521 | update_check = root.getElementsByTagName('o:updatecheck') |
| 522 | if not update_check: |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 523 | _LogMessage('Non-update check received. Returning blank payload.') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 524 | # TODO(sosa): Generate correct non-updatecheck payload to better test |
| 525 | # update clients. |
| 526 | return self.GetNoUpdatePayload() |
| 527 | |
| 528 | # Since this is an updatecheck, get information about the requester. |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 529 | query = root.getElementsByTagName('o:app')[0] |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 530 | client_version = query.getAttribute('version') |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 531 | channel = query.getAttribute('track') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 532 | board_id = (query.hasAttribute('board') and query.getAttribute('board') |
| 533 | or self._GetDefaultBoardID()) |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 534 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 535 | # Separate logic as Factory requests have static url's that override |
| 536 | # other options. |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 537 | if self.factory_config: |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 538 | return self.HandleFactoryRequest(board_id, channel) |
Nick Sanders | 723f326 | 2010-09-16 05:18:41 -0700 | [diff] [blame] | 539 | else: |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 540 | static_image_dir = self.static_dir |
| 541 | if label: |
| 542 | static_image_dir = os.path.join(static_image_dir, label) |
| 543 | |
Chris Sosa | 2c048f1 | 2010-10-27 16:05:27 -0700 | [diff] [blame^] | 544 | if self.GenerateUpdatePayloadForNonFactory(static_image_dir): |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 545 | filename = os.path.join(static_image_dir, 'update.gz') |
| 546 | hash = self._GetHash(filename) |
| 547 | sha256 = self._GetSHA256(filename) |
| 548 | size = self._GetSize(filename) |
| 549 | is_delta_format = self._IsDeltaFormatFile(filename) |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 550 | if label: |
Chris Sosa | 9841e1c | 2010-10-14 10:51:45 -0700 | [diff] [blame] | 551 | url = '%s/%s/update.gz' % (static_urlbase, label) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 552 | else: |
Chris Sosa | 9841e1c | 2010-10-14 10:51:45 -0700 | [diff] [blame] | 553 | url = '%s/update.gz' % static_urlbase |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 554 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 555 | _LogMessage('Responding to client to use url %s to get image.' % url) |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 556 | return self.GetUpdatePayload(hash, sha256, size, url, is_delta_format) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 557 | else: |
Nick Sanders | 723f326 | 2010-09-16 05:18:41 -0700 | [diff] [blame] | 558 | return self.GetNoUpdatePayload() |