Fixes #3973 Second Revision of live ansible-pull output

pull/5576/head
James Tanner 11 years ago
parent ea4bb9f57d
commit 78ec7c736f

@ -45,23 +45,18 @@ import sys
import datetime import datetime
import socket import socket
from ansible import utils from ansible import utils
from ansible.utils import cmd_functions
DEFAULT_REPO_TYPE = 'git' DEFAULT_REPO_TYPE = 'git'
DEFAULT_PLAYBOOK = 'local.yml' DEFAULT_PLAYBOOK = 'local.yml'
PLAYBOOK_ERRORS = {1: 'File does not exist', PLAYBOOK_ERRORS = {1: 'File does not exist',
2: 'File is not readable'} 2: 'File is not readable'}
VERBOSITY=0
def _run(cmd): def increment_debug(option, opt, value, parser):
print >>sys.stderr, "Running: '%s'" % cmd global VERBOSITY
cmd = subprocess.Popen(cmd, shell=True, VERBOSITY += 1
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 try_playbook(path): def try_playbook(path):
if not os.path.exists(path): if not os.path.exists(path):
@ -112,6 +107,8 @@ def main(args):
'not be updated') 'not be updated')
parser.add_option('-d', '--directory', dest='dest', default=None, parser.add_option('-d', '--directory', dest='dest', default=None,
help='directory to checkout repository to') 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, parser.add_option('-U', '--url', dest='url', default=None,
help='URL of the playbook repository') help='URL of the playbook repository')
parser.add_option('-C', '--checkout', dest='checkout', parser.add_option('-C', '--checkout', dest='checkout',
@ -119,6 +116,9 @@ def main(args):
'Defaults to behavior of repository module.') 'Defaults to behavior of repository module.')
parser.add_option('-i', '--inventory-file', dest='inventory', parser.add_option('-i', '--inventory-file', dest='inventory',
help="location of the inventory host file") 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', parser.add_option('-m', '--module-name', dest='module_name',
default=DEFAULT_REPO_TYPE, default=DEFAULT_REPO_TYPE,
help='Module name used to check out repository. ' help='Module name used to check out repository. '
@ -141,8 +141,14 @@ def main(args):
inv_opts = 'localhost,' inv_opts = 'localhost,'
limit_opts = 'localhost:%s:127.0.0.1' % hostname 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) 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: if options.checkout:
repo_opts += ' version=%s' % options.checkout repo_opts += ' version=%s' % options.checkout
path = utils.plugins.module_finder.find_plugin(options.module_name) 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"' % ( cmd = 'ansible all -i "%s" %s -m %s -a "%s"' % (
inv_opts, base_opts, options.module_name, repo_opts 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 rc != 0:
if options.force: if options.force:
print "Unable to update repository. Continuing with (forced) run of playbook." print "Unable to update repository. Continuing with (forced) run of playbook."
@ -172,7 +181,9 @@ def main(args):
if options.inventory: if options.inventory:
cmd += ' -i "%s"' % options.inventory cmd += ' -i "%s"' % options.inventory
os.chdir(options.dest) 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: if options.purge:
os.chdir('/') os.chdir('/')

@ -0,0 +1,59 @@
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# 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 <http://www.gnu.org/licenses/>.
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
Loading…
Cancel
Save