blob: 131d6e5f92d55b7184c726e99b648f478259280d [file] [log] [blame]
Derek Beckettf73baca2020-08-19 15:08:47 -07001# Lint as: python2, python3
Fang Deng5d518f42013-08-02 14:04:32 -07002# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5#
6# Expects to be run in an environment with sudo and no interactive password
7# prompt, such as within the Chromium OS development chroot.
8
9
10"""This file provides core logic for servo verify/repair process."""
11
12
Derek Beckettf73baca2020-08-19 15:08:47 -070013from __future__ import absolute_import
14from __future__ import division
15from __future__ import print_function
16
Fang Deng5d518f42013-08-02 14:04:32 -070017import logging
Raul E Rangel52ca2e82018-07-03 14:10:14 -060018import os
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -080019import re
20import tarfile
Andrew McRaef0679932020-08-13 09:15:23 +100021import threading
Garry Wang2b5eef92020-08-21 16:23:35 -070022import json
Garry Wangc1288cf2019-12-17 14:58:00 -080023import time
Derek Beckettf73baca2020-08-19 15:08:47 -070024import six
25import six.moves.xmlrpc_client
Fang Deng5d518f42013-08-02 14:04:32 -070026
27from autotest_lib.client.bin import utils
Garry Wang79e9af62019-06-12 15:19:19 -070028from autotest_lib.client.common_lib import error
Richard Barnette9a26ad62016-06-10 12:03:08 -070029from autotest_lib.client.common_lib import hosts
Garry Wang7b0e1b72020-03-25 19:08:59 -070030from autotest_lib.client.common_lib import lsbrelease_utils
Fang Deng5d518f42013-08-02 14:04:32 -070031from autotest_lib.client.common_lib.cros import retry
Garry Wang970fda92021-06-22 18:03:43 -070032from autotest_lib.server import crashcollect
Richard Barnette9a26ad62016-06-10 12:03:08 -070033from autotest_lib.server.cros.servo import servo
Richard Barnetted31580e2018-05-14 19:58:00 +000034from autotest_lib.server.hosts import servo_repair
Garry Wangebc015b2019-06-06 17:45:06 -070035from autotest_lib.server.hosts import base_servohost
Garry Wang11b5e872020-03-11 15:14:08 -070036from autotest_lib.server.hosts import servo_constants
Otabek Kasimov4ea636e2020-04-14 23:35:06 -070037from autotest_lib.server.cros.faft.utils import config
Garry Wang11b5e872020-03-11 15:14:08 -070038from autotest_lib.client.common_lib import global_config
Otabek Kasimov382c3bb2020-10-28 13:22:45 -070039from autotest_lib.server.cros.servo.topology import servo_topology
Garry Wangd7367482020-02-27 13:52:40 -080040
Otabek Kasimov15963492020-06-23 21:10:51 -070041try:
Mike Frysinger714c5b02020-09-04 23:22:54 -040042 from autotest_lib.utils.frozen_chromite.lib import metrics
Otabek Kasimov15963492020-06-23 21:10:51 -070043except ImportError:
44 metrics = utils.metrics_mock
45
Dan Shi3b2adf62015-09-02 17:46:54 -070046_CONFIG = global_config.global_config
Fang Deng5d518f42013-08-02 14:04:32 -070047
Otabek Kasimova7ba91a2020-03-09 08:31:01 -070048
Garry Wangebc015b2019-06-06 17:45:06 -070049class ServoHost(base_servohost.BaseServoHost):
50 """Host class for a servo host(e.g. beaglebone, labstation)
Dana Goyette0b6e6402019-10-04 11:09:24 -070051 that with a servo instance for a specific port.
52
53 @type _servo: servo.Servo | None
54 """
Fang Deng5d518f42013-08-02 14:04:32 -070055
Raul E Rangel52ca2e82018-07-03 14:10:14 -060056 DEFAULT_PORT = int(os.getenv('SERVOD_PORT', '9999'))
Richard Barnette9a26ad62016-06-10 12:03:08 -070057
Dan Shie5b3c512014-08-21 12:12:09 -070058 # Timeout for initializing servo signals.
Wai-Hong Tam37b6ed32017-09-19 15:52:39 -070059 INITIALIZE_SERVO_TIMEOUT_SECS = 60
Richard Barnette9a26ad62016-06-10 12:03:08 -070060
Otabek Kasimov545739c2020-08-20 00:24:21 -070061 # Default timeout for run terminal command.
62 DEFAULT_TERMINAL_TIMEOUT = 30
63
xixuan6cf6d2f2016-01-29 15:29:00 -080064 # Ready test function
65 SERVO_READY_METHOD = 'get_version'
Fang Deng5d518f42013-08-02 14:04:32 -070066
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -080067 # Directory prefix on the servo host where the servod logs are stored.
68 SERVOD_LOG_PREFIX = '/var/log/servod'
69
70 # Exit code to use when symlinks for servod logs are not found.
71 NO_SYMLINKS_CODE = 9
72
73 # Directory in the job's results directory to dump the logs into.
74 LOG_DIR = 'servod'
75
76 # Prefix for joint loglevel files in the logs.
77 JOINT_LOG_PREFIX = 'log'
78
79 # Regex group to extract timestamp from logfile name.
80 TS_GROUP = 'ts'
81
82 # This regex is used to extract the timestamp from servod logs.
Garry Wang22f2e842020-09-09 20:19:19 -070083 # files always start with log.
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -080084 TS_RE = (r'log.'
85 # The timestamp is of format %Y-%m-%d--%H-%M-%S.MS
86 r'(?P<%s>\d{4}(\-\d{2}){2}\-(-\d{2}){3}.\d{3})'
87 # The loglevel is optional depending on labstation version.
88 r'(.(INFO|DEBUG|WARNING))?' % TS_GROUP)
89 TS_EXTRACTOR = re.compile(TS_RE)
90
91 # Regex group to extract MCU name from logline in servod logs.
92 MCU_GROUP = 'mcu'
93
94 # Regex group to extract logline from MCU logline in servod logs.
95 LINE_GROUP = 'line'
96
97 # This regex is used to extract the mcu and the line content from an
98 # MCU logline in servod logs. e.g. EC or servo_v4 console logs.
99 # Here is an example log-line:
100 #
101 # 2020-01-23 13:15:12,223 - servo_v4 - EC3PO.Console - DEBUG -
102 # console.py:219:LogConsoleOutput - /dev/pts/9 - cc polarity: cc1
103 #
104 # Here is conceptually how they are formatted:
105 #
106 # <time> - <MCU> - EC3PO.Console - <LVL> - <file:line:func> - <pts> -
107 # <output>
108 #
Garry Wang22f2e842020-09-09 20:19:19 -0700109 # The log format starts with a timestamp
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800110 MCU_RE = (r'[\d\-]+ [\d:,]+ '
111 # The mcu that is logging this is next.
112 r'- (?P<%s>\w+) - '
113 # Next, we have more log outputs before the actual line.
114 # Information about the file line, logging function etc.
115 # Anchor on EC3PO Console, LogConsoleOutput and dev/pts.
116 # NOTE: if the log format changes, this regex needs to be
117 # adjusted.
118 r'EC3PO\.Console[\s\-\w\d:.]+LogConsoleOutput - /dev/pts/\d+ - '
119 # Lastly, we get the MCU's console line.
120 r'(?P<%s>.+$)' % (MCU_GROUP, LINE_GROUP))
121 MCU_EXTRACTOR = re.compile(MCU_RE)
122
Otabek Kasimov545739c2020-08-20 00:24:21 -0700123 # Regex to detect timeout messages when USBC pigtail has timeout issue.
124 # e.g.: [475635.427072 PD TMOUT RX 1/1]
125 USBC_PIGTAIL_TIMEOUT_RE = r'\[[\d \.]{1,20}(PD TMOUT RX 1\/1)\]'
126
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800127 # Suffix to identify compressed logfiles.
128 COMPRESSION_SUFFIX = '.tbz2'
129
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700130 # A suffix to mark servod log directories that came from instance that
131 # ran during this servo_host, but are not the last one running e.g. when
132 # an instance (on purpose, or due to a bug) restarted in the middle of the
133 # run.
134 OLD_LOG_SUFFIX = 'old'
135
Otabek Kasimovcc9738e2020-02-14 16:17:15 -0800136 def _init_attributes(self):
137 self._servo_state = None
138 self.servo_port = None
139 self.servo_board = None
140 self.servo_model = None
141 self.servo_serial = None
Garry Wangcb06f3b2020-10-08 20:56:21 -0700142 self.servo_setup = None
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800143 self.servo_recovery = None
Otabek Kasimovab4ae222021-03-06 01:38:47 -0800144 self.servo_fw_channel = None
Garry Wang6a680062020-11-03 13:40:29 -0800145 self.additional_servod_args = None
Otabek Kasimov39637412020-11-23 19:09:27 -0800146 self._dut_health_profile = None
Garry Wang000c6c02020-05-11 21:27:23 -0700147 # The flag that indicate if a servo is connected to a smart usbhub.
148 # TODO(xianuowang@) remove this flag once all usbhubs in the lab
149 # get replaced.
150 self.smart_usbhub = None
Otabek Kasimovcc9738e2020-02-14 16:17:15 -0800151 self._servo = None
Otabek Kasimov382c3bb2020-10-28 13:22:45 -0700152 self._topology = None
Andrew McRaef0679932020-08-13 09:15:23 +1000153 self._tunnel_proxy = None
154 self._tunnel_proxy_lock = threading.Lock()
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700155 self._initial_instance_ts = None
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800156 # Flag to make sure that multiple calls to close do not result in the
157 # logic executing multiple times.
158 self._closed = False
Andrew McRaef0679932020-08-13 09:15:23 +1000159 # Per-thread local data
160 self._local = threading.local()
Fang Deng5d518f42013-08-02 14:04:32 -0700161
Garry Wangcb06f3b2020-10-08 20:56:21 -0700162 def _initialize(self,
163 servo_host='localhost',
164 servo_port=DEFAULT_PORT,
165 servo_board=None,
166 servo_model=None,
167 servo_serial=None,
168 servo_setup=None,
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800169 servo_recovery=None,
Otabek Kasimovab4ae222021-03-06 01:38:47 -0800170 servo_fw_channel=None,
Garry Wang6a680062020-11-03 13:40:29 -0800171 additional_servod_args=None,
Garry Wangcb06f3b2020-10-08 20:56:21 -0700172 is_in_lab=None,
173 *args,
174 **dargs):
Fang Deng5d518f42013-08-02 14:04:32 -0700175 """Initialize a ServoHost instance.
176
177 A ServoHost instance represents a host that controls a servo.
178
179 @param servo_host: Name of the host where the servod process
180 is running.
Raul E Rangel52ca2e82018-07-03 14:10:14 -0600181 @param servo_port: Port the servod process is listening on. Defaults
182 to the SERVOD_PORT environment variable if set,
183 otherwise 9999.
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -0700184 @param servo_board: Board that the servo is connected to.
Nick Sanders2f3c9852018-10-24 12:10:24 -0700185 @param servo_model: Model that the servo is connected to.
Garry Wangcb06f3b2020-10-08 20:56:21 -0700186 @param servo_serial: Serial number of the servo device.
187 @param servo_setup: Type of servo setup, e.g. REGULAR or DUAL_V4.
Garry Wang6a680062020-11-03 13:40:29 -0800188 @param additional_servod_args: Additional args that will append to
189 servod start command.
Dan Shi4d478522014-02-14 13:46:32 -0800190 @param is_in_lab: True if the servo host is in Cros Lab. Default is set
191 to None, for which utils.host_is_in_lab_zone will be
192 called to check if the servo host is in Cros lab.
Fang Deng5d518f42013-08-02 14:04:32 -0700193
194 """
195 super(ServoHost, self)._initialize(hostname=servo_host,
Garry Wangebc015b2019-06-06 17:45:06 -0700196 is_in_lab=is_in_lab, *args, **dargs)
Otabek Kasimovcc9738e2020-02-14 16:17:15 -0800197 self._init_attributes()
Richard Barnette42f4db92018-08-23 15:05:15 -0700198 self.servo_port = int(servo_port)
Richard Barnettee519dcd2016-08-15 17:37:17 -0700199 self.servo_board = servo_board
Nick Sanders2f3c9852018-10-24 12:10:24 -0700200 self.servo_model = servo_model
Kevin Cheng643ce8a2016-09-15 15:42:12 -0700201 self.servo_serial = servo_serial
Garry Wangcb06f3b2020-10-08 20:56:21 -0700202 self.servo_setup = servo_setup
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800203 self.servo_recovery = servo_recovery
Otabek Kasimovab4ae222021-03-06 01:38:47 -0800204 self.servo_fw_channel = servo_fw_channel
Garry Wang6a680062020-11-03 13:40:29 -0800205 self.additional_servod_args = additional_servod_args
Wai-Hong Tam3a8a2552019-11-19 14:28:04 +0800206
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800207 # The location of the log files on the servo host for this instance.
208 self.remote_log_dir = '%s_%s' % (self.SERVOD_LOG_PREFIX,
209 self.servo_port)
Garry Wang79e9af62019-06-12 15:19:19 -0700210 # Path of the servo host lock file.
Derek Beckettf73baca2020-08-19 15:08:47 -0700211 self._lock_file = (self.TEMP_FILE_DIR + str(self.servo_port) +
212 self.LOCK_FILE_POSTFIX)
Garry Wang79e9af62019-06-12 15:19:19 -0700213 # File path to declare a reboot request.
Derek Beckettf73baca2020-08-19 15:08:47 -0700214 self._reboot_file = (self.TEMP_FILE_DIR + str(self.servo_port) +
215 self.REBOOT_FILE_POSTFIX)
Garry Wang79e9af62019-06-12 15:19:19 -0700216
217 # Lock the servo host if it's an in-lab labstation to prevent other
218 # task to reboot it until current task completes. We also wait and
219 # make sure the labstation is up here, in the case of the labstation is
220 # in the middle of reboot.
Garry Wang7c00b0f2019-06-25 17:28:17 -0700221 self._is_locked = False
Garry Wang42b4d862019-06-25 15:50:49 -0700222 if (self.wait_up(self.REBOOT_TIMEOUT) and self.is_in_lab()
223 and self.is_labstation()):
Garry Wang79e9af62019-06-12 15:19:19 -0700224 self._lock()
Garry Wang78ce64d2020-10-13 18:23:45 -0700225 try:
226 self.wait_ready()
227 except Exception as e:
228 logging.info(
229 'Unexpected error while ensure labstation'
230 ' readiness; %s', str(e))
Garry Wangebc015b2019-06-06 17:45:06 -0700231
Richard Barnette9a26ad62016-06-10 12:03:08 -0700232 self._repair_strategy = (
233 servo_repair.create_servo_repair_strategy())
Richard Barnettee519dcd2016-08-15 17:37:17 -0700234
Dana Goyetteafa62fd2020-03-16 13:45:27 -0700235 def __str__(self):
236 return "<%s '%s:%s'>" % (
237 type(self).__name__, self.hostname, self.servo_port)
238
Richard Barnette9a26ad62016-06-10 12:03:08 -0700239 def connect_servo(self):
Garry Wang8c8dc972020-06-09 13:41:51 -0700240 """ Initialize and setup servo for later use.
241 """
Greg Edelstonff2665d2021-04-21 14:32:27 -0600242 self.initialize_servo()
Garry Wang8c8dc972020-06-09 13:41:51 -0700243 self.initialize_dut_for_servo()
244
Greg Edelstonff2665d2021-04-21 14:32:27 -0600245 def initialize_servo(self):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -0700246 """Establish a connection to the servod server on this host.
Richard Barnette9a26ad62016-06-10 12:03:08 -0700247
248 Initializes `self._servo` and then verifies that all network
249 connections are working. This will create an ssh tunnel if
250 it's required.
Garry Wang8c8dc972020-06-09 13:41:51 -0700251 """
252 self._servo = servo.Servo(servo_host=self,
253 servo_serial=self.servo_serial)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700254
Garry Wang8c8dc972020-06-09 13:41:51 -0700255 def initialize_dut_for_servo(self):
256 """This method will do some setup for dut control, e.g. setup
257 main servo_v4 device, and also testing the connection between servo
258 and DUT. As a side effect of testing the connection, all signals on
259 the target servo are reset to default values, and the USB stick is
Richard Barnette9a26ad62016-06-10 12:03:08 -0700260 set to the neutral (off) position.
261 """
Garry Wang8c8dc972020-06-09 13:41:51 -0700262 if not self._servo:
263 raise hosts.AutoservVerifyError('Servo object needs to be'
264 ' initialized before initialize'
265 ' DUT.')
Richard Barnette9a26ad62016-06-10 12:03:08 -0700266 timeout, _ = retry.timeout(
Garry Wang8c8dc972020-06-09 13:41:51 -0700267 self._servo.initialize_dut,
268 timeout_sec=self.INITIALIZE_SERVO_TIMEOUT_SECS)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700269 if timeout:
Garry Wang8c8dc972020-06-09 13:41:51 -0700270 raise hosts.AutoservVerifyError('Initialize dut for servo timed'
271 ' out.')
Richard Barnette9a26ad62016-06-10 12:03:08 -0700272
Richard Barnette9a26ad62016-06-10 12:03:08 -0700273 def disconnect_servo(self):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -0700274 """Disconnect our servo if it exists.
Richard Barnette9a26ad62016-06-10 12:03:08 -0700275
276 If we've previously successfully connected to our servo,
277 disconnect any established ssh tunnel, and set `self._servo`
278 back to `None`.
279 """
280 if self._servo:
281 # N.B. This call is safe even without a tunnel:
282 # rpc_server_tracker.disconnect() silently ignores
283 # unknown ports.
284 self.rpc_server_tracker.disconnect(self.servo_port)
285 self._servo = None
Fang Deng5d518f42013-08-02 14:04:32 -0700286
Andrew McRaef0679932020-08-13 09:15:23 +1000287 def _maybe_create_servod_ssh_tunnel_proxy(self):
288 """Create a xmlrpc proxy for use with a ssh tunnel.
289 A lock is used to safely create a singleton proxy.
290 """
291 with self._tunnel_proxy_lock:
292 if self._tunnel_proxy is None:
293 self._tunnel_proxy = self.rpc_server_tracker.xmlrpc_connect(
294 None,
295 self.servo_port,
296 ready_test_name=self.SERVO_READY_METHOD,
297 timeout_seconds=60,
298 request_timeout_seconds=3600,
299 server_desc=str(self))
300
Andrew McRaef0679932020-08-13 09:15:23 +1000301 def get_servod_server_proxy(self):
302 """Return a proxy if it exists; otherwise, create a new one.
303 A proxy can either be a ssh tunnel based proxy, or a httplib
304 based proxy.
Fang Deng5d518f42013-08-02 14:04:32 -0700305
306 @returns: An xmlrpclib.ServerProxy that is connected to the servod
307 server on the host.
Fang Deng5d518f42013-08-02 14:04:32 -0700308 """
Garry Wang11b5e872020-03-11 15:14:08 -0700309 if (servo_constants.ENABLE_SSH_TUNNEL_FOR_SERVO
310 and not self.is_localhost()):
Andrew McRaef0679932020-08-13 09:15:23 +1000311 # Check for existing ssh tunnel proxy.
312 if self._tunnel_proxy is None:
313 self._maybe_create_servod_ssh_tunnel_proxy()
314 return self._tunnel_proxy
Richard Barnette9a26ad62016-06-10 12:03:08 -0700315 else:
Andrew McRaef0679932020-08-13 09:15:23 +1000316 # xmlrpc/httplib is not thread-safe, so each thread must have its
317 # own separate proxy connection.
318 if not hasattr(self._local, "_per_thread_proxy"):
319 remote = 'http://%s:%s' % (self.hostname, self.servo_port)
Derek Beckettf73baca2020-08-19 15:08:47 -0700320 self._local._per_thread_proxy = six.moves.xmlrpc_client.ServerProxy(remote)
Andrew McRaef0679932020-08-13 09:15:23 +1000321 return self._local._per_thread_proxy
Wai-Hong Tam3a8a2552019-11-19 14:28:04 +0800322
Richard Barnette1edbb162016-11-01 11:47:50 -0700323 def verify(self, silent=False):
324 """Update the servo host and verify it's in a good state.
325
326 @param silent If true, suppress logging in `status.log`.
327 """
Richard Barnetteabbdc252018-07-26 16:57:42 -0700328 message = 'Beginning verify for servo host %s port %s serial %s'
329 message %= (self.hostname, self.servo_port, self.servo_serial)
330 self.record('INFO', None, None, message)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700331 try:
Richard Barnette1edbb162016-11-01 11:47:50 -0700332 self._repair_strategy.verify(self, silent)
Garry Wang11b5e872020-03-11 15:14:08 -0700333 self._servo_state = servo_constants.SERVO_STATE_WORKING
334 self.record('INFO', None, None,
335 'ServoHost verify set servo_state as WORKING')
Garry Wang63b8c382020-03-11 22:28:40 -0700336 except Exception as e:
Otabek Kasimov120b6fa2020-07-03 00:15:27 -0700337 if not self.is_localhost():
Otabek Kasimovc6f30412020-06-30 20:08:12 -0700338 self._servo_state = self.determine_servo_state()
339 self.record('INFO', None, None,
340 'ServoHost verify set servo_state as %s'
341 % self._servo_state)
Garry Wang63b8c382020-03-11 22:28:40 -0700342 if self._is_critical_error(e):
343 raise
Fang Deng5d518f42013-08-02 14:04:32 -0700344
Garry Wang2b5eef92020-08-21 16:23:35 -0700345 def _get_default_usbkey_mount_path(self):
346 return '/media/servo_usb/%s' % self.servo_port
Fang Deng5d518f42013-08-02 14:04:32 -0700347
Garry Wang7b0e1b72020-03-25 19:08:59 -0700348 def get_image_name_from_usbkey(self, usbkey_dev):
349 """Mount usb drive and check ChromeOS image name on it if there is
350 one. This method assumes the image_usbkey_direction is already set
351 to servo side.
352
Garry Wang4b980202020-09-24 17:00:17 -0700353 @param usbkey_dev: usbkey dev path(e.g. /dev/sdb).
Garry Wang7b0e1b72020-03-25 19:08:59 -0700354
355 @returns: image_name on the usbkey, e.g. nami-release/R82.10138.0.0,
356 or empty string if no test image detected, or unexpected
357 error occurred.
Garry Wang7b0e1b72020-03-25 19:08:59 -0700358 """
Garry Wang70e5d062020-04-03 18:01:05 -0700359 logging.info('Checking ChromeOS image name on usbkey.')
Garry Wang2b5eef92020-08-21 16:23:35 -0700360 mount_dst = self._get_default_usbkey_mount_path()
Garry Wang7b0e1b72020-03-25 19:08:59 -0700361 # Unmount if there is an existing stale mount.
Garry Wang2b5eef92020-08-21 16:23:35 -0700362 self._unmount_drive(mount_dst)
363 # ChromeOS root fs is in /dev/sdx3
364 mount_src = usbkey_dev + '3'
Garry Wang7b0e1b72020-03-25 19:08:59 -0700365 try:
Garry Wang2b5eef92020-08-21 16:23:35 -0700366 if not self._mount_drive(mount_src, mount_dst):
367 logging.debug('Unexpected error occurred on mount usb drive.')
Garry Wang7b0e1b72020-03-25 19:08:59 -0700368 return ''
Garry Wang70e5d062020-04-03 18:01:05 -0700369
370 release_content = self.run(
Garry Wang2b5eef92020-08-21 16:23:35 -0700371 'cat %s/etc/lsb-release' % mount_dst,
Garry Wang70e5d062020-04-03 18:01:05 -0700372 ignore_status=True).stdout.strip()
373
374 if not re.search(r'RELEASE_TRACK=.*test', release_content):
375 logging.info('The image on usbkey is not a test image')
376 return ''
377
378 return lsbrelease_utils.get_chromeos_release_builder_path(
379 lsb_release_content=release_content)
Garry Wang7b0e1b72020-03-25 19:08:59 -0700380 finally:
Garry Wang70e5d062020-04-03 18:01:05 -0700381 logging.debug('Image check compeleted, unmounting the usb drive.')
Garry Wang2b5eef92020-08-21 16:23:35 -0700382 self._unmount_drive(mount_dst)
Garry Wang7b0e1b72020-03-25 19:08:59 -0700383
Garry Wang2b5eef92020-08-21 16:23:35 -0700384 def _extract_firmware_image_from_usbkey(self, fw_dst):
385 """Extract firmware images from the usbkey on servo, this method
386 assumes there is already a ChromeOS test image staged on servo.
387
Garry Wang4b980202020-09-24 17:00:17 -0700388 @param fw_dst: the path that we'll copy firmware images to.
Garry Wang2b5eef92020-08-21 16:23:35 -0700389
390 @returns: a json format string of firmware manifest data.
391 """
392 usbkey_dev = self._probe_and_validate_usb_dev()
393 if not usbkey_dev:
394 raise hosts.AutoservRepairError('Unexpected error occurred when'
395 ' probe usbkey dev path, please check logs for detail.')
396
397 mount_dst = self._get_default_usbkey_mount_path()
398 # Unmount if there is an existing stale mount.
399 self._unmount_drive(mount_dst)
400 # ChromeOS root fs is in /dev/sdx3
401 mount_src = usbkey_dev + '3'
402 try:
403 if not self._mount_drive(mount_src, mount_dst):
404 raise hosts.AutoservRepairError('Failed to extract firmware'
405 ' image; Unable to mount %s.' % usbkey_dev,
406 'unable to mount usbkey')
407 updater_bin = os.path.join(mount_dst,
408 'usr/sbin/chromeos-firmwareupdate')
409 self.run('%s --unpack %s' % (updater_bin, fw_dst))
410 return self.run('%s --manifest' % updater_bin).stdout
411 finally:
412 self._unmount_drive(mount_dst)
413
414 def prepare_repair_firmware_image(self, fw_dst=None):
415 """Prepare firmware image on the servohost for auto repair process
416 to consume.
417
Garry Wang4b980202020-09-24 17:00:17 -0700418 @param fw_dst: the path that we want to store firmware image on
419 the servohost.
Garry Wang2b5eef92020-08-21 16:23:35 -0700420
421 @returns: A tuple that containes ec firmware image path and bios
422 firmware image path on the servohost, or None if type of
423 image is not available based on manifest and dut's model.
424 """
425 model = self.servo_model or self._dut_host_info.model
426 if not model:
427 raise hosts.AutoservRepairError(
428 'Could not determine DUT\'s model.',
429 'model infomation unknown')
430
431 if not fw_dst:
432 fw_dst = '/tmp/firmware_image/%s' % self.servo_port
433 # Cleanup and re-create dst path to have a fresh start.
434 self.run('rm -rf %s' % fw_dst)
435 self.run('mkdir -p %s' % fw_dst)
436
437 manifest = json.loads(self._extract_firmware_image_from_usbkey(fw_dst))
Garry Wang50b56c12020-09-24 17:26:52 -0700438 # For models that have packed $MODEL_signed variant, we want use the
439 # 'signed' variant once we get DVT devices, so try to read manifest
440 # from $MODEL_signed first.
441 build = manifest.get('%s_signed' % model) or manifest.get(model)
442 if not build:
Garry Wang2b5eef92020-08-21 16:23:35 -0700443 raise hosts.AutoservRepairError('Could not find firmware manifest'
444 ' for model:%s' % model, 'model manifest not found')
445 try:
Garry Wang50b56c12020-09-24 17:26:52 -0700446 ec_image = os.path.join(fw_dst, build['ec']['image'])
Garry Wang2b5eef92020-08-21 16:23:35 -0700447 except KeyError:
448 ec_image = None
449 try:
Garry Wang50b56c12020-09-24 17:26:52 -0700450 bios_image = os.path.join(fw_dst, build['host']['image'])
Garry Wang2b5eef92020-08-21 16:23:35 -0700451 except KeyError:
452 bios_image = None
453 if not ec_image and not bios_image:
454 raise hosts.AutoservRepairError('Could not find any firmware image'
455 ' for model:%s' % model, 'cannot find firmware image')
456 return ec_image, bios_image
Garry Wang7b0e1b72020-03-25 19:08:59 -0700457
Garry Wang4b980202020-09-24 17:00:17 -0700458 def flash_ap_firmware_via_servo(self, image):
459 """Flash AP firmware by use a provided image.
460
461 This is will be a short term enhanment for infra repair use, it use
462 'futility update' which will automatically determine various parameters
463 needed for flashrom, and will preserve the GBB, VPD, and HWID for
464 AP firmware update.
465 @TODO(xianuowang@) Remove this method once b/148403277 implemented.
466
467 @param image: the firmware image path on servohost.
468 """
469 cmd = 'futility update -i %s --servo_port=%s'
470 self.run(cmd % (image, self.servo_port), timeout=900)
471
Garry Wang70e5d062020-04-03 18:01:05 -0700472 def _probe_and_validate_usb_dev(self):
473 """This method probe the usb dev path by talking to servo, and then
474 validate the dev path is valid block device to servohost.
475 Possible output:
476 1. Encounter error during probe usb dev, returns empty string.
477 2. probe usb dev completed without error but cannot find usb dev,
478 raise AutoservRepairError.
479 3. probe usb dev find a usb dev path, but failed validation in this
480 method, raise AutoservRepairError.
Garry Wang7b0e1b72020-03-25 19:08:59 -0700481
Garry Wang70e5d062020-04-03 18:01:05 -0700482 @returns: A string of usb dev path(e.g. '/dev/sdb'), or empty string
483 if unexpected error occurred during probe.
484 @raises: AutoservRepairError if servo couldn't probe the usb dev path
485 (servo.probe_host_usb_dev() returns empty string), or the dev path is
486 not valid block device to servohost.
Garry Wang7b0e1b72020-03-25 19:08:59 -0700487 """
488 logging.info('Validating image usbkey on servo.')
Garry Wang7b0e1b72020-03-25 19:08:59 -0700489 try:
Garry Wang70e5d062020-04-03 18:01:05 -0700490 usb_dev = self._servo.probe_host_usb_dev()
Garry Wang7b0e1b72020-03-25 19:08:59 -0700491 except Exception as e:
492 # We don't want any unexpected or transient servo communicating
493 # failure block usb repair, so capture all errors here.
494 logging.error(e, exc_info=True)
495 logging.error('Unexpected error occurred on get usbkey dev path,'
496 ' skipping usbkey validation.')
497 return ''
498
Garry Wang70e5d062020-04-03 18:01:05 -0700499 if usb_dev:
500 # probe_host_usb_dev() sometimes return stale record,
501 # so we need to make sure the path exists in fdisk.
Otabek Kasimov77bff672020-10-08 15:52:03 -0700502 validate_cmd = 'fdisk -l %s' % usb_dev
Garry Wang11441182020-06-16 18:34:14 -0700503 try:
Otabek Kasimov77bff672020-10-08 15:52:03 -0700504 resp = self.run(validate_cmd, ignore_status=True, timeout=30)
Garry Wang11441182020-06-16 18:34:14 -0700505 if resp.exit_status == 0:
506 return usb_dev
Garry Wang11441182020-06-16 18:34:14 -0700507 logging.error('%s is reported from "image_usbkey_dev" control'
508 ' but not detected by fdisk!', usb_dev)
509 except error.AutoservRunError as e:
510 if 'Timeout encountered' in str(e):
511 logging.warning('Timeout encountered during fdisk run,'
512 ' skipping usbkey validation.')
513 return ''
514 raise
Garry Wang70e5d062020-04-03 18:01:05 -0700515
516 raise hosts.AutoservRepairError(
517 'No usbkey detected on servo, the usbkey may be either missing'
518 ' or broken. Please replace usbkey on the servo and retry.',
519 'missing usbkey')
520
Otabek Kasimov4ea636e2020-04-14 23:35:06 -0700521 def is_ec_supported(self):
Garry Wang9b8f2342020-04-17 16:34:09 -0700522 """Check if ec is supported on the servo_board"""
Otabek Kasimov4ea636e2020-04-14 23:35:06 -0700523 if self.servo_board:
524 try:
525 frm_config = config.Config(self.servo_board, self.servo_model)
526 return frm_config.chrome_ec
527 except Exception as e:
528 logging.error('Unexpected error when read from firmware'
529 ' configs; %s', str(e))
530 return False
531
Garry Wang70e5d062020-04-03 18:01:05 -0700532 def validate_image_usbkey(self):
533 """This method first validate if there is a recover usbkey on servo
534 that accessible to servohost, and second check if a ChromeOS image is
535 already on the usb drive and return the image_name so we can avoid
536 unnecessary download and flash to the recover usbkey on servo.
537
538 Please note that, there is special error handling logic here:
539 1. If unexpected error happens, we return empty string. So repair
540 actions will not get blocked.
541 2. If no working usbkey present on servo, but no errors, we'll raise
542 AutoservRepairError here.
543
544 @returns: image_name on the usbkey, e.g. nami-release/R82.10138.0.0,
545 or empty string if no test image detected, or unexpected
546 error occurred.
547 @raises: AutoservRepairError if the usbkey is not detected on servo.
548 """
549 usb_dev = self._probe_and_validate_usb_dev()
550 if usb_dev:
551 return self.get_image_name_from_usbkey(usb_dev)
552 else:
553 return ''
Garry Wang7b0e1b72020-03-25 19:08:59 -0700554
Richard Barnette1edbb162016-11-01 11:47:50 -0700555 def repair(self, silent=False):
556 """Attempt to repair servo host.
557
558 @param silent If true, suppress logging in `status.log`.
559 """
Richard Barnetteabbdc252018-07-26 16:57:42 -0700560 message = 'Beginning repair for servo host %s port %s serial %s'
561 message %= (self.hostname, self.servo_port, self.servo_serial)
562 self.record('INFO', None, None, message)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700563 try:
Richard Barnette1edbb162016-11-01 11:47:50 -0700564 self._repair_strategy.repair(self, silent)
Garry Wang11b5e872020-03-11 15:14:08 -0700565 self._servo_state = servo_constants.SERVO_STATE_WORKING
566 self.record('INFO', None, None,
567 'ServoHost repair set servo_state as WORKING')
Garry Wang464ff1e2019-07-18 17:20:34 -0700568 # If target is a labstation then try to withdraw any existing
569 # reboot request created by this servo because it passed repair.
570 if self.is_labstation():
571 self.withdraw_reboot_request()
Garry Wang63b8c382020-03-11 22:28:40 -0700572 except Exception as e:
Otabek Kasimov120b6fa2020-07-03 00:15:27 -0700573 if not self.is_localhost():
Otabek Kasimovc6f30412020-06-30 20:08:12 -0700574 self._servo_state = self.determine_servo_state()
575 self.record('INFO', None, None,
576 'ServoHost repair set servo_state as %s'
577 % self._servo_state)
Garry Wang63b8c382020-03-11 22:28:40 -0700578 if self._is_critical_error(e):
579 self.disconnect_servo()
580 self.stop_servod()
581 raise
582
Garry Wang63b8c382020-03-11 22:28:40 -0700583 def _is_critical_error(self, error):
584 if (isinstance(error, hosts.AutoservVerifyDependencyError)
585 and not error.is_critical()):
586 logging.warning('Non-critical verify failure(s) detected during'
587 ' verify/repair servo, servo connection will'
Evan Benn2c41c262020-10-28 11:34:27 +1100588 ' still be up but may not be fully functional.'
589 ' Some repair actions and servo dependent'
Garry Wang63b8c382020-03-11 22:28:40 -0700590 ' tests may not run.')
591 return False
Evan Benn2c41c262020-10-28 11:34:27 +1100592 logging.info(
593 'Critical verify failure(s) detected during repair/verify '
594 'servo. Disconnecting servo and running `stop servod`, all'
595 ' repair actions and tests that depends on servo will not '
596 'run.')
Garry Wang63b8c382020-03-11 22:28:40 -0700597 return True
Fang Deng5d518f42013-08-02 14:04:32 -0700598
Dan Shi4d478522014-02-14 13:46:32 -0800599 def get_servo(self):
600 """Get the cached servo.Servo object.
Fang Deng5d518f42013-08-02 14:04:32 -0700601
Dan Shi4d478522014-02-14 13:46:32 -0800602 @return: a servo.Servo object.
Dana Goyette353d1d92019-06-27 10:43:59 -0700603 @rtype: autotest_lib.server.cros.servo.servo.Servo
Fang Deng5d518f42013-08-02 14:04:32 -0700604 """
Dan Shi4d478522014-02-14 13:46:32 -0800605 return self._servo
606
Garry Wang79e9af62019-06-12 15:19:19 -0700607 def request_reboot(self):
608 """Request servohost to be rebooted when it's safe to by touch a file.
609 """
610 logging.debug('Request to reboot servohost %s has been created by '
Garry Wang464ff1e2019-07-18 17:20:34 -0700611 'servo with port # %s', self.hostname, self.servo_port)
Garry Wang79e9af62019-06-12 15:19:19 -0700612 self.run('touch %s' % self._reboot_file, ignore_status=True)
613
Garry Wang464ff1e2019-07-18 17:20:34 -0700614 def withdraw_reboot_request(self):
615 """Withdraw a servohost reboot request if exists by remove the flag
616 file.
617 """
618 logging.debug('Withdrawing request to reboot servohost %s that created'
619 ' by servo with port # %s if exists.',
620 self.hostname, self.servo_port)
621 self.run('rm -f %s' % self._reboot_file, ignore_status=True)
622
Garry Wangc1288cf2019-12-17 14:58:00 -0800623 def start_servod(self, quick_startup=False):
624 """Start the servod process on servohost.
625 """
Garry Wang2ac15ee2019-12-30 19:03:02 -0800626 # Skip if running on the localhost.(crbug.com/1038168)
627 if self.is_localhost():
628 logging.debug("Servohost is a localhost, skipping start servod.")
629 return
630
631 cmd = 'start servod'
Garry Wangc1288cf2019-12-17 14:58:00 -0800632 if self.servo_board:
Garry Wang2ac15ee2019-12-30 19:03:02 -0800633 cmd += ' BOARD=%s' % self.servo_board
Garry Wangc1288cf2019-12-17 14:58:00 -0800634 if self.servo_model:
635 cmd += ' MODEL=%s' % self.servo_model
Garry Wangc1288cf2019-12-17 14:58:00 -0800636 else:
Garry Wang2ac15ee2019-12-30 19:03:02 -0800637 logging.warning('Board for DUT is unknown; starting servod'
638 ' assuming a pre-configured board.')
639
640 cmd += ' PORT=%d' % self.servo_port
641 if self.servo_serial:
642 cmd += ' SERIAL=%s' % self.servo_serial
Garry Wangd7367482020-02-27 13:52:40 -0800643
Garry Wangcb06f3b2020-10-08 20:56:21 -0700644 # Start servod with dual_v4 based on servo_setup.
Otabek Kasimov382c3bb2020-10-28 13:22:45 -0700645 if self.is_dual_setup():
Garry Wangcb06f3b2020-10-08 20:56:21 -0700646 cmd += ' DUAL_V4=1'
Garry Wangd7367482020-02-27 13:52:40 -0800647
Garry Wangcb06f3b2020-10-08 20:56:21 -0700648 # Start servod with CONFIG=cr50.xml which required for some pools.
649 if self._require_cr50_servod_config():
650 cmd += ' CONFIG=cr50.xml'
Garry Wangb5cee3e2020-09-16 14:58:13 -0700651
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800652 if self.servo_recovery == True:
653 cmd += ' REC_MODE=1'
654
Garry Wang6a680062020-11-03 13:40:29 -0800655 # Adding customized args if any.
656 if self.additional_servod_args:
657 cmd += ' ' + self.additional_servod_args
658
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800659 # Remove the symbolic links from the logs. This helps ensure that
660 # a failed servod instantiation does not cause us to grab old logs
661 # by mistake.
662 self.remove_latest_log_symlinks()
Garry Wangcdd27b22020-01-13 14:59:11 -0800663 self.run(cmd, timeout=60)
Garry Wangc1288cf2019-12-17 14:58:00 -0800664
665 # There's a lag between when `start servod` completes and when
666 # the _ServodConnectionVerifier trigger can actually succeed.
667 # The call to time.sleep() below gives time to make sure that
668 # the trigger won't fail after we return.
669
670 # Normally servod on servo_v3 and labstation take ~10 seconds to ready,
671 # But in the rare case all servo on a labstation are in heavy use they
672 # may take ~30 seconds. So the timeout value will double these value,
673 # and we'll try quick start up when first time initialize servohost,
674 # and use standard start up timeout in repair.
675 if quick_startup:
Garry Wang11b5e872020-03-11 15:14:08 -0700676 timeout = servo_constants.SERVOD_QUICK_STARTUP_TIMEOUT
Garry Wangc1288cf2019-12-17 14:58:00 -0800677 else:
Garry Wang11b5e872020-03-11 15:14:08 -0700678 timeout = servo_constants.SERVOD_STARTUP_TIMEOUT
Garry Wangc1288cf2019-12-17 14:58:00 -0800679 logging.debug('Wait %s seconds for servod process fully up.', timeout)
680 time.sleep(timeout)
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700681 # Cache the initial instance timestamp to check against servod restarts
682 self._initial_instance_ts = self.get_instance_logs_ts()
Garry Wangc1288cf2019-12-17 14:58:00 -0800683
Garry Wangc1288cf2019-12-17 14:58:00 -0800684 def stop_servod(self):
685 """Stop the servod process on servohost.
686 """
Garry Wang2ac15ee2019-12-30 19:03:02 -0800687 # Skip if running on the localhost.(crbug.com/1038168)
688 if self.is_localhost():
689 logging.debug("Servohost is a localhost, skipping stop servod.")
690 return
691
Garry Wangc1288cf2019-12-17 14:58:00 -0800692 logging.debug('Stopping servod on port %s', self.servo_port)
Garry Wangcdd27b22020-01-13 14:59:11 -0800693 self.run('stop servod PORT=%d' % self.servo_port,
694 timeout=60, ignore_status=True)
Garry Wangc1288cf2019-12-17 14:58:00 -0800695 logging.debug('Wait %s seconds for servod process fully teardown.',
Garry Wang11b5e872020-03-11 15:14:08 -0700696 servo_constants.SERVOD_TEARDOWN_TIMEOUT)
697 time.sleep(servo_constants.SERVOD_TEARDOWN_TIMEOUT)
Garry Wangc1288cf2019-12-17 14:58:00 -0800698
Garry Wangc1288cf2019-12-17 14:58:00 -0800699 def restart_servod(self, quick_startup=False):
700 """Restart the servod process on servohost.
701 """
702 self.stop_servod()
703 self.start_servod(quick_startup)
704
Garry Wangffbd2162020-04-17 16:13:48 -0700705 def _process_servodtool_error(self, response):
706 """Helper function to handle non-zero servodtool response.
707 """
708 if re.search(servo_constants.ERROR_MESSAGE_USB_HUB_NOT_COMPATIBLE,
Garry Wangad245002020-05-15 15:20:23 -0700709 response.stdout):
Garry Wangffbd2162020-04-17 16:13:48 -0700710 logging.error('The servo is not plugged on a usb hub that supports'
711 ' power-cycle!')
Garry Wang000c6c02020-05-11 21:27:23 -0700712 # change the flag so we can update this label in later process.
713 self.smart_usbhub = False
Garry Wangffbd2162020-04-17 16:13:48 -0700714 return
715
716 if re.search(servo_constants.ERROR_MESSAGE_DEVICE_NOT_FOUND %
717 self.servo_serial, response.stdout):
718 logging.error('No servo with serial %s found!', self.servo_serial)
719 return
720
721 logging.error('Unexpected error occurred from usbhub control, please'
722 ' file a bug and inform chrome-fleet-software@ team!')
723
Otabek Kasimov86062d02020-11-17 13:30:22 -0800724 def get_main_servo_usb_path(self):
725 """Helper function to collect current usb-path to main servo.
726
727 The usb-path is path to the folder where usb-device was enumerated.
728 If fail then will return an empty string ('').
729
730 @returns: string, usb-path to the main servo device.
731 e.g.: '/sys/bus/usb/devices/1-6.1.3.1'
Garry Wangffbd2162020-04-17 16:13:48 -0700732 """
Otabek Kasimov09192682020-06-01 18:17:44 -0700733 # TODO remove try-except when fix crbug.com/1087964
734 try:
735 cmd = 'servodtool device -s %s usb-path' % self.servo_serial
736 resp = self.run(cmd, ignore_status=True, timeout=30)
737 except Exception as e:
738 # Here we catch only timeout errors.
739 # Other errors is filtered by ignore_status=True
740 logging.debug('Attempt to get servo usb-path failed due to '
741 'timeout; %s', e)
742 return ''
Garry Wangffbd2162020-04-17 16:13:48 -0700743 if resp.exit_status != 0:
744 self._process_servodtool_error(resp)
745 return ''
746 usb_path = resp.stdout.strip()
747 logging.info('Usb path of servo %s is %s', self.servo_serial, usb_path)
Otabek Kasimov86062d02020-11-17 13:30:22 -0800748 return usb_path
Garry Wangffbd2162020-04-17 16:13:48 -0700749
Otabek Kasimov86062d02020-11-17 13:30:22 -0800750 def _get_servo_usb_devnum(self):
751 """Helper function to collect current usb devnum of servo."""
752 usb_path = self.get_main_servo_usb_path()
753 if not usb_path:
754 return ''
755 resp = self.run('cat %s/devnum' % usb_path, ignore_status=True)
Garry Wangffbd2162020-04-17 16:13:48 -0700756 if resp.exit_status != 0:
757 self._process_servodtool_error(resp)
758 return ''
759 return resp.stdout.strip()
760
Garry Wang358aad42020-08-02 20:56:04 -0700761 def reboot_servo_v3_on_need(self):
762 """Check and reboot servo_v3 based on below conditions.
763 1. If there is an update pending on reboot.
764 2. Servo_v3 has been up for more than 96 hours.
765 """
766 if self.get_board() != 'beaglebone_servo':
767 logging.info('Servo reboot is only applicable for servo V3.')
Otabek Kasimove6df8102020-07-21 20:15:25 -0700768 return
769
Garry Wang358aad42020-08-02 20:56:04 -0700770 update_pending_reboot = (self._check_update_status() ==
771 self.UPDATE_STATE.PENDING_REBOOT)
772 uptime_hours = float(self.check_uptime())/3600
773 logging.info('Uptime of servo_v3: %s hour(s)', uptime_hours)
774 long_up_time = uptime_hours > 96
775
776 # Skip reboot if neither condition are met.
777 if not (update_pending_reboot or long_up_time):
Otabek Kasimove6df8102020-07-21 20:15:25 -0700778 return
779
Garry Wang358aad42020-08-02 20:56:04 -0700780 if update_pending_reboot:
781 message = 'Starting reboot servo_v3 because an update is pending.'
782 reboot_method = self._post_update_reboot
783 elif long_up_time:
784 message = 'Starting reboot servo_v3 because uptime > 96 hours.'
785 reboot_method = self._servo_host_reboot
786 self.record('INFO', None, None, message)
787 logging.info(message)
Otabek Kasimove6df8102020-07-21 20:15:25 -0700788 try:
Garry Wang358aad42020-08-02 20:56:04 -0700789 reboot_method()
Otabek Kasimove6df8102020-07-21 20:15:25 -0700790 message = 'Servo_v3 reboot completed successfully.'
791 except Exception as e:
792 logging.debug("Fail to reboot servo_v3; %s", e)
793 message = ('Servo_v3 reboot failed, please check debug log '
794 'for details.')
795 logging.info(message)
796 self.record('INFO', None, None, message)
Garry Wangffbd2162020-04-17 16:13:48 -0700797
798 def _reset_servo(self):
799 logging.info('Resetting servo through smart usbhub.')
Otabek Kasimov09192682020-06-01 18:17:44 -0700800 # TODO remove try-except when fix crbug.com/1087964
801 try:
802 resp = self.run('servodtool device -s %s power-cycle' %
803 self.servo_serial, ignore_status=True,
804 timeout=30)
805 if resp.exit_status != 0:
806 self._process_servodtool_error(resp)
807 return False
808 except Exception as e:
809 # Here we catch only timeout errors.
810 # Other errors is filtered by ignore_status=True
811 logging.debug('Attempt to reset servo failed due to timeout;'
812 ' %s', e)
Garry Wangffbd2162020-04-17 16:13:48 -0700813 return False
814
815 logging.debug('Wait %s seconds for servo to come back from reset.',
816 servo_constants.SERVO_RESET_TIMEOUT_SECONDS)
817 time.sleep(servo_constants.SERVO_RESET_TIMEOUT_SECONDS)
Garry Wang000c6c02020-05-11 21:27:23 -0700818 # change the flag so we can update this label in later process.
819 self.smart_usbhub = True
Garry Wangffbd2162020-04-17 16:13:48 -0700820 return True
821
Garry Wangffbd2162020-04-17 16:13:48 -0700822 def reset_servo(self):
823 """Reset(power-cycle) the servo via smart usbhub.
824 """
825 if not self.is_labstation():
826 logging.info('Servo reset is not applicable to servo_v3.')
827 return
828
829 pre_reset_devnum = self._get_servo_usb_devnum()
830 logging.info('Servo usb devnum before reset: %s', pre_reset_devnum)
831 result = self._reset_servo()
832 if not result:
Garry Wangfd5c8b62020-06-08 15:36:54 -0700833 message = ('Failed to reset servo with serial: %s. (Please ignore'
834 ' this error if the DUT is not connected to a smart'
835 ' usbhub).' % self.servo_serial)
Garry Wangffbd2162020-04-17 16:13:48 -0700836 logging.warning(message)
837 self.record('INFO', None, None, message)
838 return
839
840 post_reset_devnum = self._get_servo_usb_devnum()
841 logging.info('Servo usb devnum after reset: %s', post_reset_devnum)
842 if not (pre_reset_devnum and post_reset_devnum):
843 message = ('Servo reset completed but unable to verify'
844 ' devnum change!')
845 elif pre_reset_devnum != post_reset_devnum:
846 message = ('Reset servo with serial %s completed successfully!'
847 % self.servo_serial)
848 else:
849 message = 'Servo reset completed but devnum is still not changed!'
850 logging.info(message)
851 self.record('INFO', None, None, message)
852
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800853 def _extract_compressed_logs(self, logdir, relevant_files):
854 """Decompress servod logs in |logdir|.
855
856 @param logdir: directory containing compressed servod logs.
857 @param relevant_files: list of files in |logdir| to consider.
858
859 @returns: tuple, (tarfiles, files) where
860 tarfiles: list of the compressed filenames that have been
861 extracted and deleted
862 files: list of the uncompressed files that were generated
863 """
864 # For all tar-files, first extract them to the directory, and
865 # then let the common flow handle them.
866 tarfiles = [cf for cf in relevant_files if
867 cf.endswith(self.COMPRESSION_SUFFIX)]
868 files = []
869 for f in tarfiles:
870 norm_name = os.path.basename(f)[:-len(self.COMPRESSION_SUFFIX)]
871 with tarfile.open(f) as tf:
872 # Each tarfile has only one member, as
873 # that's the compressed log.
874 member = tf.members[0]
875 # Manipulate so that it only extracts the basename, and not
876 # the directories etc.
877 member.name = norm_name
878 files.append(os.path.join(logdir, member.name))
879 tf.extract(member, logdir)
880 # File has been extracted: remove the compressed file.
881 os.remove(f)
882 return tarfiles, files
883
884 def _extract_mcu_logs(self, log_subdir):
885 """Extract MCU (EC, Cr50, etc) console output from servod debug logs.
886
887 Using the MCU_EXTRACTOR regex (above) extract and split out MCU console
888 lines from the logs to generate invidiual console logs e.g. after
889 this method, you can find an ec.txt and servo_v4.txt in |log_dir| if
890 those MCUs had any console input/output.
891
892 @param log_subdir: directory with log.DEBUG.txt main servod debug logs.
893 """
894 # Extract the MCU for each one. The MCU logs are only in the .DEBUG
895 # files
896 mcu_lines_file = os.path.join(log_subdir, 'log.DEBUG.txt')
897 if not os.path.exists(mcu_lines_file):
898 logging.info('No DEBUG logs found to extract MCU logs from.')
899 return
900 mcu_files = {}
901 mcu_file_template = '%s.txt'
902 with open(mcu_lines_file, 'r') as f:
903 for line in f:
904 match = self.MCU_EXTRACTOR.match(line)
905 if match:
906 mcu = match.group(self.MCU_GROUP).lower()
907 line = match.group(self.LINE_GROUP)
908 if mcu not in mcu_files:
909 mcu_file = os.path.join(log_subdir,
910 mcu_file_template % mcu)
911 mcu_files[mcu] = open(mcu_file, 'a')
912 fd = mcu_files[mcu]
913 fd.write(line + '\n')
914 for f in mcu_files:
915 mcu_files[f].close()
916
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800917 def remove_latest_log_symlinks(self):
918 """Remove the conveninence symlinks 'latest' servod logs."""
919 symlink_wildcard = '%s/latest*' % self.remote_log_dir
920 cmd = 'rm ' + symlink_wildcard
921 self.run(cmd, stderr_tee=None, ignore_status=True)
922
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700923 def probe_servod_restart(self, instance_ts, outdir):
924 """Grab servod logs from previous instances if part of this session.
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800925
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700926 If since the last time this host called start_servod() servod crashed
927 and restarted, this helper finds those logs as well, and stores them
928 with the |OLD_LOG_SUFFIX| to investigate if necessary.
Prasad Vuppalapu5bd9da12020-03-31 01:46:47 +0000929
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700930 It also issues a panicinfo command to servo devices after the restart
931 to try and collect reboot information for debugging.
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800932
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700933 @param instance_ts: the log timestamp that the current instance uses
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800934 @param outdir: directory to create a subdirectory into to place the
935 servod logs into.
936 """
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700937 if self._initial_instance_ts is None:
938 logging.info('No log timestamp grabbed successfully on servod '
939 'startup. Cannot check device restarts. Ignoring.')
940 return
941 if instance_ts == self._initial_instance_ts:
942 logging.debug('Servod appears to have run without restarting')
943 return
944 # Servod seems to have restarted (at least once). |_initial_instance_ts|
945 # is the first timestamp, and instance_ts is the current timestamp. Find
946 # all timestamps in between them, and grab the logs for each.
947 tss = self._find_instance_timestamps_between(self._initial_instance_ts,
948 instance_ts)
949 logging.info('Servod has restarted %d times between the start and the '
950 'end of this servo_host.', len(tss))
951 logging.info('This might be an issue. Will extract all logs from each '
952 'instance.')
953 logging.info('Logs that are not the currently running (about to turn '
954 'down) instance are maked with a .%s in their folder.',
955 self.OLD_LOG_SUFFIX)
956 for ts in tss:
957 self.get_instance_logs(ts, outdir, old=True)
958 # Lastly, servod has restarted due to a potential issue. Try to get
959 # panic information from servo micro and servo v4 for the current logs.
960 # This can only happen if the |_servo| attribute is initialized.
961 if self._servo:
Ruben Rodriguez Buchillon030ff162021-03-09 17:21:25 -0800962 for mcu in ['servo_micro', 'servo_v4', 'servo_v4p1']:
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700963 ctrl = '%s_uart_cmd' % mcu
964 if self._servo.has_control(ctrl):
965 logging.info('Trying to retrieve %r panicinfo into logs',
966 mcu)
967 try:
968 self._servo.set_nocheck(ctrl, 'panicinfo')
969 except error.TestFail as e:
970 logging.error('Failed to generate panicinfo for %r '
971 'logs. %s', mcu, str(e))
972
973 def _find_instance_timestamps_between(self, start_ts, end_ts):
974 """Find all log timestamps between [start_ts, end_ts).
975
976 @param start_ts: str, earliest log timestamp of interest
977 @param end_ts: str, latest log timestamp of interest
978
979 @returns: list, all timestamps between start_ts and end_ts, end_ts
980 exclusive, on the servo_host. An empty list on errors
981 """
982 # Simply get all timestamp, and then sort and remove
983 cmd = 'ls %s' % self.remote_log_dir
984 res = self.run(cmd, stderr_tee=None, ignore_status=True)
985 if res.exit_status != 0:
986 # Here we failed to find anything.
987 logging.info('Failed to find remote servod logs. Ignoring.')
988 return []
989 logfiles = res.stdout.strip().split()
990 timestamps = set()
991 for logfile in logfiles:
992 ts_match = self.TS_EXTRACTOR.match(logfile)
993 if not ts_match:
994 # Simply ignore files that fail the check. It might be the
995 # 'latest' symlinks or random files.
996 continue
997 timestamps.add(ts_match.group(self.TS_GROUP))
998 # At this point we have all unique timestamps.
999 timestamps = sorted(timestamps)
1000 for ts in [start_ts, end_ts]:
1001 if ts not in timestamps:
1002 logging.error('Timestamp %r not in servod logs. Cannot query '
1003 'for timestamps in between %r and %r', ts,
1004 start_ts, end_ts)
1005 return []
1006 return timestamps[timestamps.index(start_ts):timestamps.index(end_ts)]
1007
1008 def get_instance_logs_ts(self):
1009 """Retrieve the currently running servod instance's log timestamp
1010
1011 @returns: str, timestamp for current instance, or None on failure
1012 """
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001013 # First, extract the timestamp. This cmd gives the real filename of
1014 # the latest aka current log file.
1015 cmd = ('if [ -f %(dir)s/latest.DEBUG ];'
1016 'then realpath %(dir)s/latest.DEBUG;'
1017 'elif [ -f %(dir)s/latest ];'
1018 'then realpath %(dir)s/latest;'
1019 'else exit %(code)d;'
1020 'fi' % {'dir': self.remote_log_dir,
1021 'code': self.NO_SYMLINKS_CODE})
1022 res = self.run(cmd, stderr_tee=None, ignore_status=True)
1023 if res.exit_status != 0:
1024 if res.exit_status == self.NO_SYMLINKS_CODE:
1025 logging.warning('servod log latest symlinks not found. '
1026 'This is likely due to an error starting up '
1027 'servod. Ignoring..')
1028 else:
1029 logging.warning('Failed to find servod logs on servo host.')
1030 logging.warning(res.stderr.strip())
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001031 return None
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001032 fname = os.path.basename(res.stdout.strip())
1033 # From the fname, ought to extract the timestamp using the TS_EXTRACTOR
Ruben Rodriguez Buchillone9aa2b02020-03-04 12:14:28 -08001034 ts_match = self.TS_EXTRACTOR.match(fname)
1035 if not ts_match:
1036 logging.warning('Failed to extract timestamp from servod log file '
1037 '%r. Skipping. The servo host is using outdated '
1038 'servod logging and needs to be updated.', fname)
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001039 return None
1040 return ts_match.group(self.TS_GROUP)
1041
Garry Wang970fda92021-06-22 18:03:43 -07001042 def get_servohost_logs(self, outdir):
1043 """Get logs that can help debugging servo/servod problem from
1044 the servohost
1045 """
1046 log_dir = os.path.join(outdir, 'servohost_%s' % self.hostname)
1047 if os.path.isdir(log_dir):
1048 # In multi-DUTs testing, each DUTs will may their own servohost
1049 # instance, where could cause duplicate efforts if they share a
1050 # same servohost, so we can just skip the collect if the log
1051 # dir already exists.
1052 logging.info(
1053 'Skip dmesg and messages logs collecting as %s'
1054 ' already exists.', log_dir)
1055 return
1056 logging.info('Collecting dmesg and messages from servohost %s',
1057 self.hostname)
1058 os.mkdir(log_dir)
1059 logging.info('Saving servohost logs to %s.', log_dir)
1060 # First collect dmesg from the servohost.
1061 crashcollect.collect_command(self, 'dmesg -H',
1062 os.path.join(log_dir, 'dmesg'))
1063 # Collect messages log from the servohost.
1064 try:
1065 self.get_file('/var/log/messages', log_dir, try_rsync=False)
1066 except error.AutoservRunError as e:
1067 logging.warning('Failed to collect messages log from servohost.')
1068
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001069 def get_instance_logs(self, instance_ts, outdir, old=False):
1070 """Collect all logs with |instance_ts| and dump into a dir in |outdir|
1071
1072 This method first collects all logs on the servo_host side pertaining
1073 to this servod instance (port, instatiation). It glues them together
1074 into combined log.[level].txt files and extracts all available MCU
1075 console I/O from the logs into individual files e.g. servo_v4.txt
1076
1077 All the output can be found in a directory inside |outdir| that
1078 this generates based on |LOG_DIR|, the servod port, and the instance
1079 timestamp on the servo_host side.
1080
1081 @param instance_ts: log timestamp to grab logfiles for
1082 @param outdir: directory to create a subdirectory into to place the
1083 servod logs into.
1084 @param old: bool, whether to append |OLD_LOG_SUFFIX| to output dir
1085 """
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001086 # Create the local results log dir.
1087 log_dir = os.path.join(outdir, '%s_%s.%s' % (self.LOG_DIR,
1088 str(self.servo_port),
1089 instance_ts))
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001090 if old:
Garry Wang22f2e842020-09-09 20:19:19 -07001091 log_dir = '%s.%s' % (log_dir, self.OLD_LOG_SUFFIX)
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001092 logging.info('Saving servod logs to %r.', log_dir)
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001093 os.mkdir(log_dir)
1094 # Now, get all files with that timestamp.
1095 cmd = 'find %s -maxdepth 1 -name "log.%s*"' % (self.remote_log_dir,
1096 instance_ts)
1097 res = self.run(cmd, stderr_tee=None, ignore_status=True)
1098 files = res.stdout.strip().split()
1099 try:
1100 self.get_file(files, log_dir, try_rsync=False)
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001101 if not os.listdir(log_dir):
1102 logging.info('No servod logs retrieved. Ignoring, and removing '
1103 '%r again.', log_dir)
1104 os.rmdir(log_dir)
1105 return
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001106 except error.AutoservRunError as e:
1107 result = e.result_obj
1108 if result.exit_status != 0:
1109 stderr = result.stderr.strip()
1110 logging.warning("Couldn't retrieve servod logs. Ignoring: %s",
1111 stderr or '\n%s' % result)
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001112 # Remove the log_dir as nothing was added to it.
1113 os.rmdir(log_dir)
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001114 return
1115 local_files = [os.path.join(log_dir, f) for f in os.listdir(log_dir)]
1116 # TODO(crrev.com/c/1793030): remove no-level case once CL is pushed
1117 for level_name in ('DEBUG', 'INFO', 'WARNING', ''):
1118 # Create the joint files for each loglevel. i.e log.DEBUG
1119 joint_file = self.JOINT_LOG_PREFIX
1120 if level_name:
1121 joint_file = '%s.%s' % (self.JOINT_LOG_PREFIX, level_name)
1122 # This helps with some online tools to avoid complaints about an
1123 # unknown filetype.
1124 joint_file = joint_file + '.txt'
1125 joint_path = os.path.join(log_dir, joint_file)
1126 files = [f for f in local_files if level_name in f]
1127 if not files:
1128 # TODO(crrev.com/c/1793030): remove no-level case once CL
1129 # is pushed
1130 continue
1131 # Extract compressed logs if any.
1132 compressed, extracted = self._extract_compressed_logs(log_dir,
1133 files)
1134 files = list(set(files) - set(compressed))
1135 files.extend(extracted)
1136 # Need to sort. As they all share the same timestamp, and
1137 # loglevel, the index itself is sufficient. The highest index
1138 # is the oldest file, therefore we need a descending sort.
1139 def sortkey(f, level=level_name):
1140 """Custom sortkey to sort based on rotation number int."""
1141 if f.endswith(level_name): return 0
1142 return int(f.split('.')[-1])
1143
1144 files.sort(reverse=True, key=sortkey)
1145 # Just rename the first file rather than building from scratch.
1146 os.rename(files[0], joint_path)
1147 with open(joint_path, 'a') as joint_f:
1148 for logfile in files[1:]:
1149 # Transfer the file to the joint file line by line.
1150 with open(logfile, 'r') as log_f:
1151 for line in log_f:
1152 joint_f.write(line)
1153 # File has been written over. Delete safely.
1154 os.remove(logfile)
1155 # Need to remove all files form |local_files| so we don't
1156 # analyze them again.
1157 local_files = list(set(local_files) - set(files) - set(compressed))
1158 # Lastly, extract MCU logs from the joint logs.
1159 self._extract_mcu_logs(log_dir)
1160
Garry Wang79e9af62019-06-12 15:19:19 -07001161 def _lock(self):
1162 """lock servohost by touching a file.
1163 """
1164 logging.debug('Locking servohost %s by touching %s file',
1165 self.hostname, self._lock_file)
1166 self.run('touch %s' % self._lock_file, ignore_status=True)
Garry Wang7c00b0f2019-06-25 17:28:17 -07001167 self._is_locked = True
Garry Wang79e9af62019-06-12 15:19:19 -07001168
Garry Wang79e9af62019-06-12 15:19:19 -07001169 def _unlock(self):
1170 """Unlock servohost by removing the lock file.
1171 """
1172 logging.debug('Unlocking servohost by removing %s file',
1173 self._lock_file)
1174 self.run('rm %s' % self._lock_file, ignore_status=True)
Garry Wang7c00b0f2019-06-25 17:28:17 -07001175 self._is_locked = False
Garry Wang79e9af62019-06-12 15:19:19 -07001176
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001177 def close(self):
Congbin Guofc3b8962019-03-22 17:38:46 -07001178 """Close the associated servo and the host object."""
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001179 # NOTE: throughout this method there are multiple attempts to catch
1180 # all errors. This is WAI as log grabbing should not fail tests.
1181 # However, the goal is to catch and handle/process all errors, thus
1182 # we print the traceback and ask for a bug.
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001183 if self._closed:
1184 logging.debug('ServoHost is already closed.')
1185 return
Garry Wang22f2e842020-09-09 20:19:19 -07001186
1187 # Only attempt ssh related actions if servohost is sshable. We call
1188 # check_cached_up_status() first because it's lightweighted and return
1189 # much faster in the case servohost is down, however, we still want
1190 # to call is_up() later since check_cached_up_status() is ping based check
1191 # and not guarantee the servohost is sshable.
1192 servo_host_ready = self.check_cached_up_status() and self.is_up()
1193
1194 if servo_host_ready:
1195 instance_ts = self.get_instance_logs_ts()
1196 else:
1197 logging.info('Servohost is down, will skip servod log collecting.')
1198 instance_ts = None
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001199 # TODO(crbug.com/1011516): once enabled, remove the check against
1200 # localhost and instead check against log-rotiation enablement.
1201 logs_available = (instance_ts is not None and
1202 self.job and
1203 not self.is_localhost())
1204 if logs_available:
1205 # Probe whether there was a servod restart, and grab those old
1206 # logs as well.
1207 try:
1208 self.probe_servod_restart(instance_ts, self.job.resultdir)
1209 except (error.AutoservRunError, error.TestFail) as e:
1210 logging.info('Failed to grab servo logs due to: %s. '
1211 'This error is forgiven.', str(e))
1212 except Exception as e:
1213 logging.error('Unexpected error probing for old logs. %s. '
1214 'Forgiven. Please file a bug and fix or catch '
1215 'in log probing function', str(e),
1216 exc_info=True)
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001217 if self._servo:
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001218 outdir = None if not self.job else self.job.resultdir
Congbin Guo2e5e2a22018-07-27 10:32:48 -07001219 # In some cases when we run as lab-tools, the job object is None.
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001220 self._servo.close(outdir)
1221
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001222 if logs_available:
1223 # Grab current (not old like above) logs after the servo instance
1224 # was closed out.
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001225 try:
Garry Wang970fda92021-06-22 18:03:43 -07001226 self.get_servohost_logs(self.job.resultdir)
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001227 self.get_instance_logs(instance_ts, self.job.resultdir)
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001228 except error.AutoservRunError as e:
1229 logging.info('Failed to grab servo logs due to: %s. '
1230 'This error is forgiven.', str(e))
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001231 except Exception as e:
1232 logging.error('Unexpected error grabbing servod logs. %s. '
1233 'Forgiven. Please file a bug and fix or catch '
1234 'in log grabbing function', str(e), exc_info=True)
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001235
Garry Wang22f2e842020-09-09 20:19:19 -07001236 if self._is_locked and servo_host_ready:
Garry Wang7c00b0f2019-06-25 17:28:17 -07001237 # Remove the lock if the servohost has been locked.
Garry Wang79e9af62019-06-12 15:19:19 -07001238 try:
1239 self._unlock()
1240 except error.AutoservSSHTimeout:
1241 logging.error('Unlock servohost failed due to ssh timeout.'
1242 ' It may caused by servohost went down during'
1243 ' the task.')
Garry Wangc1288cf2019-12-17 14:58:00 -08001244 # We want always stop servod after task to minimum the impact of bad
1245 # servod process interfere other servods.(see crbug.com/1028665)
Garry Wang22f2e842020-09-09 20:19:19 -07001246 if servo_host_ready:
1247 try:
1248 self.stop_servod()
1249 except error.AutoservRunError as e:
1250 logging.info(
1251 "Failed to stop servod due to:\n%s\n"
1252 "This error is forgiven.", str(e))
Garry Wangc1288cf2019-12-17 14:58:00 -08001253
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001254 super(ServoHost, self).close()
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001255 # Mark closed.
1256 self._closed = True
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001257
Otabek Kasimovcc9738e2020-02-14 16:17:15 -08001258 def get_servo_state(self):
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001259 return self._servo_state
Otabek Kasimovcc9738e2020-02-14 16:17:15 -08001260
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001261 def _get_host_metrics_data(self):
1262 return {'port': self.servo_port,
Otabek Kasimov0ea47362020-07-11 20:55:09 -07001263 'host': self.get_dut_hostname() or self.hostname,
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001264 'board': self.servo_board or ''}
1265
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001266 def is_servo_board_present_on_servo_v3(self):
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001267 """Check if servo board is detected on servo_v3"""
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001268 logging.debug('Started to detect servo board on servo_v3')
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001269 vid_pids = ['18d1:5004', '0403:6014']
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001270 not_detected = 'The servo board is not detected on servo_v3'
1271 try:
1272 cmd = 'lsusb | grep "%s"' % "\|".join(vid_pids)
1273 result = self.run(cmd, ignore_status=True, timeout=30)
1274 if result.exit_status == 0 and result.stdout.strip():
1275 logging.debug('The servo board is detected on servo_v3')
1276 return True
1277 logging.debug('%s; %s', not_detected, result)
1278 return False
1279 except Exception as e:
1280 # can be triggered by timeout issue due running the script
1281 metrics.Counter(
1282 'chromeos/autotest/repair/servo_detection/timeout'
1283 ).increment(fields=self._get_host_metrics_data())
1284 logging.error('%s; %s', not_detected, str(e))
1285 return None
1286
Garry Wangb5cee3e2020-09-16 14:58:13 -07001287 def _require_cr50_servod_config(self):
1288 """Check whether we need start servod with CONFIG=cr50.xml"""
1289 dut_host_info = self.get_dut_host_info()
1290 if not dut_host_info:
1291 return False
1292 for pool in dut_host_info.pools:
1293 if pool.startswith(servo_constants.CR50_CONFIG_POOL_PREFIX):
1294 return True
1295 return False
1296
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001297 def get_verifier_state(self, tag):
Otabek Kasimov15963492020-06-23 21:10:51 -07001298 """Return the state of servo verifier.
1299
1300 @returns: bool or None
1301 """
1302 return self._repair_strategy.verifier_is_good(tag)
1303
Otabek Kasimov044a54b2021-05-06 19:12:18 -07001304 def get_repair_strategy_node(self, tag):
1305 """Return the instance of verifier/repair node for host by tag.
1306
1307 @returns: _DependencyNode or None
1308 """
1309 return self._repair_strategy.node_by_tag(tag)
1310
Otabek Kasimov15963492020-06-23 21:10:51 -07001311 def determine_servo_state(self):
1312 """Determine servo state based on the failed verifier.
1313
1314 @returns: servo state value
1315 The state detecting based on first fail verifier or collecting of
1316 them.
1317 """
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001318 ssh = self.get_verifier_state('servo_ssh')
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001319 servo_root_present = self.get_verifier_state('servo_root_present')
Otabek Kasimov044a54b2021-05-06 19:12:18 -07001320 servo_root_present_node = self.get_repair_strategy_node(
1321 'servo_root_present')
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001322 servo_v3_present = self.get_verifier_state('servo_v3_root_present')
Otabek Kasimov79be61b2021-02-18 12:30:44 -08001323 servo_fw = self.get_verifier_state('servo_fw')
Otabek Kasimov9049ebe2021-05-06 19:19:12 -07001324 servo_fw_update = self.get_repair_strategy_node('servo_fw_update')
Otabek Kasimov1614e2e2021-01-27 22:14:36 -08001325 disk_space = self.get_verifier_state('servo_disk_space')
Otabek Kasimov3c63cbf2021-04-30 18:51:31 -07001326 start_servod = self.get_verifier_state('start_servod')
1327 servod_started = self.get_verifier_state('servod_started')
Otabek Kasimov261bf402021-05-06 19:11:09 -07001328 servod_echo = self.get_verifier_state('servod_echo')
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001329 create_servo = self.get_verifier_state('servod_connection')
1330 init_servo = self.get_verifier_state('servod_control')
Otabek Kasimov1614e2e2021-01-27 22:14:36 -08001331 cr50_low_sbu = self.get_verifier_state('servo_cr50_low_sbu')
1332 cr50_off = self.get_verifier_state('servo_cr50_off')
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07001333 servo_topology = self.get_verifier_state('servo_topology')
Otabek Kasimov1614e2e2021-01-27 22:14:36 -08001334 dut_connected = self.get_verifier_state('servo_dut_connected')
1335 hub_connected = self.get_verifier_state('servo_hub_connected')
1336 pwr_button = self.get_verifier_state('servo_pwr_button')
1337 lid_open = self.get_verifier_state('servo_lid_open')
1338 ec_board = self.get_verifier_state('servo_ec_board')
1339 cr50_console = self.get_verifier_state('servo_cr50_console')
1340 ccd_testlab = self.get_verifier_state('servo_ccd_testlab')
Otabek Kasimov15963492020-06-23 21:10:51 -07001341
1342 if not ssh:
1343 return servo_constants.SERVO_STATE_NO_SSH
Otabek Kasimov3c63cbf2021-04-30 18:51:31 -07001344 if start_servod == hosts.VERIFY_FAILED:
1345 return servo_constants.SERVO_STATE_SERVO_HOST_ISSUE
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001346 if servo_root_present == hosts.VERIFY_FAILED:
1347 if not self.servo_serial:
1348 return servo_constants.SERVO_STATE_WRONG_CONFIG
Otabek Kasimov044a54b2021-05-06 19:12:18 -07001349 if hasattr(servo_root_present_node, 'serial_mismatch'):
1350 return servo_constants.SERVO_STATE_SERIAL_MISMATCH
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001351 return servo_constants.SERVO_STATE_NOT_CONNECTED
1352 if servo_v3_present == hosts.VERIFY_FAILED:
1353 # if we cannot find required board on servo_v3
1354 return servo_constants.SERVO_STATE_NEED_REPLACEMENT
Otabek Kasimov79be61b2021-02-18 12:30:44 -08001355 if servo_fw == hosts.VERIFY_FAILED:
Otabek Kasimov9049ebe2021-05-06 19:19:12 -07001356 logging.info(servo_fw_update)
1357 if hasattr(servo_fw_update, 'servo_updater_issue_detected'):
1358 return servo_constants.SERVO_STATE_SERVO_UPDATER_ISSUE
Otabek Kasimov79be61b2021-02-18 12:30:44 -08001359 return servo_constants.SERVO_STATE_NEED_REPLACEMENT
Otabek Kasimov15963492020-06-23 21:10:51 -07001360
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -08001361 if dut_connected == hosts.VERIFY_FAILED:
1362 return servo_constants.SERVO_STATE_DUT_NOT_CONNECTED
1363 if hub_connected == hosts.VERIFY_FAILED:
1364 logging.info('Servo HUB not connected')
1365 return servo_constants.SERVO_STATE_DUT_NOT_CONNECTED
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001366
Otabek Kasimov8e88a742021-01-11 18:03:13 -08001367 if cr50_low_sbu == hosts.VERIFY_FAILED:
1368 return servo_constants.SERVO_STATE_SBU_LOW_VOLTAGE
1369 if cr50_off == hosts.VERIFY_FAILED:
1370 return servo_constants.SERVO_STATE_CR50_NOT_ENUMERATED
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001371
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07001372 if servo_topology == hosts.VERIFY_FAILED:
1373 return servo_constants.SERVO_STATE_TOPOLOGY_ISSUE
1374
Otabek Kasimovd5065bd2020-11-23 23:32:36 -08001375 # TODO(otabek@): detect special cases detected by pwr_button
1376 if dut_connected == hosts.VERIFY_SUCCESS:
1377 if pwr_button == hosts.VERIFY_FAILED:
1378 metrics.Counter(
1379 'chromeos/autotest/repair/servo_unexpected/pwr_button2'
1380 ).increment(fields=self._get_host_metrics_data())
Otabek Kasimova7eb4dc2020-09-16 10:25:17 -07001381
Otabek Kasimov261bf402021-05-06 19:11:09 -07001382 if (servod_started == hosts.VERIFY_FAILED
1383 or servod_echo == hosts.VERIFY_FAILED):
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001384 return servo_constants.SERVO_STATE_SERVOD_ISSUE
1385
Otabek Kasimov15963492020-06-23 21:10:51 -07001386 # one of the reason why servo can not initialized
Otabek Kasimovbb3bc462020-11-03 16:40:33 -08001387 if cr50_console == hosts.VERIFY_FAILED:
1388 return servo_constants.SERVO_STATE_CR50_CONSOLE_MISSING
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001389 if ccd_testlab == hosts.VERIFY_FAILED:
Otabek Kasimov15963492020-06-23 21:10:51 -07001390 return servo_constants.SERVO_STATE_CCD_TESTLAB_ISSUE
1391
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001392 if (create_servo == hosts.VERIFY_FAILED
1393 or init_servo == hosts.VERIFY_FAILED):
Otabek Kasimov261bf402021-05-06 19:11:09 -07001394 return servo_constants.SERVO_STATE_SERVOD_PROXY_ISSUE
Otabek Kasimov15963492020-06-23 21:10:51 -07001395
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001396 if ec_board == hosts.VERIFY_FAILED:
Otabek Kasimov015c15c2020-08-20 00:40:42 -07001397 return servo_constants.SERVO_STATE_EC_BROKEN
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001398 if pwr_button == hosts.VERIFY_FAILED:
Otabek Kasimov15963492020-06-23 21:10:51 -07001399 return servo_constants.SERVO_STATE_BAD_RIBBON_CABLE
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001400 if lid_open == hosts.VERIFY_FAILED:
Otabek Kasimov15963492020-06-23 21:10:51 -07001401 return servo_constants.SERVO_STATE_LID_OPEN_FAILED
Otabek Kasimov15963492020-06-23 21:10:51 -07001402
Otabek Kasimov15963492020-06-23 21:10:51 -07001403 metrics.Counter(
1404 'chromeos/autotest/repair/unknown_servo_state'
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001405 ).increment(fields=self._get_host_metrics_data())
Otabek Kasimov15963492020-06-23 21:10:51 -07001406 logging.info('We do not have special state for this failure yet :)')
1407 return servo_constants.SERVO_STATE_BROKEN
1408
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07001409 def is_servo_topology_supported(self):
1410 """Check if servo_topology is supported."""
Otabek Kasimovda994012020-11-25 15:23:04 -08001411 if not self.is_up_fast():
1412 logging.info('Servo-Host is not reachable.')
1413 return False
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07001414 if not self.is_labstation():
1415 logging.info('Servo-topology supported only for labstation.')
1416 return False
1417 if not self.servo_serial:
1418 logging.info('Servo-topology required a servo serial.')
1419 return False
1420 return True
1421
1422 def get_topology(self):
1423 """Get servo topology."""
Otabek Kasimovfe41e2d2021-02-14 20:48:52 -08001424 if not self._topology:
1425 self._topology = servo_topology.ServoTopology(self)
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07001426 return self._topology
1427
1428 def is_dual_setup(self):
1429 """Check is servo will run in dual setup.
1430
1431 Dual setup used only for servo_v4 when used ccd_cr50 and servo_micro
1432 at the same time.
1433 """
1434 return self.servo_setup == servo_constants.SERVO_SETUP_VALUE_DUAL_V4
1435
Otabek Kasimov39637412020-11-23 19:09:27 -08001436 def set_dut_health_profile(self, dut_health_profile):
1437 """
1438 @param dut_health_profile: A DeviceHealthProfile object.
1439 """
1440 logging.debug('setting dut_health_profile field to (%s)',
1441 dut_health_profile)
1442 self._dut_health_profile = dut_health_profile
1443
1444 def get_dut_health_profile(self):
1445 """
1446 @return A DeviceHealthProfile object.
1447 """
1448 return self._dut_health_profile
1449
Otabek Kasimov51ed19a2021-05-03 12:30:50 -07001450 def print_all_servo_of_host(self):
1451 """Print all servos detected on the host."""
1452 try:
1453 logging.info('\tDevices detected on the host:')
1454 devices = self.get_topology().get_list_available_servos()
1455 for device in devices:
1456 logging.info('\t%s', device)
1457 except Exception as e:
1458 logging.debug('(Not critical) Fail list all servos: %s', e)
1459
Otabek Kasimovcc9738e2020-02-14 16:17:15 -08001460
Richard Barnetteea3e4602016-06-10 12:36:41 -07001461def make_servo_hostname(dut_hostname):
1462 """Given a DUT's hostname, return the hostname of its servo.
1463
1464 @param dut_hostname: hostname of a DUT.
1465
1466 @return hostname of the DUT's servo.
1467
1468 """
1469 host_parts = dut_hostname.split('.')
1470 host_parts[0] = host_parts[0] + '-servo'
1471 return '.'.join(host_parts)
1472
1473
Richard Barnettee519dcd2016-08-15 17:37:17 -07001474def _map_afe_board_to_servo_board(afe_board):
1475 """Map a board we get from the AFE to a servo appropriate value.
1476
1477 Many boards are identical to other boards for servo's purposes.
1478 This function makes that mapping.
1479
1480 @param afe_board string board name received from AFE.
1481 @return board we expect servo to have.
1482
1483 """
1484 KNOWN_SUFFIXES = ['-freon', '_freon', '_moblab', '-cheets']
1485 BOARD_MAP = {'gizmo': 'panther'}
1486 mapped_board = afe_board
1487 if afe_board in BOARD_MAP:
1488 mapped_board = BOARD_MAP[afe_board]
1489 else:
1490 for suffix in KNOWN_SUFFIXES:
1491 if afe_board.endswith(suffix):
1492 mapped_board = afe_board[0:-len(suffix)]
1493 break
1494 if mapped_board != afe_board:
1495 logging.info('Mapping AFE board=%s to %s', afe_board, mapped_board)
1496 return mapped_board
1497
1498
Prathmesh Prabhub4810232018-09-07 13:24:08 -07001499def get_servo_args_for_host(dut_host):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -07001500 """Return servo data associated with a given DUT.
Richard Barnetteea3e4602016-06-10 12:36:41 -07001501
Richard Barnetteea3e4602016-06-10 12:36:41 -07001502 @param dut_host Instance of `Host` on which to find the servo
1503 attributes.
Prathmesh Prabhuf605dd32018-08-28 17:09:04 -07001504 @return `servo_args` dict with host and an optional port.
Richard Barnetteea3e4602016-06-10 12:36:41 -07001505 """
Prathmesh Prabhucba44292018-08-28 17:44:45 -07001506 info = dut_host.host_info_store.get()
Derek Beckettf73baca2020-08-19 15:08:47 -07001507 servo_args = {k: v for k, v in six.iteritems(info.attributes)
Garry Wang11b5e872020-03-11 15:14:08 -07001508 if k in servo_constants.SERVO_ATTR_KEYS}
Richard Barnetteea3e4602016-06-10 12:36:41 -07001509
Andrew Luo4be621d2020-03-21 07:01:13 -07001510 if servo_constants.SERVO_HOST_SSH_PORT_ATTR in servo_args:
1511 try:
1512 servo_args[servo_constants.SERVO_HOST_SSH_PORT_ATTR] = int(
1513 servo_args[servo_constants.SERVO_HOST_SSH_PORT_ATTR])
1514 except ValueError:
1515 logging.error('servo host port is not an int: %s',
1516 servo_args[servo_constants.SERVO_HOST_SSH_PORT_ATTR])
1517 # Reset servo_args because we don't want to use an invalid port.
1518 servo_args.pop(servo_constants.SERVO_HOST_SSH_PORT_ATTR, None)
1519
Garry Wang11b5e872020-03-11 15:14:08 -07001520 if servo_constants.SERVO_PORT_ATTR in servo_args:
Prathmesh Prabhucba44292018-08-28 17:44:45 -07001521 try:
Garry Wang11b5e872020-03-11 15:14:08 -07001522 servo_args[servo_constants.SERVO_PORT_ATTR] = int(
1523 servo_args[servo_constants.SERVO_PORT_ATTR])
Prathmesh Prabhucba44292018-08-28 17:44:45 -07001524 except ValueError:
1525 logging.error('servo port is not an int: %s',
Garry Wang11b5e872020-03-11 15:14:08 -07001526 servo_args[servo_constants.SERVO_PORT_ATTR])
Prathmesh Prabhucba44292018-08-28 17:44:45 -07001527 # Reset servo_args because we don't want to use an invalid port.
Garry Wang11b5e872020-03-11 15:14:08 -07001528 servo_args.pop(servo_constants.SERVO_HOST_ATTR, None)
Prathmesh Prabhucba44292018-08-28 17:44:45 -07001529
1530 if info.board:
Garry Wang11b5e872020-03-11 15:14:08 -07001531 servo_board = _map_afe_board_to_servo_board(info.board)
1532 servo_args[servo_constants.SERVO_BOARD_ATTR] = servo_board
Nick Sanders2f3c9852018-10-24 12:10:24 -07001533 if info.model:
Garry Wang11b5e872020-03-11 15:14:08 -07001534 servo_args[servo_constants.SERVO_MODEL_ATTR] = info.model
1535 return servo_args if servo_constants.SERVO_HOST_ATTR in servo_args else None
Richard Barnetteea3e4602016-06-10 12:36:41 -07001536
1537
Prathmesh Prabhuefb1b482018-08-28 17:15:05 -07001538def _tweak_args_for_ssp_moblab(servo_args):
Garry Wang11b5e872020-03-11 15:14:08 -07001539 if (servo_args[servo_constants.SERVO_HOST_ATTR]
1540 in ['localhost', '127.0.0.1']):
1541 servo_args[servo_constants.SERVO_HOST_ATTR] = _CONFIG.get_config_value(
Prathmesh Prabhuefb1b482018-08-28 17:15:05 -07001542 'SSP', 'host_container_ip', type=str, default=None)
1543
1544
Otabek Kasimov39637412020-11-23 19:09:27 -08001545def create_servo_host(dut,
1546 servo_args,
1547 try_lab_servo=False,
1548 try_servo_repair=False,
Otabek Kasimovcdcf1ee2021-03-10 12:10:10 -08001549 try_servo_recovery=True,
Otabek Kasimov39637412020-11-23 19:09:27 -08001550 dut_host_info=None,
1551 dut_health_profile=None):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -07001552 """Create a ServoHost object for a given DUT, if appropriate.
Dan Shi4d478522014-02-14 13:46:32 -08001553
Richard Barnette9a26ad62016-06-10 12:03:08 -07001554 This function attempts to create and verify or repair a `ServoHost`
1555 object for a servo connected to the given `dut`, subject to various
1556 constraints imposed by the parameters:
1557 * When the `servo_args` parameter is not `None`, a servo
1558 host must be created, and must be checked with `repair()`.
1559 * Otherwise, if a servo exists in the lab and `try_lab_servo` is
1560 true:
1561 * If `try_servo_repair` is true, then create a servo host and
1562 check it with `repair()`.
1563 * Otherwise, if the servo responds to `ping` then create a
1564 servo host and check it with `verify()`.
Fang Denge545abb2014-12-30 18:43:47 -08001565
Richard Barnette9a26ad62016-06-10 12:03:08 -07001566 In cases where `servo_args` was not `None`, repair failure
1567 exceptions are passed back to the caller; otherwise, exceptions
Richard Barnette07c2e1d2016-10-26 14:24:28 -07001568 are logged and then discarded. Note that this only happens in cases
1569 where we're called from a test (not special task) control file that
1570 has an explicit dependency on servo. In that case, we require that
1571 repair not write to `status.log`, so as to avoid polluting test
1572 results.
1573
1574 TODO(jrbarnette): The special handling for servo in test control
1575 files is a thorn in my flesh; I dearly hope to see it cut out before
1576 my retirement.
Richard Barnette9a26ad62016-06-10 12:03:08 -07001577
1578 Parameters for a servo host consist of a host name, port number, and
1579 DUT board, and are determined from one of these sources, in order of
1580 priority:
Richard Barnetteea3e4602016-06-10 12:36:41 -07001581 * Servo attributes from the `dut` parameter take precedence over
1582 all other sources of information.
1583 * If a DNS entry for the servo based on the DUT hostname exists in
1584 the CrOS lab network, that hostname is used with the default
Richard Barnette9a26ad62016-06-10 12:03:08 -07001585 port and the DUT's board.
Richard Barnetteea3e4602016-06-10 12:36:41 -07001586 * If no other options are found, the parameters will be taken
Richard Barnette9a26ad62016-06-10 12:03:08 -07001587 from the `servo_args` dict passed in from the caller.
Richard Barnetteea3e4602016-06-10 12:36:41 -07001588
Otabek Kasimovcdcf1ee2021-03-10 12:10:10 -08001589 @param dut: An instance of `Host` from which to take
1590 servo parameters (if available).
1591 @param servo_args: A dictionary with servo parameters to use if
1592 they can't be found from `dut`. If this
1593 argument is supplied, unrepaired exceptions
1594 from `verify()` will be passed back to the
1595 caller.
1596 @param try_lab_servo: If not true, servo host creation will be
1597 skipped unless otherwise required by the
1598 caller.
1599 @param try_servo_repair: If true, check a servo host with
1600 `repair()` instead of `verify()`.
1601 @param try_servo_recovery: If true, start servod in recovery mode.
1602 Default value is True.
1603 @param dut_host_info: A HostInfo object of the DUT that connected
1604 to this servo.
1605 @param dut_health_profile: DUT repair info with history.
Dan Shi4d478522014-02-14 13:46:32 -08001606
1607 @returns: A ServoHost object or None. See comments above.
1608
1609 """
Garry Wang2d23a892021-04-02 22:33:56 -07001610 # We are explicitly looking for if servo_args is None here(which means
1611 # servo not needed), as servo_args == {} means servo is needed and
1612 # we expect load servo_args from host_info_store.
1613 if servo_args is None:
1614 servo_dependency = False
1615 local_run = False
1616 else:
1617 servo_dependency = True
1618 # If servo_args pass in directly, then this is a local test run.
1619 local_run = servo_constants.SERVO_HOST_ATTR in servo_args
1620
1621 if local_run:
1622 logging.warning('User input servo_args detected, will attempt'
1623 ' to start servod and initialize servo conncetion'
1624 ' directly. All servo/servohost verify and repair'
1625 ' steps will be skipped.')
1626
1627 # Loading servo args from host_info_store.
Richard Barnette07c2e1d2016-10-26 14:24:28 -07001628 if dut is not None and (try_lab_servo or servo_dependency):
Prathmesh Prabhub4810232018-09-07 13:24:08 -07001629 servo_args_override = get_servo_args_for_host(dut)
Richard Barnetteea3e4602016-06-10 12:36:41 -07001630 if servo_args_override is not None:
Prathmesh Prabhuefb1b482018-08-28 17:15:05 -07001631 if utils.in_moblab_ssp():
1632 _tweak_args_for_ssp_moblab(servo_args_override)
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -07001633 logging.debug(
1634 'Overriding provided servo_args (%s) with arguments'
1635 ' determined from the host (%s)',
1636 servo_args,
1637 servo_args_override,
1638 )
Richard Barnetteea3e4602016-06-10 12:36:41 -07001639 servo_args = servo_args_override
Prathmesh Prabhucba44292018-08-28 17:44:45 -07001640
Garry Wang2d23a892021-04-02 22:33:56 -07001641 if not servo_args:
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -07001642 logging.debug('No servo_args provided, and failed to find overrides.')
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001643 if try_lab_servo or servo_dependency:
Otabek Kasimov646812c2020-06-23 20:01:36 -07001644 return None, servo_constants.SERVO_STATE_MISSING_CONFIG
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001645 else:
1646 # For regular test case which not required the servo
1647 return None, None
1648
Garry Wang11b5e872020-03-11 15:14:08 -07001649 servo_hostname = servo_args.get(servo_constants.SERVO_HOST_ATTR)
1650 servo_port = servo_args.get(servo_constants.SERVO_PORT_ATTR)
Garry Wang2d23a892021-04-02 22:33:56 -07001651 if not local_run:
1652 if not _is_servo_host_information_exist(servo_hostname, servo_port):
1653 logging.debug(
1654 'Servo connection info missed hostname: %s , port: %s',
1655 servo_hostname, servo_port)
1656 return None, servo_constants.SERVO_STATE_MISSING_CONFIG
1657 if not is_servo_host_information_valid(servo_hostname, servo_port):
1658 logging.debug(
1659 'Servo connection info is incorrect hostname: %s , port: %s',
1660 servo_hostname, servo_port)
1661 return None, servo_constants.SERVO_STATE_WRONG_CONFIG
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -07001662
Garry Wang2d23a892021-04-02 22:33:56 -07001663 if try_servo_recovery == True:
1664 servo_args[servo_constants.SERVO_RECOVERY_MODE] = True
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -08001665
Garry Wangebc015b2019-06-06 17:45:06 -07001666 newhost = ServoHost(**servo_args)
Garry Wang2d23a892021-04-02 22:33:56 -07001667 if local_run:
1668 try:
1669 newhost.start_servod()
1670 except:
1671 # If we failed to start servod here, we can assume the servod
1672 # either already started or the test is running against a
1673 # non-standard servohost so the user will resiponsble for ensure
1674 # servod is running.
1675 pass
1676 try:
Greg Edelstonff2665d2021-04-21 14:32:27 -06001677 newhost.initialize_servo()
Garry Wang2d23a892021-04-02 22:33:56 -07001678 newhost.initialize_dut_for_servo()
1679 newhost._servo_state = servo_constants.SERVO_STATE_WORKING
1680 return newhost, newhost.get_servo_state()
1681 except Exception as e:
1682 logging.error('Failed to initialize servo. %s', e)
1683 return None, servo_constants.SERVO_STATE_BROKEN
1684
Andrew Luo4be621d2020-03-21 07:01:13 -07001685 if newhost.use_icmp and not newhost.is_up_fast(count=3):
Otabek Kasimov32cafe92020-12-14 16:58:12 -08001686 # ServoHost has internal check to wait if servo-host is in reboot
1687 # process. If servo-host still is not available this check will stop
1688 # further attempts as we do not have any option to recover servo_host.
Otabek Kasimov39637412020-11-23 19:09:27 -08001689 return None, servo_constants.SERVO_STATE_NO_SSH
Garry Wangffbd2162020-04-17 16:13:48 -07001690
Otabek Kasimove6df8102020-07-21 20:15:25 -07001691 # Reset or reboot servo device only during AdminRepair tasks.
1692 if try_servo_repair:
1693 if newhost._is_locked:
Otabek Kasimov51ed19a2021-05-03 12:30:50 -07001694 # Print available servos on the host for debugging.
1695 newhost.print_all_servo_of_host()
Otabek Kasimove6df8102020-07-21 20:15:25 -07001696 # Reset servo if the servo is locked, as we check if the servohost
1697 # is up, if the servohost is labstation and if the servohost is in
1698 # lab inside the locking logic.
1699 newhost.reset_servo()
1700 else:
Garry Wang358aad42020-08-02 20:56:04 -07001701 try:
1702 newhost.reboot_servo_v3_on_need()
Garry Wang1f0d5332020-08-10 19:32:32 -07001703 except Exception as e:
1704 logging.info('[Non-critical] Unexpected error while trying to'
1705 ' reboot servo_v3, skipping the reboot; %s', e)
Otabek Kasimove6df8102020-07-21 20:15:25 -07001706
Otabek Kasimov2b50cdb2020-07-06 19:16:06 -07001707 if dut:
1708 newhost.set_dut_hostname(dut.hostname)
Otabek Kasimov9e90ae12020-08-14 03:01:19 -07001709 if dut_host_info:
1710 newhost.set_dut_host_info(dut_host_info)
Otabek Kasimov39637412020-11-23 19:09:27 -08001711 if dut_health_profile and (try_lab_servo or try_servo_repair):
1712 try:
1713 if newhost.is_localhost():
1714 logging.info('Servohost is a localhost, skip device'
1715 ' health profile setup...')
1716 else:
1717 dut_health_profile.init_profile(newhost)
1718 newhost.set_dut_health_profile(dut_health_profile)
1719 except Exception as e:
1720 logging.info(
1721 '[Non-critical] Unexpected error while trying to'
1722 ' load device health profile; %s', e)
Garry Wangffbd2162020-04-17 16:13:48 -07001723
Richard Barnette9a26ad62016-06-10 12:03:08 -07001724 # Note that the logic of repair() includes everything done
1725 # by verify(). It's sufficient to call one or the other;
1726 # we don't need both.
Richard Barnette07c2e1d2016-10-26 14:24:28 -07001727 if servo_dependency:
1728 newhost.repair(silent=True)
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001729 return newhost, newhost.get_servo_state()
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -07001730
1731 if try_servo_repair:
1732 try:
1733 newhost.repair()
1734 except Exception:
1735 logging.exception('servo repair failed for %s', newhost.hostname)
Richard Barnette9a26ad62016-06-10 12:03:08 -07001736 else:
1737 try:
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -07001738 newhost.verify()
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -07001739 except Exception:
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -07001740 logging.exception('servo verify failed for %s', newhost.hostname)
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001741 return newhost, newhost.get_servo_state()
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001742
1743
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001744def _is_servo_host_information_exist(hostname, port):
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001745 if hostname is None or len(hostname.strip()) == 0:
1746 return False
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001747 if port is None:
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001748 return False
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001749 if not type(port) is int:
1750 try:
1751 int(port)
1752 except ValueError:
1753 return False
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001754 return True
1755
1756
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001757def is_servo_host_information_valid(hostname, port):
Garry Wang9b8f2342020-04-17 16:34:09 -07001758 """Check if provided servo attributes are valid.
1759
1760 @param hostname Hostname of the servohost.
1761 @param port servo port number.
1762
1763 @returns: A bool value to indicate if provided servo attribute valid.
1764 """
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001765 if not _is_servo_host_information_exist(hostname, port):
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001766 return False
1767 # checking range and correct of the port
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001768 port_int = int(port)
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001769 if port_int < 1 or port_int > 65000:
1770 return False
1771 # we expecting host contain only latters, digits and '-' or '_'
Garry Wang536d2852021-03-28 22:33:40 -07001772 if not re.match('[a-zA-Z0-9-_\.:]*$', hostname) or len(hostname) < 5:
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001773 return False
1774 return True