blob: 2f0fe956b543fcd3af230c68e789b46a49b7d41d [file] [log] [blame]
rtc@google.comded22402009-10-26 22:36:21 +00001# 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.com64244662009-11-12 00:52:08 +00005from buildutil import BuildObject
rtc@google.comded22402009-10-26 22:36:21 +00006from xml.dom import minidom
7
8import os
9import web
10
rtc@google.com64244662009-11-12 00:52:08 +000011class Autoupdate(BuildObject):
rtc@google.comded22402009-10-26 22:36:21 +000012
rtc@google.com21a5ca32009-11-04 18:23:23 +000013 # Basic functionality of handling ChromeOS autoupdate pings
14 # and building/serving update images.
15 # TODO(rtc): Clean this code up and write some tests.
rtc@google.comded22402009-10-26 22:36:21 +000016
rtc@google.com21a5ca32009-11-04 18:23:23 +000017 def GetUpdatePayload(self, hash, size, url):
18 payload = """<?xml version="1.0" encoding="UTF-8"?>
19 <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0">
20 <app appid="{%s}" status="ok">
21 <ping status="ok"/>
22 <updatecheck
23 codebase="%s"
24 hash="%s"
25 needsadmin="false"
26 size="%s"
27 status="ok"/>
28 </app>
29 </gupdate>
30 """
31 return payload % (self.app_id, url, hash, size)
rtc@google.comded22402009-10-26 22:36:21 +000032
rtc@google.com21a5ca32009-11-04 18:23:23 +000033 def GetNoUpdatePayload(self):
34 payload = """<?xml version="1.0" encoding="UTF-8"?>
35 <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0">
36 <app appid="{%s}" status="ok">
37 <ping status="ok"/>
38 <updatecheck status="noupdate"/>
39 </app>
40 </gupdate>
41 """
42 return payload % self.app_id
rtc@google.comded22402009-10-26 22:36:21 +000043
rtc@google.com21a5ca32009-11-04 18:23:23 +000044 def GetLatestImagePath(self):
45 cmd = "%s/get_latest_image.sh" % self.scripts_dir
46 return os.popen(cmd).read().strip()
rtc@google.comded22402009-10-26 22:36:21 +000047
rtc@google.com21a5ca32009-11-04 18:23:23 +000048 def GetLatestVersion(self, latest_image_path):
49 latest_version = latest_image_path.split('/')[-1]
50 return latest_version.split('-')[0]
rtc@google.comded22402009-10-26 22:36:21 +000051
rtc@google.com21a5ca32009-11-04 18:23:23 +000052 def CanUpdate(self, client_version, latest_version):
53 """
54 Returns true iff the latest_version is greater than the client_version.
55 """
56 client_tokens = client_version.split('.')
57 latest_tokens = latest_version.split('.')
58 web.debug("client version %s latest version %s" % (client_version, latest_version))
59 for i in range(0,4):
60 if int(latest_tokens[i]) == int(client_tokens[i]):
61 continue
62 return int(latest_tokens[i]) > int(client_tokens[i])
rtc@google.comded22402009-10-26 22:36:21 +000063 return False
rtc@google.comded22402009-10-26 22:36:21 +000064
rtc@google.com21a5ca32009-11-04 18:23:23 +000065 def BuildUpdateImage(self, image_path):
66 image_file = "%s/rootfs.image" % image_path
67 web.debug("checking image file %s/update.gz" % image_path)
68 if not os.path.exists("%s/update.gz" % image_path):
69 mkupdate = "%s/mk_memento_images.sh %s" % (self.scripts_dir, image_file)
70 web.debug(mkupdate)
71 err = os.system(mkupdate)
72 if err != 0:
73 web.debug("failed to create update image")
74 return False
rtc@google.comded22402009-10-26 22:36:21 +000075
rtc@google.com21a5ca32009-11-04 18:23:23 +000076 web.debug("Found an image, copying it to static")
77 err = os.system("cp %s/update.gz %s" % (image_path, self.static_dir))
78 if err != 0:
79 web.debug("Unable to move update.gz from %s to %s" % (image_path, self.static_dir))
80 return False
81 return True
rtc@google.comded22402009-10-26 22:36:21 +000082
rtc@google.com21a5ca32009-11-04 18:23:23 +000083 def GetSize(self, update_path):
84 return os.path.getsize(update_path)
rtc@google.comded22402009-10-26 22:36:21 +000085
rtc@google.com21a5ca32009-11-04 18:23:23 +000086 def GetHash(self, update_path):
87 cmd = "cat %s | openssl sha1 -binary | openssl base64 | tr \'\\n\' \' \';" % update_path
88 web.debug(cmd)
89 return os.popen(cmd).read()
rtc@google.comded22402009-10-26 22:36:21 +000090
rtc@google.com21a5ca32009-11-04 18:23:23 +000091 def HandleUpdatePing(self, data):
92 update_dom = minidom.parseString(data)
93 root = update_dom.firstChild
94 query = root.getElementsByTagName("o:app")[0]
95 client_version = query.attributes['version'].value
96 latest_image_path = self.GetLatestImagePath();
97 latest_version = self.GetLatestVersion(latest_image_path);
Sam Lefflereaacace2009-12-22 13:20:53 -080098 if client_version != "ForcedUpdate" and not self.CanUpdate(client_version, latest_version):
rtc@google.com21a5ca32009-11-04 18:23:23 +000099 web.debug("no update")
100 return self.GetNoUpdatePayload()
101
102 web.debug("update found %s " % latest_version)
103 ok = self.BuildUpdateImage(latest_image_path)
104 if ok != True:
105 web.debug("Failed to build an update image")
106 return self.GetNoUpdatePayload()
107
108 hash = self.GetHash("%s/update.gz" % self.static_dir)
109 size = self.GetSize("%s/update.gz" % self.static_dir)
110 hostname = web.ctx.host
111 url = "http://%s/static/update.gz" % hostname
112 return self.GetUpdatePayload(hash, size, url)
rtc@google.comded22402009-10-26 22:36:21 +0000113