Exemple #1
0
def ShellCmdWithRetry(cmd, timeout=None, show_output=False,
                      retry=_SSH_CMD_MAX_RETRY):
    """Runs a shell command on remote device.

    If the network is unstable and causes SSH connect fail, it will retry. When
    it retry in a short time, you may encounter unstable network. We will use
    the mechanism of RETRY_BACKOFF_FACTOR. The retry time for each failure is
    times * retries.

    Args:
        cmd: String of the full SSH command to run, including the SSH binary and its arguments.
        timeout: Optional integer, number of seconds to give.
        show_output: Boolean, True to show command output in screen.
        retry: Integer, the retry times.

    Raises:
        errors.DeviceConnectionError: For any non-zero return code of
                                      remote_cmd.
    """
    utils.RetryExceptionType(
        exception_types=(errors.DeviceConnectionError, subprocess.CalledProcessError),
        max_retries=retry,
        functor=_SshLogOutput,
        sleep_multiplier=_SSH_CMD_RETRY_SLEEP,
        retry_backoff_factor=utils.DEFAULT_RETRY_BACKOFF_FACTOR,
        cmd=cmd,
        timeout=timeout,
        show_output=show_output)
    def InitResourceHandle(cls, oauth2_credentials):
        """Authenticate and initialize a Resource object.

        Authenticate http and create a Resource object with methods
        for interacting with the service.

        Args:
            oauth2_credentials: An oauth2client.OAuth2Credentials instance.

        Returns:
            An apiclient.discovery.Resource object
        """
        http_auth = oauth2_credentials.authorize(httplib2.Http())
        return utils.RetryExceptionType(
            exception_types=cls.RETRIABLE_AUTH_ERRORS,
            max_retries=cls.RETRY_COUNT,
            functor=build,
            sleep_multiplier=cls.RETRY_SLEEP_MULTIPLIER,
            retry_backoff_factor=cls.RETRY_BACKOFF_FACTOR,
            serviceName=cls.API_NAME,
            version=cls.API_VERSION,
            # This is workaround for a known issue of some veriosn
            # of api client.
            # https://github.com/google/google-api-python-client/issues/435
            cache_discovery=False,
            http=http_auth)
Exemple #3
0
    def UpdateFetchCvd(self):
        """Download fetch_cvd from the Build API, and upload it to a remote instance.

        The version of fetch_cvd to use is retrieved from the configuration file. Once fetch_cvd
        is on the instance, future commands can use it to download relevant Cuttlefish files from
        the Build API on the instance itself.
        """
        # TODO(schuffelen): Support fetch_cvd_version="latest" when there is
        # stronger automated testing on it.
        download_dir = tempfile.mkdtemp()
        download_target = os.path.join(download_dir, _FETCHER_NAME)
        utils.RetryExceptionType(
            exception_types=ssl.SSLError,
            max_retries=_MAX_RETRY,
            functor=self._build_api.DownloadArtifact,
            sleep_multiplier=_RETRY_SLEEP_SECS,
            retry_backoff_factor=utils.DEFAULT_RETRY_BACKOFF_FACTOR,
            build_target=_FETCHER_BUILD_TARGET,
            build_id=self._fetch_cvd_version,
            resource_id=_FETCHER_NAME,
            local_dest=download_target,
            attempt_id="latest")
        fetch_cvd_stat = os.stat(download_target)
        os.chmod(download_target, fetch_cvd_stat.st_mode | stat.S_IEXEC)
        self._ssh.ScpPushFile(src_file=download_target, dst_file=_FETCHER_NAME)
        os.remove(download_target)
        os.rmdir(download_dir)
    def DisconnectAdb(self, retry=False):
        """Retry to disconnect adb.

        When retry=True, this method will retry to disconnect adb until adb
        device is completely gone.

        Args:
            retry: Boolean, True to retry disconnect on error.
        """
        retry_count = _MAX_RETRIES_ON_WAIT_ADB_GONE if retry else 0
        # Wait for adb device is reset and gone.
        utils.RetryExceptionType(exception_types=errors.AdbDisconnectFailed,
                                 max_retries=retry_count,
                                 functor=self._DisconnectAndRaiseError,
                                 sleep_multiplier=_WAIT_ADB_SLEEP_MULTIPLIER,
                                 retry_backoff_factor=
                                 _WAIT_ADB_RETRY_BACKOFF_FACTOR)
Exemple #5
0
    def WaitForSsh(self, timeout=None, max_retry=_SSH_CMD_MAX_RETRY):
        """Wait until the remote instance is ready to accept commands over SSH.

        Args:
            timeout: Integer, the maximum time in seconds to wait for the
                     command to respond.
            max_retry: Integer, the maximum number of retry.

        Raises:
            errors.DeviceConnectionError: Ssh isn't ready in the remote instance.
        """
        ssh_timeout = timeout or constants.DEFAULT_SSH_TIMEOUT
        sleep_multiplier = ssh_timeout / sum(range(max_retry + 1))
        logger.debug("Retry with interval time: %s secs", str(sleep_multiplier))
        utils.RetryExceptionType(
            exception_types=errors.DeviceConnectionError,
            max_retries=max_retry,
            functor=self.CheckSshConnection,
            sleep_multiplier=sleep_multiplier,
            retry_backoff_factor=utils.DEFAULT_RETRY_BACKOFF_FACTOR,
            timeout=_CONNECTION_TIMEOUT)
    def GetUrl(self, bucket_name, object_name):
        """Get information about a file object.

        Args:
            bucket_name: string, google cloud storage bucket name.
            object_name: string, name of the file to look for.

        Returns:
            Value of "selfLink" field from the response, which represents
            a url to the file.

        Raises:
            errors.ResourceNotFoundError: when file is not found.
        """
        item = utils.RetryExceptionType(errors.ResourceNotFoundError,
                                        self.GET_OBJ_MAX_RETRY,
                                        self.Get,
                                        self.GET_OBJ_RETRY_SLEEP,
                                        utils.DEFAULT_RETRY_BACKOFF_FACTOR,
                                        bucket_name=bucket_name,
                                        object_name=object_name)
        return item["selfLink"]
Exemple #7
0
    def InitResourceHandle(cls, oauth2_credentials):
        """Authenticate and initialize a Resource object.

        Authenticate http and create a Resource object with methods
        for interacting with the service.

        Args:
            oauth2_credentials: An oauth2client.OAuth2Credentials instance.

        Returns:
            An apiclient.discovery.Resource object
        """
        http_auth = oauth2_credentials.authorize(httplib2.Http())
        return utils.RetryExceptionType(
                exception_types=cls.RETRIABLE_AUTH_ERRORS,
                max_retries=cls.RETRY_COUNT,
                functor=build,
                sleep_multiplier=cls.RETRY_SLEEP_MULTIPLIER,
                retry_backoff_factor=cls.RETRY_BACKOFF_FACTOR,
                serviceName=cls.API_NAME,
                version=cls.API_VERSION,
                http=http_auth)