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