Add --only-if-changed option.

pull/3386/head
Stavros Korokithakis 12 years ago
parent 6d5ac43dcb
commit 53207ddbdf

@ -48,6 +48,7 @@ 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'}
def _run(cmd): def _run(cmd):
print >>sys.stderr, "Running: '%s'" % cmd print >>sys.stderr, "Running: '%s'" % cmd
cmd = subprocess.Popen(cmd, shell=True, cmd = subprocess.Popen(cmd, shell=True,
@ -56,7 +57,8 @@ def _run(cmd):
print out print out
if cmd.returncode != 0: if cmd.returncode != 0:
print >>sys.stderr, err print >>sys.stderr, err
return cmd.returncode return cmd.returncode, out
def try_playbook(path): def try_playbook(path):
if not os.path.exists(path): if not os.path.exists(path):
@ -65,6 +67,7 @@ def try_playbook(path):
return 2 return 2
return 0 return 0
def select_playbook(path, args): def select_playbook(path, args):
playbook = None playbook = None
if len(args) > 0 and args[0] is not None: if len(args) > 0 and args[0] is not None:
@ -89,12 +92,15 @@ def select_playbook(path, args):
print >>sys.stderr, "\n".join(errors) print >>sys.stderr, "\n".join(errors)
return playbook return playbook
def main(args): def main(args):
""" Set up and run a local playbook """ """ Set up and run a local playbook """
usage = "%prog [options] [playbook.yml]" usage = "%prog [options] [playbook.yml]"
parser = OptionParser(usage=usage) parser = OptionParser(usage=usage)
parser.add_option('--purge', default=False, action='store_true', parser.add_option('--purge', default=False, action='store_true',
help='Purge git checkout after playbook run') help='Purge git checkout after playbook run')
parser.add_option('-o', '--only-if-changed', dest='ifchanged', default=False, action='store_true',
help='Only run the playbook if the repository has been updated')
parser.add_option('-d', '--directory', dest='dest', default=None, parser.add_option('-d', '--directory', dest='dest', default=None,
help='Directory to clone git repository to') help='Directory to clone git repository to')
parser.add_option('-U', '--url', dest='url', default=None, parser.add_option('-U', '--url', dest='url', default=None,
@ -123,9 +129,12 @@ def main(args):
cmd = 'ansible all -c local -i "%s" --limit "%s" -m git -a "%s"' % ( cmd = 'ansible all -c local -i "%s" --limit "%s" -m git -a "%s"' % (
inv_opts, limit_opts, git_opts inv_opts, limit_opts, git_opts
) )
rc = _run(cmd) rc, out = _run(cmd)
if rc != 0: if rc != 0:
return rc return rc
elif options.ifchanged and '"changed": true' not in out:
print "Repository has not changed, quitting."
return 0
playbook = select_playbook(options.dest, args) playbook = select_playbook(options.dest, args)
@ -137,7 +146,7 @@ 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 = _run(cmd) rc, out = _run(cmd)
if options.purge: if options.purge:
os.chdir('/') os.chdir('/')

Loading…
Cancel
Save