-#!/usr/bin/python
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Library containing functions to access a remote test device."""
+from __future__ import print_function
+
import glob
import logging
import os
DEV_BIN_PATHS = '/usr/local/bin:/usr/local/sbin'
+class SSHConnectionError(Exception):
+ """Raised when SSH connection has failed."""
+
+
+class DeviceNotPingable(Exception):
+ """Raised when device is not pingable."""
+
+
def NormalizePort(port, str_ok=True):
"""Checks if |port| is a valid port number and returns the number.
'-o', 'UserKnownHostsFile=/dev/null', ]
-class SSHConnectionError(Exception):
- """Raised when SSH connection has failed."""
-
-
class RemoteAccess(object):
"""Provides access to a remote test machine."""
def __init__(self, hostname, port=None, username=None,
base_dir=DEFAULT_BASE_DIR, connect_settings=None,
- private_key=None, debug_level=logging.DEBUG):
+ private_key=None, debug_level=logging.DEBUG, ping=True):
"""Initializes a RemoteDevice object.
Args:
connect_settings: Default SSH connection settings.
private_key: The identify file to pass to `ssh -i`.
debug_level: Setting debug level for logging.
+ ping: Whether to ping the device before attempting to connect.
"""
self.hostname = hostname
self.port = port
# Setup a working directory on the device.
self.base_dir = base_dir
+ if ping and not self.Pingable():
+ raise DeviceNotPingable('Device %s is not pingable.' % self.hostname)
+
# Do not call RunCommand here because we have not set up work directory yet.
self.BaseRunCommand(['mkdir', '-p', self.base_dir])
self.work_dir = self.BaseRunCommand(
self.cleanup_cmds = []
self.RegisterCleanupCmd(['rm', '-rf', self.work_dir])
+ def Pingable(self, timeout=20):
+ """Returns True if the device is pingable.
+
+ Args:
+ timeout: Timeout in seconds (default: 20 seconds).
+
+ Returns:
+ True if the device responded to the ping before |timeout|.
+ """
+ result = cros_build_lib.RunCommand(
+ ['ping', '-c', '1', '-w', str(timeout), self.hostname],
+ error_code_ok=True,
+ capture_output=True)
+ return result.returncode == 0
+
def _SetupSSH(self):
"""Setup the ssh connection with device."""
return RemoteAccess(self.hostname, self.tempdir.tempdir, port=self.port,
return False
- def _DisableRootfsVerification(self):
+ def DisableRootfsVerification(self):
"""Disables device rootfs verification."""
logging.info('Disabling rootfs verification on device...')
self.RunCommand(
# If the image is built with rootfs verification, turn off the
# rootfs verification. After reboot, the rootfs will be mounted as
# read-write (there is no need to remount).
- self._DisableRootfsVerification()
+ self.DisableRootfsVerification()
return not self._RootfsIsReadOnly()