Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Darin Petkov | c3fd90c | 2011-05-11 21:23:00 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Gilad Arnold | d8d595c | 2014-03-21 20:00:41 | [diff] [blame] | 6 | """Devserver module for handling update client requests.""" |
| 7 | |
Don Garrett | fb15e32 | 2016-06-22 02:12:08 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 10 | import os |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 11 | |
Amin Hassani | 4f1e462 | 2019-10-03 17:40:50 | [diff] [blame] | 12 | from six.moves import urllib |
| 13 | |
| 14 | import cherrypy # pylint: disable=import-error |
Gilad Arnold | abb352e | 2012-09-23 08:24:27 | [diff] [blame] | 15 | |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 16 | # 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. |
| 20 | try: |
| 21 | from nebraska import nebraska |
| 22 | except ImportError: |
| 23 | import nebraska |
Chris Sosa | 05491b1 | 2010-11-09 01:14:16 | [diff] [blame] | 24 | |
Achuith Bhandarkar | 662fb72 | 2019-10-31 23:12:49 | [diff] [blame] | 25 | import setup_chromite # pylint: disable=unused-import |
Achuith Bhandarkar | 662fb72 | 2019-10-31 23:12:49 | [diff] [blame] | 26 | from chromite.lib.xbuddy import cherrypy_log_util |
Achuith Bhandarkar | 662fb72 | 2019-10-31 23:12:49 | [diff] [blame] | 27 | from chromite.lib.xbuddy import devserver_constants as constants |
| 28 | |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 29 | |
| 30 | # Module-local log function. |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 31 | def _Log(message, *args): |
Achuith Bhandarkar | 662fb72 | 2019-10-31 23:12:49 | [diff] [blame] | 32 | return cherrypy_log_util.LogWithTag('UPDATE', message, *args) |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 33 | |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 34 | class AutoupdateError(Exception): |
| 35 | """Exception classes used by this module.""" |
| 36 | pass |
| 37 | |
| 38 | |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 39 | def _ChangeUrlPort(url, new_port): |
| 40 | """Return the URL passed in with a different port""" |
Amin Hassani | 4f1e462 | 2019-10-03 17:40:50 | [diff] [blame] | 41 | scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url) |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 42 | 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 Garrett | fb15e32 | 2016-06-22 02:12:08 | [diff] [blame] | 49 | print(host_port) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 50 | netloc = '%s:%s' % tuple(host_port) |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 51 | |
Amin Hassani | 4f1e462 | 2019-10-03 17:40:50 | [diff] [blame] | 52 | # pylint: disable=too-many-function-args |
| 53 | return urllib.parse.urlunsplit((scheme, netloc, path, query, fragment)) |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 54 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 55 | def _NonePathJoin(*args): |
| 56 | """os.path.join that filters None's from the argument list.""" |
Amin Hassani | 4f1e462 | 2019-10-03 17:40:50 | [diff] [blame] | 57 | return os.path.join(*[x for x in args if x is not None]) |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 58 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 59 | |
Amin Hassani | 4b5d5ab | 2020-03-23 02:57:46 | [diff] [blame] | 60 | class Autoupdate(object): |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 61 | """Class that contains functionality that handles Chrome OS update pings.""" |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 62 | |
Amin Hassani | e561203 | 2020-03-25 21:50:42 | [diff] [blame] | 63 | def __init__(self, xbuddy, static_dir=None): |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 64 | """Initializes the class. |
| 65 | |
| 66 | Args: |
| 67 | xbuddy: The xbuddy path. |
Amin Hassani | 4b5d5ab | 2020-03-23 02:57:46 | [diff] [blame] | 68 | static_dir: The path to the devserver static directory. |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 69 | """ |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 70 | self.xbuddy = xbuddy |
Amin Hassani | 4b5d5ab | 2020-03-23 02:57:46 | [diff] [blame] | 71 | self.static_dir = static_dir |
Gilad Arnold | d0c7175 | 2013-12-06 19:48:45 | [diff] [blame] | 72 | |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 73 | def GetUpdateForLabel(self, label): |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 74 | """Given a label, get an update from the directory. |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 75 | |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 76 | Args: |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 77 | label: the relative directory inside the static dir |
Gilad Arnold | d8d595c | 2014-03-21 20:00:41 | [diff] [blame] | 78 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 79 | Returns: |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 80 | A relative path to the directory with the update payload. |
| 81 | This is the label if an update did not need to be generated, but can |
| 82 | be label/cache/hashed_dir_for_update. |
Gilad Arnold | d8d595c | 2014-03-21 20:00:41 | [diff] [blame] | 83 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 84 | Raises: |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 85 | AutoupdateError: If client version is higher than available update found |
| 86 | at the directory given by the label. |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 87 | """ |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 88 | _Log('Update label: %s', label) |
| 89 | static_update_path = _NonePathJoin(self.static_dir, label, |
| 90 | constants.UPDATE_FILE) |
Don Garrett | ee25e55 | 2010-11-23 20:09:35 | [diff] [blame] | 91 | |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 92 | if label and os.path.exists(static_update_path): |
| 93 | # An update payload was found for the given label, return it. |
| 94 | return label |
Don Garrett | 0c880e2 | 2010-11-18 02:13:37 | [diff] [blame] | 95 | |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 96 | # The label didn't resolve. |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 97 | _Log('Did not found any update payload for label %s.', label) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 98 | return None |
Chris Sosa | 2c048f1 | 2010-10-27 23:05:27 | [diff] [blame] | 99 | |
David Riley | ee75de2 | 2017-11-02 17:48:15 | [diff] [blame] | 100 | def GetDevserverUrl(self): |
| 101 | """Returns the devserver url base.""" |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 102 | x_forwarded_host = cherrypy.request.headers.get('X-Forwarded-Host') |
| 103 | if x_forwarded_host: |
| 104 | hostname = 'http://' + x_forwarded_host |
| 105 | else: |
| 106 | hostname = cherrypy.request.base |
| 107 | |
David Riley | ee75de2 | 2017-11-02 17:48:15 | [diff] [blame] | 108 | return hostname |
| 109 | |
| 110 | def GetStaticUrl(self): |
| 111 | """Returns the static url base that should prefix all payload responses.""" |
| 112 | hostname = self.GetDevserverUrl() |
Amin Hassani | e561203 | 2020-03-25 21:50:42 | [diff] [blame] | 113 | _Log('Handling update ping as %s', hostname) |
David Riley | ee75de2 | 2017-11-02 17:48:15 | [diff] [blame] | 114 | |
Amin Hassani | c9dd11e | 2019-07-11 22:33:55 | [diff] [blame] | 115 | static_urlbase = '%s/static' % hostname |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 116 | _Log('Using static url base %s', static_urlbase) |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 117 | return static_urlbase |
| 118 | |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 119 | def GetPathToPayload(self, label, board): |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 120 | """Find a payload locally. |
| 121 | |
| 122 | See devserver's update rpc for documentation. |
| 123 | |
| 124 | Args: |
| 125 | label: from update request |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 126 | board: from update request |
Gilad Arnold | d8d595c | 2014-03-21 20:00:41 | [diff] [blame] | 127 | |
| 128 | Returns: |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 129 | The relative path to an update from the static_dir |
Gilad Arnold | d8d595c | 2014-03-21 20:00:41 | [diff] [blame] | 130 | |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 131 | Raises: |
| 132 | AutoupdateError: If the update could not be found. |
| 133 | """ |
Amin Hassani | abbb884 | 2020-03-25 21:44:23 | [diff] [blame] | 134 | label = label or '' |
| 135 | label_list = label.split('/') |
| 136 | # Suppose that the path follows old protocol of indexing straight |
| 137 | # into static_dir with board/version label. |
| 138 | # Attempt to get the update in that directory, generating if necc. |
| 139 | path_to_payload = self.GetUpdateForLabel(label) |
| 140 | if path_to_payload is None: |
| 141 | # There was no update found in the directory. Let XBuddy find the |
| 142 | # payloads. |
| 143 | if label_list[0] == 'xbuddy': |
| 144 | # If path explicitly calls xbuddy, pop off the tag. |
| 145 | label_list.pop() |
| 146 | x_label, _ = self.xbuddy.Translate(label_list, board=board) |
| 147 | # Path has been resolved, try to get the payload. |
| 148 | path_to_payload = self.GetUpdateForLabel(x_label) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 149 | if path_to_payload is None: |
Amin Hassani | abbb884 | 2020-03-25 21:44:23 | [diff] [blame] | 150 | # No update payload found after translation. Try to get an update to |
| 151 | # a test image from GS using the label. |
| 152 | path_to_payload, _image_name = self.xbuddy.Get( |
| 153 | ['remote', label, 'full_payload']) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 154 | |
| 155 | # One of the above options should have gotten us a relative path. |
| 156 | if path_to_payload is None: |
| 157 | raise AutoupdateError('Failed to get an update for: %s' % label) |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 158 | |
| 159 | return path_to_payload |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 160 | |
Amin Hassani | 6eec879 | 2020-01-09 22:06:48 | [diff] [blame] | 161 | def HandleUpdatePing(self, data, label='', **kwargs): |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 162 | """Handles an update ping from an update client. |
| 163 | |
| 164 | Args: |
| 165 | data: XML blob from client. |
| 166 | label: optional label for the update. |
Amin Hassani | 6eec879 | 2020-01-09 22:06:48 | [diff] [blame] | 167 | kwargs: The map of query strings passed to the /update API. |
Gilad Arnold | d8d595c | 2014-03-21 20:00:41 | [diff] [blame] | 168 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 169 | Returns: |
| 170 | Update payload message for client. |
| 171 | """ |
| 172 | # Get the static url base that will form that base of our update url e.g. |
| 173 | # http://hostname:8080/static/update.gz. |
David Riley | ee75de2 | 2017-11-02 17:48:15 | [diff] [blame] | 174 | static_urlbase = self.GetStaticUrl() |
Amin Hassani | 86c6fb5 | 2020-02-28 19:03:52 | [diff] [blame] | 175 | # Change the URL's string query dictionary provided by cherrypy to a valid |
| 176 | # dictionary that has proper values for its keys. e.g. True instead of |
| 177 | # 'True'. |
| 178 | kwargs = nebraska.QueryDictToDict(kwargs) |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 179 | |
Chris Sosa | b26b120 | 2013-08-16 23:40:55 | [diff] [blame] | 180 | # Process attributes of the update check. |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 181 | request = nebraska.Request(data) |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 182 | if request.request_type == nebraska.Request.RequestType.EVENT: |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 183 | _Log('A non-update event notification received. Returning an ack.') |
Amin Hassani | 083e3fe | 2020-02-13 19:39:18 | [diff] [blame] | 184 | return nebraska.Nebraska().GetResponseToRequest( |
| 185 | request, response_props=nebraska.ResponseProperties(**kwargs)) |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 186 | |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 187 | _Log('Update Check Received.') |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 188 | |
| 189 | try: |
Amin Hassani | e7ead90 | 2019-10-11 23:42:43 | [diff] [blame] | 190 | path_to_payload = self.GetPathToPayload(label, request.board) |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 191 | base_url = _NonePathJoin(static_urlbase, path_to_payload) |
Amin Hassani | c9dd11e | 2019-07-11 22:33:55 | [diff] [blame] | 192 | local_payload_dir = _NonePathJoin(self.static_dir, path_to_payload) |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 193 | except AutoupdateError as e: |
| 194 | # Raised if we fail to generate an update payload. |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 195 | _Log('Failed to process an update request, but we will defer to ' |
| 196 | 'nebraska to respond with no-update. The error was %s', e) |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 197 | |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 198 | _Log('Responding to client to use url %s to get image', base_url) |
Amin Hassani | c91fc0d | 2019-12-04 19:07:16 | [diff] [blame] | 199 | nebraska_props = nebraska.NebraskaProperties( |
| 200 | update_payloads_address=base_url, |
| 201 | update_metadata_dir=local_payload_dir) |
Amin Hassani | c91fc0d | 2019-12-04 19:07:16 | [diff] [blame] | 202 | nebraska_obj = nebraska.Nebraska(nebraska_props=nebraska_props) |
Amin Hassani | 083e3fe | 2020-02-13 19:39:18 | [diff] [blame] | 203 | return nebraska_obj.GetResponseToRequest( |
| 204 | request, response_props=nebraska.ResponseProperties(**kwargs)) |