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, |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 20 | *args, **kwargs): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 21 | super(Autoupdate, self).__init__(*args, **kwargs) |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 22 | self.serve_only = serve_only |
| 23 | self.test_image=test_image |
| 24 | self.static_urlbase = urlbase |
| 25 | if serve_only: |
| 26 | # If we're serving out of an archived build dir (e.g. a |
| 27 | # buildbot), prepare this webserver's magic 'static/' dir with a |
| 28 | # link to the build archive. |
| 29 | web.debug('Autoupdate in "serve update images only" mode.') |
| 30 | if os.path.exists('static/archive'): |
| 31 | archive_symlink = os.readlink('static/archive') |
| 32 | if archive_symlink != self.static_dir: |
| 33 | web.debug('removing stale symlink to %s' % self.static_dir) |
| 34 | os.unlink('static/archive') |
| 35 | else: |
| 36 | os.symlink(self.static_dir, 'static/archive') |
Darin Petkov | 98da5db | 2010-04-13 10:11:40 -0700 | [diff] [blame] | 37 | self.factory_config = None |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 38 | if factory_config_path is not None: |
| 39 | self.ImportFactoryConfigFile(factory_config_path, validate_factory_config) |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 40 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 41 | def GetUpdatePayload(self, hash, size, url): |
| 42 | payload = """<?xml version="1.0" encoding="UTF-8"?> |
| 43 | <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0"> |
| 44 | <app appid="{%s}" status="ok"> |
| 45 | <ping status="ok"/> |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 46 | <updatecheck |
| 47 | codebase="%s" |
| 48 | hash="%s" |
| 49 | needsadmin="false" |
| 50 | size="%s" |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 51 | status="ok"/> |
| 52 | </app> |
| 53 | </gupdate> |
| 54 | """ |
| 55 | return payload % (self.app_id, url, hash, size) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 56 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 57 | def GetNoUpdatePayload(self): |
| 58 | payload = """<?xml version="1.0" encoding="UTF-8"?> |
| 59 | <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0"> |
| 60 | <app appid="{%s}" status="ok"> |
| 61 | <ping status="ok"/> |
| 62 | <updatecheck status="noupdate"/> |
| 63 | </app> |
| 64 | </gupdate> |
| 65 | """ |
| 66 | return payload % self.app_id |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 67 | |
Sam Leffler | 7638204 | 2010-02-18 09:58:42 -0800 | [diff] [blame] | 68 | def GetLatestImagePath(self, board_id): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 69 | 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] | 70 | return os.popen(cmd).read().strip() |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 71 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 72 | def GetLatestVersion(self, latest_image_path): |
| 73 | latest_version = latest_image_path.split('/')[-1] |
Ryan Cairns | 1b05beb | 2010-02-05 17:05:24 -0800 | [diff] [blame] | 74 | |
| 75 | # Removes the portage build prefix. |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 76 | latest_version = latest_version.lstrip('g-') |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 77 | return latest_version.split('-')[0] |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 78 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 79 | def CanUpdate(self, client_version, latest_version): |
| 80 | """ |
| 81 | Returns true iff the latest_version is greater than the client_version. |
| 82 | """ |
| 83 | client_tokens = client_version.split('.') |
| 84 | latest_tokens = latest_version.split('.') |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 85 | web.debug('client version %s latest version %s' \ |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 86 | % (client_version, latest_version)) |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 87 | for i in range(0,4): |
| 88 | if int(latest_tokens[i]) == int(client_tokens[i]): |
| 89 | continue |
| 90 | return int(latest_tokens[i]) > int(client_tokens[i]) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 91 | return False |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 92 | |
Darin Petkov | 55604f1 | 2010-04-12 11:09:25 -0700 | [diff] [blame] | 93 | def UnpackImage(self, image_path, kernel_file, rootfs_file): |
| 94 | if os.path.exists(rootfs_file) and os.path.exists(kernel_file): |
Darin Petkov | cbcd2bd | 2010-04-06 10:14:08 -0700 | [diff] [blame] | 95 | return True |
| 96 | if self.test_image: |
| 97 | image_file = 'chromiumos_test_image.bin' |
| 98 | else: |
| 99 | image_file = 'chromiumos_image.bin' |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 100 | if self.serve_only: |
Sean O'Connor | e38ea15 | 2010-04-16 13:50:40 -0700 | [diff] [blame^] | 101 | err = os.system('cd %s && unzip -o image.zip %s unpack_partitions.sh' % |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 102 | (image_path, image_file)) |
Sean O'Connor | e38ea15 | 2010-04-16 13:50:40 -0700 | [diff] [blame^] | 103 | if err: |
| 104 | web.debug('unzip image.zip failed.') |
| 105 | return False |
| 106 | |
Darin Petkov | cbcd2bd | 2010-04-06 10:14:08 -0700 | [diff] [blame] | 107 | os.system('rm -f %s/part_*' % image_path) |
| 108 | os.system('cd %s && ./unpack_partitions.sh %s' % (image_path, image_file)) |
Darin Petkov | 55604f1 | 2010-04-12 11:09:25 -0700 | [diff] [blame] | 109 | shutil.move(os.path.join(image_path, 'part_2'), kernel_file) |
Darin Petkov | cbcd2bd | 2010-04-06 10:14:08 -0700 | [diff] [blame] | 110 | shutil.move(os.path.join(image_path, 'part_3'), rootfs_file) |
| 111 | os.system('rm -f %s/part_*' % image_path) |
| 112 | return True |
| 113 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 114 | def BuildUpdateImage(self, image_path): |
Darin Petkov | 55604f1 | 2010-04-12 11:09:25 -0700 | [diff] [blame] | 115 | kernel_file = '%s/kernel.image' % image_path |
| 116 | rootfs_file = '%s/rootfs.image' % image_path |
Darin Petkov | cbcd2bd | 2010-04-06 10:14:08 -0700 | [diff] [blame] | 117 | |
Darin Petkov | 55604f1 | 2010-04-12 11:09:25 -0700 | [diff] [blame] | 118 | if not self.UnpackImage(image_path, kernel_file, rootfs_file): |
| 119 | web.debug('failed to unpack image.') |
Darin Petkov | cbcd2bd | 2010-04-06 10:14:08 -0700 | [diff] [blame] | 120 | return False |
| 121 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 122 | update_file = '%s/update.gz' % image_path |
| 123 | if (os.path.exists(update_file) and |
Darin Petkov | 55604f1 | 2010-04-12 11:09:25 -0700 | [diff] [blame] | 124 | os.path.getmtime(update_file) >= os.path.getmtime(rootfs_file)): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 125 | web.debug('Found cached update image %s/update.gz' % image_path) |
| 126 | else: |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 127 | web.debug('generating update image %s' % update_file) |
Darin Petkov | 55604f1 | 2010-04-12 11:09:25 -0700 | [diff] [blame] | 128 | mkupdate = ('%s/mk_memento_images.sh %s %s' % |
| 129 | (self.scripts_dir, kernel_file, rootfs_file)) |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 130 | web.debug(mkupdate) |
| 131 | err = os.system(mkupdate) |
| 132 | if err != 0: |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 133 | web.debug('failed to create update image') |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 134 | return False |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 135 | if not self.serve_only: |
| 136 | web.debug('Found an image, copying it to static') |
| 137 | try: |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 138 | shutil.copy(update_file, self.static_dir) |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 139 | except Exception, e: |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 140 | web.debug('Unable to copy %s to %s' % (update_file, self.static_dir)) |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 141 | return False |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 142 | return True |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 143 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 144 | def GetSize(self, update_path): |
| 145 | return os.path.getsize(update_path) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 146 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 147 | def GetHash(self, update_path): |
Darin Petkov | 8ef8345 | 2010-03-23 16:52:29 -0700 | [diff] [blame] | 148 | cmd = "cat %s | openssl sha1 -binary | openssl base64 | tr \'\\n\' \' \';" \ |
| 149 | % update_path |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 150 | return os.popen(cmd).read().rstrip() |
Darin Petkov | 8ef8345 | 2010-03-23 16:52:29 -0700 | [diff] [blame] | 151 | |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 152 | def ImportFactoryConfigFile(self, filename, validate_checksums=False): |
| 153 | """Imports a factory-floor server configuration file. The file should |
| 154 | be in this format: |
| 155 | config = [ |
| 156 | { |
| 157 | 'qual_ids': set([1, 2, 3, "x86-generic"]), |
| 158 | 'factory_image': 'generic-factory.gz', |
| 159 | 'factory_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 160 | 'release_image': 'generic-release.gz', |
| 161 | 'release_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 162 | 'oempartitionimg_image': 'generic-oem.gz', |
| 163 | 'oempartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 164 | 'stateimg_image': 'generic-state.gz', |
| 165 | 'stateimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=' |
| 166 | }, |
| 167 | { |
| 168 | 'qual_ids': set([6]), |
| 169 | 'factory_image': '6-factory.gz', |
| 170 | 'factory_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 171 | 'release_image': '6-release.gz', |
| 172 | 'release_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 173 | 'oempartitionimg_image': '6-oem.gz', |
| 174 | 'oempartitionimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=', |
| 175 | 'stateimg_image': '6-state.gz', |
| 176 | 'stateimg_checksum': 'AtiI8B64agHVN+yeBAyiNMX3+HM=' |
| 177 | }, |
| 178 | ] |
| 179 | The server will look for the files by name in the static files |
| 180 | directory. |
| 181 | |
| 182 | If validate_checksums is True, validates checksums and exits. If |
| 183 | a checksum mismatch is found, it's printed to the screen. |
| 184 | """ |
| 185 | f = open(filename, 'r') |
| 186 | output = {} |
| 187 | exec(f.read(), output) |
| 188 | self.factory_config = output['config'] |
| 189 | success = True |
| 190 | for stanza in self.factory_config: |
| 191 | for kind in ('factory', 'oempartitionimg', 'release', 'stateimg'): |
| 192 | stanza[kind + '_size'] = \ |
| 193 | os.path.getsize(self.static_dir + '/' + stanza[kind + '_image']) |
| 194 | if validate_checksums: |
| 195 | factory_checksum = self.GetHash(self.static_dir + '/' + |
| 196 | stanza[kind + '_image']) |
| 197 | if factory_checksum != stanza[kind + '_checksum']: |
| 198 | print 'Error: checksum mismatch for %s. Expected "%s" but file ' \ |
| 199 | 'has checksum "%s".' % (stanza[kind + '_image'], |
| 200 | stanza[kind + '_checksum'], |
| 201 | factory_checksum) |
| 202 | success = False |
| 203 | if validate_checksums: |
| 204 | if success is False: |
| 205 | raise Exception('Checksum mismatch in conf file.') |
| 206 | print 'Config file looks good.' |
| 207 | |
| 208 | def GetFactoryImage(self, board_id, channel): |
| 209 | kind = channel.rsplit('-', 1)[0] |
| 210 | for stanza in self.factory_config: |
| 211 | if board_id not in stanza['qual_ids']: |
| 212 | continue |
| 213 | return (stanza[kind + '_image'], |
| 214 | stanza[kind + '_checksum'], |
| 215 | stanza[kind + '_size']) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 216 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 217 | def HandleUpdatePing(self, data, label=None): |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 218 | web.debug('handle update ping') |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 219 | update_dom = minidom.parseString(data) |
| 220 | root = update_dom.firstChild |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 221 | query = root.getElementsByTagName('o:app')[0] |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 222 | client_version = query.getAttribute('version') |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 223 | channel = query.getAttribute('track') |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 224 | board_id = query.hasAttribute('board') and query.getAttribute('board') \ |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 225 | or 'x86-generic' |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 226 | latest_image_path = self.GetLatestImagePath(board_id) |
| 227 | latest_version = self.GetLatestVersion(latest_image_path) |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 228 | hostname = web.ctx.host |
| 229 | |
| 230 | # If this is a factory floor server, return the image here: |
| 231 | if self.factory_config: |
| 232 | (filename, checksum, size) = \ |
| 233 | self.GetFactoryImage(board_id, channel) |
| 234 | if filename is None: |
| 235 | web.debug('unable to find image for board %s' % board_id) |
| 236 | return self.GetNoUpdatePayload() |
| 237 | url = 'http://%s/static/%s' % (hostname, filename) |
| 238 | web.debug('returning update payload ' + url) |
| 239 | return self.GetUpdatePayload(checksum, size, url) |
| 240 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 241 | if client_version != 'ForcedUpdate' \ |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 242 | and not self.CanUpdate(client_version, latest_version): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 243 | web.debug('no update') |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 244 | return self.GetNoUpdatePayload() |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 245 | if label: |
| 246 | web.debug('Client requested version %s' % label) |
| 247 | # Check that matching build exists |
| 248 | image_path = '%s/%s' % (self.static_dir, label) |
| 249 | if not os.path.exists(image_path): |
| 250 | web.debug('%s not found.' % image_path) |
| 251 | return self.GetNoUpdatePayload() |
| 252 | # Construct a response |
| 253 | ok = self.BuildUpdateImage(image_path) |
| 254 | if ok != True: |
| 255 | web.debug('Failed to build an update image') |
| 256 | return self.GetNoUpdatePayload() |
| 257 | web.debug('serving update: ') |
| 258 | hash = self.GetHash('%s/%s/update.gz' % (self.static_dir, label)) |
| 259 | size = self.GetSize('%s/%s/update.gz' % (self.static_dir, label)) |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 260 | # In case we configured images to be hosted elsewhere |
| 261 | # (e.g. buildbot's httpd), use that. Otherwise, serve it |
| 262 | # ourselves using web.py's static resource handler. |
| 263 | if self.static_urlbase: |
| 264 | urlbase = self.static_urlbase |
| 265 | else: |
| 266 | urlbase = 'http://%s/static/archive/' % hostname |
| 267 | |
| 268 | url = '%s/%s/update.gz' % (urlbase, label) |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 269 | return self.GetUpdatePayload(hash, size, url) |
| 270 | web.debug( 'DONE') |
| 271 | else: |
| 272 | web.debug('update found %s ' % latest_version) |
| 273 | ok = self.BuildUpdateImage(latest_image_path) |
| 274 | if ok != True: |
| 275 | web.debug('Failed to build an update image') |
| 276 | return self.GetNoUpdatePayload() |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 277 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 278 | hash = self.GetHash('%s/update.gz' % self.static_dir) |
| 279 | size = self.GetSize('%s/update.gz' % self.static_dir) |
| 280 | |
| 281 | url = 'http://%s/static/update.gz' % hostname |
| 282 | return self.GetUpdatePayload(hash, size, url) |