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