blob: f3b74479fe628d5ce4b9c647ef2cdad9a0a8756b [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]
Ryan Cairns1b05beb2010-02-05 17:05:24 -080050
51 # Removes the portage build prefix.
52 latest_version = latest_version.lstrip("g-")
rtc@google.com21a5ca32009-11-04 18:23:23 +000053 return latest_version.split('-')[0]
rtc@google.comded22402009-10-26 22:36:21 +000054
rtc@google.com21a5ca32009-11-04 18:23:23 +000055 def CanUpdate(self, client_version, latest_version):
56 """
57 Returns true iff the latest_version is greater than the client_version.
58 """
59 client_tokens = client_version.split('.')
60 latest_tokens = latest_version.split('.')
61 web.debug("client version %s latest version %s" % (client_version, latest_version))
62 for i in range(0,4):
63 if int(latest_tokens[i]) == int(client_tokens[i]):
64 continue
65 return int(latest_tokens[i]) > int(client_tokens[i])
rtc@google.comded22402009-10-26 22:36:21 +000066 return False
rtc@google.comded22402009-10-26 22:36:21 +000067
rtc@google.com21a5ca32009-11-04 18:23:23 +000068 def BuildUpdateImage(self, image_path):
69 image_file = "%s/rootfs.image" % image_path
70 web.debug("checking image file %s/update.gz" % image_path)
71 if not os.path.exists("%s/update.gz" % image_path):
72 mkupdate = "%s/mk_memento_images.sh %s" % (self.scripts_dir, image_file)
73 web.debug(mkupdate)
74 err = os.system(mkupdate)
75 if err != 0:
76 web.debug("failed to create update image")
77 return False
rtc@google.comded22402009-10-26 22:36:21 +000078
rtc@google.com21a5ca32009-11-04 18:23:23 +000079 web.debug("Found an image, copying it to static")
80 err = os.system("cp %s/update.gz %s" % (image_path, self.static_dir))
81 if err != 0:
82 web.debug("Unable to move update.gz from %s to %s" % (image_path, self.static_dir))
83 return False
84 return True
rtc@google.comded22402009-10-26 22:36:21 +000085
rtc@google.com21a5ca32009-11-04 18:23:23 +000086 def GetSize(self, update_path):
87 return os.path.getsize(update_path)
rtc@google.comded22402009-10-26 22:36:21 +000088
rtc@google.com21a5ca32009-11-04 18:23:23 +000089 def GetHash(self, update_path):
90 cmd = "cat %s | openssl sha1 -binary | openssl base64 | tr \'\\n\' \' \';" % update_path
91 web.debug(cmd)
92 return os.popen(cmd).read()
rtc@google.comded22402009-10-26 22:36:21 +000093
rtc@google.com21a5ca32009-11-04 18:23:23 +000094 def HandleUpdatePing(self, data):
95 update_dom = minidom.parseString(data)
96 root = update_dom.firstChild
97 query = root.getElementsByTagName("o:app")[0]
98 client_version = query.attributes['version'].value
99 latest_image_path = self.GetLatestImagePath();
100 latest_version = self.GetLatestVersion(latest_image_path);
Sam Lefflereaacace2009-12-22 13:20:53 -0800101 if client_version != "ForcedUpdate" and not self.CanUpdate(client_version, latest_version):
rtc@google.com21a5ca32009-11-04 18:23:23 +0000102 web.debug("no update")
103 return self.GetNoUpdatePayload()
104
105 web.debug("update found %s " % latest_version)
106 ok = self.BuildUpdateImage(latest_image_path)
107 if ok != True:
108 web.debug("Failed to build an update image")
109 return self.GetNoUpdatePayload()
110
111 hash = self.GetHash("%s/update.gz" % self.static_dir)
112 size = self.GetSize("%s/update.gz" % self.static_dir)
113 hostname = web.ctx.host
114 url = "http://%s/static/update.gz" % hostname
115 return self.GetUpdatePayload(hash, size, url)
rtc@google.comded22402009-10-26 22:36:21 +0000116