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 |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 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 | 05491b1 | 2010-11-08 17:14:16 -0800 | [diff] [blame] | 11 | import subprocess |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 12 | import time |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 13 | import urlparse |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 14 | |
Chris Sosa | 05491b1 | 2010-11-08 17:14:16 -0800 | [diff] [blame] | 15 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 16 | def _LogMessage(message): |
| 17 | cherrypy.log(message, 'UPDATE') |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 18 | |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 19 | UPDATE_FILE = 'update.gz' |
| 20 | STATEFUL_FILE = 'stateful.tgz' |
| 21 | CACHE_DIR = 'cache' |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 22 | |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 23 | |
| 24 | def _ChangeUrlPort(url, new_port): |
| 25 | """Return the URL passed in with a different port""" |
| 26 | scheme, netloc, path, query, fragment = urlparse.urlsplit(url) |
| 27 | host_port = netloc.split(':') |
| 28 | |
| 29 | if len(host_port) == 1: |
| 30 | host_port.append(new_port) |
| 31 | else: |
| 32 | host_port[1] = new_port |
| 33 | |
| 34 | print host_port |
| 35 | netloc = "%s:%s" % tuple(host_port) |
| 36 | |
| 37 | return urlparse.urlunsplit((scheme, netloc, path, query, fragment)) |
| 38 | |
| 39 | |
rtc@google.com | 6424466 | 2009-11-12 00:52:08 +0000 | [diff] [blame] | 40 | class Autoupdate(BuildObject): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 41 | """Class that contains functionality that handles Chrome OS update pings. |
| 42 | |
| 43 | Members: |
Dale Curtis | 723ec47 | 2010-11-30 14:06:47 -0800 | [diff] [blame] | 44 | serve_only: Serve only pre-built updates. static_dir must contain update.gz |
| 45 | and stateful.tgz. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 46 | factory_config: Path to the factory config file if handling factory |
| 47 | requests. |
| 48 | use_test_image: Use chromiumos_test_image.bin rather than the standard. |
| 49 | static_url_base: base URL, other than devserver, for update images. |
| 50 | client_prefix: The prefix for the update engine client. |
| 51 | forced_image: Path to an image to use for all updates. |
Chris Sosa | 08d55a2 | 2011-01-19 16:08:02 -0800 | [diff] [blame] | 52 | forced_payload: Path to pre-generated payload to serve. |
| 53 | port: port to host devserver |
| 54 | proxy_port: port of local proxy to tell client to connect to you through. |
| 55 | src_image: If specified, creates a delta payload from this image. |
| 56 | vm: Set for VM images (doesn't patch kernel) |
| 57 | board: board for the image. Needed for pre-generating of updates. |
| 58 | copy_to_static_root: Copies images generated from the cache to |
| 59 | ~/static. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 60 | """ |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 61 | |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 62 | def __init__(self, serve_only=None, test_image=False, urlbase=None, |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 63 | factory_config_path=None, client_prefix=None, |
| 64 | forced_image=None, forced_payload=None, |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 65 | port=8080, proxy_port=None, src_image='', vm=False, board=None, |
Chris Sosa | 08d55a2 | 2011-01-19 16:08:02 -0800 | [diff] [blame] | 66 | copy_to_static_root=True, |
Chris Sosa | e67b78f | 2010-11-04 17:33:16 -0700 | [diff] [blame] | 67 | *args, **kwargs): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 68 | super(Autoupdate, self).__init__(*args, **kwargs) |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 69 | self.serve_only = serve_only |
Sean O'Connor | 1b4b076 | 2010-06-02 17:37:32 -0700 | [diff] [blame] | 70 | self.factory_config = factory_config_path |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 71 | self.use_test_image = test_image |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 72 | if urlbase: |
Chris Sosa | 9841e1c | 2010-10-14 10:51:45 -0700 | [diff] [blame] | 73 | self.urlbase = urlbase |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 74 | else: |
Chris Sosa | 9841e1c | 2010-10-14 10:51:45 -0700 | [diff] [blame] | 75 | self.urlbase = None |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 76 | |
Chris Sosa | b63a928 | 2010-09-02 10:43:23 -0700 | [diff] [blame] | 77 | self.client_prefix = client_prefix |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 78 | self.forced_image = forced_image |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 79 | self.forced_payload = forced_payload |
Chris Sosa | 62f720b | 2010-10-26 21:39:48 -0700 | [diff] [blame] | 80 | self.src_image = src_image |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 81 | self.proxy_port = proxy_port |
Chris Sosa | 4136e69 | 2010-10-28 23:42:37 -0700 | [diff] [blame] | 82 | self.vm = vm |
Chris Sosa | e67b78f | 2010-11-04 17:33:16 -0700 | [diff] [blame] | 83 | self.board = board |
Chris Sosa | 08d55a2 | 2011-01-19 16:08:02 -0800 | [diff] [blame] | 84 | self.copy_to_static_root = copy_to_static_root |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 85 | |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 86 | # Path to pre-generated file. |
| 87 | self.pregenerated_path = None |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 88 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 89 | def _GetSecondsSinceMidnight(self): |
| 90 | """Returns the seconds since midnight as a decimal value.""" |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 91 | now = time.localtime() |
| 92 | return now[3] * 3600 + now[4] * 60 + now[5] |
| 93 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 94 | def _GetDefaultBoardID(self): |
| 95 | """Returns the default board id stored in .default_board.""" |
| 96 | board_file = '%s/.default_board' % (self.scripts_dir) |
| 97 | try: |
| 98 | return open(board_file).read() |
| 99 | except IOError: |
| 100 | return 'x86-generic' |
| 101 | |
| 102 | def _GetLatestImageDir(self, board_id): |
| 103 | """Returns the latest image dir based on shell script.""" |
| 104 | cmd = '%s/get_latest_image.sh --board %s' % (self.scripts_dir, board_id) |
| 105 | return os.popen(cmd).read().strip() |
| 106 | |
| 107 | def _GetVersionFromDir(self, image_dir): |
| 108 | """Returns the version of the image based on the name of the directory.""" |
| 109 | latest_version = os.path.basename(image_dir) |
| 110 | return latest_version.split('-')[0] |
| 111 | |
| 112 | def _CanUpdate(self, client_version, latest_version): |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 113 | """Returns true if the latest_version is greater than the client_version. |
| 114 | """ |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 115 | client_tokens = client_version.replace('_', '').split('.') |
| 116 | latest_tokens = latest_version.replace('_', '').split('.') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 117 | _LogMessage('client version %s latest version %s' |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 118 | % (client_version, latest_version)) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 119 | for i in range(4): |
| 120 | if int(latest_tokens[i]) == int(client_tokens[i]): |
| 121 | continue |
| 122 | return int(latest_tokens[i]) > int(client_tokens[i]) |
| 123 | return False |
| 124 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 125 | def _UnpackZip(self, image_dir): |
| 126 | """Unpacks an image.zip into a given directory.""" |
| 127 | image = os.path.join(image_dir, self._GetImageName()) |
| 128 | if os.path.exists(image): |
| 129 | return True |
| 130 | else: |
| 131 | # -n, never clobber an existing file, in case we get invoked |
| 132 | # simultaneously by multiple request handlers. This means that |
| 133 | # we're assuming each image.zip file lives in a versioned |
| 134 | # directory (a la Buildbot). |
| 135 | return os.system('cd %s && unzip -n image.zip' % image_dir) == 0 |
| 136 | |
| 137 | def _GetImageName(self): |
| 138 | """Returns the name of the image that should be used.""" |
| 139 | if self.use_test_image: |
| 140 | image_name = 'chromiumos_test_image.bin' |
| 141 | else: |
| 142 | image_name = 'chromiumos_image.bin' |
| 143 | return image_name |
| 144 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 145 | def _GetSize(self, update_path): |
| 146 | """Returns the size of the file given.""" |
| 147 | return os.path.getsize(update_path) |
| 148 | |
| 149 | def _GetHash(self, update_path): |
| 150 | """Returns the sha1 of the file given.""" |
| 151 | cmd = ('cat %s | openssl sha1 -binary | openssl base64 | tr \'\\n\' \' \';' |
| 152 | % update_path) |
| 153 | return os.popen(cmd).read().rstrip() |
| 154 | |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 155 | def _IsDeltaFormatFile(self, filename): |
| 156 | try: |
| 157 | file_handle = open(filename, 'r') |
| 158 | delta_magic = 'CrAU' |
| 159 | magic = file_handle.read(len(delta_magic)) |
| 160 | return magic == delta_magic |
| 161 | except Exception: |
| 162 | return False |
| 163 | |
Darin Petkov | 91436cb | 2010-09-28 08:52:17 -0700 | [diff] [blame] | 164 | # TODO(petkov): Consider optimizing getting both SHA-1 and SHA-256 so that |
| 165 | # it takes advantage of reduced I/O and multiple processors. Something like: |
| 166 | # % tee < FILE > /dev/null \ |
| 167 | # >( openssl dgst -sha256 -binary | openssl base64 ) \ |
| 168 | # >( openssl sha1 -binary | openssl base64 ) |
| 169 | def _GetSHA256(self, update_path): |
| 170 | """Returns the sha256 of the file given.""" |
| 171 | cmd = ('cat %s | openssl dgst -sha256 -binary | openssl base64' % |
| 172 | update_path) |
| 173 | return os.popen(cmd).read().rstrip() |
| 174 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 175 | def _GetMd5(self, update_path): |
| 176 | """Returns the md5 checksum of the file given.""" |
| 177 | cmd = ("md5sum %s | awk '{print $1}'" % update_path) |
| 178 | return os.popen(cmd).read().rstrip() |
| 179 | |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 180 | def _Copy(self, source, dest): |
| 181 | """Copies a file from dest to source (if different)""" |
| 182 | _LogMessage('Copy File %s -> %s' % (source, dest)) |
| 183 | if os.path.lexists(dest): |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 184 | os.remove(dest) |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 185 | shutil.copy(source, dest) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 186 | |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 187 | def GetUpdatePayload(self, hash, sha256, size, url, is_delta_format): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 188 | """Returns a payload to the client corresponding to a new update. |
| 189 | |
| 190 | Args: |
| 191 | hash: hash of update blob |
Darin Petkov | 91436cb | 2010-09-28 08:52:17 -0700 | [diff] [blame] | 192 | sha256: SHA-256 hash of update blob |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 193 | size: size of update blob |
| 194 | url: where to find update blob |
| 195 | Returns: |
| 196 | Xml string to be passed back to client. |
| 197 | """ |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 198 | delta = 'false' |
| 199 | if is_delta_format: |
| 200 | delta = 'true' |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 201 | payload = """<?xml version="1.0" encoding="UTF-8"?> |
| 202 | <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0"> |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 203 | <daystart elapsed_seconds="%s"/> |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 204 | <app appid="{%s}" status="ok"> |
| 205 | <ping status="ok"/> |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 206 | <updatecheck |
| 207 | codebase="%s" |
| 208 | hash="%s" |
Darin Petkov | 91436cb | 2010-09-28 08:52:17 -0700 | [diff] [blame] | 209 | sha256="%s" |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 210 | needsadmin="false" |
| 211 | size="%s" |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 212 | IsDelta="%s" |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 213 | status="ok"/> |
| 214 | </app> |
| 215 | </gupdate> |
| 216 | """ |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 217 | return payload % (self._GetSecondsSinceMidnight(), |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 218 | self.app_id, url, hash, sha256, size, delta) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 219 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 220 | def GetNoUpdatePayload(self): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 221 | """Returns a payload to the client corresponding to no update.""" |
Darin Petkov | 845f117 | 2011-01-05 14:45:24 -0800 | [diff] [blame] | 222 | payload = """<?xml version="1.0" encoding="UTF-8"?> |
| 223 | <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0"> |
| 224 | <daystart elapsed_seconds="%s"/> |
| 225 | <app appid="{%s}" status="ok"> |
| 226 | <ping status="ok"/> |
| 227 | <updatecheck status="noupdate"/> |
| 228 | </app> |
| 229 | </gupdate> |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 230 | """ |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 231 | return payload % (self._GetSecondsSinceMidnight(), self.app_id) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 232 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 233 | def GenerateUpdateFile(self, src_image, image_path, output_dir): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 234 | """Generates an update gz given a full path to an image. |
| 235 | |
| 236 | Args: |
| 237 | image_path: Full path to image. |
| 238 | Returns: |
| 239 | Path to created update_payload or None on error. |
| 240 | """ |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 241 | update_path = os.path.join(output_dir, UPDATE_FILE) |
Chris Sosa | 4136e69 | 2010-10-28 23:42:37 -0700 | [diff] [blame] | 242 | patch_kernel_flag = '--patch_kernel' |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 243 | _LogMessage('Generating update image %s' % update_path) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 244 | |
Chris Sosa | 4136e69 | 2010-10-28 23:42:37 -0700 | [diff] [blame] | 245 | # Don't patch the kernel for vm images as they don't need the patch. |
| 246 | if self.vm: |
| 247 | patch_kernel_flag = '' |
| 248 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 249 | mkupdate_command = ( |
Chris Sosa | 62f720b | 2010-10-26 21:39:48 -0700 | [diff] [blame] | 250 | '%s/cros_generate_update_payload --image="%s" --output="%s" ' |
Chris Sosa | 4136e69 | 2010-10-28 23:42:37 -0700 | [diff] [blame] | 251 | '%s --noold_style --src_image="%s"' % ( |
| 252 | self.scripts_dir, image_path, update_path, patch_kernel_flag, |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 253 | src_image)) |
Chris Sosa | 62f720b | 2010-10-26 21:39:48 -0700 | [diff] [blame] | 254 | _LogMessage(mkupdate_command) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 255 | if os.system(mkupdate_command) != 0: |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 256 | _LogMessage('Failed to create update payload') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 257 | return None |
| 258 | |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 259 | return UPDATE_FILE |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 260 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 261 | def GenerateStatefulFile(self, image_path, output_dir): |
| 262 | """Generates a stateful update payload given a full path to an image. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 263 | |
| 264 | Args: |
| 265 | image_path: Full path to image. |
| 266 | Returns: |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 267 | Path to created stateful update_payload or None on error. |
Chris Sosa | 908fd6f | 2010-11-10 17:31:18 -0800 | [diff] [blame] | 268 | Raises: |
| 269 | A subprocess exception if the update generator fails to generate a |
| 270 | stateful payload. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 271 | """ |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 272 | output_gz = os.path.join(output_dir, STATEFUL_FILE) |
Chris Sosa | 908fd6f | 2010-11-10 17:31:18 -0800 | [diff] [blame] | 273 | subprocess.check_call( |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 274 | ['%s/cros_generate_stateful_update_payload' % self.scripts_dir, |
Chris Sosa | 908fd6f | 2010-11-10 17:31:18 -0800 | [diff] [blame] | 275 | '--image=%s' % image_path, |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 276 | '--output_dir=%s' % output_dir, |
Chris Sosa | 908fd6f | 2010-11-10 17:31:18 -0800 | [diff] [blame] | 277 | ]) |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 278 | return STATEFUL_FILE |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 279 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 280 | def FindCachedUpdateImageSubDir(self, src_image, dest_image): |
| 281 | """Find directory to store a cached update. |
| 282 | |
| 283 | Given one, or two images for an update, this finds which |
| 284 | cache directory should hold the update files, even if they don't exist |
| 285 | yet. The directory will be inside static_image_dir, and of the form: |
| 286 | |
| 287 | Non-delta updates: |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 288 | CACHE_DIR/12345678 |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 289 | |
| 290 | Delta updates: |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 291 | CACHE_DIR/12345678_12345678 |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 292 | """ |
| 293 | # If there is no src, we only have an image file, check image for changes |
| 294 | if not src_image: |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 295 | return os.path.join(CACHE_DIR, self._GetMd5(dest_image)) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 296 | |
| 297 | # If we have src and dest, we are a delta, and check both for changes |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 298 | return os.path.join(CACHE_DIR, |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 299 | "%s_%s" % (self._GetMd5(src_image), |
| 300 | self._GetMd5(dest_image))) |
| 301 | |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 302 | def GenerateUpdateImage(self, image_path, output_dir): |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 303 | """Force generates an update payload based on the given image_path. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 304 | |
Chris Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame] | 305 | Args: |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 306 | src_image: image we are updating from (Null/empty for non-delta) |
| 307 | image_path: full path to the image. |
| 308 | output_dir: the directory to write the update payloads in |
Chris Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame] | 309 | Returns: |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 310 | update payload name relative to output_dir |
Chris Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame] | 311 | """ |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 312 | update_file = None |
| 313 | stateful_update_file = None |
Andrew de los Reyes | 9a52871 | 2010-06-30 10:29:43 -0700 | [diff] [blame] | 314 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 315 | # Actually do the generation |
| 316 | _LogMessage('Generating update for image %s' % image_path) |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 317 | update_file = self.GenerateUpdateFile(self.src_image, |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 318 | image_path, |
| 319 | output_dir) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 320 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 321 | if update_file: |
| 322 | stateful_update_file = self.GenerateStatefulFile(image_path, |
| 323 | output_dir) |
| 324 | |
| 325 | if update_file and stateful_update_file: |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 326 | return update_file |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 327 | else: |
| 328 | _LogMessage('Failed to generate update.') |
| 329 | return None |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 330 | |
| 331 | def GenerateUpdateImageWithCache(self, image_path, static_image_dir): |
| 332 | """Force generates an update payload based on the given image_path. |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 333 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 334 | Args: |
| 335 | image_path: full path to the image. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 336 | static_image_dir: the directory to move images to after generating. |
| 337 | Returns: |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 338 | update filename (not directory) relative to static_image_dir on success, |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 339 | or None. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 340 | """ |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 341 | _LogMessage('Generating update for src %s image %s' % (self.src_image, |
| 342 | image_path)) |
Chris Sosa | e67b78f | 2010-11-04 17:33:16 -0700 | [diff] [blame] | 343 | |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 344 | # If it was pregenerated_path, don't regenerate |
| 345 | if self.pregenerated_path: |
| 346 | return self.pregenerated_path |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 347 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 348 | # Which sub_dir of static_image_dir should hold our cached update image |
| 349 | cache_sub_dir = self.FindCachedUpdateImageSubDir(self.src_image, image_path) |
| 350 | _LogMessage('Caching in sub_dir "%s"' % cache_sub_dir) |
| 351 | |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 352 | update_path = os.path.join(cache_sub_dir, UPDATE_FILE) |
| 353 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 354 | # The cached payloads exist in a cache dir |
| 355 | cache_update_payload = os.path.join(static_image_dir, |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 356 | update_path) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 357 | cache_stateful_payload = os.path.join(static_image_dir, |
| 358 | cache_sub_dir, |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 359 | STATEFUL_FILE) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 360 | |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 361 | # Check to see if this cache directory is valid. |
| 362 | if not os.path.exists(cache_update_payload) or not os.path.exists( |
| 363 | cache_stateful_payload): |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 364 | full_cache_dir = os.path.join(static_image_dir, cache_sub_dir) |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 365 | # Clean up stale state. |
| 366 | os.system('rm -rf "%s"' % full_cache_dir) |
| 367 | os.makedirs(full_cache_dir) |
| 368 | return_path = self.GenerateUpdateImage(image_path, |
| 369 | full_cache_dir) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 370 | |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 371 | # Clean up cache dir since it's not valid. |
| 372 | if not return_path: |
| 373 | os.system('rm -rf "%s"' % full_cache_dir) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 374 | return None |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 375 | else: |
| 376 | assert (return_path == update_path, |
| 377 | 'Returned path %s not equal to %s' % (return_path, update_path)) |
| 378 | |
| 379 | self.pregenerated_path = update_path |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 380 | |
Chris Sosa | 08d55a2 | 2011-01-19 16:08:02 -0800 | [diff] [blame] | 381 | # Generation complete, copy if requested. |
| 382 | if self.copy_to_static_root: |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 383 | # The final results exist directly in static |
| 384 | update_payload = os.path.join(static_image_dir, |
| 385 | UPDATE_FILE) |
| 386 | stateful_payload = os.path.join(static_image_dir, |
| 387 | STATEFUL_FILE) |
Chris Sosa | 08d55a2 | 2011-01-19 16:08:02 -0800 | [diff] [blame] | 388 | self._Copy(cache_update_payload, update_payload) |
| 389 | self._Copy(cache_stateful_payload, stateful_payload) |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 390 | return UPDATE_FILE |
| 391 | else: |
| 392 | return self.pregenerated_path |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 393 | |
| 394 | def GenerateLatestUpdateImage(self, board_id, client_version, |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 395 | static_image_dir): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 396 | """Generates an update using the latest image that has been built. |
| 397 | |
| 398 | This will only generate an update if the newest update is newer than that |
| 399 | on the client or client_version is 'ForcedUpdate'. |
| 400 | |
| 401 | Args: |
| 402 | board_id: Name of the board. |
| 403 | client_version: Current version of the client or 'ForcedUpdate' |
| 404 | static_image_dir: the directory to move images to after generating. |
| 405 | Returns: |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 406 | Name of the update image relative to static_image_dir or None |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 407 | """ |
| 408 | latest_image_dir = self._GetLatestImageDir(board_id) |
| 409 | latest_version = self._GetVersionFromDir(latest_image_dir) |
| 410 | latest_image_path = os.path.join(latest_image_dir, self._GetImageName()) |
| 411 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 412 | _LogMessage('Preparing to generate update from latest built image %s.' % |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 413 | latest_image_path) |
| 414 | |
| 415 | # Check to see whether or not we should update. |
| 416 | if client_version != 'ForcedUpdate' and not self._CanUpdate( |
| 417 | client_version, latest_version): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 418 | _LogMessage('no update') |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 419 | return None |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 420 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 421 | return self.GenerateUpdateImageWithCache(latest_image_path, |
| 422 | static_image_dir=static_image_dir) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 423 | |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 424 | def ImportFactoryConfigFile(self, filename, validate_checksums=False): |
| 425 | """Imports a factory-floor server configuration file. The file should |
| 426 | be in this format: |
| 427 | config = [ |
| 428 | { |
| 429 | 'qual_ids': set([1, 2, 3, "x86-generic"]), |
| 430 | 'factory_image': 'generic-factory.gz', |
| 431 | 'factory_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 432 | 'release_image': 'generic-release.gz', |
| 433 | 'release_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 434 | 'oempartitionimg_image': 'generic-oem.gz', |
| 435 | 'oempartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Nick Sanders | e1eea92 | 2010-05-19 22:17:08 -0700 | [diff] [blame] | 436 | 'efipartitionimg_image': 'generic-efi.gz', |
| 437 | 'efipartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 438 | 'stateimg_image': 'generic-state.gz', |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 439 | 'stateimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Tom Wai-Hong Tam | dac3df1 | 2010-06-14 09:56:15 +0800 | [diff] [blame] | 440 | 'firmware_image': 'generic-firmware.gz', |
| 441 | 'firmware_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 442 | }, |
| 443 | { |
| 444 | 'qual_ids': set([6]), |
| 445 | 'factory_image': '6-factory.gz', |
| 446 | 'factory_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 447 | 'release_image': '6-release.gz', |
| 448 | 'release_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 449 | 'oempartitionimg_image': '6-oem.gz', |
| 450 | 'oempartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Nick Sanders | e1eea92 | 2010-05-19 22:17:08 -0700 | [diff] [blame] | 451 | 'efipartitionimg_image': '6-efi.gz', |
| 452 | 'efipartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 453 | 'stateimg_image': '6-state.gz', |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 454 | 'stateimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Tom Wai-Hong Tam | dac3df1 | 2010-06-14 09:56:15 +0800 | [diff] [blame] | 455 | 'firmware_image': '6-firmware.gz', |
| 456 | 'firmware_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 457 | }, |
| 458 | ] |
| 459 | The server will look for the files by name in the static files |
| 460 | directory. |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 461 | |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 462 | If validate_checksums is True, validates checksums and exits. If |
| 463 | a checksum mismatch is found, it's printed to the screen. |
| 464 | """ |
| 465 | f = open(filename, 'r') |
| 466 | output = {} |
| 467 | exec(f.read(), output) |
| 468 | self.factory_config = output['config'] |
| 469 | success = True |
| 470 | for stanza in self.factory_config: |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 471 | for key in stanza.copy().iterkeys(): |
| 472 | suffix = '_image' |
| 473 | if key.endswith(suffix): |
| 474 | kind = key[:-len(suffix)] |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 475 | stanza[kind + '_size'] = self._GetSize(os.path.join( |
| 476 | self.static_dir, stanza[kind + '_image'])) |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 477 | if validate_checksums: |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 478 | factory_checksum = self._GetHash(os.path.join(self.static_dir, |
| 479 | stanza[kind + '_image'])) |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 480 | if factory_checksum != stanza[kind + '_checksum']: |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 481 | print ('Error: checksum mismatch for %s. Expected "%s" but file ' |
| 482 | 'has checksum "%s".' % (stanza[kind + '_image'], |
| 483 | stanza[kind + '_checksum'], |
| 484 | factory_checksum)) |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 485 | success = False |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 486 | |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 487 | if validate_checksums: |
| 488 | if success is False: |
| 489 | raise Exception('Checksum mismatch in conf file.') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 490 | |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 491 | print 'Config file looks good.' |
| 492 | |
| 493 | def GetFactoryImage(self, board_id, channel): |
Nick Sanders | 723f326 | 2010-09-16 05:18:41 -0700 | [diff] [blame] | 494 | kind = channel.rsplit('-', 1)[0] |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 495 | for stanza in self.factory_config: |
| 496 | if board_id not in stanza['qual_ids']: |
| 497 | continue |
Nick Sanders | 15cd6ae | 2010-06-30 12:30:56 -0700 | [diff] [blame] | 498 | if kind + '_image' not in stanza: |
| 499 | break |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 500 | return (stanza[kind + '_image'], |
| 501 | stanza[kind + '_checksum'], |
| 502 | stanza[kind + '_size']) |
Nick Sanders | 15cd6ae | 2010-06-30 12:30:56 -0700 | [diff] [blame] | 503 | return (None, None, None) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 504 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 505 | def HandleFactoryRequest(self, board_id, channel): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 506 | (filename, checksum, size) = self.GetFactoryImage(board_id, channel) |
| 507 | if filename is None: |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 508 | _LogMessage('unable to find image for board %s' % board_id) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 509 | return self.GetNoUpdatePayload() |
Chris Sosa | 05f9516 | 2010-10-14 18:01:52 -0700 | [diff] [blame] | 510 | url = '%s/static/%s' % (self.hostname, filename) |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 511 | is_delta_format = self._IsDeltaFormatFile(filename) |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 512 | _LogMessage('returning update payload ' + url) |
Darin Petkov | 91436cb | 2010-09-28 08:52:17 -0700 | [diff] [blame] | 513 | # Factory install is using memento updater which is using the sha-1 hash so |
| 514 | # setting sha-256 to an empty string. |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 515 | return self.GetUpdatePayload(checksum, '', size, url, is_delta_format) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 516 | |
Chris Sosa | 151643e | 2010-10-28 14:40:57 -0700 | [diff] [blame] | 517 | def GenerateUpdatePayloadForNonFactory(self, board_id, client_version, |
| 518 | static_image_dir): |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 519 | """Generates an update for non-factory image. |
Don Garrett | 710470d | 2010-11-15 17:43:44 -0800 | [diff] [blame] | 520 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 521 | Returns: |
| 522 | file name relative to static_image_dir on success. |
| 523 | """ |
Dale Curtis | 723ec47 | 2010-11-30 14:06:47 -0800 | [diff] [blame] | 524 | dest_path = os.path.join(static_image_dir, UPDATE_FILE) |
| 525 | dest_stateful = os.path.join(static_image_dir, STATEFUL_FILE) |
| 526 | |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 527 | if self.forced_payload: |
| 528 | # If the forced payload is not already in our static_image_dir, |
| 529 | # copy it there. |
Don Garrett | ee25e55 | 2010-11-23 12:09:35 -0800 | [diff] [blame] | 530 | src_path = os.path.abspath(self.forced_payload) |
Don Garrett | ee25e55 | 2010-11-23 12:09:35 -0800 | [diff] [blame] | 531 | src_stateful = os.path.join(os.path.dirname(src_path), |
| 532 | STATEFUL_FILE) |
Don Garrett | ee25e55 | 2010-11-23 12:09:35 -0800 | [diff] [blame] | 533 | |
| 534 | # Only copy the files if the source directory is different from dest. |
| 535 | if os.path.dirname(src_path) != os.path.abspath(static_image_dir): |
| 536 | self._Copy(src_path, dest_path) |
| 537 | |
| 538 | # The stateful payload is optional. |
| 539 | if os.path.exists(src_stateful): |
| 540 | self._Copy(src_stateful, dest_stateful) |
| 541 | else: |
| 542 | _LogMessage('WARN: %s not found. Expected for dev and test builds.' % |
| 543 | STATEFUL_FILE) |
| 544 | if os.path.exists(dest_stateful): |
| 545 | os.remove(dest_stateful) |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 546 | |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 547 | return UPDATE_FILE |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 548 | elif self.forced_image: |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 549 | return self.GenerateUpdateImageWithCache( |
| 550 | self.forced_image, |
| 551 | static_image_dir=static_image_dir) |
| 552 | elif self.serve_only: |
Dale Curtis | 723ec47 | 2010-11-30 14:06:47 -0800 | [diff] [blame] | 553 | # Warn if update or stateful files can't be found. |
| 554 | if not os.path.exists(dest_path): |
| 555 | _LogMessage('WARN: %s not found. Expected for dev and test builds.' % |
| 556 | UPDATE_FILE) |
| 557 | |
| 558 | if not os.path.exists(dest_stateful): |
| 559 | _LogMessage('WARN: %s not found. Expected for dev and test builds.' % |
| 560 | STATEFUL_FILE) |
| 561 | |
| 562 | return UPDATE_FILE |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 563 | else: |
| 564 | if board_id: |
| 565 | return self.GenerateLatestUpdateImage(board_id, |
| 566 | client_version, |
| 567 | static_image_dir) |
| 568 | |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 569 | _LogMessage('Failed to genereate update. ' |
| 570 | 'You must set --board when pre-generating latest update.') |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 571 | return None |
Chris Sosa | 2c048f1 | 2010-10-27 16:05:27 -0700 | [diff] [blame] | 572 | |
| 573 | def PreGenerateUpdate(self): |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 574 | """Pre-generates an update and prints out the relative path it. |
| 575 | |
| 576 | Returns relative path of the update on success. |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 577 | """ |
Chris Sosa | 2c048f1 | 2010-10-27 16:05:27 -0700 | [diff] [blame] | 578 | # Does not work with factory config. |
| 579 | assert(not self.factory_config) |
| 580 | _LogMessage('Pre-generating the update payload.') |
| 581 | # Does not work with labels so just use static dir. |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 582 | pregenerated_update = self.GenerateUpdatePayloadForNonFactory( |
| 583 | self.board, '0.0.0.0', self.static_dir) |
| 584 | if pregenerated_update: |
| 585 | print 'PREGENERATED_UPDATE=%s' % pregenerated_update |
| 586 | |
| 587 | return pregenerated_update |
Chris Sosa | 2c048f1 | 2010-10-27 16:05:27 -0700 | [diff] [blame] | 588 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 589 | def HandleUpdatePing(self, data, label=None): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 590 | """Handles an update ping from an update client. |
| 591 | |
| 592 | Args: |
| 593 | data: xml blob from client. |
| 594 | label: optional label for the update. |
| 595 | Returns: |
| 596 | Update payload message for client. |
| 597 | """ |
Chris Sosa | 9841e1c | 2010-10-14 10:51:45 -0700 | [diff] [blame] | 598 | # Set hostname as the hostname that the client is calling to and set up |
| 599 | # the url base. |
| 600 | self.hostname = cherrypy.request.base |
| 601 | if self.urlbase: |
| 602 | static_urlbase = self.urlbase |
| 603 | elif self.serve_only: |
| 604 | static_urlbase = '%s/static/archive' % self.hostname |
| 605 | else: |
| 606 | static_urlbase = '%s/static' % self.hostname |
| 607 | |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 608 | # If we have a proxy port, adjust the URL we instruct the client to |
| 609 | # use to go through the proxy. |
| 610 | if self.proxy_port: |
| 611 | static_urlbase = _ChangeUrlPort(static_urlbase, self.proxy_port) |
| 612 | |
Chris Sosa | 9841e1c | 2010-10-14 10:51:45 -0700 | [diff] [blame] | 613 | _LogMessage('Using static url base %s' % static_urlbase) |
| 614 | _LogMessage('Handling update ping as %s: %s' % (self.hostname, data)) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 615 | |
| 616 | # 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] | 617 | update_dom = minidom.parseString(data) |
| 618 | root = update_dom.firstChild |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 619 | if (root.hasAttribute('updaterversion') and |
| 620 | not root.getAttribute('updaterversion').startswith(self.client_prefix)): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 621 | _LogMessage('Got update from unsupported updater:' + |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 622 | root.getAttribute('updaterversion')) |
Andrew de los Reyes | 9223f13 | 2010-05-07 17:08:17 -0700 | [diff] [blame] | 623 | return self.GetNoUpdatePayload() |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 624 | |
| 625 | # We only generate update payloads for updatecheck requests. |
| 626 | update_check = root.getElementsByTagName('o:updatecheck') |
| 627 | if not update_check: |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 628 | _LogMessage('Non-update check received. Returning blank payload.') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 629 | # TODO(sosa): Generate correct non-updatecheck payload to better test |
| 630 | # update clients. |
| 631 | return self.GetNoUpdatePayload() |
| 632 | |
| 633 | # Since this is an updatecheck, get information about the requester. |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 634 | query = root.getElementsByTagName('o:app')[0] |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 635 | client_version = query.getAttribute('version') |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 636 | channel = query.getAttribute('track') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 637 | board_id = (query.hasAttribute('board') and query.getAttribute('board') |
| 638 | or self._GetDefaultBoardID()) |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 639 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 640 | # Separate logic as Factory requests have static url's that override |
| 641 | # other options. |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 642 | if self.factory_config: |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 643 | return self.HandleFactoryRequest(board_id, channel) |
Nick Sanders | 723f326 | 2010-09-16 05:18:41 -0700 | [diff] [blame] | 644 | else: |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 645 | static_image_dir = self.static_dir |
| 646 | if label: |
| 647 | static_image_dir = os.path.join(static_image_dir, label) |
| 648 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 649 | payload_path = self.GenerateUpdatePayloadForNonFactory(board_id, |
| 650 | client_version, |
| 651 | static_image_dir) |
| 652 | if payload_path: |
| 653 | filename = os.path.join(static_image_dir, payload_path) |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 654 | hash = self._GetHash(filename) |
| 655 | sha256 = self._GetSHA256(filename) |
| 656 | size = self._GetSize(filename) |
| 657 | is_delta_format = self._IsDeltaFormatFile(filename) |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 658 | if label: |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 659 | url = '%s/%s/%s' % (static_urlbase, label, payload_path) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 660 | else: |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 661 | url = '%s/%s' % (static_urlbase, payload_path) |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 662 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 663 | _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] | 664 | return self.GetUpdatePayload(hash, sha256, size, url, is_delta_format) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 665 | else: |
Nick Sanders | 723f326 | 2010-09-16 05:18:41 -0700 | [diff] [blame] | 666 | return self.GetNoUpdatePayload() |