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