rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 1 | # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 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 | |
| 8 | import os |
Darin Petkov | 798fe7d | 2010-03-22 15:18:13 -0700 | [diff] [blame] | 9 | import shutil |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 10 | import sys |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 11 | import time |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 12 | import web |
| 13 | |
rtc@google.com | 6424466 | 2009-11-12 00:52:08 +0000 | [diff] [blame] | 14 | class Autoupdate(BuildObject): |
Darin Petkov | 798fe7d | 2010-03-22 15:18:13 -0700 | [diff] [blame] | 15 | # Basic functionality of handling ChromeOS autoupdate pings |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 16 | # and building/serving update images. |
| 17 | # TODO(rtc): Clean this code up and write some tests. |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 18 | |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 19 | def __init__(self, serve_only=None, test_image=False, urlbase=None, |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 20 | factory_config_path=None, validate_factory_config=None, |
Andrew de los Reyes | fb4444b | 2010-06-29 18:11:28 -0700 | [diff] [blame] | 21 | client_prefix=None, |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 22 | *args, **kwargs): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 23 | super(Autoupdate, self).__init__(*args, **kwargs) |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 24 | self.serve_only = serve_only |
Sean O'Connor | 1b4b076 | 2010-06-02 17:37:32 -0700 | [diff] [blame] | 25 | self.factory_config = factory_config_path |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 26 | self.test_image = test_image |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 27 | self.static_urlbase = urlbase |
Chris Sosa | b63a928 | 2010-09-02 10:43:23 -0700 | [diff] [blame^] | 28 | self.client_prefix = client_prefix |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 29 | if serve_only: |
| 30 | # If we're serving out of an archived build dir (e.g. a |
| 31 | # buildbot), prepare this webserver's magic 'static/' dir with a |
| 32 | # link to the build archive. |
| 33 | web.debug('Autoupdate in "serve update images only" mode.') |
| 34 | if os.path.exists('static/archive'): |
Sean O'Connor | 1b4b076 | 2010-06-02 17:37:32 -0700 | [diff] [blame] | 35 | if self.static_dir != os.readlink('static/archive'): |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 36 | web.debug('removing stale symlink to %s' % self.static_dir) |
| 37 | os.unlink('static/archive') |
Sean O'Connor | 1b4b076 | 2010-06-02 17:37:32 -0700 | [diff] [blame] | 38 | os.symlink(self.static_dir, 'static/archive') |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 39 | else: |
Sean O'Connor | 1b4b076 | 2010-06-02 17:37:32 -0700 | [diff] [blame] | 40 | os.symlink(self.static_dir, 'static/archive') |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 41 | if factory_config_path is not None: |
| 42 | self.ImportFactoryConfigFile(factory_config_path, validate_factory_config) |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 43 | |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 44 | def GetSecondsSinceMidnight(self): |
| 45 | now = time.localtime() |
| 46 | return now[3] * 3600 + now[4] * 60 + now[5] |
| 47 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 48 | def GetUpdatePayload(self, hash, size, url): |
| 49 | payload = """<?xml version="1.0" encoding="UTF-8"?> |
| 50 | <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0"> |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 51 | <daystart elapsed_seconds="%s"/> |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 52 | <app appid="{%s}" status="ok"> |
| 53 | <ping status="ok"/> |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 54 | <updatecheck |
| 55 | codebase="%s" |
| 56 | hash="%s" |
| 57 | needsadmin="false" |
| 58 | size="%s" |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 59 | status="ok"/> |
| 60 | </app> |
| 61 | </gupdate> |
| 62 | """ |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 63 | return payload % (self.GetSecondsSinceMidnight(), |
| 64 | self.app_id, url, hash, size) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 65 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 66 | def GetNoUpdatePayload(self): |
| 67 | payload = """<?xml version="1.0" encoding="UTF-8"?> |
| 68 | <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0"> |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 69 | <daystart elapsed_seconds="%s"/> |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 70 | <app appid="{%s}" status="ok"> |
| 71 | <ping status="ok"/> |
| 72 | <updatecheck status="noupdate"/> |
| 73 | </app> |
| 74 | </gupdate> |
| 75 | """ |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 76 | return payload % (self.GetSecondsSinceMidnight(), self.app_id) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 77 | |
Andrew de los Reyes | 9a52871 | 2010-06-30 10:29:43 -0700 | [diff] [blame] | 78 | def GetDefaultBoardID(self): |
| 79 | board_file = '%s/.default_board' % (self.scripts_dir) |
| 80 | try: |
| 81 | return open(board_file).read() |
| 82 | except IOError: |
| 83 | return 'x86-generic' |
| 84 | |
Sam Leffler | 7638204 | 2010-02-18 09:58:42 -0800 | [diff] [blame] | 85 | def GetLatestImagePath(self, board_id): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 86 | cmd = '%s/get_latest_image.sh --board %s' % (self.scripts_dir, board_id) |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 87 | return os.popen(cmd).read().strip() |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 88 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 89 | def GetLatestVersion(self, latest_image_path): |
| 90 | latest_version = latest_image_path.split('/')[-1] |
Ryan Cairns | 1b05beb | 2010-02-05 17:05:24 -0800 | [diff] [blame] | 91 | |
| 92 | # Removes the portage build prefix. |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 93 | latest_version = latest_version.lstrip('g-') |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 94 | return latest_version.split('-')[0] |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 95 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 96 | def CanUpdate(self, client_version, latest_version): |
| 97 | """ |
| 98 | Returns true iff the latest_version is greater than the client_version. |
| 99 | """ |
Chris Sosa | b63a928 | 2010-09-02 10:43:23 -0700 | [diff] [blame^] | 100 | client_tokens = client_version.replace('_', '').split('.') |
| 101 | latest_tokens = latest_version.replace('_', '').split('.') |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 102 | web.debug('client version %s latest version %s' \ |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 103 | % (client_version, latest_version)) |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 104 | for i in range(4): |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 105 | if int(latest_tokens[i]) == int(client_tokens[i]): |
| 106 | continue |
| 107 | return int(latest_tokens[i]) > int(client_tokens[i]) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 108 | return False |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 109 | |
Chris Sosa | b63a928 | 2010-09-02 10:43:23 -0700 | [diff] [blame^] | 110 | def UnpackStatefulPartition(self, image_path, image_file, stateful_file): |
| 111 | """Given an image, unpacks the stateful partition to stateful_file.""" |
| 112 | stateful_part = "part_1" |
| 113 | unpack_command = ( |
| 114 | 'cd %s && ' |
| 115 | '$(grep %s\ unpack_partitions.sh | sed s/\\"\$TARGET\\"/%s/)' % |
| 116 | (image_path, stateful_part, image_file)) |
| 117 | web.debug(unpack_command) |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 118 | if os.system(unpack_command) == 0: |
Chris Sosa | b63a928 | 2010-09-02 10:43:23 -0700 | [diff] [blame^] | 119 | shutil.move(os.path.join(image_path, stateful_part), stateful_file) |
Darin Petkov | cbcd2bd | 2010-04-06 10:14:08 -0700 | [diff] [blame] | 120 | return True |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 121 | return False |
| 122 | |
| 123 | def UnpackZip(self, image_path, image_file): |
Sean O'Connor | a7f867e | 2010-05-27 17:53:32 -0700 | [diff] [blame] | 124 | image = os.path.join(image_path, image_file) |
| 125 | if os.path.exists(image): |
| 126 | return True |
| 127 | else: |
Sean O'Connor | 1b4b076 | 2010-06-02 17:37:32 -0700 | [diff] [blame] | 128 | # -n, never clobber an existing file, in case we get invoked |
| 129 | # simultaneously by multiple request handlers. This means that |
| 130 | # we're assuming each image.zip file lives in a versioned |
| 131 | # directory (a la Buildbot). |
| 132 | return os.system('cd %s && unzip -n image.zip %s unpack_partitions.sh' % |
Sean O'Connor | a7f867e | 2010-05-27 17:53:32 -0700 | [diff] [blame] | 133 | (image_path, image_file)) == 0 |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 134 | |
| 135 | def GetImageBinPath(self, image_path): |
Darin Petkov | cbcd2bd | 2010-04-06 10:14:08 -0700 | [diff] [blame] | 136 | if self.test_image: |
| 137 | image_file = 'chromiumos_test_image.bin' |
| 138 | else: |
| 139 | image_file = 'chromiumos_image.bin' |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 140 | return image_file |
Darin Petkov | cbcd2bd | 2010-04-06 10:14:08 -0700 | [diff] [blame] | 141 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 142 | def BuildUpdateImage(self, image_path): |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 143 | stateful_file = '%s/stateful.image' % image_path |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 144 | image_file = self.GetImageBinPath(image_path) |
| 145 | bin_path = os.path.join(image_path, image_file) |
Darin Petkov | cbcd2bd | 2010-04-06 10:14:08 -0700 | [diff] [blame] | 146 | |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 147 | # Get appropriate update.gz to compare timestamps. |
| 148 | if self.serve_only: |
| 149 | cached_update_file = os.path.join(image_path, 'update.gz') |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 150 | else: |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 151 | cached_update_file = os.path.join(self.static_dir, 'update.gz') |
| 152 | |
Chris Sosa | b63a928 | 2010-09-02 10:43:23 -0700 | [diff] [blame^] | 153 | # If the new chromiumos image is newer, re-create everything. |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 154 | if (os.path.exists(cached_update_file) and |
| 155 | os.path.getmtime(cached_update_file) >= os.path.getmtime(bin_path)): |
| 156 | web.debug('Using cached update image at %s instead of %s' % |
| 157 | (cached_update_file, bin_path)) |
| 158 | else: |
| 159 | # Unpack zip file if we are serving from a directory. |
| 160 | if self.serve_only and not self.UnpackZip(image_path, image_file): |
| 161 | web.debug('unzip image.zip failed.') |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 162 | return False |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 163 | |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 164 | update_file = os.path.join(image_path, 'update.gz') |
| 165 | web.debug('Generating update image %s' % update_file) |
Chris Sosa | b63a928 | 2010-09-02 10:43:23 -0700 | [diff] [blame^] | 166 | mkupdate_command = ( |
| 167 | '%s/cros_generate_update_payload --image=%s --output=%s' % |
| 168 | (self.scripts_dir, bin_path, update_file)) |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 169 | if os.system(mkupdate_command) != 0: |
| 170 | web.debug('Failed to create update image') |
| 171 | return False |
| 172 | |
Chris Sosa | b63a928 | 2010-09-02 10:43:23 -0700 | [diff] [blame^] | 173 | # Unpack to get stateful partition. |
| 174 | if not self.UnpackStatefulPartition(image_path, image_file, |
| 175 | stateful_file): |
| 176 | web.debug('Failed to unpack stateful partition.') |
| 177 | return False |
| 178 | |
Sean O'Connor | a7f867e | 2010-05-27 17:53:32 -0700 | [diff] [blame] | 179 | mkstatefulupdate_command = 'gzip -f %s' % stateful_file |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 180 | if os.system(mkstatefulupdate_command) != 0: |
Chris Sosa | b63a928 | 2010-09-02 10:43:23 -0700 | [diff] [blame^] | 181 | web.debug('Failed to create stateful update gz') |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 182 | return False |
| 183 | |
| 184 | # Add gz suffix |
| 185 | stateful_file = '%s.gz' % stateful_file |
| 186 | |
Chris Sosa | b63a928 | 2010-09-02 10:43:23 -0700 | [diff] [blame^] | 187 | # Cleanup of image files. |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 188 | if not self.serve_only: |
| 189 | try: |
| 190 | web.debug('Found a new image to serve, copying it to static') |
| 191 | shutil.copy(update_file, self.static_dir) |
| 192 | shutil.copy(stateful_file, self.static_dir) |
| 193 | os.remove(update_file) |
| 194 | os.remove(stateful_file) |
| 195 | except Exception, e: |
| 196 | web.debug('%s' % e) |
| 197 | return False |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 198 | return True |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 199 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 200 | def GetSize(self, update_path): |
| 201 | return os.path.getsize(update_path) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 202 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 203 | def GetHash(self, update_path): |
Darin Petkov | 8ef8345 | 2010-03-23 16:52:29 -0700 | [diff] [blame] | 204 | cmd = "cat %s | openssl sha1 -binary | openssl base64 | tr \'\\n\' \' \';" \ |
| 205 | % update_path |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 206 | return os.popen(cmd).read().rstrip() |
Darin Petkov | 8ef8345 | 2010-03-23 16:52:29 -0700 | [diff] [blame] | 207 | |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 208 | def ImportFactoryConfigFile(self, filename, validate_checksums=False): |
| 209 | """Imports a factory-floor server configuration file. The file should |
| 210 | be in this format: |
| 211 | config = [ |
| 212 | { |
| 213 | 'qual_ids': set([1, 2, 3, "x86-generic"]), |
| 214 | 'factory_image': 'generic-factory.gz', |
| 215 | 'factory_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 216 | 'release_image': 'generic-release.gz', |
| 217 | 'release_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 218 | 'oempartitionimg_image': 'generic-oem.gz', |
| 219 | 'oempartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Nick Sanders | e1eea92 | 2010-05-19 22:17:08 -0700 | [diff] [blame] | 220 | 'efipartitionimg_image': 'generic-efi.gz', |
| 221 | 'efipartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 222 | 'stateimg_image': 'generic-state.gz', |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 223 | 'stateimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Tom Wai-Hong Tam | dac3df1 | 2010-06-14 09:56:15 +0800 | [diff] [blame] | 224 | 'firmware_image': 'generic-firmware.gz', |
| 225 | 'firmware_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 226 | }, |
| 227 | { |
| 228 | 'qual_ids': set([6]), |
| 229 | 'factory_image': '6-factory.gz', |
| 230 | 'factory_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 231 | 'release_image': '6-release.gz', |
| 232 | 'release_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 233 | 'oempartitionimg_image': '6-oem.gz', |
| 234 | 'oempartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Nick Sanders | e1eea92 | 2010-05-19 22:17:08 -0700 | [diff] [blame] | 235 | 'efipartitionimg_image': '6-efi.gz', |
| 236 | 'efipartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 237 | 'stateimg_image': '6-state.gz', |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 238 | 'stateimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Tom Wai-Hong Tam | dac3df1 | 2010-06-14 09:56:15 +0800 | [diff] [blame] | 239 | 'firmware_image': '6-firmware.gz', |
| 240 | 'firmware_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 241 | }, |
| 242 | ] |
| 243 | The server will look for the files by name in the static files |
| 244 | directory. |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 245 | |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 246 | If validate_checksums is True, validates checksums and exits. If |
| 247 | a checksum mismatch is found, it's printed to the screen. |
| 248 | """ |
| 249 | f = open(filename, 'r') |
| 250 | output = {} |
| 251 | exec(f.read(), output) |
| 252 | self.factory_config = output['config'] |
| 253 | success = True |
| 254 | for stanza in self.factory_config: |
Tom Wai-Hong Tam | 65fc607 | 2010-05-20 11:44:26 +0800 | [diff] [blame] | 255 | for key in stanza.copy().iterkeys(): |
| 256 | suffix = '_image' |
| 257 | if key.endswith(suffix): |
| 258 | kind = key[:-len(suffix)] |
| 259 | stanza[kind + '_size'] = \ |
| 260 | os.path.getsize(self.static_dir + '/' + stanza[kind + '_image']) |
| 261 | if validate_checksums: |
| 262 | factory_checksum = self.GetHash(self.static_dir + '/' + |
| 263 | stanza[kind + '_image']) |
| 264 | if factory_checksum != stanza[kind + '_checksum']: |
| 265 | print 'Error: checksum mismatch for %s. Expected "%s" but file ' \ |
| 266 | 'has checksum "%s".' % (stanza[kind + '_image'], |
| 267 | stanza[kind + '_checksum'], |
| 268 | factory_checksum) |
| 269 | success = False |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 270 | if validate_checksums: |
| 271 | if success is False: |
| 272 | raise Exception('Checksum mismatch in conf file.') |
| 273 | print 'Config file looks good.' |
| 274 | |
| 275 | def GetFactoryImage(self, board_id, channel): |
| 276 | kind = channel.rsplit('-', 1)[0] |
| 277 | for stanza in self.factory_config: |
| 278 | if board_id not in stanza['qual_ids']: |
| 279 | continue |
Nick Sanders | 15cd6ae | 2010-06-30 12:30:56 -0700 | [diff] [blame] | 280 | if kind + '_image' not in stanza: |
| 281 | break |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 282 | return (stanza[kind + '_image'], |
| 283 | stanza[kind + '_checksum'], |
| 284 | stanza[kind + '_size']) |
Nick Sanders | 15cd6ae | 2010-06-30 12:30:56 -0700 | [diff] [blame] | 285 | return (None, None, None) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 286 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 287 | def HandleUpdatePing(self, data, label=None): |
Darin Petkov | d35ee24 | 2010-07-14 16:45:31 -0700 | [diff] [blame] | 288 | web.debug('handle update ping: %s' % data) |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 289 | update_dom = minidom.parseString(data) |
| 290 | root = update_dom.firstChild |
Andrew de los Reyes | 9223f13 | 2010-05-07 17:08:17 -0700 | [diff] [blame] | 291 | if root.hasAttribute('updaterversion') and \ |
| 292 | not root.getAttribute('updaterversion').startswith( |
Andrew de los Reyes | fb4444b | 2010-06-29 18:11:28 -0700 | [diff] [blame] | 293 | self.client_prefix): |
Andrew de los Reyes | 9223f13 | 2010-05-07 17:08:17 -0700 | [diff] [blame] | 294 | web.debug('Got update from unsupported updater:' + \ |
| 295 | root.getAttribute('updaterversion')) |
| 296 | return self.GetNoUpdatePayload() |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 297 | query = root.getElementsByTagName('o:app')[0] |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 298 | client_version = query.getAttribute('version') |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 299 | channel = query.getAttribute('track') |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 300 | board_id = query.hasAttribute('board') and query.getAttribute('board') \ |
Andrew de los Reyes | 9a52871 | 2010-06-30 10:29:43 -0700 | [diff] [blame] | 301 | or self.GetDefaultBoardID() |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 302 | latest_image_path = self.GetLatestImagePath(board_id) |
| 303 | latest_version = self.GetLatestVersion(latest_image_path) |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 304 | hostname = web.ctx.host |
| 305 | |
| 306 | # If this is a factory floor server, return the image here: |
| 307 | if self.factory_config: |
| 308 | (filename, checksum, size) = \ |
| 309 | self.GetFactoryImage(board_id, channel) |
| 310 | if filename is None: |
| 311 | web.debug('unable to find image for board %s' % board_id) |
| 312 | return self.GetNoUpdatePayload() |
| 313 | url = 'http://%s/static/%s' % (hostname, filename) |
| 314 | web.debug('returning update payload ' + url) |
| 315 | return self.GetUpdatePayload(checksum, size, url) |
| 316 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 317 | if client_version != 'ForcedUpdate' \ |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 318 | and not self.CanUpdate(client_version, latest_version): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 319 | web.debug('no update') |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 320 | return self.GetNoUpdatePayload() |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 321 | if label: |
| 322 | web.debug('Client requested version %s' % label) |
| 323 | # Check that matching build exists |
| 324 | image_path = '%s/%s' % (self.static_dir, label) |
| 325 | if not os.path.exists(image_path): |
| 326 | web.debug('%s not found.' % image_path) |
| 327 | return self.GetNoUpdatePayload() |
| 328 | # Construct a response |
| 329 | ok = self.BuildUpdateImage(image_path) |
| 330 | if ok != True: |
| 331 | web.debug('Failed to build an update image') |
| 332 | return self.GetNoUpdatePayload() |
| 333 | web.debug('serving update: ') |
| 334 | hash = self.GetHash('%s/%s/update.gz' % (self.static_dir, label)) |
| 335 | size = self.GetSize('%s/%s/update.gz' % (self.static_dir, label)) |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 336 | # In case we configured images to be hosted elsewhere |
| 337 | # (e.g. buildbot's httpd), use that. Otherwise, serve it |
| 338 | # ourselves using web.py's static resource handler. |
| 339 | if self.static_urlbase: |
| 340 | urlbase = self.static_urlbase |
| 341 | else: |
| 342 | urlbase = 'http://%s/static/archive/' % hostname |
| 343 | |
| 344 | url = '%s/%s/update.gz' % (urlbase, label) |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 345 | return self.GetUpdatePayload(hash, size, url) |
Chris Sosa | a73ec16 | 2010-05-03 20:18:02 -0700 | [diff] [blame] | 346 | web.debug('DONE') |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 347 | else: |
| 348 | web.debug('update found %s ' % latest_version) |
| 349 | ok = self.BuildUpdateImage(latest_image_path) |
| 350 | if ok != True: |
| 351 | web.debug('Failed to build an update image') |
| 352 | return self.GetNoUpdatePayload() |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 353 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 354 | hash = self.GetHash('%s/update.gz' % self.static_dir) |
| 355 | size = self.GetSize('%s/update.gz' % self.static_dir) |
| 356 | |
| 357 | url = 'http://%s/static/update.gz' % hostname |
| 358 | return self.GetUpdatePayload(hash, size, url) |