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 | 4539a6b | 2020-03-25 13:57:08 -0700 | [diff] [blame^] | 67 | proxy_port=None, critical_update=False): |
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. |
| 76 | critical_update: Whether provisioned payload is critical. |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 77 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 78 | self.xbuddy = xbuddy |
Amin Hassani | 4b5d5ab | 2020-03-22 19:57:46 -0700 | [diff] [blame] | 79 | self.static_dir = static_dir |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 80 | self.payload_path = payload_path |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 81 | self.proxy_port = proxy_port |
Satoru Takabayashi | d733cbe | 2011-11-15 09:36:32 -0800 | [diff] [blame] | 82 | self.critical_update = critical_update |
Gilad Arnold | d0c7175 | 2013-12-06 11:48:45 -0800 | [diff] [blame] | 83 | |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 84 | def GetUpdateForLabel(self, label): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 85 | """Given a label, get an update from the directory. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 86 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 87 | Args: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 88 | label: the relative directory inside the static dir |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 89 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 90 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 91 | A relative path to the directory with the update payload. |
| 92 | This is the label if an update did not need to be generated, but can |
| 93 | be label/cache/hashed_dir_for_update. |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 94 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 95 | Raises: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 96 | AutoupdateError: If client version is higher than available update found |
| 97 | at the directory given by the label. |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 98 | """ |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 99 | _Log('Update label: %s', label) |
| 100 | static_update_path = _NonePathJoin(self.static_dir, label, |
| 101 | constants.UPDATE_FILE) |
Don Garrett | ee25e55 | 2010-11-23 12:09:35 -0800 | [diff] [blame] | 102 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 103 | if label and os.path.exists(static_update_path): |
| 104 | # An update payload was found for the given label, return it. |
| 105 | return label |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 106 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 107 | # The label didn't resolve. |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 108 | _Log('Did not found any update payload for label %s.', label) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 109 | return None |
Chris Sosa | 2c048f1 | 2010-10-27 16:05:27 -0700 | [diff] [blame] | 110 | |
David Riley | ee75de2 | 2017-11-02 10:48:15 -0700 | [diff] [blame] | 111 | def GetDevserverUrl(self): |
| 112 | """Returns the devserver url base.""" |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 113 | x_forwarded_host = cherrypy.request.headers.get('X-Forwarded-Host') |
| 114 | if x_forwarded_host: |
| 115 | hostname = 'http://' + x_forwarded_host |
| 116 | else: |
| 117 | hostname = cherrypy.request.base |
| 118 | |
David Riley | ee75de2 | 2017-11-02 10:48:15 -0700 | [diff] [blame] | 119 | return hostname |
| 120 | |
| 121 | def GetStaticUrl(self): |
| 122 | """Returns the static url base that should prefix all payload responses.""" |
| 123 | hostname = self.GetDevserverUrl() |
| 124 | |
Amin Hassani | c9dd11e | 2019-07-11 15:33:55 -0700 | [diff] [blame] | 125 | static_urlbase = '%s/static' % hostname |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 126 | # If we have a proxy port, adjust the URL we instruct the client to |
| 127 | # use to go through the proxy. |
| 128 | if self.proxy_port: |
| 129 | static_urlbase = _ChangeUrlPort(static_urlbase, self.proxy_port) |
| 130 | |
| 131 | _Log('Using static url base %s', static_urlbase) |
| 132 | _Log('Handling update ping as %s', hostname) |
| 133 | return static_urlbase |
| 134 | |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 135 | def GetPathToPayload(self, label, board): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 136 | """Find a payload locally. |
| 137 | |
| 138 | See devserver's update rpc for documentation. |
| 139 | |
| 140 | Args: |
| 141 | label: from update request |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 142 | board: from update request |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 143 | |
| 144 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 145 | The relative path to an update from the static_dir |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 146 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 147 | Raises: |
| 148 | AutoupdateError: If the update could not be found. |
| 149 | """ |
| 150 | path_to_payload = None |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 151 | # TODO(crbug.com/1006305): deprecate --payload flag |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 152 | if self.payload_path: |
| 153 | # Copy the image from the path to '/forced_payload' |
| 154 | label = 'forced_payload' |
| 155 | dest_path = os.path.join(self.static_dir, label, constants.UPDATE_FILE) |
| 156 | dest_stateful = os.path.join(self.static_dir, label, |
| 157 | constants.STATEFUL_FILE) |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 158 | dest_meta = os.path.join(self.static_dir, label, |
| 159 | constants.UPDATE_METADATA_FILE) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 160 | |
| 161 | src_path = os.path.abspath(self.payload_path) |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 162 | src_meta = os.path.abspath(self.payload_path + '.json') |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 163 | src_stateful = os.path.join(os.path.dirname(src_path), |
| 164 | constants.STATEFUL_FILE) |
| 165 | common_util.MkDirP(os.path.join(self.static_dir, label)) |
Alex Deymo | 3e2d495 | 2013-09-03 21:49:41 -0700 | [diff] [blame] | 166 | common_util.SymlinkFile(src_path, dest_path) |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 167 | common_util.SymlinkFile(src_meta, dest_meta) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 168 | if os.path.exists(src_stateful): |
| 169 | # The stateful payload is optional. |
Alex Deymo | 3e2d495 | 2013-09-03 21:49:41 -0700 | [diff] [blame] | 170 | common_util.SymlinkFile(src_stateful, dest_stateful) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 171 | else: |
| 172 | _Log('WARN: %s not found. Expected for dev and test builds', |
| 173 | constants.STATEFUL_FILE) |
| 174 | if os.path.exists(dest_stateful): |
| 175 | os.remove(dest_stateful) |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 176 | path_to_payload = self.GetUpdateForLabel(label) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 177 | else: |
| 178 | label = label or '' |
| 179 | label_list = label.split('/') |
| 180 | # Suppose that the path follows old protocol of indexing straight |
| 181 | # into static_dir with board/version label. |
| 182 | # Attempt to get the update in that directory, generating if necc. |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 183 | path_to_payload = self.GetUpdateForLabel(label) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 184 | if path_to_payload is None: |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 185 | # There was no update found in the directory. Let XBuddy find the |
| 186 | # payloads. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 187 | if label_list[0] == 'xbuddy': |
| 188 | # If path explicitly calls xbuddy, pop off the tag. |
| 189 | label_list.pop() |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 190 | x_label, _ = self.xbuddy.Translate(label_list, board=board) |
| 191 | # Path has been resolved, try to get the payload. |
| 192 | path_to_payload = self.GetUpdateForLabel(x_label) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 193 | if path_to_payload is None: |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 194 | # No update payload found after translation. Try to get an update to |
| 195 | # a test image from GS using the label. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 196 | path_to_payload, _image_name = self.xbuddy.Get( |
| 197 | ['remote', label, 'full_payload']) |
| 198 | |
| 199 | # One of the above options should have gotten us a relative path. |
| 200 | if path_to_payload is None: |
| 201 | raise AutoupdateError('Failed to get an update for: %s' % label) |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 202 | |
| 203 | return path_to_payload |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 204 | |
Amin Hassani | 6eec879 | 2020-01-09 14:06:48 -0800 | [diff] [blame] | 205 | def HandleUpdatePing(self, data, label='', **kwargs): |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 206 | """Handles an update ping from an update client. |
| 207 | |
| 208 | Args: |
| 209 | data: XML blob from client. |
| 210 | label: optional label for the update. |
Amin Hassani | 6eec879 | 2020-01-09 14:06:48 -0800 | [diff] [blame] | 211 | kwargs: The map of query strings passed to the /update API. |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 212 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 213 | Returns: |
| 214 | Update payload message for client. |
| 215 | """ |
| 216 | # Get the static url base that will form that base of our update url e.g. |
| 217 | # http://hostname:8080/static/update.gz. |
David Riley | ee75de2 | 2017-11-02 10:48:15 -0700 | [diff] [blame] | 218 | static_urlbase = self.GetStaticUrl() |
Amin Hassani | 86c6fb5 | 2020-02-28 11:03:52 -0800 | [diff] [blame] | 219 | # Change the URL's string query dictionary provided by cherrypy to a valid |
| 220 | # dictionary that has proper values for its keys. e.g. True instead of |
| 221 | # 'True'. |
| 222 | kwargs = nebraska.QueryDictToDict(kwargs) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 223 | |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 224 | # Process attributes of the update check. |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 225 | request = nebraska.Request(data) |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 226 | if request.request_type == nebraska.Request.RequestType.EVENT: |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 227 | _Log('A non-update event notification received. Returning an ack.') |
Amin Hassani | 083e3fe | 2020-02-13 11:39:18 -0800 | [diff] [blame] | 228 | return nebraska.Nebraska().GetResponseToRequest( |
| 229 | request, response_props=nebraska.ResponseProperties(**kwargs)) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 230 | |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 231 | _Log('Update Check Received.') |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 232 | |
| 233 | try: |
Amin Hassani | e7ead90 | 2019-10-11 16:42:43 -0700 | [diff] [blame] | 234 | path_to_payload = self.GetPathToPayload(label, request.board) |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 235 | base_url = _NonePathJoin(static_urlbase, path_to_payload) |
Amin Hassani | c9dd11e | 2019-07-11 15:33:55 -0700 | [diff] [blame] | 236 | local_payload_dir = _NonePathJoin(self.static_dir, path_to_payload) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 237 | except AutoupdateError as e: |
| 238 | # Raised if we fail to generate an update payload. |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 239 | _Log('Failed to process an update request, but we will defer to ' |
| 240 | 'nebraska to respond with no-update. The error was %s', e) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 241 | |
Amin Hassani | 6eec879 | 2020-01-09 14:06:48 -0800 | [diff] [blame] | 242 | if self.critical_update: |
| 243 | kwargs['critical_update'] = True |
| 244 | |
Amin Hassani | 8d718d1 | 2019-06-02 21:28:39 -0700 | [diff] [blame] | 245 | _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] | 246 | nebraska_props = nebraska.NebraskaProperties( |
| 247 | update_payloads_address=base_url, |
| 248 | update_metadata_dir=local_payload_dir) |
Amin Hassani | c91fc0d | 2019-12-04 11:07:16 -0800 | [diff] [blame] | 249 | nebraska_obj = nebraska.Nebraska(nebraska_props=nebraska_props) |
Amin Hassani | 083e3fe | 2020-02-13 11:39:18 -0800 | [diff] [blame] | 250 | return nebraska_obj.GetResponseToRequest( |
| 251 | request, response_props=nebraska.ResponseProperties(**kwargs)) |