blob: 69dcd36c3e7f5063a7f8b75f92e2bb3d746b7306 [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
Richard Barnette9a26ad62016-06-10 12:03:08 -070032from autotest_lib.server.cros.servo import servo
Richard Barnetted31580e2018-05-14 19:58:00 +000033from autotest_lib.server.hosts import servo_repair
Garry Wangebc015b2019-06-06 17:45:06 -070034from autotest_lib.server.hosts import base_servohost
Garry Wang11b5e872020-03-11 15:14:08 -070035from autotest_lib.server.hosts import servo_constants
Otabek Kasimov4ea636e2020-04-14 23:35:06 -070036from autotest_lib.server.cros.faft.utils import config
Garry Wang11b5e872020-03-11 15:14:08 -070037from autotest_lib.client.common_lib import global_config
Otabek Kasimov382c3bb2020-10-28 13:22:45 -070038from autotest_lib.server.cros.servo.topology import servo_topology
Garry Wangd7367482020-02-27 13:52:40 -080039
Otabek Kasimov15963492020-06-23 21:10:51 -070040try:
Mike Frysinger714c5b02020-09-04 23:22:54 -040041 from autotest_lib.utils.frozen_chromite.lib import metrics
Otabek Kasimov15963492020-06-23 21:10:51 -070042except ImportError:
43 metrics = utils.metrics_mock
44
Dan Shi3b2adf62015-09-02 17:46:54 -070045_CONFIG = global_config.global_config
Fang Deng5d518f42013-08-02 14:04:32 -070046
Otabek Kasimova7ba91a2020-03-09 08:31:01 -070047
Garry Wangebc015b2019-06-06 17:45:06 -070048class ServoHost(base_servohost.BaseServoHost):
49 """Host class for a servo host(e.g. beaglebone, labstation)
Dana Goyette0b6e6402019-10-04 11:09:24 -070050 that with a servo instance for a specific port.
51
52 @type _servo: servo.Servo | None
53 """
Fang Deng5d518f42013-08-02 14:04:32 -070054
Raul E Rangel52ca2e82018-07-03 14:10:14 -060055 DEFAULT_PORT = int(os.getenv('SERVOD_PORT', '9999'))
Richard Barnette9a26ad62016-06-10 12:03:08 -070056
Dan Shie5b3c512014-08-21 12:12:09 -070057 # Timeout for initializing servo signals.
Wai-Hong Tam37b6ed32017-09-19 15:52:39 -070058 INITIALIZE_SERVO_TIMEOUT_SECS = 60
Richard Barnette9a26ad62016-06-10 12:03:08 -070059
Otabek Kasimov545739c2020-08-20 00:24:21 -070060 # Default timeout for run terminal command.
61 DEFAULT_TERMINAL_TIMEOUT = 30
62
xixuan6cf6d2f2016-01-29 15:29:00 -080063 # Ready test function
64 SERVO_READY_METHOD = 'get_version'
Fang Deng5d518f42013-08-02 14:04:32 -070065
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -080066 # Directory prefix on the servo host where the servod logs are stored.
67 SERVOD_LOG_PREFIX = '/var/log/servod'
68
69 # Exit code to use when symlinks for servod logs are not found.
70 NO_SYMLINKS_CODE = 9
71
72 # Directory in the job's results directory to dump the logs into.
73 LOG_DIR = 'servod'
74
75 # Prefix for joint loglevel files in the logs.
76 JOINT_LOG_PREFIX = 'log'
77
78 # Regex group to extract timestamp from logfile name.
79 TS_GROUP = 'ts'
80
81 # This regex is used to extract the timestamp from servod logs.
Garry Wang22f2e842020-09-09 20:19:19 -070082 # files always start with log.
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -080083 TS_RE = (r'log.'
84 # The timestamp is of format %Y-%m-%d--%H-%M-%S.MS
85 r'(?P<%s>\d{4}(\-\d{2}){2}\-(-\d{2}){3}.\d{3})'
86 # The loglevel is optional depending on labstation version.
87 r'(.(INFO|DEBUG|WARNING))?' % TS_GROUP)
88 TS_EXTRACTOR = re.compile(TS_RE)
89
90 # Regex group to extract MCU name from logline in servod logs.
91 MCU_GROUP = 'mcu'
92
93 # Regex group to extract logline from MCU logline in servod logs.
94 LINE_GROUP = 'line'
95
96 # This regex is used to extract the mcu and the line content from an
97 # MCU logline in servod logs. e.g. EC or servo_v4 console logs.
98 # Here is an example log-line:
99 #
100 # 2020-01-23 13:15:12,223 - servo_v4 - EC3PO.Console - DEBUG -
101 # console.py:219:LogConsoleOutput - /dev/pts/9 - cc polarity: cc1
102 #
103 # Here is conceptually how they are formatted:
104 #
105 # <time> - <MCU> - EC3PO.Console - <LVL> - <file:line:func> - <pts> -
106 # <output>
107 #
Garry Wang22f2e842020-09-09 20:19:19 -0700108 # The log format starts with a timestamp
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800109 MCU_RE = (r'[\d\-]+ [\d:,]+ '
110 # The mcu that is logging this is next.
111 r'- (?P<%s>\w+) - '
112 # Next, we have more log outputs before the actual line.
113 # Information about the file line, logging function etc.
114 # Anchor on EC3PO Console, LogConsoleOutput and dev/pts.
115 # NOTE: if the log format changes, this regex needs to be
116 # adjusted.
117 r'EC3PO\.Console[\s\-\w\d:.]+LogConsoleOutput - /dev/pts/\d+ - '
118 # Lastly, we get the MCU's console line.
119 r'(?P<%s>.+$)' % (MCU_GROUP, LINE_GROUP))
120 MCU_EXTRACTOR = re.compile(MCU_RE)
121
Otabek Kasimov545739c2020-08-20 00:24:21 -0700122 # Regex to detect timeout messages when USBC pigtail has timeout issue.
123 # e.g.: [475635.427072 PD TMOUT RX 1/1]
124 USBC_PIGTAIL_TIMEOUT_RE = r'\[[\d \.]{1,20}(PD TMOUT RX 1\/1)\]'
125
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800126 # Suffix to identify compressed logfiles.
127 COMPRESSION_SUFFIX = '.tbz2'
128
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700129 # A suffix to mark servod log directories that came from instance that
130 # ran during this servo_host, but are not the last one running e.g. when
131 # an instance (on purpose, or due to a bug) restarted in the middle of the
132 # run.
133 OLD_LOG_SUFFIX = 'old'
134
Otabek Kasimovcc9738e2020-02-14 16:17:15 -0800135 def _init_attributes(self):
136 self._servo_state = None
137 self.servo_port = None
138 self.servo_board = None
139 self.servo_model = None
140 self.servo_serial = None
Garry Wangcb06f3b2020-10-08 20:56:21 -0700141 self.servo_setup = None
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800142 self.servo_recovery = None
Otabek Kasimovab4ae222021-03-06 01:38:47 -0800143 self.servo_fw_channel = None
Garry Wang6a680062020-11-03 13:40:29 -0800144 self.additional_servod_args = None
Otabek Kasimov39637412020-11-23 19:09:27 -0800145 self._dut_health_profile = None
Garry Wang000c6c02020-05-11 21:27:23 -0700146 # The flag that indicate if a servo is connected to a smart usbhub.
147 # TODO(xianuowang@) remove this flag once all usbhubs in the lab
148 # get replaced.
149 self.smart_usbhub = None
Otabek Kasimovcc9738e2020-02-14 16:17:15 -0800150 self._servo = None
Otabek Kasimov382c3bb2020-10-28 13:22:45 -0700151 self._topology = None
Andrew McRaef0679932020-08-13 09:15:23 +1000152 self._tunnel_proxy = None
153 self._tunnel_proxy_lock = threading.Lock()
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700154 self._initial_instance_ts = None
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800155 # Flag to make sure that multiple calls to close do not result in the
156 # logic executing multiple times.
157 self._closed = False
Andrew McRaef0679932020-08-13 09:15:23 +1000158 # Per-thread local data
159 self._local = threading.local()
Fang Deng5d518f42013-08-02 14:04:32 -0700160
Garry Wangcb06f3b2020-10-08 20:56:21 -0700161 def _initialize(self,
162 servo_host='localhost',
163 servo_port=DEFAULT_PORT,
164 servo_board=None,
165 servo_model=None,
166 servo_serial=None,
167 servo_setup=None,
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800168 servo_recovery=None,
Otabek Kasimovab4ae222021-03-06 01:38:47 -0800169 servo_fw_channel=None,
Garry Wang6a680062020-11-03 13:40:29 -0800170 additional_servod_args=None,
Garry Wangcb06f3b2020-10-08 20:56:21 -0700171 is_in_lab=None,
172 *args,
173 **dargs):
Fang Deng5d518f42013-08-02 14:04:32 -0700174 """Initialize a ServoHost instance.
175
176 A ServoHost instance represents a host that controls a servo.
177
178 @param servo_host: Name of the host where the servod process
179 is running.
Raul E Rangel52ca2e82018-07-03 14:10:14 -0600180 @param servo_port: Port the servod process is listening on. Defaults
181 to the SERVOD_PORT environment variable if set,
182 otherwise 9999.
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -0700183 @param servo_board: Board that the servo is connected to.
Nick Sanders2f3c9852018-10-24 12:10:24 -0700184 @param servo_model: Model that the servo is connected to.
Garry Wangcb06f3b2020-10-08 20:56:21 -0700185 @param servo_serial: Serial number of the servo device.
186 @param servo_setup: Type of servo setup, e.g. REGULAR or DUAL_V4.
Garry Wang6a680062020-11-03 13:40:29 -0800187 @param additional_servod_args: Additional args that will append to
188 servod start command.
Dan Shi4d478522014-02-14 13:46:32 -0800189 @param is_in_lab: True if the servo host is in Cros Lab. Default is set
190 to None, for which utils.host_is_in_lab_zone will be
191 called to check if the servo host is in Cros lab.
Fang Deng5d518f42013-08-02 14:04:32 -0700192
193 """
194 super(ServoHost, self)._initialize(hostname=servo_host,
Garry Wangebc015b2019-06-06 17:45:06 -0700195 is_in_lab=is_in_lab, *args, **dargs)
Otabek Kasimovcc9738e2020-02-14 16:17:15 -0800196 self._init_attributes()
Richard Barnette42f4db92018-08-23 15:05:15 -0700197 self.servo_port = int(servo_port)
Richard Barnettee519dcd2016-08-15 17:37:17 -0700198 self.servo_board = servo_board
Nick Sanders2f3c9852018-10-24 12:10:24 -0700199 self.servo_model = servo_model
Kevin Cheng643ce8a2016-09-15 15:42:12 -0700200 self.servo_serial = servo_serial
Garry Wangcb06f3b2020-10-08 20:56:21 -0700201 self.servo_setup = servo_setup
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800202 self.servo_recovery = servo_recovery
Otabek Kasimovab4ae222021-03-06 01:38:47 -0800203 self.servo_fw_channel = servo_fw_channel
Garry Wang6a680062020-11-03 13:40:29 -0800204 self.additional_servod_args = additional_servod_args
Wai-Hong Tam3a8a2552019-11-19 14:28:04 +0800205
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800206 # The location of the log files on the servo host for this instance.
207 self.remote_log_dir = '%s_%s' % (self.SERVOD_LOG_PREFIX,
208 self.servo_port)
Garry Wang79e9af62019-06-12 15:19:19 -0700209 # Path of the servo host lock file.
Derek Beckettf73baca2020-08-19 15:08:47 -0700210 self._lock_file = (self.TEMP_FILE_DIR + str(self.servo_port) +
211 self.LOCK_FILE_POSTFIX)
Garry Wang79e9af62019-06-12 15:19:19 -0700212 # File path to declare a reboot request.
Derek Beckettf73baca2020-08-19 15:08:47 -0700213 self._reboot_file = (self.TEMP_FILE_DIR + str(self.servo_port) +
214 self.REBOOT_FILE_POSTFIX)
Garry Wang79e9af62019-06-12 15:19:19 -0700215
216 # Lock the servo host if it's an in-lab labstation to prevent other
217 # task to reboot it until current task completes. We also wait and
218 # make sure the labstation is up here, in the case of the labstation is
219 # in the middle of reboot.
Garry Wang7c00b0f2019-06-25 17:28:17 -0700220 self._is_locked = False
Garry Wang42b4d862019-06-25 15:50:49 -0700221 if (self.wait_up(self.REBOOT_TIMEOUT) and self.is_in_lab()
222 and self.is_labstation()):
Garry Wang79e9af62019-06-12 15:19:19 -0700223 self._lock()
Garry Wang78ce64d2020-10-13 18:23:45 -0700224 try:
225 self.wait_ready()
226 except Exception as e:
227 logging.info(
228 'Unexpected error while ensure labstation'
229 ' readiness; %s', str(e))
Garry Wangebc015b2019-06-06 17:45:06 -0700230
Richard Barnette9a26ad62016-06-10 12:03:08 -0700231 self._repair_strategy = (
232 servo_repair.create_servo_repair_strategy())
Richard Barnettee519dcd2016-08-15 17:37:17 -0700233
Dana Goyetteafa62fd2020-03-16 13:45:27 -0700234 def __str__(self):
235 return "<%s '%s:%s'>" % (
236 type(self).__name__, self.hostname, self.servo_port)
237
Richard Barnette9a26ad62016-06-10 12:03:08 -0700238 def connect_servo(self):
Garry Wang8c8dc972020-06-09 13:41:51 -0700239 """ Initialize and setup servo for later use.
240 """
Greg Edelstonff2665d2021-04-21 14:32:27 -0600241 self.initialize_servo()
Garry Wang8c8dc972020-06-09 13:41:51 -0700242 self.initialize_dut_for_servo()
243
Greg Edelstonff2665d2021-04-21 14:32:27 -0600244 def initialize_servo(self):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -0700245 """Establish a connection to the servod server on this host.
Richard Barnette9a26ad62016-06-10 12:03:08 -0700246
247 Initializes `self._servo` and then verifies that all network
248 connections are working. This will create an ssh tunnel if
249 it's required.
Garry Wang8c8dc972020-06-09 13:41:51 -0700250 """
251 self._servo = servo.Servo(servo_host=self,
252 servo_serial=self.servo_serial)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700253
Garry Wang8c8dc972020-06-09 13:41:51 -0700254 def initialize_dut_for_servo(self):
255 """This method will do some setup for dut control, e.g. setup
256 main servo_v4 device, and also testing the connection between servo
257 and DUT. As a side effect of testing the connection, all signals on
258 the target servo are reset to default values, and the USB stick is
Richard Barnette9a26ad62016-06-10 12:03:08 -0700259 set to the neutral (off) position.
260 """
Garry Wang8c8dc972020-06-09 13:41:51 -0700261 if not self._servo:
262 raise hosts.AutoservVerifyError('Servo object needs to be'
263 ' initialized before initialize'
264 ' DUT.')
Richard Barnette9a26ad62016-06-10 12:03:08 -0700265 timeout, _ = retry.timeout(
Garry Wang8c8dc972020-06-09 13:41:51 -0700266 self._servo.initialize_dut,
267 timeout_sec=self.INITIALIZE_SERVO_TIMEOUT_SECS)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700268 if timeout:
Garry Wang8c8dc972020-06-09 13:41:51 -0700269 raise hosts.AutoservVerifyError('Initialize dut for servo timed'
270 ' out.')
Richard Barnette9a26ad62016-06-10 12:03:08 -0700271
Richard Barnette9a26ad62016-06-10 12:03:08 -0700272 def disconnect_servo(self):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -0700273 """Disconnect our servo if it exists.
Richard Barnette9a26ad62016-06-10 12:03:08 -0700274
275 If we've previously successfully connected to our servo,
276 disconnect any established ssh tunnel, and set `self._servo`
277 back to `None`.
278 """
279 if self._servo:
280 # N.B. This call is safe even without a tunnel:
281 # rpc_server_tracker.disconnect() silently ignores
282 # unknown ports.
283 self.rpc_server_tracker.disconnect(self.servo_port)
284 self._servo = None
Fang Deng5d518f42013-08-02 14:04:32 -0700285
Andrew McRaef0679932020-08-13 09:15:23 +1000286 def _maybe_create_servod_ssh_tunnel_proxy(self):
287 """Create a xmlrpc proxy for use with a ssh tunnel.
288 A lock is used to safely create a singleton proxy.
289 """
290 with self._tunnel_proxy_lock:
291 if self._tunnel_proxy is None:
292 self._tunnel_proxy = self.rpc_server_tracker.xmlrpc_connect(
293 None,
294 self.servo_port,
295 ready_test_name=self.SERVO_READY_METHOD,
296 timeout_seconds=60,
297 request_timeout_seconds=3600,
298 server_desc=str(self))
299
Andrew McRaef0679932020-08-13 09:15:23 +1000300 def get_servod_server_proxy(self):
301 """Return a proxy if it exists; otherwise, create a new one.
302 A proxy can either be a ssh tunnel based proxy, or a httplib
303 based proxy.
Fang Deng5d518f42013-08-02 14:04:32 -0700304
305 @returns: An xmlrpclib.ServerProxy that is connected to the servod
306 server on the host.
Fang Deng5d518f42013-08-02 14:04:32 -0700307 """
Garry Wang11b5e872020-03-11 15:14:08 -0700308 if (servo_constants.ENABLE_SSH_TUNNEL_FOR_SERVO
309 and not self.is_localhost()):
Andrew McRaef0679932020-08-13 09:15:23 +1000310 # Check for existing ssh tunnel proxy.
311 if self._tunnel_proxy is None:
312 self._maybe_create_servod_ssh_tunnel_proxy()
313 return self._tunnel_proxy
Richard Barnette9a26ad62016-06-10 12:03:08 -0700314 else:
Andrew McRaef0679932020-08-13 09:15:23 +1000315 # xmlrpc/httplib is not thread-safe, so each thread must have its
316 # own separate proxy connection.
317 if not hasattr(self._local, "_per_thread_proxy"):
318 remote = 'http://%s:%s' % (self.hostname, self.servo_port)
Derek Beckettf73baca2020-08-19 15:08:47 -0700319 self._local._per_thread_proxy = six.moves.xmlrpc_client.ServerProxy(remote)
Andrew McRaef0679932020-08-13 09:15:23 +1000320 return self._local._per_thread_proxy
Wai-Hong Tam3a8a2552019-11-19 14:28:04 +0800321
Richard Barnette1edbb162016-11-01 11:47:50 -0700322 def verify(self, silent=False):
323 """Update the servo host and verify it's in a good state.
324
325 @param silent If true, suppress logging in `status.log`.
326 """
Richard Barnetteabbdc252018-07-26 16:57:42 -0700327 message = 'Beginning verify for servo host %s port %s serial %s'
328 message %= (self.hostname, self.servo_port, self.servo_serial)
329 self.record('INFO', None, None, message)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700330 try:
Richard Barnette1edbb162016-11-01 11:47:50 -0700331 self._repair_strategy.verify(self, silent)
Garry Wang11b5e872020-03-11 15:14:08 -0700332 self._servo_state = servo_constants.SERVO_STATE_WORKING
333 self.record('INFO', None, None,
334 'ServoHost verify set servo_state as WORKING')
Garry Wang63b8c382020-03-11 22:28:40 -0700335 except Exception as e:
Otabek Kasimov120b6fa2020-07-03 00:15:27 -0700336 if not self.is_localhost():
Otabek Kasimovc6f30412020-06-30 20:08:12 -0700337 self._servo_state = self.determine_servo_state()
338 self.record('INFO', None, None,
339 'ServoHost verify set servo_state as %s'
340 % self._servo_state)
Garry Wang63b8c382020-03-11 22:28:40 -0700341 if self._is_critical_error(e):
342 raise
Fang Deng5d518f42013-08-02 14:04:32 -0700343
Garry Wang2b5eef92020-08-21 16:23:35 -0700344 def _get_default_usbkey_mount_path(self):
345 return '/media/servo_usb/%s' % self.servo_port
Fang Deng5d518f42013-08-02 14:04:32 -0700346
Garry Wang7b0e1b72020-03-25 19:08:59 -0700347 def get_image_name_from_usbkey(self, usbkey_dev):
348 """Mount usb drive and check ChromeOS image name on it if there is
349 one. This method assumes the image_usbkey_direction is already set
350 to servo side.
351
Garry Wang4b980202020-09-24 17:00:17 -0700352 @param usbkey_dev: usbkey dev path(e.g. /dev/sdb).
Garry Wang7b0e1b72020-03-25 19:08:59 -0700353
354 @returns: image_name on the usbkey, e.g. nami-release/R82.10138.0.0,
355 or empty string if no test image detected, or unexpected
356 error occurred.
Garry Wang7b0e1b72020-03-25 19:08:59 -0700357 """
Garry Wang70e5d062020-04-03 18:01:05 -0700358 logging.info('Checking ChromeOS image name on usbkey.')
Garry Wang2b5eef92020-08-21 16:23:35 -0700359 mount_dst = self._get_default_usbkey_mount_path()
Garry Wang7b0e1b72020-03-25 19:08:59 -0700360 # Unmount if there is an existing stale mount.
Garry Wang2b5eef92020-08-21 16:23:35 -0700361 self._unmount_drive(mount_dst)
362 # ChromeOS root fs is in /dev/sdx3
363 mount_src = usbkey_dev + '3'
Garry Wang7b0e1b72020-03-25 19:08:59 -0700364 try:
Garry Wang2b5eef92020-08-21 16:23:35 -0700365 if not self._mount_drive(mount_src, mount_dst):
366 logging.debug('Unexpected error occurred on mount usb drive.')
Garry Wang7b0e1b72020-03-25 19:08:59 -0700367 return ''
Garry Wang70e5d062020-04-03 18:01:05 -0700368
369 release_content = self.run(
Garry Wang2b5eef92020-08-21 16:23:35 -0700370 'cat %s/etc/lsb-release' % mount_dst,
Garry Wang70e5d062020-04-03 18:01:05 -0700371 ignore_status=True).stdout.strip()
372
373 if not re.search(r'RELEASE_TRACK=.*test', release_content):
374 logging.info('The image on usbkey is not a test image')
375 return ''
376
377 return lsbrelease_utils.get_chromeos_release_builder_path(
378 lsb_release_content=release_content)
Garry Wang7b0e1b72020-03-25 19:08:59 -0700379 finally:
Garry Wang70e5d062020-04-03 18:01:05 -0700380 logging.debug('Image check compeleted, unmounting the usb drive.')
Garry Wang2b5eef92020-08-21 16:23:35 -0700381 self._unmount_drive(mount_dst)
Garry Wang7b0e1b72020-03-25 19:08:59 -0700382
Garry Wang2b5eef92020-08-21 16:23:35 -0700383 def _extract_firmware_image_from_usbkey(self, fw_dst):
384 """Extract firmware images from the usbkey on servo, this method
385 assumes there is already a ChromeOS test image staged on servo.
386
Garry Wang4b980202020-09-24 17:00:17 -0700387 @param fw_dst: the path that we'll copy firmware images to.
Garry Wang2b5eef92020-08-21 16:23:35 -0700388
389 @returns: a json format string of firmware manifest data.
390 """
391 usbkey_dev = self._probe_and_validate_usb_dev()
392 if not usbkey_dev:
393 raise hosts.AutoservRepairError('Unexpected error occurred when'
394 ' probe usbkey dev path, please check logs for detail.')
395
396 mount_dst = self._get_default_usbkey_mount_path()
397 # Unmount if there is an existing stale mount.
398 self._unmount_drive(mount_dst)
399 # ChromeOS root fs is in /dev/sdx3
400 mount_src = usbkey_dev + '3'
401 try:
402 if not self._mount_drive(mount_src, mount_dst):
403 raise hosts.AutoservRepairError('Failed to extract firmware'
404 ' image; Unable to mount %s.' % usbkey_dev,
405 'unable to mount usbkey')
406 updater_bin = os.path.join(mount_dst,
407 'usr/sbin/chromeos-firmwareupdate')
408 self.run('%s --unpack %s' % (updater_bin, fw_dst))
409 return self.run('%s --manifest' % updater_bin).stdout
410 finally:
411 self._unmount_drive(mount_dst)
412
413 def prepare_repair_firmware_image(self, fw_dst=None):
414 """Prepare firmware image on the servohost for auto repair process
415 to consume.
416
Garry Wang4b980202020-09-24 17:00:17 -0700417 @param fw_dst: the path that we want to store firmware image on
418 the servohost.
Garry Wang2b5eef92020-08-21 16:23:35 -0700419
420 @returns: A tuple that containes ec firmware image path and bios
421 firmware image path on the servohost, or None if type of
422 image is not available based on manifest and dut's model.
423 """
424 model = self.servo_model or self._dut_host_info.model
425 if not model:
426 raise hosts.AutoservRepairError(
427 'Could not determine DUT\'s model.',
428 'model infomation unknown')
429
430 if not fw_dst:
431 fw_dst = '/tmp/firmware_image/%s' % self.servo_port
432 # Cleanup and re-create dst path to have a fresh start.
433 self.run('rm -rf %s' % fw_dst)
434 self.run('mkdir -p %s' % fw_dst)
435
436 manifest = json.loads(self._extract_firmware_image_from_usbkey(fw_dst))
Garry Wang50b56c12020-09-24 17:26:52 -0700437 # For models that have packed $MODEL_signed variant, we want use the
438 # 'signed' variant once we get DVT devices, so try to read manifest
439 # from $MODEL_signed first.
440 build = manifest.get('%s_signed' % model) or manifest.get(model)
441 if not build:
Garry Wang2b5eef92020-08-21 16:23:35 -0700442 raise hosts.AutoservRepairError('Could not find firmware manifest'
443 ' for model:%s' % model, 'model manifest not found')
444 try:
Garry Wang50b56c12020-09-24 17:26:52 -0700445 ec_image = os.path.join(fw_dst, build['ec']['image'])
Garry Wang2b5eef92020-08-21 16:23:35 -0700446 except KeyError:
447 ec_image = None
448 try:
Garry Wang50b56c12020-09-24 17:26:52 -0700449 bios_image = os.path.join(fw_dst, build['host']['image'])
Garry Wang2b5eef92020-08-21 16:23:35 -0700450 except KeyError:
451 bios_image = None
452 if not ec_image and not bios_image:
453 raise hosts.AutoservRepairError('Could not find any firmware image'
454 ' for model:%s' % model, 'cannot find firmware image')
455 return ec_image, bios_image
Garry Wang7b0e1b72020-03-25 19:08:59 -0700456
Garry Wang4b980202020-09-24 17:00:17 -0700457 def flash_ap_firmware_via_servo(self, image):
458 """Flash AP firmware by use a provided image.
459
460 This is will be a short term enhanment for infra repair use, it use
461 'futility update' which will automatically determine various parameters
462 needed for flashrom, and will preserve the GBB, VPD, and HWID for
463 AP firmware update.
464 @TODO(xianuowang@) Remove this method once b/148403277 implemented.
465
466 @param image: the firmware image path on servohost.
467 """
468 cmd = 'futility update -i %s --servo_port=%s'
469 self.run(cmd % (image, self.servo_port), timeout=900)
470
Garry Wang70e5d062020-04-03 18:01:05 -0700471 def _probe_and_validate_usb_dev(self):
472 """This method probe the usb dev path by talking to servo, and then
473 validate the dev path is valid block device to servohost.
474 Possible output:
475 1. Encounter error during probe usb dev, returns empty string.
476 2. probe usb dev completed without error but cannot find usb dev,
477 raise AutoservRepairError.
478 3. probe usb dev find a usb dev path, but failed validation in this
479 method, raise AutoservRepairError.
Garry Wang7b0e1b72020-03-25 19:08:59 -0700480
Garry Wang70e5d062020-04-03 18:01:05 -0700481 @returns: A string of usb dev path(e.g. '/dev/sdb'), or empty string
482 if unexpected error occurred during probe.
483 @raises: AutoservRepairError if servo couldn't probe the usb dev path
484 (servo.probe_host_usb_dev() returns empty string), or the dev path is
485 not valid block device to servohost.
Garry Wang7b0e1b72020-03-25 19:08:59 -0700486 """
487 logging.info('Validating image usbkey on servo.')
Garry Wang7b0e1b72020-03-25 19:08:59 -0700488 try:
Garry Wang70e5d062020-04-03 18:01:05 -0700489 usb_dev = self._servo.probe_host_usb_dev()
Garry Wang7b0e1b72020-03-25 19:08:59 -0700490 except Exception as e:
491 # We don't want any unexpected or transient servo communicating
492 # failure block usb repair, so capture all errors here.
493 logging.error(e, exc_info=True)
494 logging.error('Unexpected error occurred on get usbkey dev path,'
495 ' skipping usbkey validation.')
496 return ''
497
Garry Wang70e5d062020-04-03 18:01:05 -0700498 if usb_dev:
499 # probe_host_usb_dev() sometimes return stale record,
500 # so we need to make sure the path exists in fdisk.
Otabek Kasimov77bff672020-10-08 15:52:03 -0700501 validate_cmd = 'fdisk -l %s' % usb_dev
Garry Wang11441182020-06-16 18:34:14 -0700502 try:
Otabek Kasimov77bff672020-10-08 15:52:03 -0700503 resp = self.run(validate_cmd, ignore_status=True, timeout=30)
Garry Wang11441182020-06-16 18:34:14 -0700504 if resp.exit_status == 0:
505 return usb_dev
Garry Wang11441182020-06-16 18:34:14 -0700506 logging.error('%s is reported from "image_usbkey_dev" control'
507 ' but not detected by fdisk!', usb_dev)
508 except error.AutoservRunError as e:
509 if 'Timeout encountered' in str(e):
510 logging.warning('Timeout encountered during fdisk run,'
511 ' skipping usbkey validation.')
512 return ''
513 raise
Garry Wang70e5d062020-04-03 18:01:05 -0700514
515 raise hosts.AutoservRepairError(
516 'No usbkey detected on servo, the usbkey may be either missing'
517 ' or broken. Please replace usbkey on the servo and retry.',
518 'missing usbkey')
519
Otabek Kasimov4ea636e2020-04-14 23:35:06 -0700520 def is_ec_supported(self):
Garry Wang9b8f2342020-04-17 16:34:09 -0700521 """Check if ec is supported on the servo_board"""
Otabek Kasimov4ea636e2020-04-14 23:35:06 -0700522 if self.servo_board:
523 try:
524 frm_config = config.Config(self.servo_board, self.servo_model)
525 return frm_config.chrome_ec
526 except Exception as e:
527 logging.error('Unexpected error when read from firmware'
528 ' configs; %s', str(e))
529 return False
530
Garry Wang70e5d062020-04-03 18:01:05 -0700531 def validate_image_usbkey(self):
532 """This method first validate if there is a recover usbkey on servo
533 that accessible to servohost, and second check if a ChromeOS image is
534 already on the usb drive and return the image_name so we can avoid
535 unnecessary download and flash to the recover usbkey on servo.
536
537 Please note that, there is special error handling logic here:
538 1. If unexpected error happens, we return empty string. So repair
539 actions will not get blocked.
540 2. If no working usbkey present on servo, but no errors, we'll raise
541 AutoservRepairError here.
542
543 @returns: image_name on the usbkey, e.g. nami-release/R82.10138.0.0,
544 or empty string if no test image detected, or unexpected
545 error occurred.
546 @raises: AutoservRepairError if the usbkey is not detected on servo.
547 """
548 usb_dev = self._probe_and_validate_usb_dev()
549 if usb_dev:
550 return self.get_image_name_from_usbkey(usb_dev)
551 else:
552 return ''
Garry Wang7b0e1b72020-03-25 19:08:59 -0700553
Richard Barnette1edbb162016-11-01 11:47:50 -0700554 def repair(self, silent=False):
555 """Attempt to repair servo host.
556
557 @param silent If true, suppress logging in `status.log`.
558 """
Richard Barnetteabbdc252018-07-26 16:57:42 -0700559 message = 'Beginning repair for servo host %s port %s serial %s'
560 message %= (self.hostname, self.servo_port, self.servo_serial)
561 self.record('INFO', None, None, message)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700562 try:
Richard Barnette1edbb162016-11-01 11:47:50 -0700563 self._repair_strategy.repair(self, silent)
Garry Wang11b5e872020-03-11 15:14:08 -0700564 self._servo_state = servo_constants.SERVO_STATE_WORKING
565 self.record('INFO', None, None,
566 'ServoHost repair set servo_state as WORKING')
Garry Wang464ff1e2019-07-18 17:20:34 -0700567 # If target is a labstation then try to withdraw any existing
568 # reboot request created by this servo because it passed repair.
569 if self.is_labstation():
570 self.withdraw_reboot_request()
Garry Wang63b8c382020-03-11 22:28:40 -0700571 except Exception as e:
Otabek Kasimov120b6fa2020-07-03 00:15:27 -0700572 if not self.is_localhost():
Otabek Kasimovc6f30412020-06-30 20:08:12 -0700573 self._servo_state = self.determine_servo_state()
574 self.record('INFO', None, None,
575 'ServoHost repair set servo_state as %s'
576 % self._servo_state)
Garry Wang63b8c382020-03-11 22:28:40 -0700577 if self._is_critical_error(e):
578 self.disconnect_servo()
579 self.stop_servod()
580 raise
581
Garry Wang63b8c382020-03-11 22:28:40 -0700582 def _is_critical_error(self, error):
583 if (isinstance(error, hosts.AutoservVerifyDependencyError)
584 and not error.is_critical()):
585 logging.warning('Non-critical verify failure(s) detected during'
586 ' verify/repair servo, servo connection will'
Evan Benn2c41c262020-10-28 11:34:27 +1100587 ' still be up but may not be fully functional.'
588 ' Some repair actions and servo dependent'
Garry Wang63b8c382020-03-11 22:28:40 -0700589 ' tests may not run.')
590 return False
Evan Benn2c41c262020-10-28 11:34:27 +1100591 logging.info(
592 'Critical verify failure(s) detected during repair/verify '
593 'servo. Disconnecting servo and running `stop servod`, all'
594 ' repair actions and tests that depends on servo will not '
595 'run.')
Garry Wang63b8c382020-03-11 22:28:40 -0700596 return True
Fang Deng5d518f42013-08-02 14:04:32 -0700597
Dan Shi4d478522014-02-14 13:46:32 -0800598 def get_servo(self):
599 """Get the cached servo.Servo object.
Fang Deng5d518f42013-08-02 14:04:32 -0700600
Dan Shi4d478522014-02-14 13:46:32 -0800601 @return: a servo.Servo object.
Dana Goyette353d1d92019-06-27 10:43:59 -0700602 @rtype: autotest_lib.server.cros.servo.servo.Servo
Fang Deng5d518f42013-08-02 14:04:32 -0700603 """
Dan Shi4d478522014-02-14 13:46:32 -0800604 return self._servo
605
Garry Wang79e9af62019-06-12 15:19:19 -0700606 def request_reboot(self):
607 """Request servohost to be rebooted when it's safe to by touch a file.
608 """
609 logging.debug('Request to reboot servohost %s has been created by '
Garry Wang464ff1e2019-07-18 17:20:34 -0700610 'servo with port # %s', self.hostname, self.servo_port)
Garry Wang79e9af62019-06-12 15:19:19 -0700611 self.run('touch %s' % self._reboot_file, ignore_status=True)
612
Garry Wang464ff1e2019-07-18 17:20:34 -0700613 def withdraw_reboot_request(self):
614 """Withdraw a servohost reboot request if exists by remove the flag
615 file.
616 """
617 logging.debug('Withdrawing request to reboot servohost %s that created'
618 ' by servo with port # %s if exists.',
619 self.hostname, self.servo_port)
620 self.run('rm -f %s' % self._reboot_file, ignore_status=True)
621
Garry Wangc1288cf2019-12-17 14:58:00 -0800622 def start_servod(self, quick_startup=False):
623 """Start the servod process on servohost.
624 """
Garry Wang2ac15ee2019-12-30 19:03:02 -0800625 # Skip if running on the localhost.(crbug.com/1038168)
626 if self.is_localhost():
627 logging.debug("Servohost is a localhost, skipping start servod.")
628 return
629
630 cmd = 'start servod'
Garry Wangc1288cf2019-12-17 14:58:00 -0800631 if self.servo_board:
Garry Wang2ac15ee2019-12-30 19:03:02 -0800632 cmd += ' BOARD=%s' % self.servo_board
Garry Wangc1288cf2019-12-17 14:58:00 -0800633 if self.servo_model:
634 cmd += ' MODEL=%s' % self.servo_model
Garry Wangc1288cf2019-12-17 14:58:00 -0800635 else:
Garry Wang2ac15ee2019-12-30 19:03:02 -0800636 logging.warning('Board for DUT is unknown; starting servod'
637 ' assuming a pre-configured board.')
638
639 cmd += ' PORT=%d' % self.servo_port
640 if self.servo_serial:
641 cmd += ' SERIAL=%s' % self.servo_serial
Garry Wangd7367482020-02-27 13:52:40 -0800642
Garry Wangcb06f3b2020-10-08 20:56:21 -0700643 # Start servod with dual_v4 based on servo_setup.
Otabek Kasimov382c3bb2020-10-28 13:22:45 -0700644 if self.is_dual_setup():
Garry Wangcb06f3b2020-10-08 20:56:21 -0700645 cmd += ' DUAL_V4=1'
Garry Wangd7367482020-02-27 13:52:40 -0800646
Garry Wangcb06f3b2020-10-08 20:56:21 -0700647 # Start servod with CONFIG=cr50.xml which required for some pools.
648 if self._require_cr50_servod_config():
649 cmd += ' CONFIG=cr50.xml'
Garry Wangb5cee3e2020-09-16 14:58:13 -0700650
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800651 if self.servo_recovery == True:
652 cmd += ' REC_MODE=1'
653
Garry Wang6a680062020-11-03 13:40:29 -0800654 # Adding customized args if any.
655 if self.additional_servod_args:
656 cmd += ' ' + self.additional_servod_args
657
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800658 # Remove the symbolic links from the logs. This helps ensure that
659 # a failed servod instantiation does not cause us to grab old logs
660 # by mistake.
661 self.remove_latest_log_symlinks()
Garry Wangcdd27b22020-01-13 14:59:11 -0800662 self.run(cmd, timeout=60)
Garry Wangc1288cf2019-12-17 14:58:00 -0800663
664 # There's a lag between when `start servod` completes and when
665 # the _ServodConnectionVerifier trigger can actually succeed.
666 # The call to time.sleep() below gives time to make sure that
667 # the trigger won't fail after we return.
668
669 # Normally servod on servo_v3 and labstation take ~10 seconds to ready,
670 # But in the rare case all servo on a labstation are in heavy use they
671 # may take ~30 seconds. So the timeout value will double these value,
672 # and we'll try quick start up when first time initialize servohost,
673 # and use standard start up timeout in repair.
674 if quick_startup:
Garry Wang11b5e872020-03-11 15:14:08 -0700675 timeout = servo_constants.SERVOD_QUICK_STARTUP_TIMEOUT
Garry Wangc1288cf2019-12-17 14:58:00 -0800676 else:
Garry Wang11b5e872020-03-11 15:14:08 -0700677 timeout = servo_constants.SERVOD_STARTUP_TIMEOUT
Garry Wangc1288cf2019-12-17 14:58:00 -0800678 logging.debug('Wait %s seconds for servod process fully up.', timeout)
679 time.sleep(timeout)
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700680 # Cache the initial instance timestamp to check against servod restarts
681 self._initial_instance_ts = self.get_instance_logs_ts()
Garry Wangc1288cf2019-12-17 14:58:00 -0800682
Garry Wangc1288cf2019-12-17 14:58:00 -0800683 def stop_servod(self):
684 """Stop the servod process on servohost.
685 """
Garry Wang2ac15ee2019-12-30 19:03:02 -0800686 # Skip if running on the localhost.(crbug.com/1038168)
687 if self.is_localhost():
688 logging.debug("Servohost is a localhost, skipping stop servod.")
689 return
690
Garry Wangc1288cf2019-12-17 14:58:00 -0800691 logging.debug('Stopping servod on port %s', self.servo_port)
Garry Wangcdd27b22020-01-13 14:59:11 -0800692 self.run('stop servod PORT=%d' % self.servo_port,
693 timeout=60, ignore_status=True)
Garry Wangc1288cf2019-12-17 14:58:00 -0800694 logging.debug('Wait %s seconds for servod process fully teardown.',
Garry Wang11b5e872020-03-11 15:14:08 -0700695 servo_constants.SERVOD_TEARDOWN_TIMEOUT)
696 time.sleep(servo_constants.SERVOD_TEARDOWN_TIMEOUT)
Garry Wangc1288cf2019-12-17 14:58:00 -0800697
Garry Wangc1288cf2019-12-17 14:58:00 -0800698 def restart_servod(self, quick_startup=False):
699 """Restart the servod process on servohost.
700 """
701 self.stop_servod()
702 self.start_servod(quick_startup)
703
Garry Wangffbd2162020-04-17 16:13:48 -0700704 def _process_servodtool_error(self, response):
705 """Helper function to handle non-zero servodtool response.
706 """
707 if re.search(servo_constants.ERROR_MESSAGE_USB_HUB_NOT_COMPATIBLE,
Garry Wangad245002020-05-15 15:20:23 -0700708 response.stdout):
Garry Wangffbd2162020-04-17 16:13:48 -0700709 logging.error('The servo is not plugged on a usb hub that supports'
710 ' power-cycle!')
Garry Wang000c6c02020-05-11 21:27:23 -0700711 # change the flag so we can update this label in later process.
712 self.smart_usbhub = False
Garry Wangffbd2162020-04-17 16:13:48 -0700713 return
714
715 if re.search(servo_constants.ERROR_MESSAGE_DEVICE_NOT_FOUND %
716 self.servo_serial, response.stdout):
717 logging.error('No servo with serial %s found!', self.servo_serial)
718 return
719
720 logging.error('Unexpected error occurred from usbhub control, please'
721 ' file a bug and inform chrome-fleet-software@ team!')
722
Otabek Kasimov86062d02020-11-17 13:30:22 -0800723 def get_main_servo_usb_path(self):
724 """Helper function to collect current usb-path to main servo.
725
726 The usb-path is path to the folder where usb-device was enumerated.
727 If fail then will return an empty string ('').
728
729 @returns: string, usb-path to the main servo device.
730 e.g.: '/sys/bus/usb/devices/1-6.1.3.1'
Garry Wangffbd2162020-04-17 16:13:48 -0700731 """
Otabek Kasimov09192682020-06-01 18:17:44 -0700732 # TODO remove try-except when fix crbug.com/1087964
733 try:
734 cmd = 'servodtool device -s %s usb-path' % self.servo_serial
735 resp = self.run(cmd, ignore_status=True, timeout=30)
736 except Exception as e:
737 # Here we catch only timeout errors.
738 # Other errors is filtered by ignore_status=True
739 logging.debug('Attempt to get servo usb-path failed due to '
740 'timeout; %s', e)
741 return ''
Garry Wangffbd2162020-04-17 16:13:48 -0700742 if resp.exit_status != 0:
743 self._process_servodtool_error(resp)
744 return ''
745 usb_path = resp.stdout.strip()
746 logging.info('Usb path of servo %s is %s', self.servo_serial, usb_path)
Otabek Kasimov86062d02020-11-17 13:30:22 -0800747 return usb_path
Garry Wangffbd2162020-04-17 16:13:48 -0700748
Otabek Kasimov86062d02020-11-17 13:30:22 -0800749 def _get_servo_usb_devnum(self):
750 """Helper function to collect current usb devnum of servo."""
751 usb_path = self.get_main_servo_usb_path()
752 if not usb_path:
753 return ''
754 resp = self.run('cat %s/devnum' % usb_path, ignore_status=True)
Garry Wangffbd2162020-04-17 16:13:48 -0700755 if resp.exit_status != 0:
756 self._process_servodtool_error(resp)
757 return ''
758 return resp.stdout.strip()
759
Garry Wang358aad42020-08-02 20:56:04 -0700760 def reboot_servo_v3_on_need(self):
761 """Check and reboot servo_v3 based on below conditions.
762 1. If there is an update pending on reboot.
763 2. Servo_v3 has been up for more than 96 hours.
764 """
765 if self.get_board() != 'beaglebone_servo':
766 logging.info('Servo reboot is only applicable for servo V3.')
Otabek Kasimove6df8102020-07-21 20:15:25 -0700767 return
768
Garry Wang358aad42020-08-02 20:56:04 -0700769 update_pending_reboot = (self._check_update_status() ==
770 self.UPDATE_STATE.PENDING_REBOOT)
771 uptime_hours = float(self.check_uptime())/3600
772 logging.info('Uptime of servo_v3: %s hour(s)', uptime_hours)
773 long_up_time = uptime_hours > 96
774
775 # Skip reboot if neither condition are met.
776 if not (update_pending_reboot or long_up_time):
Otabek Kasimove6df8102020-07-21 20:15:25 -0700777 return
778
Garry Wang358aad42020-08-02 20:56:04 -0700779 if update_pending_reboot:
780 message = 'Starting reboot servo_v3 because an update is pending.'
781 reboot_method = self._post_update_reboot
782 elif long_up_time:
783 message = 'Starting reboot servo_v3 because uptime > 96 hours.'
784 reboot_method = self._servo_host_reboot
785 self.record('INFO', None, None, message)
786 logging.info(message)
Otabek Kasimove6df8102020-07-21 20:15:25 -0700787 try:
Garry Wang358aad42020-08-02 20:56:04 -0700788 reboot_method()
Otabek Kasimove6df8102020-07-21 20:15:25 -0700789 message = 'Servo_v3 reboot completed successfully.'
790 except Exception as e:
791 logging.debug("Fail to reboot servo_v3; %s", e)
792 message = ('Servo_v3 reboot failed, please check debug log '
793 'for details.')
794 logging.info(message)
795 self.record('INFO', None, None, message)
Garry Wangffbd2162020-04-17 16:13:48 -0700796
797 def _reset_servo(self):
798 logging.info('Resetting servo through smart usbhub.')
Otabek Kasimov09192682020-06-01 18:17:44 -0700799 # TODO remove try-except when fix crbug.com/1087964
800 try:
801 resp = self.run('servodtool device -s %s power-cycle' %
802 self.servo_serial, ignore_status=True,
803 timeout=30)
804 if resp.exit_status != 0:
805 self._process_servodtool_error(resp)
806 return False
807 except Exception as e:
808 # Here we catch only timeout errors.
809 # Other errors is filtered by ignore_status=True
810 logging.debug('Attempt to reset servo failed due to timeout;'
811 ' %s', e)
Garry Wangffbd2162020-04-17 16:13:48 -0700812 return False
813
814 logging.debug('Wait %s seconds for servo to come back from reset.',
815 servo_constants.SERVO_RESET_TIMEOUT_SECONDS)
816 time.sleep(servo_constants.SERVO_RESET_TIMEOUT_SECONDS)
Garry Wang000c6c02020-05-11 21:27:23 -0700817 # change the flag so we can update this label in later process.
818 self.smart_usbhub = True
Garry Wangffbd2162020-04-17 16:13:48 -0700819 return True
820
Garry Wangffbd2162020-04-17 16:13:48 -0700821 def reset_servo(self):
822 """Reset(power-cycle) the servo via smart usbhub.
823 """
824 if not self.is_labstation():
825 logging.info('Servo reset is not applicable to servo_v3.')
826 return
827
828 pre_reset_devnum = self._get_servo_usb_devnum()
829 logging.info('Servo usb devnum before reset: %s', pre_reset_devnum)
830 result = self._reset_servo()
831 if not result:
Garry Wangfd5c8b62020-06-08 15:36:54 -0700832 message = ('Failed to reset servo with serial: %s. (Please ignore'
833 ' this error if the DUT is not connected to a smart'
834 ' usbhub).' % self.servo_serial)
Garry Wangffbd2162020-04-17 16:13:48 -0700835 logging.warning(message)
836 self.record('INFO', None, None, message)
837 return
838
839 post_reset_devnum = self._get_servo_usb_devnum()
840 logging.info('Servo usb devnum after reset: %s', post_reset_devnum)
841 if not (pre_reset_devnum and post_reset_devnum):
842 message = ('Servo reset completed but unable to verify'
843 ' devnum change!')
844 elif pre_reset_devnum != post_reset_devnum:
845 message = ('Reset servo with serial %s completed successfully!'
846 % self.servo_serial)
847 else:
848 message = 'Servo reset completed but devnum is still not changed!'
849 logging.info(message)
850 self.record('INFO', None, None, message)
851
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800852 def _extract_compressed_logs(self, logdir, relevant_files):
853 """Decompress servod logs in |logdir|.
854
855 @param logdir: directory containing compressed servod logs.
856 @param relevant_files: list of files in |logdir| to consider.
857
858 @returns: tuple, (tarfiles, files) where
859 tarfiles: list of the compressed filenames that have been
860 extracted and deleted
861 files: list of the uncompressed files that were generated
862 """
863 # For all tar-files, first extract them to the directory, and
864 # then let the common flow handle them.
865 tarfiles = [cf for cf in relevant_files if
866 cf.endswith(self.COMPRESSION_SUFFIX)]
867 files = []
868 for f in tarfiles:
869 norm_name = os.path.basename(f)[:-len(self.COMPRESSION_SUFFIX)]
870 with tarfile.open(f) as tf:
871 # Each tarfile has only one member, as
872 # that's the compressed log.
873 member = tf.members[0]
874 # Manipulate so that it only extracts the basename, and not
875 # the directories etc.
876 member.name = norm_name
877 files.append(os.path.join(logdir, member.name))
878 tf.extract(member, logdir)
879 # File has been extracted: remove the compressed file.
880 os.remove(f)
881 return tarfiles, files
882
883 def _extract_mcu_logs(self, log_subdir):
884 """Extract MCU (EC, Cr50, etc) console output from servod debug logs.
885
886 Using the MCU_EXTRACTOR regex (above) extract and split out MCU console
887 lines from the logs to generate invidiual console logs e.g. after
888 this method, you can find an ec.txt and servo_v4.txt in |log_dir| if
889 those MCUs had any console input/output.
890
891 @param log_subdir: directory with log.DEBUG.txt main servod debug logs.
892 """
893 # Extract the MCU for each one. The MCU logs are only in the .DEBUG
894 # files
895 mcu_lines_file = os.path.join(log_subdir, 'log.DEBUG.txt')
896 if not os.path.exists(mcu_lines_file):
897 logging.info('No DEBUG logs found to extract MCU logs from.')
898 return
899 mcu_files = {}
900 mcu_file_template = '%s.txt'
901 with open(mcu_lines_file, 'r') as f:
902 for line in f:
903 match = self.MCU_EXTRACTOR.match(line)
904 if match:
905 mcu = match.group(self.MCU_GROUP).lower()
906 line = match.group(self.LINE_GROUP)
907 if mcu not in mcu_files:
908 mcu_file = os.path.join(log_subdir,
909 mcu_file_template % mcu)
910 mcu_files[mcu] = open(mcu_file, 'a')
911 fd = mcu_files[mcu]
912 fd.write(line + '\n')
913 for f in mcu_files:
914 mcu_files[f].close()
915
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800916 def remove_latest_log_symlinks(self):
917 """Remove the conveninence symlinks 'latest' servod logs."""
918 symlink_wildcard = '%s/latest*' % self.remote_log_dir
919 cmd = 'rm ' + symlink_wildcard
920 self.run(cmd, stderr_tee=None, ignore_status=True)
921
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700922 def probe_servod_restart(self, instance_ts, outdir):
923 """Grab servod logs from previous instances if part of this session.
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800924
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700925 If since the last time this host called start_servod() servod crashed
926 and restarted, this helper finds those logs as well, and stores them
927 with the |OLD_LOG_SUFFIX| to investigate if necessary.
Prasad Vuppalapu5bd9da12020-03-31 01:46:47 +0000928
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700929 It also issues a panicinfo command to servo devices after the restart
930 to try and collect reboot information for debugging.
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800931
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700932 @param instance_ts: the log timestamp that the current instance uses
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -0800933 @param outdir: directory to create a subdirectory into to place the
934 servod logs into.
935 """
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700936 if self._initial_instance_ts is None:
937 logging.info('No log timestamp grabbed successfully on servod '
938 'startup. Cannot check device restarts. Ignoring.')
939 return
940 if instance_ts == self._initial_instance_ts:
941 logging.debug('Servod appears to have run without restarting')
942 return
943 # Servod seems to have restarted (at least once). |_initial_instance_ts|
944 # is the first timestamp, and instance_ts is the current timestamp. Find
945 # all timestamps in between them, and grab the logs for each.
946 tss = self._find_instance_timestamps_between(self._initial_instance_ts,
947 instance_ts)
948 logging.info('Servod has restarted %d times between the start and the '
949 'end of this servo_host.', len(tss))
950 logging.info('This might be an issue. Will extract all logs from each '
951 'instance.')
952 logging.info('Logs that are not the currently running (about to turn '
953 'down) instance are maked with a .%s in their folder.',
954 self.OLD_LOG_SUFFIX)
955 for ts in tss:
956 self.get_instance_logs(ts, outdir, old=True)
957 # Lastly, servod has restarted due to a potential issue. Try to get
958 # panic information from servo micro and servo v4 for the current logs.
959 # This can only happen if the |_servo| attribute is initialized.
960 if self._servo:
Ruben Rodriguez Buchillon030ff162021-03-09 17:21:25 -0800961 for mcu in ['servo_micro', 'servo_v4', 'servo_v4p1']:
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -0700962 ctrl = '%s_uart_cmd' % mcu
963 if self._servo.has_control(ctrl):
964 logging.info('Trying to retrieve %r panicinfo into logs',
965 mcu)
966 try:
967 self._servo.set_nocheck(ctrl, 'panicinfo')
968 except error.TestFail as e:
969 logging.error('Failed to generate panicinfo for %r '
970 'logs. %s', mcu, str(e))
971
972 def _find_instance_timestamps_between(self, start_ts, end_ts):
973 """Find all log timestamps between [start_ts, end_ts).
974
975 @param start_ts: str, earliest log timestamp of interest
976 @param end_ts: str, latest log timestamp of interest
977
978 @returns: list, all timestamps between start_ts and end_ts, end_ts
979 exclusive, on the servo_host. An empty list on errors
980 """
981 # Simply get all timestamp, and then sort and remove
982 cmd = 'ls %s' % self.remote_log_dir
983 res = self.run(cmd, stderr_tee=None, ignore_status=True)
984 if res.exit_status != 0:
985 # Here we failed to find anything.
986 logging.info('Failed to find remote servod logs. Ignoring.')
987 return []
988 logfiles = res.stdout.strip().split()
989 timestamps = set()
990 for logfile in logfiles:
991 ts_match = self.TS_EXTRACTOR.match(logfile)
992 if not ts_match:
993 # Simply ignore files that fail the check. It might be the
994 # 'latest' symlinks or random files.
995 continue
996 timestamps.add(ts_match.group(self.TS_GROUP))
997 # At this point we have all unique timestamps.
998 timestamps = sorted(timestamps)
999 for ts in [start_ts, end_ts]:
1000 if ts not in timestamps:
1001 logging.error('Timestamp %r not in servod logs. Cannot query '
1002 'for timestamps in between %r and %r', ts,
1003 start_ts, end_ts)
1004 return []
1005 return timestamps[timestamps.index(start_ts):timestamps.index(end_ts)]
1006
1007 def get_instance_logs_ts(self):
1008 """Retrieve the currently running servod instance's log timestamp
1009
1010 @returns: str, timestamp for current instance, or None on failure
1011 """
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001012 # First, extract the timestamp. This cmd gives the real filename of
1013 # the latest aka current log file.
1014 cmd = ('if [ -f %(dir)s/latest.DEBUG ];'
1015 'then realpath %(dir)s/latest.DEBUG;'
1016 'elif [ -f %(dir)s/latest ];'
1017 'then realpath %(dir)s/latest;'
1018 'else exit %(code)d;'
1019 'fi' % {'dir': self.remote_log_dir,
1020 'code': self.NO_SYMLINKS_CODE})
1021 res = self.run(cmd, stderr_tee=None, ignore_status=True)
1022 if res.exit_status != 0:
1023 if res.exit_status == self.NO_SYMLINKS_CODE:
1024 logging.warning('servod log latest symlinks not found. '
1025 'This is likely due to an error starting up '
1026 'servod. Ignoring..')
1027 else:
1028 logging.warning('Failed to find servod logs on servo host.')
1029 logging.warning(res.stderr.strip())
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001030 return None
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001031 fname = os.path.basename(res.stdout.strip())
1032 # From the fname, ought to extract the timestamp using the TS_EXTRACTOR
Ruben Rodriguez Buchillone9aa2b02020-03-04 12:14:28 -08001033 ts_match = self.TS_EXTRACTOR.match(fname)
1034 if not ts_match:
1035 logging.warning('Failed to extract timestamp from servod log file '
1036 '%r. Skipping. The servo host is using outdated '
1037 'servod logging and needs to be updated.', fname)
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001038 return None
1039 return ts_match.group(self.TS_GROUP)
1040
1041 def get_instance_logs(self, instance_ts, outdir, old=False):
1042 """Collect all logs with |instance_ts| and dump into a dir in |outdir|
1043
1044 This method first collects all logs on the servo_host side pertaining
1045 to this servod instance (port, instatiation). It glues them together
1046 into combined log.[level].txt files and extracts all available MCU
1047 console I/O from the logs into individual files e.g. servo_v4.txt
1048
1049 All the output can be found in a directory inside |outdir| that
1050 this generates based on |LOG_DIR|, the servod port, and the instance
1051 timestamp on the servo_host side.
1052
1053 @param instance_ts: log timestamp to grab logfiles for
1054 @param outdir: directory to create a subdirectory into to place the
1055 servod logs into.
1056 @param old: bool, whether to append |OLD_LOG_SUFFIX| to output dir
1057 """
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001058 # Create the local results log dir.
1059 log_dir = os.path.join(outdir, '%s_%s.%s' % (self.LOG_DIR,
1060 str(self.servo_port),
1061 instance_ts))
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001062 if old:
Garry Wang22f2e842020-09-09 20:19:19 -07001063 log_dir = '%s.%s' % (log_dir, self.OLD_LOG_SUFFIX)
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001064 logging.info('Saving servod logs to %r.', log_dir)
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001065 os.mkdir(log_dir)
1066 # Now, get all files with that timestamp.
1067 cmd = 'find %s -maxdepth 1 -name "log.%s*"' % (self.remote_log_dir,
1068 instance_ts)
1069 res = self.run(cmd, stderr_tee=None, ignore_status=True)
1070 files = res.stdout.strip().split()
1071 try:
1072 self.get_file(files, log_dir, try_rsync=False)
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001073 if not os.listdir(log_dir):
1074 logging.info('No servod logs retrieved. Ignoring, and removing '
1075 '%r again.', log_dir)
1076 os.rmdir(log_dir)
1077 return
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001078 except error.AutoservRunError as e:
1079 result = e.result_obj
1080 if result.exit_status != 0:
1081 stderr = result.stderr.strip()
1082 logging.warning("Couldn't retrieve servod logs. Ignoring: %s",
1083 stderr or '\n%s' % result)
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001084 # Remove the log_dir as nothing was added to it.
1085 os.rmdir(log_dir)
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001086 return
1087 local_files = [os.path.join(log_dir, f) for f in os.listdir(log_dir)]
1088 # TODO(crrev.com/c/1793030): remove no-level case once CL is pushed
1089 for level_name in ('DEBUG', 'INFO', 'WARNING', ''):
1090 # Create the joint files for each loglevel. i.e log.DEBUG
1091 joint_file = self.JOINT_LOG_PREFIX
1092 if level_name:
1093 joint_file = '%s.%s' % (self.JOINT_LOG_PREFIX, level_name)
1094 # This helps with some online tools to avoid complaints about an
1095 # unknown filetype.
1096 joint_file = joint_file + '.txt'
1097 joint_path = os.path.join(log_dir, joint_file)
1098 files = [f for f in local_files if level_name in f]
1099 if not files:
1100 # TODO(crrev.com/c/1793030): remove no-level case once CL
1101 # is pushed
1102 continue
1103 # Extract compressed logs if any.
1104 compressed, extracted = self._extract_compressed_logs(log_dir,
1105 files)
1106 files = list(set(files) - set(compressed))
1107 files.extend(extracted)
1108 # Need to sort. As they all share the same timestamp, and
1109 # loglevel, the index itself is sufficient. The highest index
1110 # is the oldest file, therefore we need a descending sort.
1111 def sortkey(f, level=level_name):
1112 """Custom sortkey to sort based on rotation number int."""
1113 if f.endswith(level_name): return 0
1114 return int(f.split('.')[-1])
1115
1116 files.sort(reverse=True, key=sortkey)
1117 # Just rename the first file rather than building from scratch.
1118 os.rename(files[0], joint_path)
1119 with open(joint_path, 'a') as joint_f:
1120 for logfile in files[1:]:
1121 # Transfer the file to the joint file line by line.
1122 with open(logfile, 'r') as log_f:
1123 for line in log_f:
1124 joint_f.write(line)
1125 # File has been written over. Delete safely.
1126 os.remove(logfile)
1127 # Need to remove all files form |local_files| so we don't
1128 # analyze them again.
1129 local_files = list(set(local_files) - set(files) - set(compressed))
1130 # Lastly, extract MCU logs from the joint logs.
1131 self._extract_mcu_logs(log_dir)
1132
Garry Wang79e9af62019-06-12 15:19:19 -07001133 def _lock(self):
1134 """lock servohost by touching a file.
1135 """
1136 logging.debug('Locking servohost %s by touching %s file',
1137 self.hostname, self._lock_file)
1138 self.run('touch %s' % self._lock_file, ignore_status=True)
Garry Wang7c00b0f2019-06-25 17:28:17 -07001139 self._is_locked = True
Garry Wang79e9af62019-06-12 15:19:19 -07001140
Garry Wang79e9af62019-06-12 15:19:19 -07001141 def _unlock(self):
1142 """Unlock servohost by removing the lock file.
1143 """
1144 logging.debug('Unlocking servohost by removing %s file',
1145 self._lock_file)
1146 self.run('rm %s' % self._lock_file, ignore_status=True)
Garry Wang7c00b0f2019-06-25 17:28:17 -07001147 self._is_locked = False
Garry Wang79e9af62019-06-12 15:19:19 -07001148
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001149 def close(self):
Congbin Guofc3b8962019-03-22 17:38:46 -07001150 """Close the associated servo and the host object."""
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001151 # NOTE: throughout this method there are multiple attempts to catch
1152 # all errors. This is WAI as log grabbing should not fail tests.
1153 # However, the goal is to catch and handle/process all errors, thus
1154 # we print the traceback and ask for a bug.
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001155 if self._closed:
1156 logging.debug('ServoHost is already closed.')
1157 return
Garry Wang22f2e842020-09-09 20:19:19 -07001158
1159 # Only attempt ssh related actions if servohost is sshable. We call
1160 # check_cached_up_status() first because it's lightweighted and return
1161 # much faster in the case servohost is down, however, we still want
1162 # to call is_up() later since check_cached_up_status() is ping based check
1163 # and not guarantee the servohost is sshable.
1164 servo_host_ready = self.check_cached_up_status() and self.is_up()
1165
1166 if servo_host_ready:
1167 instance_ts = self.get_instance_logs_ts()
1168 else:
1169 logging.info('Servohost is down, will skip servod log collecting.')
1170 instance_ts = None
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001171 # TODO(crbug.com/1011516): once enabled, remove the check against
1172 # localhost and instead check against log-rotiation enablement.
1173 logs_available = (instance_ts is not None and
1174 self.job and
1175 not self.is_localhost())
1176 if logs_available:
1177 # Probe whether there was a servod restart, and grab those old
1178 # logs as well.
1179 try:
1180 self.probe_servod_restart(instance_ts, self.job.resultdir)
1181 except (error.AutoservRunError, error.TestFail) as e:
1182 logging.info('Failed to grab servo logs due to: %s. '
1183 'This error is forgiven.', str(e))
1184 except Exception as e:
1185 logging.error('Unexpected error probing for old logs. %s. '
1186 'Forgiven. Please file a bug and fix or catch '
1187 'in log probing function', str(e),
1188 exc_info=True)
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001189 if self._servo:
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001190 outdir = None if not self.job else self.job.resultdir
Congbin Guo2e5e2a22018-07-27 10:32:48 -07001191 # In some cases when we run as lab-tools, the job object is None.
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001192 self._servo.close(outdir)
1193
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001194 if logs_available:
1195 # Grab current (not old like above) logs after the servo instance
1196 # was closed out.
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001197 try:
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001198 self.get_instance_logs(instance_ts, self.job.resultdir)
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001199 except error.AutoservRunError as e:
1200 logging.info('Failed to grab servo logs due to: %s. '
1201 'This error is forgiven.', str(e))
Ruben Rodriguez Buchillon5bac3062020-03-25 21:32:58 -07001202 except Exception as e:
1203 logging.error('Unexpected error grabbing servod logs. %s. '
1204 'Forgiven. Please file a bug and fix or catch '
1205 'in log grabbing function', str(e), exc_info=True)
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001206
Garry Wang22f2e842020-09-09 20:19:19 -07001207 if self._is_locked and servo_host_ready:
Garry Wang7c00b0f2019-06-25 17:28:17 -07001208 # Remove the lock if the servohost has been locked.
Garry Wang79e9af62019-06-12 15:19:19 -07001209 try:
1210 self._unlock()
1211 except error.AutoservSSHTimeout:
1212 logging.error('Unlock servohost failed due to ssh timeout.'
1213 ' It may caused by servohost went down during'
1214 ' the task.')
Garry Wangc1288cf2019-12-17 14:58:00 -08001215 # We want always stop servod after task to minimum the impact of bad
1216 # servod process interfere other servods.(see crbug.com/1028665)
Garry Wang22f2e842020-09-09 20:19:19 -07001217 if servo_host_ready:
1218 try:
1219 self.stop_servod()
1220 except error.AutoservRunError as e:
1221 logging.info(
1222 "Failed to stop servod due to:\n%s\n"
1223 "This error is forgiven.", str(e))
Garry Wangc1288cf2019-12-17 14:58:00 -08001224
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001225 super(ServoHost, self).close()
Ruben Rodriguez Buchillon93084d02020-01-21 15:17:36 -08001226 # Mark closed.
1227 self._closed = True
Congbin Guoa1f9cba2018-07-03 11:36:59 -07001228
Otabek Kasimovcc9738e2020-02-14 16:17:15 -08001229 def get_servo_state(self):
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001230 return self._servo_state
Otabek Kasimovcc9738e2020-02-14 16:17:15 -08001231
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001232 def _get_host_metrics_data(self):
1233 return {'port': self.servo_port,
Otabek Kasimov0ea47362020-07-11 20:55:09 -07001234 'host': self.get_dut_hostname() or self.hostname,
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001235 'board': self.servo_board or ''}
1236
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001237 def is_servo_board_present_on_servo_v3(self):
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001238 """Check if servo board is detected on servo_v3"""
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001239 logging.debug('Started to detect servo board on servo_v3')
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001240 vid_pids = ['18d1:5004', '0403:6014']
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001241 not_detected = 'The servo board is not detected on servo_v3'
1242 try:
1243 cmd = 'lsusb | grep "%s"' % "\|".join(vid_pids)
1244 result = self.run(cmd, ignore_status=True, timeout=30)
1245 if result.exit_status == 0 and result.stdout.strip():
1246 logging.debug('The servo board is detected on servo_v3')
1247 return True
1248 logging.debug('%s; %s', not_detected, result)
1249 return False
1250 except Exception as e:
1251 # can be triggered by timeout issue due running the script
1252 metrics.Counter(
1253 'chromeos/autotest/repair/servo_detection/timeout'
1254 ).increment(fields=self._get_host_metrics_data())
1255 logging.error('%s; %s', not_detected, str(e))
1256 return None
1257
Garry Wangb5cee3e2020-09-16 14:58:13 -07001258 def _require_cr50_servod_config(self):
1259 """Check whether we need start servod with CONFIG=cr50.xml"""
1260 dut_host_info = self.get_dut_host_info()
1261 if not dut_host_info:
1262 return False
1263 for pool in dut_host_info.pools:
1264 if pool.startswith(servo_constants.CR50_CONFIG_POOL_PREFIX):
1265 return True
1266 return False
1267
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001268 def get_verifier_state(self, tag):
Otabek Kasimov15963492020-06-23 21:10:51 -07001269 """Return the state of servo verifier.
1270
1271 @returns: bool or None
1272 """
1273 return self._repair_strategy.verifier_is_good(tag)
1274
1275 def determine_servo_state(self):
1276 """Determine servo state based on the failed verifier.
1277
1278 @returns: servo state value
1279 The state detecting based on first fail verifier or collecting of
1280 them.
1281 """
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001282 ssh = self.get_verifier_state('servo_ssh')
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001283 servo_root_present = self.get_verifier_state('servo_root_present')
1284 servo_v3_present = self.get_verifier_state('servo_v3_root_present')
Otabek Kasimov79be61b2021-02-18 12:30:44 -08001285 servo_fw = self.get_verifier_state('servo_fw')
Otabek Kasimov1614e2e2021-01-27 22:14:36 -08001286 disk_space = self.get_verifier_state('servo_disk_space')
Otabek Kasimov3c63cbf2021-04-30 18:51:31 -07001287 start_servod = self.get_verifier_state('start_servod')
1288 servod_started = self.get_verifier_state('servod_started')
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001289 create_servo = self.get_verifier_state('servod_connection')
1290 init_servo = self.get_verifier_state('servod_control')
Otabek Kasimov1614e2e2021-01-27 22:14:36 -08001291 cr50_low_sbu = self.get_verifier_state('servo_cr50_low_sbu')
1292 cr50_off = self.get_verifier_state('servo_cr50_off')
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07001293 servo_topology = self.get_verifier_state('servo_topology')
Otabek Kasimov1614e2e2021-01-27 22:14:36 -08001294 dut_connected = self.get_verifier_state('servo_dut_connected')
1295 hub_connected = self.get_verifier_state('servo_hub_connected')
1296 pwr_button = self.get_verifier_state('servo_pwr_button')
1297 lid_open = self.get_verifier_state('servo_lid_open')
1298 ec_board = self.get_verifier_state('servo_ec_board')
1299 cr50_console = self.get_verifier_state('servo_cr50_console')
1300 ccd_testlab = self.get_verifier_state('servo_ccd_testlab')
Otabek Kasimov15963492020-06-23 21:10:51 -07001301
1302 if not ssh:
1303 return servo_constants.SERVO_STATE_NO_SSH
Otabek Kasimov3c63cbf2021-04-30 18:51:31 -07001304 if start_servod == hosts.VERIFY_FAILED:
1305 return servo_constants.SERVO_STATE_SERVO_HOST_ISSUE
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001306 if servo_root_present == hosts.VERIFY_FAILED:
1307 if not self.servo_serial:
1308 return servo_constants.SERVO_STATE_WRONG_CONFIG
1309 return servo_constants.SERVO_STATE_NOT_CONNECTED
1310 if servo_v3_present == hosts.VERIFY_FAILED:
1311 # if we cannot find required board on servo_v3
1312 return servo_constants.SERVO_STATE_NEED_REPLACEMENT
Otabek Kasimov79be61b2021-02-18 12:30:44 -08001313 if servo_fw == hosts.VERIFY_FAILED:
1314 return servo_constants.SERVO_STATE_NEED_REPLACEMENT
Otabek Kasimov15963492020-06-23 21:10:51 -07001315
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -08001316 if dut_connected == hosts.VERIFY_FAILED:
1317 return servo_constants.SERVO_STATE_DUT_NOT_CONNECTED
1318 if hub_connected == hosts.VERIFY_FAILED:
1319 logging.info('Servo HUB not connected')
1320 return servo_constants.SERVO_STATE_DUT_NOT_CONNECTED
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001321
Otabek Kasimov8e88a742021-01-11 18:03:13 -08001322 if cr50_low_sbu == hosts.VERIFY_FAILED:
1323 return servo_constants.SERVO_STATE_SBU_LOW_VOLTAGE
1324 if cr50_off == hosts.VERIFY_FAILED:
1325 return servo_constants.SERVO_STATE_CR50_NOT_ENUMERATED
Otabek Kasimov6c6e2cb0b2021-03-09 17:18:45 -08001326
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07001327 if servo_topology == hosts.VERIFY_FAILED:
1328 return servo_constants.SERVO_STATE_TOPOLOGY_ISSUE
1329
Otabek Kasimovd5065bd2020-11-23 23:32:36 -08001330 # TODO(otabek@): detect special cases detected by pwr_button
1331 if dut_connected == hosts.VERIFY_SUCCESS:
1332 if pwr_button == hosts.VERIFY_FAILED:
1333 metrics.Counter(
1334 'chromeos/autotest/repair/servo_unexpected/pwr_button2'
1335 ).increment(fields=self._get_host_metrics_data())
Otabek Kasimova7eb4dc2020-09-16 10:25:17 -07001336
Otabek Kasimov3c63cbf2021-04-30 18:51:31 -07001337 if servod_started == hosts.VERIFY_FAILED:
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001338 return servo_constants.SERVO_STATE_SERVOD_ISSUE
1339
Otabek Kasimov15963492020-06-23 21:10:51 -07001340 # one of the reason why servo can not initialized
Otabek Kasimovbb3bc462020-11-03 16:40:33 -08001341 if cr50_console == hosts.VERIFY_FAILED:
1342 return servo_constants.SERVO_STATE_CR50_CONSOLE_MISSING
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001343 if ccd_testlab == hosts.VERIFY_FAILED:
Otabek Kasimov15963492020-06-23 21:10:51 -07001344 return servo_constants.SERVO_STATE_CCD_TESTLAB_ISSUE
1345
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001346 if (create_servo == hosts.VERIFY_FAILED
1347 or init_servo == hosts.VERIFY_FAILED):
Otabek Kasimov15963492020-06-23 21:10:51 -07001348 return servo_constants.SERVO_STATE_SERVOD_ISSUE
1349
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001350 if ec_board == hosts.VERIFY_FAILED:
Otabek Kasimov015c15c2020-08-20 00:40:42 -07001351 return servo_constants.SERVO_STATE_EC_BROKEN
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001352 if pwr_button == hosts.VERIFY_FAILED:
Otabek Kasimov15963492020-06-23 21:10:51 -07001353 return servo_constants.SERVO_STATE_BAD_RIBBON_CABLE
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001354 if lid_open == hosts.VERIFY_FAILED:
Otabek Kasimov15963492020-06-23 21:10:51 -07001355 return servo_constants.SERVO_STATE_LID_OPEN_FAILED
Otabek Kasimov15963492020-06-23 21:10:51 -07001356
Otabek Kasimov15963492020-06-23 21:10:51 -07001357 metrics.Counter(
1358 'chromeos/autotest/repair/unknown_servo_state'
Otabek Kasimovc6f30412020-06-30 20:08:12 -07001359 ).increment(fields=self._get_host_metrics_data())
Otabek Kasimov15963492020-06-23 21:10:51 -07001360 logging.info('We do not have special state for this failure yet :)')
1361 return servo_constants.SERVO_STATE_BROKEN
1362
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07001363 def is_servo_topology_supported(self):
1364 """Check if servo_topology is supported."""
Otabek Kasimovda994012020-11-25 15:23:04 -08001365 if not self.is_up_fast():
1366 logging.info('Servo-Host is not reachable.')
1367 return False
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07001368 if not self.is_labstation():
1369 logging.info('Servo-topology supported only for labstation.')
1370 return False
1371 if not self.servo_serial:
1372 logging.info('Servo-topology required a servo serial.')
1373 return False
1374 return True
1375
1376 def get_topology(self):
1377 """Get servo topology."""
Otabek Kasimovfe41e2d2021-02-14 20:48:52 -08001378 if not self._topology:
1379 self._topology = servo_topology.ServoTopology(self)
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07001380 return self._topology
1381
1382 def is_dual_setup(self):
1383 """Check is servo will run in dual setup.
1384
1385 Dual setup used only for servo_v4 when used ccd_cr50 and servo_micro
1386 at the same time.
1387 """
1388 return self.servo_setup == servo_constants.SERVO_SETUP_VALUE_DUAL_V4
1389
Otabek Kasimov39637412020-11-23 19:09:27 -08001390 def set_dut_health_profile(self, dut_health_profile):
1391 """
1392 @param dut_health_profile: A DeviceHealthProfile object.
1393 """
1394 logging.debug('setting dut_health_profile field to (%s)',
1395 dut_health_profile)
1396 self._dut_health_profile = dut_health_profile
1397
1398 def get_dut_health_profile(self):
1399 """
1400 @return A DeviceHealthProfile object.
1401 """
1402 return self._dut_health_profile
1403
Otabek Kasimov51ed19a2021-05-03 12:30:50 -07001404 def print_all_servo_of_host(self):
1405 """Print all servos detected on the host."""
1406 try:
1407 logging.info('\tDevices detected on the host:')
1408 devices = self.get_topology().get_list_available_servos()
1409 for device in devices:
1410 logging.info('\t%s', device)
1411 except Exception as e:
1412 logging.debug('(Not critical) Fail list all servos: %s', e)
1413
Otabek Kasimovcc9738e2020-02-14 16:17:15 -08001414
Richard Barnetteea3e4602016-06-10 12:36:41 -07001415def make_servo_hostname(dut_hostname):
1416 """Given a DUT's hostname, return the hostname of its servo.
1417
1418 @param dut_hostname: hostname of a DUT.
1419
1420 @return hostname of the DUT's servo.
1421
1422 """
1423 host_parts = dut_hostname.split('.')
1424 host_parts[0] = host_parts[0] + '-servo'
1425 return '.'.join(host_parts)
1426
1427
Richard Barnettee519dcd2016-08-15 17:37:17 -07001428def _map_afe_board_to_servo_board(afe_board):
1429 """Map a board we get from the AFE to a servo appropriate value.
1430
1431 Many boards are identical to other boards for servo's purposes.
1432 This function makes that mapping.
1433
1434 @param afe_board string board name received from AFE.
1435 @return board we expect servo to have.
1436
1437 """
1438 KNOWN_SUFFIXES = ['-freon', '_freon', '_moblab', '-cheets']
1439 BOARD_MAP = {'gizmo': 'panther'}
1440 mapped_board = afe_board
1441 if afe_board in BOARD_MAP:
1442 mapped_board = BOARD_MAP[afe_board]
1443 else:
1444 for suffix in KNOWN_SUFFIXES:
1445 if afe_board.endswith(suffix):
1446 mapped_board = afe_board[0:-len(suffix)]
1447 break
1448 if mapped_board != afe_board:
1449 logging.info('Mapping AFE board=%s to %s', afe_board, mapped_board)
1450 return mapped_board
1451
1452
Prathmesh Prabhub4810232018-09-07 13:24:08 -07001453def get_servo_args_for_host(dut_host):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -07001454 """Return servo data associated with a given DUT.
Richard Barnetteea3e4602016-06-10 12:36:41 -07001455
Richard Barnetteea3e4602016-06-10 12:36:41 -07001456 @param dut_host Instance of `Host` on which to find the servo
1457 attributes.
Prathmesh Prabhuf605dd32018-08-28 17:09:04 -07001458 @return `servo_args` dict with host and an optional port.
Richard Barnetteea3e4602016-06-10 12:36:41 -07001459 """
Prathmesh Prabhucba44292018-08-28 17:44:45 -07001460 info = dut_host.host_info_store.get()
Derek Beckettf73baca2020-08-19 15:08:47 -07001461 servo_args = {k: v for k, v in six.iteritems(info.attributes)
Garry Wang11b5e872020-03-11 15:14:08 -07001462 if k in servo_constants.SERVO_ATTR_KEYS}
Richard Barnetteea3e4602016-06-10 12:36:41 -07001463
Andrew Luo4be621d2020-03-21 07:01:13 -07001464 if servo_constants.SERVO_HOST_SSH_PORT_ATTR in servo_args:
1465 try:
1466 servo_args[servo_constants.SERVO_HOST_SSH_PORT_ATTR] = int(
1467 servo_args[servo_constants.SERVO_HOST_SSH_PORT_ATTR])
1468 except ValueError:
1469 logging.error('servo host port is not an int: %s',
1470 servo_args[servo_constants.SERVO_HOST_SSH_PORT_ATTR])
1471 # Reset servo_args because we don't want to use an invalid port.
1472 servo_args.pop(servo_constants.SERVO_HOST_SSH_PORT_ATTR, None)
1473
Garry Wang11b5e872020-03-11 15:14:08 -07001474 if servo_constants.SERVO_PORT_ATTR in servo_args:
Prathmesh Prabhucba44292018-08-28 17:44:45 -07001475 try:
Garry Wang11b5e872020-03-11 15:14:08 -07001476 servo_args[servo_constants.SERVO_PORT_ATTR] = int(
1477 servo_args[servo_constants.SERVO_PORT_ATTR])
Prathmesh Prabhucba44292018-08-28 17:44:45 -07001478 except ValueError:
1479 logging.error('servo port is not an int: %s',
Garry Wang11b5e872020-03-11 15:14:08 -07001480 servo_args[servo_constants.SERVO_PORT_ATTR])
Prathmesh Prabhucba44292018-08-28 17:44:45 -07001481 # Reset servo_args because we don't want to use an invalid port.
Garry Wang11b5e872020-03-11 15:14:08 -07001482 servo_args.pop(servo_constants.SERVO_HOST_ATTR, None)
Prathmesh Prabhucba44292018-08-28 17:44:45 -07001483
1484 if info.board:
Garry Wang11b5e872020-03-11 15:14:08 -07001485 servo_board = _map_afe_board_to_servo_board(info.board)
1486 servo_args[servo_constants.SERVO_BOARD_ATTR] = servo_board
Nick Sanders2f3c9852018-10-24 12:10:24 -07001487 if info.model:
Garry Wang11b5e872020-03-11 15:14:08 -07001488 servo_args[servo_constants.SERVO_MODEL_ATTR] = info.model
1489 return servo_args if servo_constants.SERVO_HOST_ATTR in servo_args else None
Richard Barnetteea3e4602016-06-10 12:36:41 -07001490
1491
Prathmesh Prabhuefb1b482018-08-28 17:15:05 -07001492def _tweak_args_for_ssp_moblab(servo_args):
Garry Wang11b5e872020-03-11 15:14:08 -07001493 if (servo_args[servo_constants.SERVO_HOST_ATTR]
1494 in ['localhost', '127.0.0.1']):
1495 servo_args[servo_constants.SERVO_HOST_ATTR] = _CONFIG.get_config_value(
Prathmesh Prabhuefb1b482018-08-28 17:15:05 -07001496 'SSP', 'host_container_ip', type=str, default=None)
1497
1498
Otabek Kasimov39637412020-11-23 19:09:27 -08001499def create_servo_host(dut,
1500 servo_args,
1501 try_lab_servo=False,
1502 try_servo_repair=False,
Otabek Kasimovcdcf1ee2021-03-10 12:10:10 -08001503 try_servo_recovery=True,
Otabek Kasimov39637412020-11-23 19:09:27 -08001504 dut_host_info=None,
1505 dut_health_profile=None):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -07001506 """Create a ServoHost object for a given DUT, if appropriate.
Dan Shi4d478522014-02-14 13:46:32 -08001507
Richard Barnette9a26ad62016-06-10 12:03:08 -07001508 This function attempts to create and verify or repair a `ServoHost`
1509 object for a servo connected to the given `dut`, subject to various
1510 constraints imposed by the parameters:
1511 * When the `servo_args` parameter is not `None`, a servo
1512 host must be created, and must be checked with `repair()`.
1513 * Otherwise, if a servo exists in the lab and `try_lab_servo` is
1514 true:
1515 * If `try_servo_repair` is true, then create a servo host and
1516 check it with `repair()`.
1517 * Otherwise, if the servo responds to `ping` then create a
1518 servo host and check it with `verify()`.
Fang Denge545abb2014-12-30 18:43:47 -08001519
Richard Barnette9a26ad62016-06-10 12:03:08 -07001520 In cases where `servo_args` was not `None`, repair failure
1521 exceptions are passed back to the caller; otherwise, exceptions
Richard Barnette07c2e1d2016-10-26 14:24:28 -07001522 are logged and then discarded. Note that this only happens in cases
1523 where we're called from a test (not special task) control file that
1524 has an explicit dependency on servo. In that case, we require that
1525 repair not write to `status.log`, so as to avoid polluting test
1526 results.
1527
1528 TODO(jrbarnette): The special handling for servo in test control
1529 files is a thorn in my flesh; I dearly hope to see it cut out before
1530 my retirement.
Richard Barnette9a26ad62016-06-10 12:03:08 -07001531
1532 Parameters for a servo host consist of a host name, port number, and
1533 DUT board, and are determined from one of these sources, in order of
1534 priority:
Richard Barnetteea3e4602016-06-10 12:36:41 -07001535 * Servo attributes from the `dut` parameter take precedence over
1536 all other sources of information.
1537 * If a DNS entry for the servo based on the DUT hostname exists in
1538 the CrOS lab network, that hostname is used with the default
Richard Barnette9a26ad62016-06-10 12:03:08 -07001539 port and the DUT's board.
Richard Barnetteea3e4602016-06-10 12:36:41 -07001540 * If no other options are found, the parameters will be taken
Richard Barnette9a26ad62016-06-10 12:03:08 -07001541 from the `servo_args` dict passed in from the caller.
Richard Barnetteea3e4602016-06-10 12:36:41 -07001542
Otabek Kasimovcdcf1ee2021-03-10 12:10:10 -08001543 @param dut: An instance of `Host` from which to take
1544 servo parameters (if available).
1545 @param servo_args: A dictionary with servo parameters to use if
1546 they can't be found from `dut`. If this
1547 argument is supplied, unrepaired exceptions
1548 from `verify()` will be passed back to the
1549 caller.
1550 @param try_lab_servo: If not true, servo host creation will be
1551 skipped unless otherwise required by the
1552 caller.
1553 @param try_servo_repair: If true, check a servo host with
1554 `repair()` instead of `verify()`.
1555 @param try_servo_recovery: If true, start servod in recovery mode.
1556 Default value is True.
1557 @param dut_host_info: A HostInfo object of the DUT that connected
1558 to this servo.
1559 @param dut_health_profile: DUT repair info with history.
Dan Shi4d478522014-02-14 13:46:32 -08001560
1561 @returns: A ServoHost object or None. See comments above.
1562
1563 """
Garry Wang2d23a892021-04-02 22:33:56 -07001564 # We are explicitly looking for if servo_args is None here(which means
1565 # servo not needed), as servo_args == {} means servo is needed and
1566 # we expect load servo_args from host_info_store.
1567 if servo_args is None:
1568 servo_dependency = False
1569 local_run = False
1570 else:
1571 servo_dependency = True
1572 # If servo_args pass in directly, then this is a local test run.
1573 local_run = servo_constants.SERVO_HOST_ATTR in servo_args
1574
1575 if local_run:
1576 logging.warning('User input servo_args detected, will attempt'
1577 ' to start servod and initialize servo conncetion'
1578 ' directly. All servo/servohost verify and repair'
1579 ' steps will be skipped.')
1580
1581 # Loading servo args from host_info_store.
Richard Barnette07c2e1d2016-10-26 14:24:28 -07001582 if dut is not None and (try_lab_servo or servo_dependency):
Prathmesh Prabhub4810232018-09-07 13:24:08 -07001583 servo_args_override = get_servo_args_for_host(dut)
Richard Barnetteea3e4602016-06-10 12:36:41 -07001584 if servo_args_override is not None:
Prathmesh Prabhuefb1b482018-08-28 17:15:05 -07001585 if utils.in_moblab_ssp():
1586 _tweak_args_for_ssp_moblab(servo_args_override)
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -07001587 logging.debug(
1588 'Overriding provided servo_args (%s) with arguments'
1589 ' determined from the host (%s)',
1590 servo_args,
1591 servo_args_override,
1592 )
Richard Barnetteea3e4602016-06-10 12:36:41 -07001593 servo_args = servo_args_override
Prathmesh Prabhucba44292018-08-28 17:44:45 -07001594
Garry Wang2d23a892021-04-02 22:33:56 -07001595 if not servo_args:
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -07001596 logging.debug('No servo_args provided, and failed to find overrides.')
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001597 if try_lab_servo or servo_dependency:
Otabek Kasimov646812c2020-06-23 20:01:36 -07001598 return None, servo_constants.SERVO_STATE_MISSING_CONFIG
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001599 else:
1600 # For regular test case which not required the servo
1601 return None, None
1602
Garry Wang11b5e872020-03-11 15:14:08 -07001603 servo_hostname = servo_args.get(servo_constants.SERVO_HOST_ATTR)
1604 servo_port = servo_args.get(servo_constants.SERVO_PORT_ATTR)
Garry Wang2d23a892021-04-02 22:33:56 -07001605 if not local_run:
1606 if not _is_servo_host_information_exist(servo_hostname, servo_port):
1607 logging.debug(
1608 'Servo connection info missed hostname: %s , port: %s',
1609 servo_hostname, servo_port)
1610 return None, servo_constants.SERVO_STATE_MISSING_CONFIG
1611 if not is_servo_host_information_valid(servo_hostname, servo_port):
1612 logging.debug(
1613 'Servo connection info is incorrect hostname: %s , port: %s',
1614 servo_hostname, servo_port)
1615 return None, servo_constants.SERVO_STATE_WRONG_CONFIG
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -07001616
Garry Wang2d23a892021-04-02 22:33:56 -07001617 if try_servo_recovery == True:
1618 servo_args[servo_constants.SERVO_RECOVERY_MODE] = True
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -08001619
Garry Wangebc015b2019-06-06 17:45:06 -07001620 newhost = ServoHost(**servo_args)
Garry Wang2d23a892021-04-02 22:33:56 -07001621 if local_run:
1622 try:
1623 newhost.start_servod()
1624 except:
1625 # If we failed to start servod here, we can assume the servod
1626 # either already started or the test is running against a
1627 # non-standard servohost so the user will resiponsble for ensure
1628 # servod is running.
1629 pass
1630 try:
Greg Edelstonff2665d2021-04-21 14:32:27 -06001631 newhost.initialize_servo()
Garry Wang2d23a892021-04-02 22:33:56 -07001632 newhost.initialize_dut_for_servo()
1633 newhost._servo_state = servo_constants.SERVO_STATE_WORKING
1634 return newhost, newhost.get_servo_state()
1635 except Exception as e:
1636 logging.error('Failed to initialize servo. %s', e)
1637 return None, servo_constants.SERVO_STATE_BROKEN
1638
Andrew Luo4be621d2020-03-21 07:01:13 -07001639 if newhost.use_icmp and not newhost.is_up_fast(count=3):
Otabek Kasimov32cafe92020-12-14 16:58:12 -08001640 # ServoHost has internal check to wait if servo-host is in reboot
1641 # process. If servo-host still is not available this check will stop
1642 # further attempts as we do not have any option to recover servo_host.
Otabek Kasimov39637412020-11-23 19:09:27 -08001643 return None, servo_constants.SERVO_STATE_NO_SSH
Garry Wangffbd2162020-04-17 16:13:48 -07001644
Otabek Kasimove6df8102020-07-21 20:15:25 -07001645 # Reset or reboot servo device only during AdminRepair tasks.
1646 if try_servo_repair:
1647 if newhost._is_locked:
Otabek Kasimov51ed19a2021-05-03 12:30:50 -07001648 # Print available servos on the host for debugging.
1649 newhost.print_all_servo_of_host()
Otabek Kasimove6df8102020-07-21 20:15:25 -07001650 # Reset servo if the servo is locked, as we check if the servohost
1651 # is up, if the servohost is labstation and if the servohost is in
1652 # lab inside the locking logic.
1653 newhost.reset_servo()
1654 else:
Garry Wang358aad42020-08-02 20:56:04 -07001655 try:
1656 newhost.reboot_servo_v3_on_need()
Garry Wang1f0d5332020-08-10 19:32:32 -07001657 except Exception as e:
1658 logging.info('[Non-critical] Unexpected error while trying to'
1659 ' reboot servo_v3, skipping the reboot; %s', e)
Otabek Kasimove6df8102020-07-21 20:15:25 -07001660
Otabek Kasimov2b50cdb2020-07-06 19:16:06 -07001661 if dut:
1662 newhost.set_dut_hostname(dut.hostname)
Otabek Kasimov9e90ae12020-08-14 03:01:19 -07001663 if dut_host_info:
1664 newhost.set_dut_host_info(dut_host_info)
Otabek Kasimov39637412020-11-23 19:09:27 -08001665 if dut_health_profile and (try_lab_servo or try_servo_repair):
1666 try:
1667 if newhost.is_localhost():
1668 logging.info('Servohost is a localhost, skip device'
1669 ' health profile setup...')
1670 else:
1671 dut_health_profile.init_profile(newhost)
1672 newhost.set_dut_health_profile(dut_health_profile)
1673 except Exception as e:
1674 logging.info(
1675 '[Non-critical] Unexpected error while trying to'
1676 ' load device health profile; %s', e)
Garry Wangffbd2162020-04-17 16:13:48 -07001677
Richard Barnette9a26ad62016-06-10 12:03:08 -07001678 # Note that the logic of repair() includes everything done
1679 # by verify(). It's sufficient to call one or the other;
1680 # we don't need both.
Richard Barnette07c2e1d2016-10-26 14:24:28 -07001681 if servo_dependency:
1682 newhost.repair(silent=True)
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001683 return newhost, newhost.get_servo_state()
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -07001684
1685 if try_servo_repair:
1686 try:
1687 newhost.repair()
1688 except Exception:
1689 logging.exception('servo repair failed for %s', newhost.hostname)
Richard Barnette9a26ad62016-06-10 12:03:08 -07001690 else:
1691 try:
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -07001692 newhost.verify()
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -07001693 except Exception:
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -07001694 logging.exception('servo verify failed for %s', newhost.hostname)
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001695 return newhost, newhost.get_servo_state()
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001696
1697
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001698def _is_servo_host_information_exist(hostname, port):
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001699 if hostname is None or len(hostname.strip()) == 0:
1700 return False
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001701 if port is None:
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001702 return False
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001703 if not type(port) is int:
1704 try:
1705 int(port)
1706 except ValueError:
1707 return False
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001708 return True
1709
1710
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001711def is_servo_host_information_valid(hostname, port):
Garry Wang9b8f2342020-04-17 16:34:09 -07001712 """Check if provided servo attributes are valid.
1713
1714 @param hostname Hostname of the servohost.
1715 @param port servo port number.
1716
1717 @returns: A bool value to indicate if provided servo attribute valid.
1718 """
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001719 if not _is_servo_host_information_exist(hostname, port):
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001720 return False
1721 # checking range and correct of the port
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001722 port_int = int(port)
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001723 if port_int < 1 or port_int > 65000:
1724 return False
1725 # we expecting host contain only latters, digits and '-' or '_'
Garry Wang536d2852021-03-28 22:33:40 -07001726 if not re.match('[a-zA-Z0-9-_\.:]*$', hostname) or len(hostname) < 5:
Otabek Kasimov7267a7a2020-03-04 11:18:45 -08001727 return False
1728 return True