Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Use pexpect timeouts instead of stopit timeouts
Browse files Browse the repository at this point in the history
Thanks to chambrid for the review.
  • Loading branch information
Noah Lavine committed Dec 15, 2017
1 parent 694bdf6 commit 986eb96
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Expand Up @@ -2,7 +2,6 @@ ansible
pexpect
future
sh
stopit
pyxdg

# These requirements are for DEVELOPMENT ONLY!
Expand Down
22 changes: 17 additions & 5 deletions rho/ansible_utils.py
Expand Up @@ -90,12 +90,18 @@ class AnsibleProcessException(Exception):
pass


class AnsibleTimeoutException(Exception):
"""Exception raised when an Ansible process times out."""

pass


# pylint: disable=too-many-arguments,too-many-branches
# pylint: disable=too-many-statements,too-many-locals
def run_with_vault(cmd_string, vault_pass, env=None, log_path=None,
log_to_stdout=None, log_to_stdout_env=None,
ansible_verbosity=0, print_before_run=False,
error_on_failure=True):
ansible_verbosity=0, timeout=None,
print_before_run=False, error_on_failure=True):
"""Runs ansible command allowing for password to be provided after
process triggered.
Expand All @@ -110,6 +116,7 @@ def run_with_vault(cmd_string, vault_pass, env=None, log_path=None,
the provided function as a filter. Defaults to None.
:param log_to_stdout_env: a dictionary of environment variables
:param ansible_verbosity: the number of v's of Ansible verbosity.
:param timeout: timeout, in seconds, for the called process.
:param print_before_run: if true, print the command string before running
it. Defaults to False.
Expand All @@ -135,7 +142,7 @@ def run_with_vault(cmd_string, vault_pass, env=None, log_path=None,
log.debug('Running Ansible: %s', cmd_string)
if print_before_run:
print('Running:', cmd_string)
child = pexpect.spawn(cmd_string, timeout=None,
child = pexpect.spawn(cmd_string, timeout=timeout,
env=env)

if log_to_stdout is not None:
Expand Down Expand Up @@ -183,10 +190,15 @@ def run_with_vault(cmd_string, vault_pass, env=None, log_path=None,
tail_process.terminate()
sys.exit(1)
except pexpect.TIMEOUT:
print('Error: unexpected Ansible output')
child.terminate()
if tail_process is not None:
tail_process.terminate()
sys.exit(1)

if not timeout:
print('Error: unexpected Ansible output')
sys.exit(1)

raise AnsibleTimeoutException()

if tail_process is not None:
tail_process.terminate()
Expand Down
13 changes: 7 additions & 6 deletions rho/host_discovery.py
Expand Up @@ -14,8 +14,6 @@
import os
import re

import stopit

from rho import ansible_utils
from rho.translation import _
from rho.utilities import (log, str_to_ascii, PING_INVENTORY_PATH,
Expand Down Expand Up @@ -135,16 +133,19 @@ def create_ping_inventory(vault, vault_pass, profile_ranges, profile_port,
# verbosity can break our parsing of Ansible's output. This is
# a temporary fix - a better solution would be less-fragile
# output parsing.
with stopit.ThreadingTimeout(30 * 60) as timeout_mgr:
try:
ansible_utils.run_with_vault(
cmd_string, vault_pass,
log_path=PING_LOG_PATH,
env=my_env,
log_to_stdout=process_discovery_scan,
log_to_stdout_env=log_env,
ansible_verbosity=0, error_on_failure=False)

if timeout_mgr.state == timeout_mgr.TIMED_OUT:
ansible_verbosity=0,
timeout=30 * 60,
error_on_failure=False)
except ansible_utils.AnsibleTimeoutException:
# If the discovery scan times out, we'll just parse whatever
# information we have in the log file.
log.warning('Host discovery timed out before completion.')

with open(PING_LOG_PATH, 'r') as ping_log:
Expand Down

0 comments on commit 986eb96

Please sign in to comment.