blob: 6c7017cba6f327e8324a6cb6656f1ee65002788a [file] [log] [blame]
Amin Hassani8d718d12019-06-02 21:28:39 -07001# -*- coding: utf-8 -*-
Darin Petkovc3fd90c2011-05-11 14:23:00 -07002# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
rtc@google.comded22402009-10-26 22:36:21 +00003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
Gilad Arnoldd8d595c2014-03-21 13:00:41 -07006"""Devserver module for handling update client requests."""
7
Don Garrettfb15e322016-06-21 19:12:08 -07008from __future__ import print_function
9
rtc@google.comded22402009-10-26 22:36:21 +000010import os
Chris Sosa7c931362010-10-11 19:49:01 -070011
Amin Hassani4f1e4622019-10-03 10:40:50 -070012from six.moves import urllib
13
14import cherrypy # pylint: disable=import-error
Gilad Arnoldabb352e2012-09-23 01:24:27 -070015
Amin Hassani8d718d12019-06-02 21:28:39 -070016# TODO(crbug.com/872441): We try to import nebraska from different places
17# because when we install the devserver, we copy the nebraska.py into the main
18# directory. Once this bug is resolved, we can always import from nebraska
19# directory.
20try:
21 from nebraska import nebraska
22except ImportError:
23 import nebraska
Chris Sosa05491b12010-11-08 17:14:16 -080024
Achuith Bhandarkar662fb722019-10-31 16:12:49 -070025import setup_chromite # pylint: disable=unused-import
Achuith Bhandarkar662fb722019-10-31 16:12:49 -070026from chromite.lib.xbuddy import cherrypy_log_util
27from chromite.lib.xbuddy import common_util
28from chromite.lib.xbuddy import devserver_constants as constants
29
Gilad Arnoldc65330c2012-09-20 15:17:48 -070030
31# Module-local log function.
Chris Sosa6a3697f2013-01-29 16:44:43 -080032def _Log(message, *args):
Achuith Bhandarkar662fb722019-10-31 16:12:49 -070033 return cherrypy_log_util.LogWithTag('UPDATE', message, *args)
rtc@google.comded22402009-10-26 22:36:21 +000034
Gilad Arnold0c9c8602012-10-02 23:58:58 -070035class AutoupdateError(Exception):
36 """Exception classes used by this module."""
37 pass
38
39
Don Garrett0ad09372010-12-06 16:20:30 -080040def _ChangeUrlPort(url, new_port):
41 """Return the URL passed in with a different port"""
Amin Hassani4f1e4622019-10-03 10:40:50 -070042 scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url)
Don Garrett0ad09372010-12-06 16:20:30 -080043 host_port = netloc.split(':')
44
45 if len(host_port) == 1:
46 host_port.append(new_port)
47 else:
48 host_port[1] = new_port
49
Don Garrettfb15e322016-06-21 19:12:08 -070050 print(host_port)
joychen121fc9b2013-08-02 14:30:30 -070051 netloc = '%s:%s' % tuple(host_port)
Don Garrett0ad09372010-12-06 16:20:30 -080052
Amin Hassani4f1e4622019-10-03 10:40:50 -070053 # pylint: disable=too-many-function-args
54 return urllib.parse.urlunsplit((scheme, netloc, path, query, fragment))
Don Garrett0ad09372010-12-06 16:20:30 -080055
Chris Sosa6a3697f2013-01-29 16:44:43 -080056def _NonePathJoin(*args):
57 """os.path.join that filters None's from the argument list."""
Amin Hassani4f1e4622019-10-03 10:40:50 -070058 return os.path.join(*[x for x in args if x is not None])
Don Garrett0ad09372010-12-06 16:20:30 -080059
Chris Sosa6a3697f2013-01-29 16:44:43 -080060
Amin Hassani4b5d5ab2020-03-22 19:57:46 -070061class Autoupdate(object):
Amin Hassanie9ffb862019-09-25 17:10:40 -070062 """Class that contains functionality that handles Chrome OS update pings."""
rtc@google.comded22402009-10-26 22:36:21 +000063
Gilad Arnold0c9c8602012-10-02 23:58:58 -070064 _PAYLOAD_URL_PREFIX = '/static/'
Chris Sosa6a3697f2013-01-29 16:44:43 -080065
Amin Hassani4b5d5ab2020-03-22 19:57:46 -070066 def __init__(self, xbuddy, static_dir=None, payload_path=None,
Amin Hassanid703c612020-03-25 14:35:48 -070067 proxy_port=None):
Amin Hassanie9ffb862019-09-25 17:10:40 -070068 """Initializes the class.
69
70 Args:
71 xbuddy: The xbuddy path.
Amin Hassani4b5d5ab2020-03-22 19:57:46 -070072 static_dir: The path to the devserver static directory.
Amin Hassanie9ffb862019-09-25 17:10:40 -070073 payload_path: The path to pre-generated payload to serve.
74 proxy_port: The port of local proxy to tell client to connect to you
75 through.
Amin Hassanie9ffb862019-09-25 17:10:40 -070076 """
joychen121fc9b2013-08-02 14:30:30 -070077 self.xbuddy = xbuddy
Amin Hassani4b5d5ab2020-03-22 19:57:46 -070078 self.static_dir = static_dir
Gilad Arnold0c9c8602012-10-02 23:58:58 -070079 self.payload_path = payload_path
Don Garrett0ad09372010-12-06 16:20:30 -080080 self.proxy_port = proxy_port
Gilad Arnoldd0c71752013-12-06 11:48:45 -080081
Amin Hassanie9ffb862019-09-25 17:10:40 -070082 def GetUpdateForLabel(self, label):
joychen121fc9b2013-08-02 14:30:30 -070083 """Given a label, get an update from the directory.
Chris Sosa0356d3b2010-09-16 15:46:22 -070084
joychen121fc9b2013-08-02 14:30:30 -070085 Args:
joychen121fc9b2013-08-02 14:30:30 -070086 label: the relative directory inside the static dir
Gilad Arnoldd8d595c2014-03-21 13:00:41 -070087
Chris Sosa6a3697f2013-01-29 16:44:43 -080088 Returns:
joychen121fc9b2013-08-02 14:30:30 -070089 A relative path to the directory with the update payload.
90 This is the label if an update did not need to be generated, but can
91 be label/cache/hashed_dir_for_update.
Gilad Arnoldd8d595c2014-03-21 13:00:41 -070092
Chris Sosa6a3697f2013-01-29 16:44:43 -080093 Raises:
joychen121fc9b2013-08-02 14:30:30 -070094 AutoupdateError: If client version is higher than available update found
95 at the directory given by the label.
Don Garrettf90edf02010-11-16 17:36:14 -080096 """
Amin Hassanie9ffb862019-09-25 17:10:40 -070097 _Log('Update label: %s', label)
98 static_update_path = _NonePathJoin(self.static_dir, label,
99 constants.UPDATE_FILE)
Don Garrettee25e552010-11-23 12:09:35 -0800100
joychen121fc9b2013-08-02 14:30:30 -0700101 if label and os.path.exists(static_update_path):
102 # An update payload was found for the given label, return it.
103 return label
Don Garrett0c880e22010-11-17 18:13:37 -0800104
joychen121fc9b2013-08-02 14:30:30 -0700105 # The label didn't resolve.
Amin Hassanie9ffb862019-09-25 17:10:40 -0700106 _Log('Did not found any update payload for label %s.', label)
joychen121fc9b2013-08-02 14:30:30 -0700107 return None
Chris Sosa2c048f12010-10-27 16:05:27 -0700108
David Rileyee75de22017-11-02 10:48:15 -0700109 def GetDevserverUrl(self):
110 """Returns the devserver url base."""
Chris Sosa6a3697f2013-01-29 16:44:43 -0800111 x_forwarded_host = cherrypy.request.headers.get('X-Forwarded-Host')
112 if x_forwarded_host:
113 hostname = 'http://' + x_forwarded_host
114 else:
115 hostname = cherrypy.request.base
116
David Rileyee75de22017-11-02 10:48:15 -0700117 return hostname
118
119 def GetStaticUrl(self):
120 """Returns the static url base that should prefix all payload responses."""
121 hostname = self.GetDevserverUrl()
122
Amin Hassanic9dd11e2019-07-11 15:33:55 -0700123 static_urlbase = '%s/static' % hostname
Chris Sosa6a3697f2013-01-29 16:44:43 -0800124 # If we have a proxy port, adjust the URL we instruct the client to
125 # use to go through the proxy.
126 if self.proxy_port:
127 static_urlbase = _ChangeUrlPort(static_urlbase, self.proxy_port)
128
129 _Log('Using static url base %s', static_urlbase)
130 _Log('Handling update ping as %s', hostname)
131 return static_urlbase
132
Amin Hassanie9ffb862019-09-25 17:10:40 -0700133 def GetPathToPayload(self, label, board):
joychen121fc9b2013-08-02 14:30:30 -0700134 """Find a payload locally.
135
136 See devserver's update rpc for documentation.
137
138 Args:
139 label: from update request
joychen121fc9b2013-08-02 14:30:30 -0700140 board: from update request
Gilad Arnoldd8d595c2014-03-21 13:00:41 -0700141
142 Returns:
joychen121fc9b2013-08-02 14:30:30 -0700143 The relative path to an update from the static_dir
Gilad Arnoldd8d595c2014-03-21 13:00:41 -0700144
joychen121fc9b2013-08-02 14:30:30 -0700145 Raises:
146 AutoupdateError: If the update could not be found.
147 """
148 path_to_payload = None
Amin Hassanie9ffb862019-09-25 17:10:40 -0700149 # TODO(crbug.com/1006305): deprecate --payload flag
joychen121fc9b2013-08-02 14:30:30 -0700150 if self.payload_path:
151 # Copy the image from the path to '/forced_payload'
152 label = 'forced_payload'
153 dest_path = os.path.join(self.static_dir, label, constants.UPDATE_FILE)
154 dest_stateful = os.path.join(self.static_dir, label,
155 constants.STATEFUL_FILE)
Amin Hassani8d718d12019-06-02 21:28:39 -0700156 dest_meta = os.path.join(self.static_dir, label,
157 constants.UPDATE_METADATA_FILE)
joychen121fc9b2013-08-02 14:30:30 -0700158
159 src_path = os.path.abspath(self.payload_path)
Amin Hassanie9ffb862019-09-25 17:10:40 -0700160 src_meta = os.path.abspath(self.payload_path + '.json')
joychen121fc9b2013-08-02 14:30:30 -0700161 src_stateful = os.path.join(os.path.dirname(src_path),
162 constants.STATEFUL_FILE)
163 common_util.MkDirP(os.path.join(self.static_dir, label))
Alex Deymo3e2d4952013-09-03 21:49:41 -0700164 common_util.SymlinkFile(src_path, dest_path)
Amin Hassanie9ffb862019-09-25 17:10:40 -0700165 common_util.SymlinkFile(src_meta, dest_meta)
joychen121fc9b2013-08-02 14:30:30 -0700166 if os.path.exists(src_stateful):
167 # The stateful payload is optional.
Alex Deymo3e2d4952013-09-03 21:49:41 -0700168 common_util.SymlinkFile(src_stateful, dest_stateful)
joychen121fc9b2013-08-02 14:30:30 -0700169 else:
170 _Log('WARN: %s not found. Expected for dev and test builds',
171 constants.STATEFUL_FILE)
172 if os.path.exists(dest_stateful):
173 os.remove(dest_stateful)
Amin Hassanie9ffb862019-09-25 17:10:40 -0700174 path_to_payload = self.GetUpdateForLabel(label)
joychen121fc9b2013-08-02 14:30:30 -0700175 else:
176 label = label or ''
177 label_list = label.split('/')
178 # Suppose that the path follows old protocol of indexing straight
179 # into static_dir with board/version label.
180 # Attempt to get the update in that directory, generating if necc.
Amin Hassanie9ffb862019-09-25 17:10:40 -0700181 path_to_payload = self.GetUpdateForLabel(label)
joychen121fc9b2013-08-02 14:30:30 -0700182 if path_to_payload is None:
Amin Hassanie9ffb862019-09-25 17:10:40 -0700183 # There was no update found in the directory. Let XBuddy find the
184 # payloads.
joychen121fc9b2013-08-02 14:30:30 -0700185 if label_list[0] == 'xbuddy':
186 # If path explicitly calls xbuddy, pop off the tag.
187 label_list.pop()
Amin Hassanie9ffb862019-09-25 17:10:40 -0700188 x_label, _ = self.xbuddy.Translate(label_list, board=board)
189 # Path has been resolved, try to get the payload.
190 path_to_payload = self.GetUpdateForLabel(x_label)
joychen121fc9b2013-08-02 14:30:30 -0700191 if path_to_payload is None:
Amin Hassanie9ffb862019-09-25 17:10:40 -0700192 # No update payload found after translation. Try to get an update to
193 # a test image from GS using the label.
joychen121fc9b2013-08-02 14:30:30 -0700194 path_to_payload, _image_name = self.xbuddy.Get(
195 ['remote', label, 'full_payload'])
196
197 # One of the above options should have gotten us a relative path.
198 if path_to_payload is None:
199 raise AutoupdateError('Failed to get an update for: %s' % label)
Amin Hassani8d718d12019-06-02 21:28:39 -0700200
201 return path_to_payload
joychen121fc9b2013-08-02 14:30:30 -0700202
Amin Hassani6eec8792020-01-09 14:06:48 -0800203 def HandleUpdatePing(self, data, label='', **kwargs):
Chris Sosa6a3697f2013-01-29 16:44:43 -0800204 """Handles an update ping from an update client.
205
206 Args:
207 data: XML blob from client.
208 label: optional label for the update.
Amin Hassani6eec8792020-01-09 14:06:48 -0800209 kwargs: The map of query strings passed to the /update API.
Gilad Arnoldd8d595c2014-03-21 13:00:41 -0700210
Chris Sosa6a3697f2013-01-29 16:44:43 -0800211 Returns:
212 Update payload message for client.
213 """
214 # Get the static url base that will form that base of our update url e.g.
215 # http://hostname:8080/static/update.gz.
David Rileyee75de22017-11-02 10:48:15 -0700216 static_urlbase = self.GetStaticUrl()
Amin Hassani86c6fb52020-02-28 11:03:52 -0800217 # Change the URL's string query dictionary provided by cherrypy to a valid
218 # dictionary that has proper values for its keys. e.g. True instead of
219 # 'True'.
220 kwargs = nebraska.QueryDictToDict(kwargs)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800221
Chris Sosab26b1202013-08-16 16:40:55 -0700222 # Process attributes of the update check.
Amin Hassani8d718d12019-06-02 21:28:39 -0700223 request = nebraska.Request(data)
Amin Hassani8d718d12019-06-02 21:28:39 -0700224 if request.request_type == nebraska.Request.RequestType.EVENT:
Gilad Arnolde7819e72014-03-21 12:50:48 -0700225 _Log('A non-update event notification received. Returning an ack.')
Amin Hassani083e3fe2020-02-13 11:39:18 -0800226 return nebraska.Nebraska().GetResponseToRequest(
227 request, response_props=nebraska.ResponseProperties(**kwargs))
Chris Sosa6a3697f2013-01-29 16:44:43 -0800228
Amin Hassani8d718d12019-06-02 21:28:39 -0700229 _Log('Update Check Received.')
Chris Sosa6a3697f2013-01-29 16:44:43 -0800230
231 try:
Amin Hassanie7ead902019-10-11 16:42:43 -0700232 path_to_payload = self.GetPathToPayload(label, request.board)
Amin Hassani8d718d12019-06-02 21:28:39 -0700233 base_url = _NonePathJoin(static_urlbase, path_to_payload)
Amin Hassanic9dd11e2019-07-11 15:33:55 -0700234 local_payload_dir = _NonePathJoin(self.static_dir, path_to_payload)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800235 except AutoupdateError as e:
236 # Raised if we fail to generate an update payload.
Amin Hassani8d718d12019-06-02 21:28:39 -0700237 _Log('Failed to process an update request, but we will defer to '
238 'nebraska to respond with no-update. The error was %s', e)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800239
Amin Hassani8d718d12019-06-02 21:28:39 -0700240 _Log('Responding to client to use url %s to get image', base_url)
Amin Hassanic91fc0d2019-12-04 11:07:16 -0800241 nebraska_props = nebraska.NebraskaProperties(
242 update_payloads_address=base_url,
243 update_metadata_dir=local_payload_dir)
Amin Hassanic91fc0d2019-12-04 11:07:16 -0800244 nebraska_obj = nebraska.Nebraska(nebraska_props=nebraska_props)
Amin Hassani083e3fe2020-02-13 11:39:18 -0800245 return nebraska_obj.GetResponseToRequest(
246 request, response_props=nebraska.ResponseProperties(**kwargs))