Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Darin Petkov | c3fd90c | 2011-05-11 14:23:00 -0700 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [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 13:00:41 -0700 | [diff] [blame] | 6 | """Devserver module for handling update client requests.""" |
| 7 | |
Don Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 10 | import os |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 11 | |
Amin Hassani | 4f1e462 | 2019-10-03 10:40:50 -0700 | [diff] [blame] | 12 | from six.moves import urllib |
| 13 | |
| 14 | import cherrypy # pylint: disable=import-error |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 15 | |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [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-08 17:14:16 -0800 | [diff] [blame] | 24 | |
Achuith Bhandarkar | 662fb72 | 2019-10-31 16:12:49 -0700 | [diff] [blame] | 25 | import setup_chromite # pylint: disable=unused-import |
Achuith Bhandarkar | 662fb72 | 2019-10-31 16:12:49 -0700 | [diff] [blame] | 26 | from chromite.lib.xbuddy import cherrypy_log_util |
| 27 | from chromite.lib.xbuddy import common_util |
| 28 | from chromite.lib.xbuddy import devserver_constants as constants |
| 29 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 30 | |
| 31 | # Module-local log function. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 32 | def _Log(message, *args): |
Achuith Bhandarkar | 662fb72 | 2019-10-31 16:12:49 -0700 | [diff] [blame] | 33 | return cherrypy_log_util.LogWithTag('UPDATE', message, *args) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 34 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 35 | class AutoupdateError(Exception): |
| 36 | """Exception classes used by this module.""" |
| 37 | pass |
| 38 | |
| 39 | |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 40 | def _ChangeUrlPort(url, new_port): |
| 41 | """Return the URL passed in with a different port""" |
Amin Hassani | 4f1e462 | 2019-10-03 10:40:50 -0700 | [diff] [blame] | 42 | scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url) |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 43 | 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 Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 50 | print(host_port) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 51 | netloc = '%s:%s' % tuple(host_port) |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 52 | |
Amin Hassani | 4f1e462 | 2019-10-03 10:40:50 -0700 | [diff] [blame] | 53 | # pylint: disable=too-many-function-args |
| 54 | return urllib.parse.urlunsplit((scheme, netloc, path, query, fragment)) |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 55 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 56 | def _NonePathJoin(*args): |
| 57 | """os.path.join that filters None's from the argument list.""" |
Amin Hassani | 4f1e462 | 2019-10-03 10:40:50 -0700 | [diff] [blame] | 58 | return os.path.join(*[x for x in args if x is not None]) |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 59 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 60 | |
Amin Hassani | 4b5d5ab | 2020-03-22 19:57:46 -0700 | [diff] [blame] | 61 | class Autoupdate(object): |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 62 | """Class that contains functionality that handles Chrome OS update pings.""" |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 63 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 64 | _PAYLOAD_URL_PREFIX = '/static/' |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 65 | |
Amin Hassani | 4b5d5ab | 2020-03-22 19:57:46 -0700 | [diff] [blame] | 66 | def __init__(self, xbuddy, static_dir=None, payload_path=None, |
Amin Hassani | d703c61 | 2020-03-25 14:35:48 -0700 | [diff] [blame^] | 67 | proxy_port=None): |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 68 | """Initializes the class. |
| 69 | |
| 70 | Args: |
| 71 | xbuddy: The xbuddy path. |
Amin Hassani | 4b5d5ab | 2020-03-22 19:57:46 -0700 | [diff] [blame] | 72 | static_dir: The path to the devserver static directory. |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 73 | 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 Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 76 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 77 | self.xbuddy = xbuddy |
Amin Hassani | 4b5d5ab | 2020-03-22 19:57:46 -0700 | [diff] [blame] | 78 | self.static_dir = static_dir |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 79 | self.payload_path = payload_path |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 80 | self.proxy_port = proxy_port |
Gilad Arnold | d0c7175 | 2013-12-06 11:48:45 -0800 | [diff] [blame] | 81 | |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 82 | def GetUpdateForLabel(self, label): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 83 | """Given a label, get an update from the directory. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 84 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 85 | Args: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 86 | label: the relative directory inside the static dir |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 87 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 88 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 89 | 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 Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 92 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 93 | Raises: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 94 | AutoupdateError: If client version is higher than available update found |
| 95 | at the directory given by the label. |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 96 | """ |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 97 | _Log('Update label: %s', label) |
| 98 | static_update_path = _NonePathJoin(self.static_dir, label, |
| 99 | constants.UPDATE_FILE) |
Don Garrett | ee25e55 | 2010-11-23 12:09:35 -0800 | [diff] [blame] | 100 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 101 | 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 Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 104 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 105 | # The label didn't resolve. |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 106 | _Log('Did not found any update payload for label %s.', label) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 107 | return None |
Chris Sosa | 2c048f1 | 2010-10-27 16:05:27 -0700 | [diff] [blame] | 108 | |
David Riley | ee75de2 | 2017-11-02 10:48:15 -0700 | [diff] [blame] | 109 | def GetDevserverUrl(self): |
| 110 | """Returns the devserver url base.""" |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 111 | 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 Riley | ee75de2 | 2017-11-02 10:48:15 -0700 | [diff] [blame] | 117 | 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 Hassani | c9dd11e | 2019-07-11 15:33:55 -0700 | [diff] [blame] | 123 | static_urlbase = '%s/static' % hostname |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 124 | # 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 Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 133 | def GetPathToPayload(self, label, board): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 134 | """Find a payload locally. |
| 135 | |
| 136 | See devserver's update rpc for documentation. |
| 137 | |
| 138 | Args: |
| 139 | label: from update request |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 140 | board: from update request |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 141 | |
| 142 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 143 | The relative path to an update from the static_dir |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 144 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 145 | Raises: |
| 146 | AutoupdateError: If the update could not be found. |
| 147 | """ |
| 148 | path_to_payload = None |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 149 | # TODO(crbug.com/1006305): deprecate --payload flag |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 150 | 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 Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 156 | dest_meta = os.path.join(self.static_dir, label, |
| 157 | constants.UPDATE_METADATA_FILE) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 158 | |
| 159 | src_path = os.path.abspath(self.payload_path) |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 160 | src_meta = os.path.abspath(self.payload_path + '.json') |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 161 | 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 Deymo | 3e2d495 | 2013-09-03 21:49:41 -0700 | [diff] [blame] | 164 | common_util.SymlinkFile(src_path, dest_path) |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 165 | common_util.SymlinkFile(src_meta, dest_meta) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 166 | if os.path.exists(src_stateful): |
| 167 | # The stateful payload is optional. |
Alex Deymo | 3e2d495 | 2013-09-03 21:49:41 -0700 | [diff] [blame] | 168 | common_util.SymlinkFile(src_stateful, dest_stateful) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 169 | 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 Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 174 | path_to_payload = self.GetUpdateForLabel(label) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 175 | 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 Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 181 | path_to_payload = self.GetUpdateForLabel(label) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 182 | if path_to_payload is None: |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 183 | # There was no update found in the directory. Let XBuddy find the |
| 184 | # payloads. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 185 | if label_list[0] == 'xbuddy': |
| 186 | # If path explicitly calls xbuddy, pop off the tag. |
| 187 | label_list.pop() |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 188 | 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) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 191 | if path_to_payload is None: |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 192 | # No update payload found after translation. Try to get an update to |
| 193 | # a test image from GS using the label. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 194 | 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 Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 200 | |
| 201 | return path_to_payload |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 202 | |
Amin Hassani | 6eec879 | 2020-01-09 14:06:48 -0800 | [diff] [blame] | 203 | def HandleUpdatePing(self, data, label='', **kwargs): |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 204 | """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 Hassani | 6eec879 | 2020-01-09 14:06:48 -0800 | [diff] [blame] | 209 | kwargs: The map of query strings passed to the /update API. |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 210 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 211 | 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 Riley | ee75de2 | 2017-11-02 10:48:15 -0700 | [diff] [blame] | 216 | static_urlbase = self.GetStaticUrl() |
Amin Hassani | 86c6fb5 | 2020-02-28 11:03:52 -0800 | [diff] [blame] | 217 | # 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 Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 221 | |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 222 | # Process attributes of the update check. |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 223 | request = nebraska.Request(data) |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 224 | if request.request_type == nebraska.Request.RequestType.EVENT: |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 225 | _Log('A non-update event notification received. Returning an ack.') |
Amin Hassani | 083e3fe | 2020-02-13 11:39:18 -0800 | [diff] [blame] | 226 | return nebraska.Nebraska().GetResponseToRequest( |
| 227 | request, response_props=nebraska.ResponseProperties(**kwargs)) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 228 | |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 229 | _Log('Update Check Received.') |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 230 | |
| 231 | try: |
Amin Hassani | e7ead90 | 2019-10-11 16:42:43 -0700 | [diff] [blame] | 232 | path_to_payload = self.GetPathToPayload(label, request.board) |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 233 | base_url = _NonePathJoin(static_urlbase, path_to_payload) |
Amin Hassani | c9dd11e | 2019-07-11 15:33:55 -0700 | [diff] [blame] | 234 | local_payload_dir = _NonePathJoin(self.static_dir, path_to_payload) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 235 | except AutoupdateError as e: |
| 236 | # Raised if we fail to generate an update payload. |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 237 | _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 Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 239 | |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 240 | _Log('Responding to client to use url %s to get image', base_url) |
Amin Hassani | c91fc0d | 2019-12-04 11:07:16 -0800 | [diff] [blame] | 241 | nebraska_props = nebraska.NebraskaProperties( |
| 242 | update_payloads_address=base_url, |
| 243 | update_metadata_dir=local_payload_dir) |
Amin Hassani | c91fc0d | 2019-12-04 11:07:16 -0800 | [diff] [blame] | 244 | nebraska_obj = nebraska.Nebraska(nebraska_props=nebraska_props) |
Amin Hassani | 083e3fe | 2020-02-13 11:39:18 -0800 | [diff] [blame] | 245 | return nebraska_obj.GetResponseToRequest( |
| 246 | request, response_props=nebraska.ResponseProperties(**kwargs)) |