blob: ea2421a2a68d9a659cd20ac5be9a0566363a5776 [file] [log] [blame]
Fang Deng5d518f42013-08-02 14:04:32 -07001# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4#
5# Expects to be run in an environment with sudo and no interactive password
6# prompt, such as within the Chromium OS development chroot.
7
8
9"""This file provides core logic for servo verify/repair process."""
10
11
Fang Deng5d518f42013-08-02 14:04:32 -070012import logging
Fang Deng5d518f42013-08-02 14:04:32 -070013import xmlrpclib
Raul E Rangel52ca2e82018-07-03 14:10:14 -060014import os
Fang Deng5d518f42013-08-02 14:04:32 -070015
16from autotest_lib.client.bin import utils
Garry Wang79e9af62019-06-12 15:19:19 -070017from autotest_lib.client.common_lib import error
beeps5e8c45a2013-12-17 22:05:11 -080018from autotest_lib.client.common_lib import global_config
Richard Barnette9a26ad62016-06-10 12:03:08 -070019from autotest_lib.client.common_lib import hosts
Fang Deng5d518f42013-08-02 14:04:32 -070020from autotest_lib.client.common_lib.cros import retry
Christopher Wileycef1f902014-06-19 11:11:23 -070021from autotest_lib.client.common_lib.cros.network import ping_runner
Richard Barnette9a26ad62016-06-10 12:03:08 -070022from autotest_lib.server.cros.servo import servo
Richard Barnetted31580e2018-05-14 19:58:00 +000023from autotest_lib.server.hosts import servo_repair
Garry Wangebc015b2019-06-06 17:45:06 -070024from autotest_lib.server.hosts import base_servohost
Dan Shi5e2efb72017-02-07 11:40:23 -080025
Fang Deng5d518f42013-08-02 14:04:32 -070026
Simran Basi0739d682015-02-25 16:22:56 -080027# Names of the host attributes in the database that represent the values for
28# the servo_host and servo_port for a servo connected to the DUT.
29SERVO_HOST_ATTR = 'servo_host'
30SERVO_PORT_ATTR = 'servo_port'
Richard Barnettee519dcd2016-08-15 17:37:17 -070031SERVO_BOARD_ATTR = 'servo_board'
Nick Sanders2f3c9852018-10-24 12:10:24 -070032# Model is inferred from host labels.
33SERVO_MODEL_ATTR = 'servo_model'
Kevin Cheng643ce8a2016-09-15 15:42:12 -070034SERVO_SERIAL_ATTR = 'servo_serial'
Prathmesh Prabhucba44292018-08-28 17:44:45 -070035SERVO_ATTR_KEYS = (
36 SERVO_BOARD_ATTR,
37 SERVO_HOST_ATTR,
38 SERVO_PORT_ATTR,
39 SERVO_SERIAL_ATTR,
40)
Simran Basi0739d682015-02-25 16:22:56 -080041
Dan Shi3b2adf62015-09-02 17:46:54 -070042_CONFIG = global_config.global_config
xixuan6cf6d2f2016-01-29 15:29:00 -080043ENABLE_SSH_TUNNEL_FOR_SERVO = _CONFIG.get_config_value(
44 'CROS', 'enable_ssh_tunnel_for_servo', type=bool, default=False)
Simran Basi0739d682015-02-25 16:22:56 -080045
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -070046AUTOTEST_BASE = _CONFIG.get_config_value(
47 'SCHEDULER', 'drone_installation_directory',
48 default='/usr/local/autotest')
49
Fang Deng5d518f42013-08-02 14:04:32 -070050
Garry Wangebc015b2019-06-06 17:45:06 -070051class ServoHost(base_servohost.BaseServoHost):
52 """Host class for a servo host(e.g. beaglebone, labstation)
53 that with a servo instance for a specific port."""
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
xixuan6cf6d2f2016-01-29 15:29:00 -080060 # Ready test function
61 SERVO_READY_METHOD = 'get_version'
Fang Deng5d518f42013-08-02 14:04:32 -070062
63
Richard Barnette17bfc6c2016-08-04 18:41:43 -070064 def _initialize(self, servo_host='localhost',
Richard Barnettee519dcd2016-08-15 17:37:17 -070065 servo_port=DEFAULT_PORT, servo_board=None,
Nick Sanders2f3c9852018-10-24 12:10:24 -070066 servo_model=None, servo_serial=None, is_in_lab=None,
67 *args, **dargs):
Fang Deng5d518f42013-08-02 14:04:32 -070068 """Initialize a ServoHost instance.
69
70 A ServoHost instance represents a host that controls a servo.
71
72 @param servo_host: Name of the host where the servod process
73 is running.
Raul E Rangel52ca2e82018-07-03 14:10:14 -060074 @param servo_port: Port the servod process is listening on. Defaults
75 to the SERVOD_PORT environment variable if set,
76 otherwise 9999.
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -070077 @param servo_board: Board that the servo is connected to.
Nick Sanders2f3c9852018-10-24 12:10:24 -070078 @param servo_model: Model that the servo is connected to.
Dan Shi4d478522014-02-14 13:46:32 -080079 @param is_in_lab: True if the servo host is in Cros Lab. Default is set
80 to None, for which utils.host_is_in_lab_zone will be
81 called to check if the servo host is in Cros lab.
Fang Deng5d518f42013-08-02 14:04:32 -070082
83 """
84 super(ServoHost, self)._initialize(hostname=servo_host,
Garry Wangebc015b2019-06-06 17:45:06 -070085 is_in_lab=is_in_lab, *args, **dargs)
Richard Barnette42f4db92018-08-23 15:05:15 -070086 self.servo_port = int(servo_port)
Richard Barnettee519dcd2016-08-15 17:37:17 -070087 self.servo_board = servo_board
Nick Sanders2f3c9852018-10-24 12:10:24 -070088 self.servo_model = servo_model
Kevin Cheng643ce8a2016-09-15 15:42:12 -070089 self.servo_serial = servo_serial
Richard Barnettee519dcd2016-08-15 17:37:17 -070090 self._servo = None
Garry Wang79e9af62019-06-12 15:19:19 -070091 # Path of the servo host lock file.
92 self._lock_file = (self.TEMP_FILE_DIR + str(self.servo_port)
93 + self.LOCK_FILE_POSTFIX)
94 # File path to declare a reboot request.
95 self._reboot_file = (self.TEMP_FILE_DIR + str(self.servo_port)
96 + self.REBOOT_FILE_POSTFIX)
97
98 # Lock the servo host if it's an in-lab labstation to prevent other
99 # task to reboot it until current task completes. We also wait and
100 # make sure the labstation is up here, in the case of the labstation is
101 # in the middle of reboot.
Garry Wang7c00b0f2019-06-25 17:28:17 -0700102 self._is_locked = False
Garry Wang42b4d862019-06-25 15:50:49 -0700103 if (self.wait_up(self.REBOOT_TIMEOUT) and self.is_in_lab()
104 and self.is_labstation()):
Garry Wang79e9af62019-06-12 15:19:19 -0700105 self._lock()
Garry Wangebc015b2019-06-06 17:45:06 -0700106
Richard Barnette9a26ad62016-06-10 12:03:08 -0700107 self._repair_strategy = (
108 servo_repair.create_servo_repair_strategy())
Richard Barnettee519dcd2016-08-15 17:37:17 -0700109
Richard Barnette9a26ad62016-06-10 12:03:08 -0700110
111 def connect_servo(self):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -0700112 """Establish a connection to the servod server on this host.
Richard Barnette9a26ad62016-06-10 12:03:08 -0700113
114 Initializes `self._servo` and then verifies that all network
115 connections are working. This will create an ssh tunnel if
116 it's required.
117
118 As a side effect of testing the connection, all signals on the
119 target servo are reset to default values, and the USB stick is
120 set to the neutral (off) position.
121 """
Kevin Cheng643ce8a2016-09-15 15:42:12 -0700122 servo_obj = servo.Servo(servo_host=self, servo_serial=self.servo_serial)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700123 timeout, _ = retry.timeout(
124 servo_obj.initialize_dut,
125 timeout_sec=self.INITIALIZE_SERVO_TIMEOUT_SECS)
126 if timeout:
127 raise hosts.AutoservVerifyError(
128 'Servo initialize timed out.')
129 self._servo = servo_obj
130
131
132 def disconnect_servo(self):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -0700133 """Disconnect our servo if it exists.
Richard Barnette9a26ad62016-06-10 12:03:08 -0700134
135 If we've previously successfully connected to our servo,
136 disconnect any established ssh tunnel, and set `self._servo`
137 back to `None`.
138 """
139 if self._servo:
140 # N.B. This call is safe even without a tunnel:
141 # rpc_server_tracker.disconnect() silently ignores
142 # unknown ports.
143 self.rpc_server_tracker.disconnect(self.servo_port)
144 self._servo = None
Fang Deng5d518f42013-08-02 14:04:32 -0700145
146
Fang Deng5d518f42013-08-02 14:04:32 -0700147 def get_servod_server_proxy(self):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -0700148 """Return a proxy that can be used to communicate with servod server.
Fang Deng5d518f42013-08-02 14:04:32 -0700149
150 @returns: An xmlrpclib.ServerProxy that is connected to the servod
151 server on the host.
Fang Deng5d518f42013-08-02 14:04:32 -0700152 """
Richard Barnette9a26ad62016-06-10 12:03:08 -0700153 if ENABLE_SSH_TUNNEL_FOR_SERVO and not self.is_localhost():
154 return self.rpc_server_tracker.xmlrpc_connect(
155 None, self.servo_port,
156 ready_test_name=self.SERVO_READY_METHOD,
Allen Li2b1a8992018-11-27 14:17:18 -0800157 timeout_seconds=60,
Allen Li556f4532018-12-03 18:11:23 -0800158 request_timeout_seconds=3600)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700159 else:
160 remote = 'http://%s:%s' % (self.hostname, self.servo_port)
161 return xmlrpclib.ServerProxy(remote)
Fang Deng5d518f42013-08-02 14:04:32 -0700162
163
Richard Barnette1edbb162016-11-01 11:47:50 -0700164 def verify(self, silent=False):
165 """Update the servo host and verify it's in a good state.
166
167 @param silent If true, suppress logging in `status.log`.
168 """
Richard Barnetteabbdc252018-07-26 16:57:42 -0700169 message = 'Beginning verify for servo host %s port %s serial %s'
170 message %= (self.hostname, self.servo_port, self.servo_serial)
171 self.record('INFO', None, None, message)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700172 try:
Richard Barnette1edbb162016-11-01 11:47:50 -0700173 self._repair_strategy.verify(self, silent)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700174 except:
175 self.disconnect_servo()
176 raise
Fang Deng5d518f42013-08-02 14:04:32 -0700177
178
Richard Barnette1edbb162016-11-01 11:47:50 -0700179 def repair(self, silent=False):
180 """Attempt to repair servo host.
181
182 @param silent If true, suppress logging in `status.log`.
183 """
Richard Barnetteabbdc252018-07-26 16:57:42 -0700184 message = 'Beginning repair for servo host %s port %s serial %s'
185 message %= (self.hostname, self.servo_port, self.servo_serial)
186 self.record('INFO', None, None, message)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700187 try:
Richard Barnette1edbb162016-11-01 11:47:50 -0700188 self._repair_strategy.repair(self, silent)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700189 except:
190 self.disconnect_servo()
191 raise
Fang Deng5d518f42013-08-02 14:04:32 -0700192
193
Dan Shi4d478522014-02-14 13:46:32 -0800194 def get_servo(self):
195 """Get the cached servo.Servo object.
Fang Deng5d518f42013-08-02 14:04:32 -0700196
Dan Shi4d478522014-02-14 13:46:32 -0800197 @return: a servo.Servo object.
Fang Deng5d518f42013-08-02 14:04:32 -0700198 """
Dan Shi4d478522014-02-14 13:46:32 -0800199 return self._servo
200
201
Garry Wang79e9af62019-06-12 15:19:19 -0700202 def request_reboot(self):
203 """Request servohost to be rebooted when it's safe to by touch a file.
204 """
205 logging.debug('Request to reboot servohost %s has been created by '
206 'servo with port %s', self.hostname, self.servo_port)
207 self.run('touch %s' % self._reboot_file, ignore_status=True)
208
209
210 def _lock(self):
211 """lock servohost by touching a file.
212 """
213 logging.debug('Locking servohost %s by touching %s file',
214 self.hostname, self._lock_file)
215 self.run('touch %s' % self._lock_file, ignore_status=True)
Garry Wang7c00b0f2019-06-25 17:28:17 -0700216 self._is_locked = True
Garry Wang79e9af62019-06-12 15:19:19 -0700217
218
219 def _unlock(self):
220 """Unlock servohost by removing the lock file.
221 """
222 logging.debug('Unlocking servohost by removing %s file',
223 self._lock_file)
224 self.run('rm %s' % self._lock_file, ignore_status=True)
Garry Wang7c00b0f2019-06-25 17:28:17 -0700225 self._is_locked = False
Garry Wang79e9af62019-06-12 15:19:19 -0700226
227
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700228 def close(self):
Congbin Guofc3b8962019-03-22 17:38:46 -0700229 """Close the associated servo and the host object."""
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700230 if self._servo:
Congbin Guo2e5e2a22018-07-27 10:32:48 -0700231 # In some cases when we run as lab-tools, the job object is None.
Congbin Guofc3b8962019-03-22 17:38:46 -0700232 if self.job and not self._servo.uart_logs_dir:
233 self._servo.uart_logs_dir = self.job.resultdir
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700234 self._servo.close()
235
Garry Wang7c00b0f2019-06-25 17:28:17 -0700236 if self._is_locked:
237 # Remove the lock if the servohost has been locked.
Garry Wang79e9af62019-06-12 15:19:19 -0700238 try:
239 self._unlock()
240 except error.AutoservSSHTimeout:
241 logging.error('Unlock servohost failed due to ssh timeout.'
242 ' It may caused by servohost went down during'
243 ' the task.')
244
Congbin Guoa1f9cba2018-07-03 11:36:59 -0700245 super(ServoHost, self).close()
246
247
Richard Barnetteea3e4602016-06-10 12:36:41 -0700248def make_servo_hostname(dut_hostname):
249 """Given a DUT's hostname, return the hostname of its servo.
250
251 @param dut_hostname: hostname of a DUT.
252
253 @return hostname of the DUT's servo.
254
255 """
256 host_parts = dut_hostname.split('.')
257 host_parts[0] = host_parts[0] + '-servo'
258 return '.'.join(host_parts)
259
260
261def servo_host_is_up(servo_hostname):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -0700262 """Given a servo host name, return if it's up or not.
Richard Barnetteea3e4602016-06-10 12:36:41 -0700263
264 @param servo_hostname: hostname of the servo host.
265
266 @return True if it's up, False otherwise
267 """
268 # Technically, this duplicates the SSH ping done early in the servo
269 # proxy initialization code. However, this ping ends in a couple
270 # seconds when if fails, rather than the 60 seconds it takes to decide
271 # that an SSH ping has timed out. Specifically, that timeout happens
272 # when our servo DNS name resolves, but there is no host at that IP.
273 logging.info('Pinging servo host at %s', servo_hostname)
274 ping_config = ping_runner.PingConfig(
275 servo_hostname, count=3,
276 ignore_result=True, ignore_status=True)
277 return ping_runner.PingRunner().ping(ping_config).received > 0
278
279
Richard Barnettee519dcd2016-08-15 17:37:17 -0700280def _map_afe_board_to_servo_board(afe_board):
281 """Map a board we get from the AFE to a servo appropriate value.
282
283 Many boards are identical to other boards for servo's purposes.
284 This function makes that mapping.
285
286 @param afe_board string board name received from AFE.
287 @return board we expect servo to have.
288
289 """
290 KNOWN_SUFFIXES = ['-freon', '_freon', '_moblab', '-cheets']
291 BOARD_MAP = {'gizmo': 'panther'}
292 mapped_board = afe_board
293 if afe_board in BOARD_MAP:
294 mapped_board = BOARD_MAP[afe_board]
295 else:
296 for suffix in KNOWN_SUFFIXES:
297 if afe_board.endswith(suffix):
298 mapped_board = afe_board[0:-len(suffix)]
299 break
300 if mapped_board != afe_board:
301 logging.info('Mapping AFE board=%s to %s', afe_board, mapped_board)
302 return mapped_board
303
304
Prathmesh Prabhub4810232018-09-07 13:24:08 -0700305def get_servo_args_for_host(dut_host):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -0700306 """Return servo data associated with a given DUT.
Richard Barnetteea3e4602016-06-10 12:36:41 -0700307
Richard Barnetteea3e4602016-06-10 12:36:41 -0700308 @param dut_host Instance of `Host` on which to find the servo
309 attributes.
Prathmesh Prabhuf605dd32018-08-28 17:09:04 -0700310 @return `servo_args` dict with host and an optional port.
Richard Barnetteea3e4602016-06-10 12:36:41 -0700311 """
Prathmesh Prabhucba44292018-08-28 17:44:45 -0700312 info = dut_host.host_info_store.get()
313 servo_args = {k: v for k, v in info.attributes.iteritems()
314 if k in SERVO_ATTR_KEYS}
Richard Barnetteea3e4602016-06-10 12:36:41 -0700315
Prathmesh Prabhucba44292018-08-28 17:44:45 -0700316 if SERVO_PORT_ATTR in servo_args:
317 try:
318 servo_args[SERVO_PORT_ATTR] = int(servo_args[SERVO_PORT_ATTR])
319 except ValueError:
320 logging.error('servo port is not an int: %s',
321 servo_args[SERVO_PORT_ATTR])
322 # Reset servo_args because we don't want to use an invalid port.
323 servo_args.pop(SERVO_HOST_ATTR, None)
324
325 if info.board:
326 servo_args[SERVO_BOARD_ATTR] = _map_afe_board_to_servo_board(info.board)
Nick Sanders2f3c9852018-10-24 12:10:24 -0700327 if info.model:
328 servo_args[SERVO_MODEL_ATTR] = info.model
Prathmesh Prabhu6f5f6362018-09-05 17:20:31 -0700329 return servo_args if SERVO_HOST_ATTR in servo_args else None
Richard Barnetteea3e4602016-06-10 12:36:41 -0700330
331
Prathmesh Prabhuefb1b482018-08-28 17:15:05 -0700332def _tweak_args_for_ssp_moblab(servo_args):
333 if servo_args[SERVO_HOST_ATTR] in ['localhost', '127.0.0.1']:
334 servo_args[SERVO_HOST_ATTR] = _CONFIG.get_config_value(
335 'SSP', 'host_container_ip', type=str, default=None)
336
337
Dan Shi023aae32016-05-25 11:13:01 -0700338def create_servo_host(dut, servo_args, try_lab_servo=False,
Richard Barnette9a26ad62016-06-10 12:03:08 -0700339 try_servo_repair=False):
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -0700340 """Create a ServoHost object for a given DUT, if appropriate.
Dan Shi4d478522014-02-14 13:46:32 -0800341
Richard Barnette9a26ad62016-06-10 12:03:08 -0700342 This function attempts to create and verify or repair a `ServoHost`
343 object for a servo connected to the given `dut`, subject to various
344 constraints imposed by the parameters:
345 * When the `servo_args` parameter is not `None`, a servo
346 host must be created, and must be checked with `repair()`.
347 * Otherwise, if a servo exists in the lab and `try_lab_servo` is
348 true:
349 * If `try_servo_repair` is true, then create a servo host and
350 check it with `repair()`.
351 * Otherwise, if the servo responds to `ping` then create a
352 servo host and check it with `verify()`.
Fang Denge545abb2014-12-30 18:43:47 -0800353
Richard Barnette9a26ad62016-06-10 12:03:08 -0700354 In cases where `servo_args` was not `None`, repair failure
355 exceptions are passed back to the caller; otherwise, exceptions
Richard Barnette07c2e1d2016-10-26 14:24:28 -0700356 are logged and then discarded. Note that this only happens in cases
357 where we're called from a test (not special task) control file that
358 has an explicit dependency on servo. In that case, we require that
359 repair not write to `status.log`, so as to avoid polluting test
360 results.
361
362 TODO(jrbarnette): The special handling for servo in test control
363 files is a thorn in my flesh; I dearly hope to see it cut out before
364 my retirement.
Richard Barnette9a26ad62016-06-10 12:03:08 -0700365
366 Parameters for a servo host consist of a host name, port number, and
367 DUT board, and are determined from one of these sources, in order of
368 priority:
Richard Barnetteea3e4602016-06-10 12:36:41 -0700369 * Servo attributes from the `dut` parameter take precedence over
370 all other sources of information.
371 * If a DNS entry for the servo based on the DUT hostname exists in
372 the CrOS lab network, that hostname is used with the default
Richard Barnette9a26ad62016-06-10 12:03:08 -0700373 port and the DUT's board.
Richard Barnetteea3e4602016-06-10 12:36:41 -0700374 * If no other options are found, the parameters will be taken
Richard Barnette9a26ad62016-06-10 12:03:08 -0700375 from the `servo_args` dict passed in from the caller.
Richard Barnetteea3e4602016-06-10 12:36:41 -0700376
377 @param dut An instance of `Host` from which to take
378 servo parameters (if available).
379 @param servo_args A dictionary with servo parameters to use if
380 they can't be found from `dut`. If this
381 argument is supplied, unrepaired exceptions
382 from `verify()` will be passed back to the
383 caller.
384 @param try_lab_servo If not true, servo host creation will be
385 skipped unless otherwise required by the
386 caller.
Richard Barnette9a26ad62016-06-10 12:03:08 -0700387 @param try_servo_repair If true, check a servo host with
388 `repair()` instead of `verify()`.
Dan Shi4d478522014-02-14 13:46:32 -0800389
390 @returns: A ServoHost object or None. See comments above.
391
392 """
Richard Barnette07c2e1d2016-10-26 14:24:28 -0700393 servo_dependency = servo_args is not None
Richard Barnette07c2e1d2016-10-26 14:24:28 -0700394 if dut is not None and (try_lab_servo or servo_dependency):
Prathmesh Prabhub4810232018-09-07 13:24:08 -0700395 servo_args_override = get_servo_args_for_host(dut)
Richard Barnetteea3e4602016-06-10 12:36:41 -0700396 if servo_args_override is not None:
Prathmesh Prabhuefb1b482018-08-28 17:15:05 -0700397 if utils.in_moblab_ssp():
398 _tweak_args_for_ssp_moblab(servo_args_override)
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -0700399 logging.debug(
400 'Overriding provided servo_args (%s) with arguments'
401 ' determined from the host (%s)',
402 servo_args,
403 servo_args_override,
404 )
Richard Barnetteea3e4602016-06-10 12:36:41 -0700405 servo_args = servo_args_override
Prathmesh Prabhucba44292018-08-28 17:44:45 -0700406
Richard Barnetteea3e4602016-06-10 12:36:41 -0700407 if servo_args is None:
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -0700408 logging.debug('No servo_args provided, and failed to find overrides.')
Richard Barnetteea3e4602016-06-10 12:36:41 -0700409 return None
Prathmesh Prabhucba44292018-08-28 17:44:45 -0700410 if SERVO_HOST_ATTR not in servo_args:
411 logging.debug('%s attribute missing from servo_args: %s',
412 SERVO_HOST_ATTR, servo_args)
413 return None
Richard Barnette07c2e1d2016-10-26 14:24:28 -0700414 if (not servo_dependency and not try_servo_repair and
Richard Barnette9a26ad62016-06-10 12:03:08 -0700415 not servo_host_is_up(servo_args[SERVO_HOST_ATTR])):
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -0700416 logging.debug('ServoHost is not up.')
Dan Shibbb0cb62014-03-24 17:50:57 -0700417 return None
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -0700418
Garry Wangebc015b2019-06-06 17:45:06 -0700419 newhost = ServoHost(**servo_args)
420
Richard Barnette9a26ad62016-06-10 12:03:08 -0700421 # Note that the logic of repair() includes everything done
422 # by verify(). It's sufficient to call one or the other;
423 # we don't need both.
Richard Barnette07c2e1d2016-10-26 14:24:28 -0700424 if servo_dependency:
425 newhost.repair(silent=True)
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -0700426 return newhost
427
428 if try_servo_repair:
429 try:
430 newhost.repair()
431 except Exception:
432 logging.exception('servo repair failed for %s', newhost.hostname)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700433 else:
434 try:
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -0700435 newhost.verify()
Kevin Cheng5f2ba6c2016-09-28 10:20:05 -0700436 except Exception:
Prathmesh Prabhu88bf6052018-08-28 16:21:26 -0700437 logging.exception('servo verify failed for %s', newhost.hostname)
Richard Barnette9a26ad62016-06-10 12:03:08 -0700438 return newhost