<commit_before>#!/usr/bin/env python

# Copyright (C) 2011-2013 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
#
# See the License for the specific language governing permissions and
# limitations under the License.

import errno
import time
import socket
import logging
from sshclient import SSHClient

import fakeprovider

log = logging.getLogger("nodepool.utils")


def iterate_timeout(max_seconds, purpose):
    start = time.time()
    count = 0
    while (time.time() < start + max_seconds):
        count += 1
        yield count
        time.sleep(2)
    raise Exception("Timeout waiting for %s" % purpose)


def ssh_connect(ip, username, connect_kwargs={}, timeout=60):
    if ip == 'fake':
        return fakeprovider.FakeSSHClient()
    # HPcloud may return ECONNREFUSED or EHOSTUNREACH
    # for about 30 seconds after adding the IP
    for count in iterate_timeout(timeout, "ssh access"):
        try:
            client = SSHClient(ip, username, **connect_kwargs)
            break
        except socket.error, e:
            if e[0] not in [errno.ECONNREFUSED, errno.EHOSTUNREACH]:
                log.exception('Exception while testing ssh access:')

    out = client.ssh("test ssh access", "echo access okay", output=True)<commit_msg>Retry ssh connections on auth failure.<commit_after>#!/usr/bin/env python

# Copyright (C) 2011-2013 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
#
# See the License for the specific language governing permissions and
# limitations under the License.

import errno
import time
import socket
import logging
from sshclient import SSHClient

import fakeprovider
import paramiko

log = logging.getLogger("nodepool.utils")


def iterate_timeout(max_seconds, purpose):
    start = time.time()
    count = 0
    while (time.time() < start + max_seconds):
        count += 1
        yield count
        time.sleep(2)
    raise Exception("Timeout waiting for %s" % purpose)


def ssh_connect(ip, username, connect_kwargs={}, timeout=60):
    if ip == 'fake':
        return fakeprovider.FakeSSHClient()
    # HPcloud may return ECONNREFUSED or EHOSTUNREACH
    # for about 30 seconds after adding the IP
    for count in iterate_timeout(timeout, "ssh access"):
        try:
            client = SSHClient(ip, username, **connect_kwargs)
            break
        except paramiko.AuthenticationException, e:
            # This covers the case where the cloud user is created
            # after sshd is up (Fedora for example)
            log.info('Password auth exception. Try number %i...' % count)
        except socket.error, e:
            if e[0] not in [errno.ECONNREFUSED, errno.EHOSTUNREACH]:
                log.exception('Exception while testing ssh access:')

    out = client.ssh("test ssh access", "echo access okay", output=True)
{'fraction_non_alphanumeric': 0.08641634456101602, 'fraction_numerical': 0.011043622308117063, 'mean_word_length': 3.2473622508792497, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6573620', 'n_tokens_mistral': 1029, 'n_tokens_neox': 969, 'n_words': 395}