Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / lib / remote_access.py
index ec1b7ee..b118c5b 100644 (file)
@@ -1,10 +1,11 @@
-#!/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
@@ -40,6 +41,14 @@ SSH_ERROR_CODE = 255
 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.
 
@@ -124,10 +133,6 @@ def CompileSSHConnectSettings(ConnectTimeout=30, ConnectionAttempts=4):
           '-o', 'UserKnownHostsFile=/dev/null', ]
 
 
-class SSHConnectionError(Exception):
-  """Raised when SSH connection has failed."""
-
-
 class RemoteAccess(object):
   """Provides access to a remote test machine."""
 
@@ -439,7 +444,7 @@ class RemoteDevice(object):
 
   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:
@@ -450,6 +455,7 @@ class RemoteDevice(object):
       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
@@ -464,6 +470,9 @@ class RemoteDevice(object):
     # 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(
@@ -475,6 +484,21 @@ class RemoteDevice(object):
     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,
@@ -646,7 +670,7 @@ class ChromiumOSDevice(RemoteDevice):
 
     return False
 
-  def _DisableRootfsVerification(self):
+  def DisableRootfsVerification(self):
     """Disables device rootfs verification."""
     logging.info('Disabling rootfs verification on device...')
     self.RunCommand(
@@ -676,7 +700,7 @@ class ChromiumOSDevice(RemoteDevice):
     # 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()