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

Commit

Permalink
Present more status during discovery for larger environments. Closes #…
Browse files Browse the repository at this point in the history
…442. (#467)

* Present more status during discovery for larger environments. Closes #442.

* update based on code comment.
  • Loading branch information
chambridge authored Nov 13, 2017
1 parent fbd4a6d commit 7d67baa
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
12 changes: 7 additions & 5 deletions rho/ansible_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def log_yaml_inventory(label, inventory):

# pylint: disable=too-many-arguments
def run_with_vault(cmd_string, vault_pass, env=None, log_path=None,
log_to_stdout=True, ansible_verbosity=0):
log_to_stdout=None, ansible_verbosity=0):
"""Runs ansible command allowing for password to be provided after
process triggered.
Expand All @@ -97,8 +97,8 @@ def run_with_vault(cmd_string, vault_pass, env=None, log_path=None,
:param env: the environment to run the subprocess in.
:param log_path: a path to write the process's log to. Defaults to
'XDG_DATA_HOME/rho/ansible_log'.
:param log_to_stdout: if True, write Ansible's log to stdout. Defaults to
True.
:param log_to_stdout: if not None, write Ansible's log to stdout using
the provided function as a filter. Defaults to None.
:param ansible_verbosity: the number of v's of Ansible verbosity.
:returns: the popen.spawn object for the process.
"""
Expand All @@ -123,8 +123,10 @@ def run_with_vault(cmd_string, vault_pass, env=None, log_path=None,
child = pexpect.spawn(cmd_string, timeout=None,
env=env)

if log_to_stdout:
utilities.threaded_tailing(log_path, ansible_verbosity)
if log_to_stdout is not None:
utilities.threaded_tailing(path=log_path,
output_filter=log_to_stdout,
ansible_verbosity=ansible_verbosity)

child.expect('Vault password:')
child.sendline(vault_pass)
Expand Down
9 changes: 6 additions & 3 deletions rho/host_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@

"""Discover hosts on a network."""

from __future__ import print_function
from collections import defaultdict
import os
import re

from rho import ansible_utils
from rho.translation import _
from rho.utilities import log, str_to_ascii, PING_INVENTORY_PATH, PING_LOG_PATH
from rho.utilities import (log, str_to_ascii, PING_INVENTORY_PATH,
PING_LOG_PATH,
tail_discovery_scan)


def process_ping_output(out_lines):
Expand Down Expand Up @@ -122,7 +125,7 @@ def create_ping_inventory(vault, vault_pass, profile_ranges, profile_port,
ansible_utils.run_with_vault(cmd_string, vault_pass,
log_path=PING_LOG_PATH,
env=my_env,
log_to_stdout=True,
log_to_stdout=tail_discovery_scan,
ansible_verbosity=0)

with open(PING_LOG_PATH, 'r') as ping_log:
Expand All @@ -141,7 +144,7 @@ def create_ping_inventory(vault, vault_pass, profile_ranges, profile_port,
print(_('Failed to connect with auth "%s" to %d systems.') %
(credential.get('name'), num_failed))
if num_success > 0 or num_failed > 0:
print()
print('')

return list(success_hosts), success_port_map, success_auth_map, \
list(failed_hosts)
2 changes: 1 addition & 1 deletion rho/inventory_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def inventory_scan(hosts_yml_path, facts_to_collect, report_path,
cmd_string, vault_pass,
env=my_env,
log_path=log_path,
log_to_stdout=True,
log_to_stdout=utilities.tail_host_scan,
ansible_verbosity=verbosity)

return process.exitstatus == 0 and process.signalstatus is None
58 changes: 52 additions & 6 deletions rho/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,68 @@ def setup_logging(verbosity):
log.addHandler(stderr_handler)


def threaded_tailing(path, ansible_verbosity=0):
def threaded_tailing(path, output_filter, ansible_verbosity=0):
"""Follow and provide output using a thread
:param path: path to file to follow
:param output_filter: The function to handle the output filtering
:param ansible_verbosity: the verbosity level
"""
thread = threading.Thread(
target=tail_and_follow, args=(path, ansible_verbosity))
thread = threading.Thread(target=output_filter,
args=(path, ansible_verbosity))
thread.daemon = True
thread.start()


def tail_and_follow(path, ansible_verbosity):
"""Follow and provide output
# pylint: disable=unused-argument
def tail_discovery_scan(path, ansible_verbosity):
"""Follow and provide discovery scan output
:param path: tuple containing thepath to file to follow
:param path: tuple containing the path to file to follow
:param ansible_verbosity: the verbosity level
"""
hosts_processed = 0
hosts_successful = 0
hosts_unreachable = 0
hosts_failed = 0
if len(path) > 0: # pylint: disable=len-as-condition
# pylint: disable=no-member
for line in sh.tail('-f', '-n', '+0', path, _iter=True):
print_status = False
line = line.strip('\n')
if 'SUCCESS' in line:
hosts_successful += 1
hosts_processed += 1
print_status = True
elif 'FAILED' in line:
hosts_failed += 1
hosts_processed += 1
print_status = True
elif 'UNREACHABLE' in line:
hosts_unreachable += 1
hosts_processed += 1
print_status = True

# Display every 5 processed
if hosts_processed % 5 == 0 and print_status:
print(_('%d hosts processed with the current credential. ' %
hosts_processed))
if hosts_successful > 0:
print(_('%d hosts connected successfully with '
'the current credential.' % hosts_successful))
if hosts_failed > 0:
print(_('%d hosts failed to connect with the '
'current credential.' % hosts_failed))
if hosts_unreachable > 0:
print(_('%d hosts were unreachable.' % hosts_unreachable))

print('\n')


def tail_host_scan(path, ansible_verbosity):
"""Follow and provide host scan output
:param path: tuple containing the path to file to follow
:param ansible_verbosity: the verbosity level
"""
if len(path) > 0: # pylint: disable=len-as-condition
Expand Down
2 changes: 1 addition & 1 deletion test/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def setUp(self):
def test_threaded_tailing(self):
follow_list_out = six.StringIO()
with redirect_stdout(follow_list_out):
utilities.threaded_tailing(TMP_FOLLOW, 3)
utilities.threaded_tailing(TMP_FOLLOW, utilities.tail_host_scan, 3)
time.sleep(2)
self.assertEqual(follow_list_out.getvalue(), 'follow\n')

Expand Down

0 comments on commit 7d67baa

Please sign in to comment.