blob: 72bb1a93cac2d2676fd243b101bad95238aa115c [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
Achuith Bhandarkar662fb722019-10-31 16:12:49 -070027from chromite.lib.xbuddy import devserver_constants as constants
28
Gilad Arnoldc65330c2012-09-20 15:17:48 -070029
30# Module-local log function.
Chris Sosa6a3697f2013-01-29 16:44:43 -080031def _Log(message, *args):
Achuith Bhandarkar662fb722019-10-31 16:12:49 -070032 return cherrypy_log_util.LogWithTag('UPDATE', message, *args)
rtc@google.comded22402009-10-26 22:36:21 +000033
Gilad Arnold0c9c8602012-10-02 23:58:58 -070034class AutoupdateError(Exception):
35 """Exception classes used by this module."""
36 pass
37
38
Don Garrett0ad09372010-12-06 16:20:30 -080039def _ChangeUrlPort(url, new_port):
40 """Return the URL passed in with a different port"""
Amin Hassani4f1e4622019-10-03 10:40:50 -070041 scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url)
Don Garrett0ad09372010-12-06 16:20:30 -080042 host_port = netloc.split(':')
43
44 if len(host_port) == 1:
45 host_port.append(new_port)
46 else:
47 host_port[1] = new_port
48
Don Garrettfb15e322016-06-21 19:12:08 -070049 print(host_port)
joychen121fc9b2013-08-02 14:30:30 -070050 netloc = '%s:%s' % tuple(host_port)
Don Garrett0ad09372010-12-06 16:20:30 -080051
Amin Hassani4f1e4622019-10-03 10:40:50 -070052 # pylint: disable=too-many-function-args
53 return urllib.parse.urlunsplit((scheme, netloc, path, query, fragment))
Don Garrett0ad09372010-12-06 16:20:30 -080054
Chris Sosa6a3697f2013-01-29 16:44:43 -080055def _NonePathJoin(*args):
56 """os.path.join that filters None's from the argument list."""
Amin Hassani4f1e4622019-10-03 10:40:50 -070057 return os.path.join(*[x for x in args if x is not None])
Don Garrett0ad09372010-12-06 16:20:30 -080058
Chris Sosa6a3697f2013-01-29 16:44:43 -080059
Amin Hassani4b5d5ab2020-03-22 19:57:46 -070060class Autoupdate(object):
Amin Hassanie9ffb862019-09-25 17:10:40 -070061 """Class that contains functionality that handles Chrome OS update pings."""
rtc@google.comded22402009-10-26 22:36:21 +000062
Amin Hassaniabbb8842020-03-25 14:44:23 -070063 def __init__(self, xbuddy, static_dir=None, proxy_port=None):
Amin Hassanie9ffb862019-09-25 17:10:40 -070064 """Initializes the class.
65
66 Args:
67 xbuddy: The xbuddy path.
Amin Hassani4b5d5ab2020-03-22 19:57:46 -070068 static_dir: The path to the devserver static directory.
Amin Hassanie9ffb862019-09-25 17:10:40 -070069 proxy_port: The port of local proxy to tell client to connect to you
70 through.
Amin Hassanie9ffb862019-09-25 17:10:40 -070071 """
joychen121fc9b2013-08-02 14:30:30 -070072 self.xbuddy = xbuddy
Amin Hassani4b5d5ab2020-03-22 19:57:46 -070073 self.static_dir = static_dir
Don Garrett0ad09372010-12-06 16:20:30 -080074 self.proxy_port = proxy_port
Gilad Arnoldd0c71752013-12-06 11:48:45 -080075
Amin Hassanie9ffb862019-09-25 17:10:40 -070076 def GetUpdateForLabel(self, label):
joychen121fc9b2013-08-02 14:30:30 -070077 """Given a label, get an update from the directory.
Chris Sosa0356d3b2010-09-16 15:46:22 -070078
joychen121fc9b2013-08-02 14:30:30 -070079 Args:
joychen121fc9b2013-08-02 14:30:30 -070080 label: the relative directory inside the static dir
Gilad Arnoldd8d595c2014-03-21 13:00:41 -070081
Chris Sosa6a3697f2013-01-29 16:44:43 -080082 Returns:
joychen121fc9b2013-08-02 14:30:30 -070083 A relative path to the directory with the update payload.
84 This is the label if an update did not need to be generated, but can
85 be label/cache/hashed_dir_for_update.
Gilad Arnoldd8d595c2014-03-21 13:00:41 -070086
Chris Sosa6a3697f2013-01-29 16:44:43 -080087 Raises:
joychen121fc9b2013-08-02 14:30:30 -070088 AutoupdateError: If client version is higher than available update found
89 at the directory given by the label.
Don Garrettf90edf02010-11-16 17:36:14 -080090 """
Amin Hassanie9ffb862019-09-25 17:10:40 -070091 _Log('Update label: %s', label)
92 static_update_path = _NonePathJoin(self.static_dir, label,
93 constants.UPDATE_FILE)
Don Garrettee25e552010-11-23 12:09:35 -080094
joychen121fc9b2013-08-02 14:30:30 -070095 if label and os.path.exists(static_update_path):
96 # An update payload was found for the given label, return it.
97 return label
Don Garrett0c880e22010-11-17 18:13:37 -080098
joychen121fc9b2013-08-02 14:30:30 -070099 # The label didn't resolve.
Amin Hassanie9ffb862019-09-25 17:10:40 -0700100 _Log('Did not found any update payload for label %s.', label)
joychen121fc9b2013-08-02 14:30:30 -0700101 return None
Chris Sosa2c048f12010-10-27 16:05:27 -0700102
David Rileyee75de22017-11-02 10:48:15 -0700103 def GetDevserverUrl(self):
104 """Returns the devserver url base."""
Chris Sosa6a3697f2013-01-29 16:44:43 -0800105 x_forwarded_host = cherrypy.request.headers.get('X-Forwarded-Host')
106 if x_forwarded_host:
107 hostname = 'http://' + x_forwarded_host
108 else:
109 hostname = cherrypy.request.base
110
David Rileyee75de22017-11-02 10:48:15 -0700111 return hostname
112
113 def GetStaticUrl(self):
114 """Returns the static url base that should prefix all payload responses."""
115 hostname = self.GetDevserverUrl()
116
Amin Hassanic9dd11e2019-07-11 15:33:55 -0700117 static_urlbase = '%s/static' % hostname
Chris Sosa6a3697f2013-01-29 16:44:43 -0800118 # If we have a proxy port, adjust the URL we instruct the client to
119 # use to go through the proxy.
120 if self.proxy_port:
121 static_urlbase = _ChangeUrlPort(static_urlbase, self.proxy_port)
122
123 _Log('Using static url base %s', static_urlbase)
124 _Log('Handling update ping as %s', hostname)
125 return static_urlbase
126
Amin Hassanie9ffb862019-09-25 17:10:40 -0700127 def GetPathToPayload(self, label, board):
joychen121fc9b2013-08-02 14:30:30 -0700128 """Find a payload locally.
129
130 See devserver's update rpc for documentation.
131
132 Args:
133 label: from update request
joychen121fc9b2013-08-02 14:30:30 -0700134 board: from update request
Gilad Arnoldd8d595c2014-03-21 13:00:41 -0700135
136 Returns:
joychen121fc9b2013-08-02 14:30:30 -0700137 The relative path to an update from the static_dir
Gilad Arnoldd8d595c2014-03-21 13:00:41 -0700138
joychen121fc9b2013-08-02 14:30:30 -0700139 Raises:
140 AutoupdateError: If the update could not be found.
141 """
Amin Hassaniabbb8842020-03-25 14:44:23 -0700142 label = label or ''
143 label_list = label.split('/')
144 # Suppose that the path follows old protocol of indexing straight
145 # into static_dir with board/version label.
146 # Attempt to get the update in that directory, generating if necc.
147 path_to_payload = self.GetUpdateForLabel(label)
148 if path_to_payload is None:
149 # There was no update found in the directory. Let XBuddy find the
150 # payloads.
151 if label_list[0] == 'xbuddy':
152 # If path explicitly calls xbuddy, pop off the tag.
153 label_list.pop()
154 x_label, _ = self.xbuddy.Translate(label_list, board=board)
155 # Path has been resolved, try to get the payload.
156 path_to_payload = self.GetUpdateForLabel(x_label)
joychen121fc9b2013-08-02 14:30:30 -0700157 if path_to_payload is None:
Amin Hassaniabbb8842020-03-25 14:44:23 -0700158 # No update payload found after translation. Try to get an update to
159 # a test image from GS using the label.
160 path_to_payload, _image_name = self.xbuddy.Get(
161 ['remote', label, 'full_payload'])
joychen121fc9b2013-08-02 14:30:30 -0700162
163 # One of the above options should have gotten us a relative path.
164 if path_to_payload is None:
165 raise AutoupdateError('Failed to get an update for: %s' % label)
Amin Hassani8d718d12019-06-02 21:28:39 -0700166
167 return path_to_payload
joychen121fc9b2013-08-02 14:30:30 -0700168
Amin Hassani6eec8792020-01-09 14:06:48 -0800169 def HandleUpdatePing(self, data, label='', **kwargs):
Chris Sosa6a3697f2013-01-29 16:44:43 -0800170 """Handles an update ping from an update client.
171
172 Args:
173 data: XML blob from client.
174 label: optional label for the update.
Amin Hassani6eec8792020-01-09 14:06:48 -0800175 kwargs: The map of query strings passed to the /update API.
Gilad Arnoldd8d595c2014-03-21 13:00:41 -0700176
Chris Sosa6a3697f2013-01-29 16:44:43 -0800177 Returns:
178 Update payload message for client.
179 """
180 # Get the static url base that will form that base of our update url e.g.
181 # http://hostname:8080/static/update.gz.
David Rileyee75de22017-11-02 10:48:15 -0700182 static_urlbase = self.GetStaticUrl()
Amin Hassani86c6fb52020-02-28 11:03:52 -0800183 # Change the URL's string query dictionary provided by cherrypy to a valid
184 # dictionary that has proper values for its keys. e.g. True instead of
185 # 'True'.
186 kwargs = nebraska.QueryDictToDict(kwargs)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800187
Chris Sosab26b1202013-08-16 16:40:55 -0700188 # Process attributes of the update check.
Amin Hassani8d718d12019-06-02 21:28:39 -0700189 request = nebraska.Request(data)
Amin Hassani8d718d12019-06-02 21:28:39 -0700190 if request.request_type == nebraska.Request.RequestType.EVENT:
Gilad Arnolde7819e72014-03-21 12:50:48 -0700191 _Log('A non-update event notification received. Returning an ack.')
Amin Hassani083e3fe2020-02-13 11:39:18 -0800192 return nebraska.Nebraska().GetResponseToRequest(
193 request, response_props=nebraska.ResponseProperties(**kwargs))
Chris Sosa6a3697f2013-01-29 16:44:43 -0800194
Amin Hassani8d718d12019-06-02 21:28:39 -0700195 _Log('Update Check Received.')
Chris Sosa6a3697f2013-01-29 16:44:43 -0800196
197 try:
Amin Hassanie7ead902019-10-11 16:42:43 -0700198 path_to_payload = self.GetPathToPayload(label, request.board)
Amin Hassani8d718d12019-06-02 21:28:39 -0700199 base_url = _NonePathJoin(static_urlbase, path_to_payload)
Amin Hassanic9dd11e2019-07-11 15:33:55 -0700200 local_payload_dir = _NonePathJoin(self.static_dir, path_to_payload)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800201 except AutoupdateError as e:
202 # Raised if we fail to generate an update payload.
Amin Hassani8d718d12019-06-02 21:28:39 -0700203 _Log('Failed to process an update request, but we will defer to '
204 'nebraska to respond with no-update. The error was %s', e)
Chris Sosa6a3697f2013-01-29 16:44:43 -0800205
Amin Hassani8d718d12019-06-02 21:28:39 -0700206 _Log('Responding to client to use url %s to get image', base_url)
Amin Hassanic91fc0d2019-12-04 11:07:16 -0800207 nebraska_props = nebraska.NebraskaProperties(
208 update_payloads_address=base_url,
209 update_metadata_dir=local_payload_dir)
Amin Hassanic91fc0d2019-12-04 11:07:16 -0800210 nebraska_obj = nebraska.Nebraska(nebraska_props=nebraska_props)
Amin Hassani083e3fe2020-02-13 11:39:18 -0800211 return nebraska_obj.GetResponseToRequest(
212 request, response_props=nebraska.ResponseProperties(**kwargs))