Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 1 | # Copyright (c) 2019 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 is a base host class for servohost and labstation.""" |
| 10 | |
| 11 | |
| 12 | import httplib |
| 13 | import logging |
| 14 | import socket |
| 15 | import xmlrpclib |
| 16 | |
| 17 | from autotest_lib.client.bin import utils |
| 18 | from autotest_lib.client.common_lib import error |
| 19 | from autotest_lib.client.common_lib import hosts |
| 20 | from autotest_lib.client.common_lib import lsbrelease_utils |
| 21 | from autotest_lib.client.common_lib.cros import dev_server |
| 22 | from autotest_lib.client.cros import constants as client_constants |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 23 | from autotest_lib.server import site_utils as server_utils |
| 24 | from autotest_lib.server.cros import autoupdater |
| 25 | from autotest_lib.server.hosts import ssh_host |
| 26 | from autotest_lib.site_utils.rpm_control_system import rpm_client |
| 27 | |
| 28 | try: |
| 29 | from chromite.lib import metrics |
| 30 | except ImportError: |
| 31 | metrics = utils.metrics_mock |
| 32 | |
| 33 | |
| 34 | class BaseServoHost(ssh_host.SSHHost): |
| 35 | """Base host class for a host that manage servo(s). |
| 36 | E.g. beaglebone, labstation. |
| 37 | """ |
Garry Wang | 3d84a16 | 2020-01-24 13:29:43 +0000 | [diff] [blame] | 38 | REBOOT_CMD = 'sleep 5; reboot & sleep 10; reboot -f' |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 39 | |
Garry Wang | 79e9af6 | 2019-06-12 15:19:19 -0700 | [diff] [blame] | 40 | TEMP_FILE_DIR = '/var/lib/servod/' |
| 41 | |
| 42 | LOCK_FILE_POSTFIX = '_in_use' |
| 43 | REBOOT_FILE_POSTFIX = '_reboot' |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 44 | |
Garry Wang | 5715ee5 | 2019-12-23 11:00:47 -0800 | [diff] [blame] | 45 | # Time to wait a rebooting servohost, in seconds. |
Garry Wang | fb25343 | 2019-09-11 17:08:38 -0700 | [diff] [blame] | 46 | REBOOT_TIMEOUT = 240 |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 47 | |
Garry Wang | 5715ee5 | 2019-12-23 11:00:47 -0800 | [diff] [blame] | 48 | # Timeout value to power cycle a servohost, in seconds. |
| 49 | BOOT_TIMEOUT = 240 |
| 50 | |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 51 | |
| 52 | def _initialize(self, hostname, is_in_lab=None, *args, **dargs): |
| 53 | """Construct a BaseServoHost object. |
| 54 | |
| 55 | @param is_in_lab: True if the servo host is in Cros Lab. Default is set |
| 56 | to None, for which utils.host_is_in_lab_zone will be |
| 57 | called to check if the servo host is in Cros lab. |
| 58 | |
| 59 | """ |
| 60 | super(BaseServoHost, self)._initialize(hostname=hostname, |
| 61 | *args, **dargs) |
| 62 | self._is_localhost = (self.hostname == 'localhost') |
| 63 | if self._is_localhost: |
| 64 | self._is_in_lab = False |
| 65 | elif is_in_lab is None: |
| 66 | self._is_in_lab = utils.host_is_in_lab_zone(self.hostname) |
| 67 | else: |
| 68 | self._is_in_lab = is_in_lab |
| 69 | |
| 70 | # Commands on the servo host must be run by the superuser. |
| 71 | # Our account on a remote host is root, but if our target is |
| 72 | # localhost then we might be running unprivileged. If so, |
| 73 | # `sudo` will have to be added to the commands. |
| 74 | if self._is_localhost: |
| 75 | self._sudo_required = utils.system_output('id -u') != '0' |
| 76 | else: |
| 77 | self._sudo_required = False |
| 78 | |
| 79 | self._is_labstation = None |
Gregory Nisbet | 8e2fbb2 | 2019-12-05 11:36:37 -0800 | [diff] [blame] | 80 | self._dut_host_info = None |
Otabek Kasimov | 2b50cdb | 2020-07-06 19:16:06 -0700 | [diff] [blame] | 81 | self._dut_hostname = None |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 82 | |
| 83 | |
| 84 | def get_board(self): |
| 85 | """Determine the board for this servo host. E.g. fizz-labstation |
| 86 | |
Garry Wang | 5e118c0 | 2019-09-25 14:24:57 -0700 | [diff] [blame] | 87 | @returns a string representing this labstation's board or None if |
| 88 | target host is not using a ChromeOS image(e.g. test in chroot). |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 89 | """ |
Garry Wang | 5e118c0 | 2019-09-25 14:24:57 -0700 | [diff] [blame] | 90 | output = self.run('cat /etc/lsb-release', ignore_status=True).stdout |
| 91 | return lsbrelease_utils.get_current_board(lsb_release_content=output) |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 92 | |
| 93 | |
Garry Wang | d736748 | 2020-02-27 13:52:40 -0800 | [diff] [blame] | 94 | def set_dut_host_info(self, dut_host_info): |
| 95 | """ |
| 96 | @param dut_host_info: A HostInfo object. |
| 97 | """ |
| 98 | logging.info('setting dut_host_info field to (%s)', dut_host_info) |
| 99 | self._dut_host_info = dut_host_info |
| 100 | |
| 101 | |
| 102 | def get_dut_host_info(self): |
| 103 | """ |
| 104 | @return A HostInfo object. |
| 105 | """ |
| 106 | return self._dut_host_info |
Gregory Nisbet | 8e2fbb2 | 2019-12-05 11:36:37 -0800 | [diff] [blame] | 107 | |
| 108 | |
Otabek Kasimov | 2b50cdb | 2020-07-06 19:16:06 -0700 | [diff] [blame] | 109 | def set_dut_hostname(self, dut_hostname): |
| 110 | """ |
| 111 | @param dut_hostname: hostname of the DUT that connected to this servo. |
| 112 | """ |
| 113 | logging.info('setting dut_hostname as (%s)', dut_hostname) |
| 114 | self._dut_hostname = dut_hostname |
| 115 | |
| 116 | |
| 117 | def get_dut_hostname(self): |
| 118 | """ |
| 119 | @returns hostname of the DUT that connected to this servo. |
| 120 | """ |
| 121 | return self._dut_hostname |
| 122 | |
| 123 | |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 124 | def is_labstation(self): |
| 125 | """Determine if the host is a labstation |
| 126 | |
| 127 | @returns True if ths host is a labstation otherwise False. |
| 128 | """ |
| 129 | if self._is_labstation is None: |
| 130 | board = self.get_board() |
Garry Wang | 88dc863 | 2019-07-24 16:53:50 -0700 | [diff] [blame] | 131 | self._is_labstation = board is not None and 'labstation' in board |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 132 | |
| 133 | return self._is_labstation |
| 134 | |
| 135 | |
Garry Wang | 1483183 | 2020-03-04 17:21:49 -0800 | [diff] [blame] | 136 | def _get_lsb_release_content(self): |
| 137 | """Return the content of lsb-release file of host.""" |
| 138 | return self.run( |
| 139 | 'cat "%s"' % client_constants.LSB_RELEASE).stdout.strip() |
| 140 | |
| 141 | |
| 142 | def get_release_version(self): |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 143 | """Get the value of attribute CHROMEOS_RELEASE_VERSION from lsb-release. |
| 144 | |
| 145 | @returns The version string in lsb-release, under attribute |
Garry Wang | 1483183 | 2020-03-04 17:21:49 -0800 | [diff] [blame] | 146 | CHROMEOS_RELEASE_VERSION(e.g. 12900.0.0). None on fail. |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 147 | """ |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 148 | return lsbrelease_utils.get_chromeos_release_version( |
Garry Wang | 1483183 | 2020-03-04 17:21:49 -0800 | [diff] [blame] | 149 | lsb_release_content=self._get_lsb_release_content() |
| 150 | ) |
| 151 | |
| 152 | |
| 153 | def get_full_release_path(self): |
| 154 | """Get full release path from servohost as string. |
| 155 | |
| 156 | @returns full release path as a string |
| 157 | (e.g. fizz-labstation-release/R82.12900.0.0). None on fail. |
| 158 | """ |
| 159 | return lsbrelease_utils.get_chromeos_release_builder_path( |
| 160 | lsb_release_content=self._get_lsb_release_content() |
| 161 | ) |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 162 | |
| 163 | |
| 164 | def _check_update_status(self): |
| 165 | dummy_updater = autoupdater.ChromiumOSUpdater(update_url="", host=self) |
| 166 | return dummy_updater.check_update_status() |
| 167 | |
| 168 | |
| 169 | def is_in_lab(self): |
| 170 | """Check whether the servo host is a lab device. |
| 171 | |
| 172 | @returns: True if the servo host is in Cros Lab, otherwise False. |
| 173 | |
| 174 | """ |
| 175 | return self._is_in_lab |
| 176 | |
| 177 | |
| 178 | def is_localhost(self): |
| 179 | """Checks whether the servo host points to localhost. |
| 180 | |
| 181 | @returns: True if it points to localhost, otherwise False. |
| 182 | |
| 183 | """ |
| 184 | return self._is_localhost |
| 185 | |
| 186 | |
| 187 | def is_cros_host(self): |
| 188 | """Check if a servo host is running chromeos. |
| 189 | |
| 190 | @return: True if the servo host is running chromeos. |
| 191 | False if it isn't, or we don't have enough information. |
| 192 | """ |
| 193 | try: |
| 194 | result = self.run('grep -q CHROMEOS /etc/lsb-release', |
| 195 | ignore_status=True, timeout=10) |
| 196 | except (error.AutoservRunError, error.AutoservSSHTimeout): |
| 197 | return False |
| 198 | return result.exit_status == 0 |
| 199 | |
| 200 | |
| 201 | def reboot(self, *args, **dargs): |
| 202 | """Reboot using special servo host reboot command.""" |
| 203 | super(BaseServoHost, self).reboot(reboot_cmd=self.REBOOT_CMD, |
| 204 | *args, **dargs) |
| 205 | |
| 206 | |
Garry Wang | 1483183 | 2020-03-04 17:21:49 -0800 | [diff] [blame] | 207 | def update_image(self, wait_for_update=False, stable_version=None): |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 208 | """Update the image on the servo host, if needed. |
| 209 | |
| 210 | This method recognizes the following cases: |
| 211 | * If the Host is not running Chrome OS, do nothing. |
| 212 | * If a previously triggered update is now complete, reboot |
| 213 | to the new version. |
| 214 | * If the host is processing a previously triggered update, |
| 215 | do nothing. |
| 216 | * If the host is running a version of Chrome OS different |
| 217 | from the default for servo Hosts, trigger an update, but |
| 218 | don't wait for it to complete. |
| 219 | |
| 220 | @param wait_for_update If an update needs to be applied and |
| 221 | this is true, then don't return until the update is |
| 222 | downloaded and finalized, and the host rebooted. |
Garry Wang | 1483183 | 2020-03-04 17:21:49 -0800 | [diff] [blame] | 223 | @stable_version the target build number.(e.g. R82-12900.0.0) |
| 224 | |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 225 | @raises dev_server.DevServerException: If all the devservers are down. |
| 226 | @raises site_utils.ParseBuildNameException: If the devserver returns |
| 227 | an invalid build name. |
| 228 | @raises AutoservRunError: If the update_engine_client isn't present on |
| 229 | the host, and the host is a cros_host. |
| 230 | |
| 231 | """ |
| 232 | # servod could be running in a Ubuntu workstation. |
| 233 | if not self.is_cros_host(): |
| 234 | logging.info('Not attempting an update, either %s is not running ' |
| 235 | 'chromeos or we cannot find enough information about ' |
| 236 | 'the host.', self.hostname) |
| 237 | return |
| 238 | |
| 239 | if lsbrelease_utils.is_moblab(): |
| 240 | logging.info('Not attempting an update, %s is running moblab.', |
| 241 | self.hostname) |
| 242 | return |
| 243 | |
Garry Wang | 1483183 | 2020-03-04 17:21:49 -0800 | [diff] [blame] | 244 | if not stable_version: |
| 245 | logging.debug("BaseServoHost::update_image attempting to get" |
| 246 | " servo cros stable version") |
| 247 | try: |
| 248 | stable_version = (self.get_dut_host_info(). |
| 249 | servo_cros_stable_version) |
| 250 | except AttributeError: |
| 251 | logging.error("BaseServoHost::update_image failed to get" |
| 252 | " servo cros stable version.") |
Gregory Nisbet | 8e2fbb2 | 2019-12-05 11:36:37 -0800 | [diff] [blame] | 253 | |
Garry Wang | 1483183 | 2020-03-04 17:21:49 -0800 | [diff] [blame] | 254 | target_build = "%s-release/%s" % (self.get_board(), stable_version) |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 255 | target_build_number = server_utils.ParseBuildName( |
| 256 | target_build)[3] |
Garry Wang | 1483183 | 2020-03-04 17:21:49 -0800 | [diff] [blame] | 257 | current_build_number = self.get_release_version() |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 258 | |
| 259 | if current_build_number == target_build_number: |
| 260 | logging.info('servo host %s does not require an update.', |
| 261 | self.hostname) |
| 262 | return |
| 263 | |
| 264 | status = self._check_update_status() |
| 265 | if status in autoupdater.UPDATER_PROCESSING_UPDATE: |
| 266 | logging.info('servo host %s already processing an update, update ' |
| 267 | 'engine client status=%s', self.hostname, status) |
| 268 | elif status == autoupdater.UPDATER_NEED_REBOOT: |
| 269 | logging.info('An update has been completed and pending reboot now.') |
| 270 | # Labstation reboot is handled separately here as it require |
| 271 | # synchronized reboot among all managed DUTs. |
| 272 | if not self.is_labstation(): |
| 273 | self._servo_host_reboot() |
| 274 | else: |
| 275 | # For servo image staging, we want it as more widely distributed as |
| 276 | # possible, so that devservers' load can be evenly distributed. |
| 277 | # So use hostname instead of target_build as hash. |
| 278 | ds = dev_server.ImageServer.resolve(self.hostname, |
| 279 | hostname=self.hostname) |
| 280 | url = ds.get_update_url(target_build) |
| 281 | |
| 282 | updater = autoupdater.ChromiumOSUpdater(update_url=url, host=self) |
| 283 | |
| 284 | logging.info('Using devserver url: %s to trigger update on ' |
| 285 | 'servo host %s, from %s to %s', url, self.hostname, |
| 286 | current_build_number, target_build_number) |
| 287 | try: |
| 288 | ds.stage_artifacts(target_build, |
| 289 | artifacts=['full_payload']) |
| 290 | except Exception as e: |
| 291 | logging.error('Staging artifacts failed: %s', str(e)) |
| 292 | logging.error('Abandoning update for this cycle.') |
| 293 | else: |
| 294 | try: |
| 295 | updater.trigger_update() |
| 296 | except autoupdater.RootFSUpdateError as e: |
| 297 | trigger_download_status = 'failed with %s' % str(e) |
| 298 | metrics.Counter('chromeos/autotest/servo/' |
| 299 | 'rootfs_update_failed').increment() |
| 300 | else: |
| 301 | trigger_download_status = 'passed' |
| 302 | logging.info('Triggered download and update %s for %s, ' |
| 303 | 'update engine currently in status %s', |
| 304 | trigger_download_status, self.hostname, |
| 305 | updater.check_update_status()) |
| 306 | |
| 307 | if wait_for_update: |
| 308 | logging.info('Waiting for servo update to complete.') |
| 309 | self.run('update_engine_client --follow', ignore_status=True) |
| 310 | |
| 311 | |
| 312 | def has_power(self): |
| 313 | """Return whether or not the servo host is powered by PoE or RPM.""" |
| 314 | # TODO(fdeng): See crbug.com/302791 |
| 315 | # For now, assume all servo hosts in the lab have power. |
| 316 | return self.is_in_lab() |
| 317 | |
| 318 | |
| 319 | def power_cycle(self): |
| 320 | """Cycle power to this host via PoE(servo v3) or RPM(labstation) |
| 321 | if it is a lab device. |
| 322 | |
| 323 | @raises AutoservRepairError if it fails to power cycle the |
| 324 | servo host. |
| 325 | |
| 326 | """ |
| 327 | if self.has_power(): |
| 328 | try: |
| 329 | rpm_client.set_power(self, 'CYCLE') |
| 330 | except (socket.error, xmlrpclib.Error, |
| 331 | httplib.BadStatusLine, |
| 332 | rpm_client.RemotePowerException) as e: |
| 333 | raise hosts.AutoservRepairError( |
| 334 | 'Power cycling %s failed: %s' % (self.hostname, e), |
| 335 | 'power_cycle_via_rpm_failed' |
| 336 | ) |
| 337 | else: |
| 338 | logging.info('Skipping power cycling, not a lab device.') |
| 339 | |
| 340 | |
| 341 | def _servo_host_reboot(self): |
| 342 | """Reboot this servo host because a reboot is requested.""" |
| 343 | logging.info('Rebooting servo host %s from build %s', self.hostname, |
Garry Wang | 1483183 | 2020-03-04 17:21:49 -0800 | [diff] [blame] | 344 | self.get_release_version()) |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 345 | # Tell the reboot() call not to wait for completion. |
| 346 | # Otherwise, the call will log reboot failure if servo does |
| 347 | # not come back. The logged reboot failure will lead to |
| 348 | # test job failure. If the test does not require servo, we |
| 349 | # don't want servo failure to fail the test with error: |
| 350 | # `Host did not return from reboot` in status.log. |
| 351 | self.reboot(fastsync=True, wait=False) |
| 352 | |
| 353 | # We told the reboot() call not to wait, but we need to wait |
| 354 | # for the reboot before we continue. Alas. The code from |
| 355 | # here below is basically a copy of Host.wait_for_restart(), |
| 356 | # with the logging bits ripped out, so that they can't cause |
| 357 | # the failure logging problem described above. |
| 358 | # |
| 359 | # The black stain that this has left on my soul can never be |
| 360 | # erased. |
| 361 | old_boot_id = self.get_boot_id() |
| 362 | if not self.wait_down(timeout=self.WAIT_DOWN_REBOOT_TIMEOUT, |
| 363 | warning_timer=self.WAIT_DOWN_REBOOT_WARNING, |
| 364 | old_boot_id=old_boot_id): |
| 365 | raise error.AutoservHostError( |
| 366 | 'servo host %s failed to shut down.' % |
| 367 | self.hostname) |
Garry Wang | 79e9af6 | 2019-06-12 15:19:19 -0700 | [diff] [blame] | 368 | if self.wait_up(timeout=self.REBOOT_TIMEOUT): |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 369 | logging.info('servo host %s back from reboot, with build %s', |
Garry Wang | 1483183 | 2020-03-04 17:21:49 -0800 | [diff] [blame] | 370 | self.hostname, self.get_release_version()) |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 371 | else: |
| 372 | raise error.AutoservHostError( |
| 373 | 'servo host %s failed to come back from reboot.' % |
| 374 | self.hostname) |
| 375 | |
| 376 | |
| 377 | def make_ssh_command(self, user='root', port=22, opts='', hosts_file=None, |
| 378 | connect_timeout=None, alive_interval=None, alive_count_max=None, |
| 379 | connection_attempts=None): |
| 380 | """Override default make_ssh_command to use tuned options. |
| 381 | |
| 382 | Tuning changes: |
| 383 | - ConnectTimeout=30; maximum of 30 seconds allowed for an SSH |
| 384 | connection failure. Consistency with remote_access.py. |
| 385 | |
| 386 | - ServerAliveInterval=180; which causes SSH to ping connection every |
| 387 | 180 seconds. In conjunction with ServerAliveCountMax ensures |
| 388 | that if the connection dies, Autotest will bail out quickly. |
| 389 | |
| 390 | - ServerAliveCountMax=3; consistency with remote_access.py. |
| 391 | |
| 392 | - ConnectAttempts=4; reduce flakiness in connection errors; |
| 393 | consistency with remote_access.py. |
| 394 | |
| 395 | - UserKnownHostsFile=/dev/null; we don't care about the keys. |
| 396 | |
| 397 | - SSH protocol forced to 2; needed for ServerAliveInterval. |
| 398 | |
| 399 | @param user User name to use for the ssh connection. |
| 400 | @param port Port on the target host to use for ssh connection. |
| 401 | @param opts Additional options to the ssh command. |
| 402 | @param hosts_file Ignored. |
| 403 | @param connect_timeout Ignored. |
| 404 | @param alive_interval Ignored. |
| 405 | @param alive_count_max Ignored. |
| 406 | @param connection_attempts Ignored. |
| 407 | |
| 408 | @returns: An ssh command with the requested settings. |
| 409 | |
| 410 | """ |
| 411 | options = ' '.join([opts, '-o Protocol=2']) |
| 412 | return super(BaseServoHost, self).make_ssh_command( |
| 413 | user=user, port=port, opts=options, hosts_file='/dev/null', |
| 414 | connect_timeout=30, alive_interval=180, alive_count_max=3, |
| 415 | connection_attempts=4) |
| 416 | |
| 417 | |
| 418 | def _make_scp_cmd(self, sources, dest): |
| 419 | """Format scp command. |
| 420 | |
| 421 | Given a list of source paths and a destination path, produces the |
| 422 | appropriate scp command for encoding it. Remote paths must be |
| 423 | pre-encoded. Overrides _make_scp_cmd in AbstractSSHHost |
| 424 | to allow additional ssh options. |
| 425 | |
| 426 | @param sources: A list of source paths to copy from. |
| 427 | @param dest: Destination path to copy to. |
| 428 | |
| 429 | @returns: An scp command that copies |sources| on local machine to |
| 430 | |dest| on the remote servo host. |
| 431 | |
| 432 | """ |
| 433 | command = ('scp -rq %s -o BatchMode=yes -o StrictHostKeyChecking=no ' |
| 434 | '-o UserKnownHostsFile=/dev/null -P %d %s "%s"') |
| 435 | return command % (self._master_ssh.ssh_option, |
| 436 | self.port, sources, dest) |
| 437 | |
| 438 | |
| 439 | def run(self, command, timeout=3600, ignore_status=False, |
| 440 | stdout_tee=utils.TEE_TO_LOGS, stderr_tee=utils.TEE_TO_LOGS, |
| 441 | connect_timeout=30, ssh_failure_retry_ok=False, |
| 442 | options='', stdin=None, verbose=True, args=()): |
| 443 | """Run a command on the servo host. |
| 444 | |
| 445 | Extends method `run` in SSHHost. If the servo host is a remote device, |
| 446 | it will call `run` in SSHost without changing anything. |
| 447 | If the servo host is 'localhost', it will call utils.system_output. |
| 448 | |
| 449 | @param command: The command line string. |
| 450 | @param timeout: Time limit in seconds before attempting to |
| 451 | kill the running process. The run() function |
| 452 | will take a few seconds longer than 'timeout' |
| 453 | to complete if it has to kill the process. |
| 454 | @param ignore_status: Do not raise an exception, no matter |
| 455 | what the exit code of the command is. |
| 456 | @param stdout_tee/stderr_tee: Where to tee the stdout/stderr. |
| 457 | @param connect_timeout: SSH connection timeout (in seconds) |
| 458 | Ignored if host is 'localhost'. |
| 459 | @param options: String with additional ssh command options |
| 460 | Ignored if host is 'localhost'. |
| 461 | @param ssh_failure_retry_ok: when True and ssh connection failure is |
| 462 | suspected, OK to retry command (but not |
| 463 | compulsory, and likely not needed here) |
| 464 | @param stdin: Stdin to pass (a string) to the executed command. |
| 465 | @param verbose: Log the commands. |
| 466 | @param args: Sequence of strings to pass as arguments to command by |
| 467 | quoting them in " and escaping their contents if necessary. |
| 468 | |
| 469 | @returns: A utils.CmdResult object. |
| 470 | |
| 471 | @raises AutoservRunError if the command failed. |
| 472 | @raises AutoservSSHTimeout SSH connection has timed out. Only applies |
| 473 | when servo host is not 'localhost'. |
| 474 | |
| 475 | """ |
Gregory Nisbet | 32e7402 | 2020-07-14 18:42:30 -0700 | [diff] [blame] | 476 | run_args = { |
| 477 | 'command' : command, |
| 478 | 'timeout' : timeout, |
| 479 | 'ignore_status' : ignore_status, |
| 480 | 'stdout_tee' : stdout_tee, |
| 481 | 'stderr_tee' : stderr_tee, |
| 482 | # connect_timeout n/a for localhost |
| 483 | # options n/a for localhost |
| 484 | 'ssh_failure_retry_ok': ssh_failure_retry_ok, |
| 485 | 'stdin' : stdin, |
| 486 | 'verbose' : verbose, |
| 487 | 'args' : args, |
| 488 | } |
Garry Wang | ebc015b | 2019-06-06 17:45:06 -0700 | [diff] [blame] | 489 | if self.is_localhost(): |
| 490 | if self._sudo_required: |
| 491 | run_args['command'] = 'sudo -n sh -c "%s"' % utils.sh_escape( |
| 492 | command) |
| 493 | try: |
| 494 | return utils.run(**run_args) |
| 495 | except error.CmdError as e: |
| 496 | logging.error(e) |
| 497 | raise error.AutoservRunError('command execution error', |
| 498 | e.result_obj) |
| 499 | else: |
| 500 | run_args['connect_timeout'] = connect_timeout |
| 501 | run_args['options'] = options |
| 502 | return super(BaseServoHost, self).run(**run_args) |