From 78ec7c736fe56fd36adb1648f735315a714f5b98 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Fri, 10 Jan 2014 11:18:02 -0500 Subject: [PATCH] Fixes #3973 Second Revision of live ansible-pull output --- bin/ansible-pull | 37 ++++++++++++------- lib/ansible/utils/cmd_functions.py | 59 ++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 lib/ansible/utils/cmd_functions.py diff --git a/bin/ansible-pull b/bin/ansible-pull index 78f4d216e3..3253ced80c 100755 --- a/bin/ansible-pull +++ b/bin/ansible-pull @@ -45,23 +45,18 @@ import sys import datetime import socket from ansible import utils +from ansible.utils import cmd_functions DEFAULT_REPO_TYPE = 'git' DEFAULT_PLAYBOOK = 'local.yml' PLAYBOOK_ERRORS = {1: 'File does not exist', 2: 'File is not readable'} +VERBOSITY=0 -def _run(cmd): - print >>sys.stderr, "Running: '%s'" % cmd - cmd = subprocess.Popen(cmd, shell=True, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (out, err) = cmd.communicate() - print out - if cmd.returncode != 0: - print >>sys.stderr, err - return cmd.returncode, out - +def increment_debug(option, opt, value, parser): + global VERBOSITY + VERBOSITY += 1 def try_playbook(path): if not os.path.exists(path): @@ -112,6 +107,8 @@ def main(args): 'not be updated') parser.add_option('-d', '--directory', dest='dest', default=None, help='directory to checkout repository to') + #parser.add_option('-l', '--live', default=True, action='store_live', + # help='Print the ansible-playbook output while running') parser.add_option('-U', '--url', dest='url', default=None, help='URL of the playbook repository') parser.add_option('-C', '--checkout', dest='checkout', @@ -119,6 +116,9 @@ def main(args): 'Defaults to behavior of repository module.') parser.add_option('-i', '--inventory-file', dest='inventory', help="location of the inventory host file") + parser.add_option('-v', '--verbose', default=False, action="callback", + callback=increment_debug, + help='Pass -vvvv to ansible-playbook') parser.add_option('-m', '--module-name', dest='module_name', default=DEFAULT_REPO_TYPE, help='Module name used to check out repository. ' @@ -141,8 +141,14 @@ def main(args): inv_opts = 'localhost,' limit_opts = 'localhost:%s:127.0.0.1' % hostname - base_opts = '-c local --limit "%s"' % limit_opts repo_opts = "name=%s dest=%s" % (options.url, options.dest) + + if VERBOSITY == 0: + base_opts = '-c local --limit "%s"' % limit_opts + elif VERBOSITY > 0: + debug_level = ''.join([ "v" for x in range(0, VERBOSITY) ]) + base_opts = '-%s -c local --limit "%s"' % (debug_level, limit_opts) + if options.checkout: repo_opts += ' version=%s' % options.checkout path = utils.plugins.module_finder.find_plugin(options.module_name) @@ -152,7 +158,10 @@ def main(args): cmd = 'ansible all -i "%s" %s -m %s -a "%s"' % ( inv_opts, base_opts, options.module_name, repo_opts ) - rc, out = _run(cmd) + + # RUN THE CHECKOUT COMMAND + rc, out, err = cmd_functions.run_cmd(cmd, live=True) + if rc != 0: if options.force: print "Unable to update repository. Continuing with (forced) run of playbook." @@ -172,7 +181,9 @@ def main(args): if options.inventory: cmd += ' -i "%s"' % options.inventory os.chdir(options.dest) - rc, out = _run(cmd) + + # RUN THE PLAYBOOK COMMAND + rc, out, err = cmd_functions.run_cmd(cmd, live=True) if options.purge: os.chdir('/') diff --git a/lib/ansible/utils/cmd_functions.py b/lib/ansible/utils/cmd_functions.py new file mode 100644 index 0000000000..6525260f10 --- /dev/null +++ b/lib/ansible/utils/cmd_functions.py @@ -0,0 +1,59 @@ +# (c) 2012, Michael DeHaan +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +import os +import sys +import shlex +import subprocess +import select + +def run_cmd(cmd, live=False, readsize=10): + + #readsize = 10 + + cmdargs = shlex.split(cmd) + p = subprocess.Popen(cmdargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + stdout = '' + stderr = '' + rpipes = [p.stdout, p.stderr] + while True: + rfd, wfd, efd = select.select(rpipes, [], rpipes, 1) + + if p.stdout in rfd: + dat = os.read(p.stdout.fileno(), readsize) + if live: + sys.stdout.write(dat) + stdout += dat + if dat == '': + rpipes.remove(p.stdout) + if p.stderr in rfd: + dat = os.read(p.stderr.fileno(), readsize) + stderr += dat + if live: + sys.stdout.write(dat) + if dat == '': + rpipes.remove(p.stderr) + # only break out if we've emptied the pipes, or there is nothing to + # read from and the process has finished. + if (not rpipes or not rfd) and p.poll() is not None: + break + # Calling wait while there are still pipes to read can cause a lock + elif not rpipes and p.poll() == None: + p.wait() + + return p.returncode, stdout, stderr