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 |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 10 | import web |
| 11 | |
rtc@google.com | 6424466 | 2009-11-12 00:52:08 +0000 | [diff] [blame] | 12 | class Autoupdate(BuildObject): |
Darin Petkov | 798fe7d | 2010-03-22 15:18:13 -0700 | [diff] [blame] | 13 | # Basic functionality of handling ChromeOS autoupdate pings |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 14 | # and building/serving update images. |
| 15 | # TODO(rtc): Clean this code up and write some tests. |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 16 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 17 | def __init__(self, serve_only=None, test_image=False, *args, **kwargs): |
| 18 | self.serve_only = serve_only |
| 19 | if serve_only: |
| 20 | web.debug('Autoupdate in "serve update images only" mode.') |
| 21 | self.test_image=test_image |
| 22 | super(Autoupdate, self).__init__(*args, **kwargs) |
| 23 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 24 | def GetUpdatePayload(self, hash, size, url): |
| 25 | payload = """<?xml version="1.0" encoding="UTF-8"?> |
| 26 | <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0"> |
| 27 | <app appid="{%s}" status="ok"> |
| 28 | <ping status="ok"/> |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 29 | <updatecheck |
| 30 | codebase="%s" |
| 31 | hash="%s" |
| 32 | needsadmin="false" |
| 33 | size="%s" |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 34 | status="ok"/> |
| 35 | </app> |
| 36 | </gupdate> |
| 37 | """ |
| 38 | return payload % (self.app_id, url, hash, size) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 39 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 40 | def GetNoUpdatePayload(self): |
| 41 | payload = """<?xml version="1.0" encoding="UTF-8"?> |
| 42 | <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0"> |
| 43 | <app appid="{%s}" status="ok"> |
| 44 | <ping status="ok"/> |
| 45 | <updatecheck status="noupdate"/> |
| 46 | </app> |
| 47 | </gupdate> |
| 48 | """ |
| 49 | return payload % self.app_id |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 50 | |
Sam Leffler | 7638204 | 2010-02-18 09:58:42 -0800 | [diff] [blame] | 51 | def GetLatestImagePath(self, board_id): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 52 | 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] | 53 | return os.popen(cmd).read().strip() |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 54 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 55 | def GetLatestVersion(self, latest_image_path): |
| 56 | latest_version = latest_image_path.split('/')[-1] |
Ryan Cairns | 1b05beb | 2010-02-05 17:05:24 -0800 | [diff] [blame] | 57 | |
| 58 | # Removes the portage build prefix. |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 59 | latest_version = latest_version.lstrip('g-') |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 60 | return latest_version.split('-')[0] |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 61 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 62 | def CanUpdate(self, client_version, latest_version): |
| 63 | """ |
| 64 | Returns true iff the latest_version is greater than the client_version. |
| 65 | """ |
| 66 | client_tokens = client_version.split('.') |
| 67 | latest_tokens = latest_version.split('.') |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 68 | web.debug('client version %s latest version %s' \ |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 69 | % (client_version, latest_version)) |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 70 | for i in range(0,4): |
| 71 | if int(latest_tokens[i]) == int(client_tokens[i]): |
| 72 | continue |
| 73 | return int(latest_tokens[i]) > int(client_tokens[i]) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 74 | return False |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 75 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 76 | def BuildUpdateImage(self, image_path): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 77 | if self.test_image: |
| 78 | image_file = '%s/rootfs_test.image' % image_path |
| 79 | else: |
| 80 | image_file = '%s/rootfs.image' % image_path |
| 81 | update_file = '%s/update.gz' % image_path |
| 82 | if (os.path.exists(update_file) and |
| 83 | os.path.getmtime(update_file) >= os.path.getmtime(image_file)): |
| 84 | web.debug('Found cached update image %s/update.gz' % image_path) |
| 85 | else: |
| 86 | web.debug('generating update image %s/update.gz' % image_path) |
| 87 | mkupdate = '%s/mk_memento_images.sh %s' % (self.scripts_dir, image_file) |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 88 | web.debug(mkupdate) |
| 89 | err = os.system(mkupdate) |
| 90 | if err != 0: |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 91 | web.debug('failed to create update image') |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 92 | return False |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 93 | if not self.serve_only: |
| 94 | web.debug('Found an image, copying it to static') |
| 95 | try: |
Darin Petkov | 798fe7d | 2010-03-22 15:18:13 -0700 | [diff] [blame] | 96 | shutil.copy('%s/update.gz' % image_path, self.static_dir) |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 97 | except Exception, e: |
| 98 | web.debug('Unable to copy update.gz from %s to %s' \ |
| 99 | % (image_path, self.static_dir)) |
| 100 | return False |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 101 | return True |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 102 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 103 | def GetSize(self, update_path): |
| 104 | return os.path.getsize(update_path) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 105 | |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 106 | def GetHash(self, update_path): |
Darin Petkov | 8ef8345 | 2010-03-23 16:52:29 -0700 | [diff] [blame] | 107 | cmd = "cat %s | openssl sha1 -binary | openssl base64 | tr \'\\n\' \' \';" \ |
| 108 | % update_path |
| 109 | web.debug(cmd) |
| 110 | return os.popen(cmd).read() |
| 111 | |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 112 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 113 | def HandleUpdatePing(self, data, label=None): |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 114 | update_dom = minidom.parseString(data) |
| 115 | root = update_dom.firstChild |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 116 | query = root.getElementsByTagName('o:app')[0] |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 117 | client_version = query.getAttribute('version') |
| 118 | board_id = query.hasAttribute('board') and query.getAttribute('board') \ |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 119 | or 'x86-generic' |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 120 | latest_image_path = self.GetLatestImagePath(board_id) |
| 121 | latest_version = self.GetLatestVersion(latest_image_path) |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 122 | if client_version != 'ForcedUpdate' \ |
Charlie Lee | 8c99308 | 2010-02-24 13:27:37 -0800 | [diff] [blame] | 123 | and not self.CanUpdate(client_version, latest_version): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 124 | web.debug('no update') |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 125 | return self.GetNoUpdatePayload() |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 126 | hostname = web.ctx.host |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 127 | if label: |
| 128 | web.debug('Client requested version %s' % label) |
| 129 | # Check that matching build exists |
| 130 | image_path = '%s/%s' % (self.static_dir, label) |
| 131 | if not os.path.exists(image_path): |
| 132 | web.debug('%s not found.' % image_path) |
| 133 | return self.GetNoUpdatePayload() |
| 134 | # Construct a response |
| 135 | ok = self.BuildUpdateImage(image_path) |
| 136 | if ok != True: |
| 137 | web.debug('Failed to build an update image') |
| 138 | return self.GetNoUpdatePayload() |
| 139 | web.debug('serving update: ') |
| 140 | hash = self.GetHash('%s/%s/update.gz' % (self.static_dir, label)) |
| 141 | size = self.GetSize('%s/%s/update.gz' % (self.static_dir, label)) |
| 142 | url = 'http://%s/static/archive/%s/update.gz' % (hostname, label) |
| 143 | return self.GetUpdatePayload(hash, size, url) |
| 144 | web.debug( 'DONE') |
| 145 | else: |
| 146 | web.debug('update found %s ' % latest_version) |
| 147 | ok = self.BuildUpdateImage(latest_image_path) |
| 148 | if ok != True: |
| 149 | web.debug('Failed to build an update image') |
| 150 | return self.GetNoUpdatePayload() |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 151 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 152 | hash = self.GetHash('%s/update.gz' % self.static_dir) |
| 153 | size = self.GetSize('%s/update.gz' % self.static_dir) |
| 154 | |
| 155 | url = 'http://%s/static/update.gz' % hostname |
| 156 | return self.GetUpdatePayload(hash, size, url) |