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 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 10 | import json |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 11 | import os |
Gilad Arnold | d0c7175 | 2013-12-06 19:48:45 | [diff] [blame] | 12 | import threading |
Darin Petkov | 2b2ff4b | 2010-07-27 22:02:09 | [diff] [blame] | 13 | import time |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 14 | |
Amin Hassani | 4f1e462 | 2019-10-03 17:40:50 | [diff] [blame] | 15 | from six.moves import urllib |
| 16 | |
| 17 | import cherrypy # pylint: disable=import-error |
Gilad Arnold | abb352e | 2012-09-23 08:24:27 | [diff] [blame] | 18 | |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 19 | # TODO(crbug.com/872441): We try to import nebraska from different places |
| 20 | # because when we install the devserver, we copy the nebraska.py into the main |
| 21 | # directory. Once this bug is resolved, we can always import from nebraska |
| 22 | # directory. |
| 23 | try: |
| 24 | from nebraska import nebraska |
| 25 | except ImportError: |
| 26 | import nebraska |
Chris Sosa | 05491b1 | 2010-11-09 01:14:16 | [diff] [blame] | 27 | |
Achuith Bhandarkar | 662fb72 | 2019-10-31 23:12:49 | [diff] [blame] | 28 | import setup_chromite # pylint: disable=unused-import |
| 29 | from chromite.lib.xbuddy import build_util |
| 30 | from chromite.lib.xbuddy import cherrypy_log_util |
| 31 | from chromite.lib.xbuddy import common_util |
| 32 | from chromite.lib.xbuddy import devserver_constants as constants |
| 33 | |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 34 | |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 35 | # Module-local log function. |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 36 | def _Log(message, *args): |
Achuith Bhandarkar | 662fb72 | 2019-10-31 23:12:49 | [diff] [blame] | 37 | return cherrypy_log_util.LogWithTag('UPDATE', message, *args) |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 38 | |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 39 | class AutoupdateError(Exception): |
| 40 | """Exception classes used by this module.""" |
| 41 | pass |
| 42 | |
| 43 | |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 44 | def _ChangeUrlPort(url, new_port): |
| 45 | """Return the URL passed in with a different port""" |
Amin Hassani | 4f1e462 | 2019-10-03 17:40:50 | [diff] [blame] | 46 | scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url) |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 47 | host_port = netloc.split(':') |
| 48 | |
| 49 | if len(host_port) == 1: |
| 50 | host_port.append(new_port) |
| 51 | else: |
| 52 | host_port[1] = new_port |
| 53 | |
Don Garrett | fb15e32 | 2016-06-22 02:12:08 | [diff] [blame] | 54 | print(host_port) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 55 | netloc = '%s:%s' % tuple(host_port) |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 56 | |
Amin Hassani | 4f1e462 | 2019-10-03 17:40:50 | [diff] [blame] | 57 | # pylint: disable=too-many-function-args |
| 58 | return urllib.parse.urlunsplit((scheme, netloc, path, query, fragment)) |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 59 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 60 | def _NonePathJoin(*args): |
| 61 | """os.path.join that filters None's from the argument list.""" |
Amin Hassani | 4f1e462 | 2019-10-03 17:40:50 | [diff] [blame] | 62 | 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] | 63 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 64 | |
| 65 | class HostInfo(object): |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 66 | """Records information about an individual host. |
| 67 | |
Amin Hassani | e7ead90 | 2019-10-11 23:42:43 | [diff] [blame] | 68 | Attributes: |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 69 | attrs: Static attributes (legacy) |
| 70 | log: Complete log of recorded client entries |
| 71 | """ |
| 72 | |
| 73 | def __init__(self): |
| 74 | # A dictionary of current attributes pertaining to the host. |
| 75 | self.attrs = {} |
| 76 | |
| 77 | # A list of pairs consisting of a timestamp and a dictionary of recorded |
| 78 | # attributes. |
| 79 | self.log = [] |
| 80 | |
| 81 | def __repr__(self): |
| 82 | return 'attrs=%s, log=%s' % (self.attrs, self.log) |
| 83 | |
| 84 | def AddLogEntry(self, entry): |
| 85 | """Append a new log entry.""" |
| 86 | # Append a timestamp. |
| 87 | assert not 'timestamp' in entry, 'Oops, timestamp field already in use' |
| 88 | entry['timestamp'] = time.strftime('%Y-%m-%d %H:%M:%S') |
| 89 | # Add entry to hosts' message log. |
| 90 | self.log.append(entry) |
| 91 | |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 92 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 93 | class HostInfoTable(object): |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 94 | """Records information about a set of hosts who engage in update activity. |
| 95 | |
Amin Hassani | e7ead90 | 2019-10-11 23:42:43 | [diff] [blame] | 96 | Attributes: |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 97 | table: Table of information on hosts. |
| 98 | """ |
| 99 | |
| 100 | def __init__(self): |
| 101 | # A dictionary of host information. Keys are normally IP addresses. |
| 102 | self.table = {} |
| 103 | |
| 104 | def __repr__(self): |
| 105 | return '%s' % self.table |
| 106 | |
| 107 | def GetInitHostInfo(self, host_id): |
| 108 | """Return a host's info object, or create a new one if none exists.""" |
| 109 | return self.table.setdefault(host_id, HostInfo()) |
| 110 | |
| 111 | def GetHostInfo(self, host_id): |
| 112 | """Return an info object for given host, if such exists.""" |
Chris Sosa | 1885d03 | 2012-11-30 01:07:27 | [diff] [blame] | 113 | return self.table.get(host_id) |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 114 | |
| 115 | |
joychen | 921e1fb | 2013-06-28 18:12:20 | [diff] [blame] | 116 | class Autoupdate(build_util.BuildObject): |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 117 | """Class that contains functionality that handles Chrome OS update pings.""" |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 118 | |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 119 | _PAYLOAD_URL_PREFIX = '/static/' |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 120 | |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 121 | def __init__(self, xbuddy, payload_path=None, proxy_port=None, |
Amin Hassani | c9dd11e | 2019-07-11 22:33:55 | [diff] [blame] | 122 | critical_update=False, max_updates=-1, host_log=False, |
| 123 | *args, **kwargs): |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 124 | """Initializes the class. |
| 125 | |
| 126 | Args: |
| 127 | xbuddy: The xbuddy path. |
| 128 | payload_path: The path to pre-generated payload to serve. |
| 129 | proxy_port: The port of local proxy to tell client to connect to you |
| 130 | through. |
| 131 | critical_update: Whether provisioned payload is critical. |
| 132 | max_updates: The maximum number of updates we'll try to provision. |
| 133 | host_log: Record full history of host update events. |
| 134 | """ |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 135 | super(Autoupdate, self).__init__(*args, **kwargs) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 136 | self.xbuddy = xbuddy |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 137 | self.payload_path = payload_path |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 138 | self.proxy_port = proxy_port |
Satoru Takabayashi | d733cbe | 2011-11-15 17:36:32 | [diff] [blame] | 139 | self.critical_update = critical_update |
Jay Srinivasan | ac69d26 | 2012-10-31 02:05:53 | [diff] [blame] | 140 | self.max_updates = max_updates |
Gilad Arnold | 8318eac | 2012-10-04 19:52:23 | [diff] [blame] | 141 | self.host_log = host_log |
Don Garrett | fff4c32 | 2010-11-19 21:37:12 | [diff] [blame] | 142 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 143 | # Initialize empty host info cache. Used to keep track of various bits of |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 144 | # information about a given host. A host is identified by its IP address. |
| 145 | # The info stored for each host includes a complete log of events for this |
| 146 | # host, as well as a dictionary of current attributes derived from events. |
| 147 | self.host_infos = HostInfoTable() |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 148 | |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 149 | self._update_count_lock = threading.Lock() |
Gilad Arnold | d0c7175 | 2013-12-06 19:48:45 | [diff] [blame] | 150 | |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 151 | def GetUpdateForLabel(self, label): |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 152 | """Given a label, get an update from the directory. |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 153 | |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 154 | Args: |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 155 | label: the relative directory inside the static dir |
Gilad Arnold | d8d595c | 2014-03-21 20:00:41 | [diff] [blame] | 156 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 157 | Returns: |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 158 | A relative path to the directory with the update payload. |
| 159 | This is the label if an update did not need to be generated, but can |
| 160 | be label/cache/hashed_dir_for_update. |
Gilad Arnold | d8d595c | 2014-03-21 20:00:41 | [diff] [blame] | 161 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 162 | Raises: |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 163 | AutoupdateError: If client version is higher than available update found |
| 164 | at the directory given by the label. |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 165 | """ |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 166 | _Log('Update label: %s', label) |
| 167 | static_update_path = _NonePathJoin(self.static_dir, label, |
| 168 | constants.UPDATE_FILE) |
Don Garrett | ee25e55 | 2010-11-23 20:09:35 | [diff] [blame] | 169 | |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 170 | if label and os.path.exists(static_update_path): |
| 171 | # An update payload was found for the given label, return it. |
| 172 | return label |
Don Garrett | 0c880e2 | 2010-11-18 02:13:37 | [diff] [blame] | 173 | |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 174 | # The label didn't resolve. |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 175 | _Log('Did not found any update payload for label %s.', label) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 176 | return None |
Chris Sosa | 2c048f1 | 2010-10-27 23:05:27 | [diff] [blame] | 177 | |
Amin Hassani | e7ead90 | 2019-10-11 23:42:43 | [diff] [blame] | 178 | def _LogRequest(self, request): |
| 179 | """Logs the incoming request in the hostlog. |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 180 | |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 181 | Args: |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 182 | request: A nebraska.Request object representing the update request. |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 183 | |
| 184 | Returns: |
| 185 | A named tuple containing attributes of the update requests as the |
Amin Hassani | 542b549 | 2019-09-26 21:53:26 | [diff] [blame] | 186 | following fields: 'board', 'event_result' and 'event_type'. |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 187 | """ |
Amin Hassani | e7ead90 | 2019-10-11 23:42:43 | [diff] [blame] | 188 | if not self.host_log: |
| 189 | return |
| 190 | |
| 191 | # Add attributes to log message. Some of these values might be None. |
| 192 | log_message = { |
| 193 | 'version': request.version, |
| 194 | 'track': request.track, |
| 195 | 'board': request.board or self.GetDefaultBoardID(), |
| 196 | 'event_result': request.app_requests[0].event_result, |
| 197 | 'event_type': request.app_requests[0].event_type, |
| 198 | 'previous_version': request.app_requests[0].previous_version, |
| 199 | } |
| 200 | if log_message['previous_version'] is None: |
| 201 | del log_message['previous_version'] |
Jay Srinivasan | ac69d26 | 2012-10-31 02:05:53 | [diff] [blame] | 202 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 203 | # Determine request IP, strip any IPv6 data for simplicity. |
| 204 | client_ip = cherrypy.request.remote.ip.split(':')[-1] |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 205 | # Obtain (or init) info object for this client. |
| 206 | curr_host_info = self.host_infos.GetInitHostInfo(client_ip) |
Amin Hassani | e7ead90 | 2019-10-11 23:42:43 | [diff] [blame] | 207 | curr_host_info.AddLogEntry(log_message) |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 208 | |
David Riley | ee75de2 | 2017-11-02 17:48:15 | [diff] [blame] | 209 | def GetDevserverUrl(self): |
| 210 | """Returns the devserver url base.""" |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 211 | x_forwarded_host = cherrypy.request.headers.get('X-Forwarded-Host') |
| 212 | if x_forwarded_host: |
| 213 | hostname = 'http://' + x_forwarded_host |
| 214 | else: |
| 215 | hostname = cherrypy.request.base |
| 216 | |
David Riley | ee75de2 | 2017-11-02 17:48:15 | [diff] [blame] | 217 | return hostname |
| 218 | |
| 219 | def GetStaticUrl(self): |
| 220 | """Returns the static url base that should prefix all payload responses.""" |
| 221 | hostname = self.GetDevserverUrl() |
| 222 | |
Amin Hassani | c9dd11e | 2019-07-11 22:33:55 | [diff] [blame] | 223 | static_urlbase = '%s/static' % hostname |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 224 | # If we have a proxy port, adjust the URL we instruct the client to |
| 225 | # use to go through the proxy. |
| 226 | if self.proxy_port: |
| 227 | static_urlbase = _ChangeUrlPort(static_urlbase, self.proxy_port) |
| 228 | |
| 229 | _Log('Using static url base %s', static_urlbase) |
| 230 | _Log('Handling update ping as %s', hostname) |
| 231 | return static_urlbase |
| 232 | |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 233 | def GetPathToPayload(self, label, board): |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 234 | """Find a payload locally. |
| 235 | |
| 236 | See devserver's update rpc for documentation. |
| 237 | |
| 238 | Args: |
| 239 | label: from update request |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 240 | board: from update request |
Gilad Arnold | d8d595c | 2014-03-21 20:00:41 | [diff] [blame] | 241 | |
| 242 | Returns: |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 243 | The relative path to an update from the static_dir |
Gilad Arnold | d8d595c | 2014-03-21 20:00:41 | [diff] [blame] | 244 | |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 245 | Raises: |
| 246 | AutoupdateError: If the update could not be found. |
| 247 | """ |
| 248 | path_to_payload = None |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 249 | # TODO(crbug.com/1006305): deprecate --payload flag |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 250 | if self.payload_path: |
| 251 | # Copy the image from the path to '/forced_payload' |
| 252 | label = 'forced_payload' |
| 253 | dest_path = os.path.join(self.static_dir, label, constants.UPDATE_FILE) |
| 254 | dest_stateful = os.path.join(self.static_dir, label, |
| 255 | constants.STATEFUL_FILE) |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 256 | dest_meta = os.path.join(self.static_dir, label, |
| 257 | constants.UPDATE_METADATA_FILE) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 258 | |
| 259 | src_path = os.path.abspath(self.payload_path) |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 260 | src_meta = os.path.abspath(self.payload_path + '.json') |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 261 | src_stateful = os.path.join(os.path.dirname(src_path), |
| 262 | constants.STATEFUL_FILE) |
| 263 | common_util.MkDirP(os.path.join(self.static_dir, label)) |
Alex Deymo | 3e2d495 | 2013-09-04 04:49:41 | [diff] [blame] | 264 | common_util.SymlinkFile(src_path, dest_path) |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 265 | common_util.SymlinkFile(src_meta, dest_meta) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 266 | if os.path.exists(src_stateful): |
| 267 | # The stateful payload is optional. |
Alex Deymo | 3e2d495 | 2013-09-04 04:49:41 | [diff] [blame] | 268 | common_util.SymlinkFile(src_stateful, dest_stateful) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 269 | else: |
| 270 | _Log('WARN: %s not found. Expected for dev and test builds', |
| 271 | constants.STATEFUL_FILE) |
| 272 | if os.path.exists(dest_stateful): |
| 273 | os.remove(dest_stateful) |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 274 | path_to_payload = self.GetUpdateForLabel(label) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 275 | else: |
| 276 | label = label or '' |
| 277 | label_list = label.split('/') |
| 278 | # Suppose that the path follows old protocol of indexing straight |
| 279 | # into static_dir with board/version label. |
| 280 | # Attempt to get the update in that directory, generating if necc. |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 281 | path_to_payload = self.GetUpdateForLabel(label) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 282 | if path_to_payload is None: |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 283 | # There was no update found in the directory. Let XBuddy find the |
| 284 | # payloads. |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 285 | if label_list[0] == 'xbuddy': |
| 286 | # If path explicitly calls xbuddy, pop off the tag. |
| 287 | label_list.pop() |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 288 | x_label, _ = self.xbuddy.Translate(label_list, board=board) |
| 289 | # Path has been resolved, try to get the payload. |
| 290 | path_to_payload = self.GetUpdateForLabel(x_label) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 291 | if path_to_payload is None: |
Amin Hassani | e9ffb86 | 2019-09-26 00:10:40 | [diff] [blame] | 292 | # No update payload found after translation. Try to get an update to |
| 293 | # a test image from GS using the label. |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 294 | path_to_payload, _image_name = self.xbuddy.Get( |
| 295 | ['remote', label, 'full_payload']) |
| 296 | |
| 297 | # One of the above options should have gotten us a relative path. |
| 298 | if path_to_payload is None: |
| 299 | raise AutoupdateError('Failed to get an update for: %s' % label) |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 300 | |
| 301 | return path_to_payload |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 302 | |
Amin Hassani | 6eec879 | 2020-01-09 22:06:48 | [diff] [blame] | 303 | def HandleUpdatePing(self, data, label='', **kwargs): |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 304 | """Handles an update ping from an update client. |
| 305 | |
| 306 | Args: |
| 307 | data: XML blob from client. |
| 308 | label: optional label for the update. |
Amin Hassani | 6eec879 | 2020-01-09 22:06:48 | [diff] [blame] | 309 | kwargs: The map of query strings passed to the /update API. |
Gilad Arnold | d8d595c | 2014-03-21 20:00:41 | [diff] [blame] | 310 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 311 | Returns: |
| 312 | Update payload message for client. |
| 313 | """ |
| 314 | # Get the static url base that will form that base of our update url e.g. |
| 315 | # http://hostname:8080/static/update.gz. |
David Riley | ee75de2 | 2017-11-02 17:48:15 | [diff] [blame] | 316 | static_urlbase = self.GetStaticUrl() |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 317 | |
Chris Sosa | b26b120 | 2013-08-16 23:40:55 | [diff] [blame] | 318 | # Process attributes of the update check. |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 319 | request = nebraska.Request(data) |
Amin Hassani | e7ead90 | 2019-10-11 23:42:43 | [diff] [blame] | 320 | self._LogRequest(request) |
Chris Sosa | b26b120 | 2013-08-16 23:40:55 | [diff] [blame] | 321 | |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 322 | if request.request_type == nebraska.Request.RequestType.EVENT: |
Amin Hassani | a50fa63 | 2019-10-16 03:49:51 | [diff] [blame] | 323 | if (request.app_requests[0].event_type == |
| 324 | nebraska.Request.EVENT_TYPE_UPDATE_DOWNLOAD_STARTED and |
| 325 | request.app_requests[0].event_result == |
| 326 | nebraska.Request.EVENT_RESULT_SUCCESS): |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 327 | with self._update_count_lock: |
| 328 | if self.max_updates == 0: |
Amin Hassani | 6aa075c | 2020-02-21 18:36:44 | [diff] [blame^] | 329 | _Log('Received too many download_started notifications. This ' |
| 330 | 'probably means a bug in the test environment, such as too ' |
| 331 | 'many clients running concurrently. Alternatively, it could ' |
| 332 | 'be a bug in the update client.') |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 333 | elif self.max_updates > 0: |
| 334 | self.max_updates -= 1 |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 335 | |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 336 | _Log('A non-update event notification received. Returning an ack.') |
Amin Hassani | 083e3fe | 2020-02-13 19:39:18 | [diff] [blame] | 337 | return nebraska.Nebraska().GetResponseToRequest( |
| 338 | request, response_props=nebraska.ResponseProperties(**kwargs)) |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 339 | |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 340 | # Make sure that we did not already exceed the max number of allowed update |
| 341 | # responses. Note that the counter is only decremented when the client |
| 342 | # reports an actual download, to avoid race conditions between concurrent |
| 343 | # update requests from the same client due to a timeout. |
Amin Hassani | 6aa075c | 2020-02-21 18:36:44 | [diff] [blame^] | 344 | if self.max_updates == 0: |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 345 | _Log('Request received but max number of updates already served.') |
Amin Hassani | 083e3fe | 2020-02-13 19:39:18 | [diff] [blame] | 346 | kwargs['no_update'] = True |
| 347 | # Override the noupdate to make sure the response is noupdate. |
| 348 | return nebraska.Nebraska().GetResponseToRequest( |
| 349 | request, response_props=nebraska.ResponseProperties(**kwargs)) |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 350 | |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 351 | _Log('Update Check Received.') |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 352 | |
| 353 | try: |
Amin Hassani | e7ead90 | 2019-10-11 23:42:43 | [diff] [blame] | 354 | path_to_payload = self.GetPathToPayload(label, request.board) |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 355 | base_url = _NonePathJoin(static_urlbase, path_to_payload) |
Amin Hassani | c9dd11e | 2019-07-11 22:33:55 | [diff] [blame] | 356 | local_payload_dir = _NonePathJoin(self.static_dir, path_to_payload) |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 357 | except AutoupdateError as e: |
| 358 | # Raised if we fail to generate an update payload. |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 359 | _Log('Failed to process an update request, but we will defer to ' |
| 360 | 'nebraska to respond with no-update. The error was %s', e) |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 361 | |
Amin Hassani | 6eec879 | 2020-01-09 22:06:48 | [diff] [blame] | 362 | if self.critical_update: |
| 363 | kwargs['critical_update'] = True |
| 364 | |
Amin Hassani | 8d718d1 | 2019-06-03 04:28:39 | [diff] [blame] | 365 | _Log('Responding to client to use url %s to get image', base_url) |
Amin Hassani | c91fc0d | 2019-12-04 19:07:16 | [diff] [blame] | 366 | nebraska_props = nebraska.NebraskaProperties( |
| 367 | update_payloads_address=base_url, |
| 368 | update_metadata_dir=local_payload_dir) |
Amin Hassani | c91fc0d | 2019-12-04 19:07:16 | [diff] [blame] | 369 | nebraska_obj = nebraska.Nebraska(nebraska_props=nebraska_props) |
Amin Hassani | 083e3fe | 2020-02-13 19:39:18 | [diff] [blame] | 370 | return nebraska_obj.GetResponseToRequest( |
| 371 | request, response_props=nebraska.ResponseProperties(**kwargs)) |
Gilad Arnold | d0c7175 | 2013-12-06 19:48:45 | [diff] [blame] | 372 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 373 | def HandleHostInfoPing(self, ip): |
| 374 | """Returns host info dictionary for the given IP in JSON format.""" |
| 375 | assert ip, 'No ip provided.' |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 376 | if ip in self.host_infos.table: |
| 377 | return json.dumps(self.host_infos.GetHostInfo(ip).attrs) |
| 378 | |
| 379 | def HandleHostLogPing(self, ip): |
| 380 | """Returns a complete log of events for host in JSON format.""" |
Gilad Arnold | 4ba437d | 2012-10-05 22:28:27 | [diff] [blame] | 381 | # If all events requested, return a dictionary of logs keyed by IP address. |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 382 | if ip == 'all': |
| 383 | return json.dumps( |
| 384 | dict([(key, self.host_infos.table[key].log) |
| 385 | for key in self.host_infos.table])) |
Gilad Arnold | 4ba437d | 2012-10-05 22:28:27 | [diff] [blame] | 386 | |
| 387 | # Otherwise we're looking for a specific IP address, so find its log. |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 388 | if ip in self.host_infos.table: |
| 389 | return json.dumps(self.host_infos.GetHostInfo(ip).log) |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 390 | |
Gilad Arnold | 4ba437d | 2012-10-05 22:28:27 | [diff] [blame] | 391 | # If no events were logged for this IP, return an empty log. |
| 392 | return json.dumps([]) |