2012-04-30 21:27:57 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# (c) 2012, Stephen Fromm <sfromm@gmail.com>
|
|
|
|
#
|
|
|
|
# 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/>.
|
2012-10-05 21:31:13 +00:00
|
|
|
# ansible-pull is a script that runs ansible in local mode
|
|
|
|
# after checking out a playbooks directory from git. There is an
|
|
|
|
# example playbook to bootstrap this script in the examples/ dir which
|
|
|
|
# installs ansible and sets it up to run on cron.
|
|
|
|
|
|
|
|
# usage:
|
|
|
|
# ansible-pull -d /var/lib/ansible \
|
|
|
|
# -U http://example.net/content.git [-C production] \
|
|
|
|
# [path/playbook.yml]
|
|
|
|
#
|
|
|
|
# the -d and -U arguments are required; the -C argument is optional.
|
|
|
|
#
|
|
|
|
# ansible-pull accepts an optional argument to specify a playbook
|
|
|
|
# location underneath the workdir and then searches the git repo
|
|
|
|
# for playbooks in the following order, stopping at the first match:
|
|
|
|
#
|
|
|
|
# 1. $workdir/path/playbook.yml, if specified
|
|
|
|
# 2. $workdir/$hostname.yml
|
|
|
|
# 3. $workdir/local.yml
|
|
|
|
#
|
|
|
|
# the git repo must contain at least one of these playbooks.
|
2012-04-30 21:27:57 +00:00
|
|
|
|
|
|
|
import os
|
2012-10-12 14:53:56 +00:00
|
|
|
import shutil
|
2012-05-02 16:48:15 +00:00
|
|
|
import subprocess
|
2012-04-30 21:27:57 +00:00
|
|
|
import sys
|
2012-10-05 21:31:13 +00:00
|
|
|
import datetime
|
2012-09-13 06:42:54 +00:00
|
|
|
import socket
|
2012-05-02 16:48:15 +00:00
|
|
|
from optparse import OptionParser
|
2012-04-30 21:27:57 +00:00
|
|
|
|
|
|
|
DEFAULT_PLAYBOOK = 'local.yml'
|
2013-06-30 21:20:58 +00:00
|
|
|
PLAYBOOK_ERRORS = {1: 'File does not exist',
|
|
|
|
2: 'File is not readable'}
|
|
|
|
|
2012-04-30 21:27:57 +00:00
|
|
|
|
2012-05-02 16:48:15 +00:00
|
|
|
def _run(cmd):
|
2012-10-12 14:35:21 +00:00
|
|
|
print >>sys.stderr, "Running: '%s'" % cmd
|
2012-05-02 16:48:15 +00:00
|
|
|
cmd = subprocess.Popen(cmd, shell=True,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(out, err) = cmd.communicate()
|
|
|
|
print out
|
|
|
|
if cmd.returncode != 0:
|
2012-10-12 14:35:21 +00:00
|
|
|
print >>sys.stderr, err
|
2013-06-30 21:20:58 +00:00
|
|
|
return cmd.returncode, out
|
|
|
|
|
2012-05-02 16:48:15 +00:00
|
|
|
|
2012-10-12 14:35:21 +00:00
|
|
|
def try_playbook(path):
|
|
|
|
if not os.path.exists(path):
|
|
|
|
return 1
|
|
|
|
if not os.access(path, os.R_OK):
|
|
|
|
return 2
|
|
|
|
return 0
|
|
|
|
|
2013-06-30 21:20:58 +00:00
|
|
|
|
2012-10-12 14:35:21 +00:00
|
|
|
def select_playbook(path, args):
|
|
|
|
playbook = None
|
|
|
|
if len(args) > 0 and args[0] is not None:
|
|
|
|
playbook = "%s/%s" % (path, args[0])
|
|
|
|
rc = try_playbook(playbook)
|
|
|
|
if rc != 0:
|
|
|
|
print >>sys.stderr, "%s: %s" % (playbook, PLAYBOOK_ERRORS[rc])
|
|
|
|
return None
|
|
|
|
return playbook
|
|
|
|
else:
|
|
|
|
hostpb = "%s/%s.yml" % (path, socket.getfqdn())
|
|
|
|
localpb = "%s/%s" % (path, DEFAULT_PLAYBOOK)
|
|
|
|
errors = []
|
|
|
|
for pb in [hostpb, localpb]:
|
|
|
|
rc = try_playbook(pb)
|
|
|
|
if rc == 0:
|
|
|
|
playbook = pb
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
errors.append("%s: %s" % (pb, PLAYBOOK_ERRORS[rc]))
|
|
|
|
if playbook is None:
|
|
|
|
print >>sys.stderr, "\n".join(errors)
|
|
|
|
return playbook
|
|
|
|
|
2013-06-30 21:20:58 +00:00
|
|
|
|
2012-04-30 21:27:57 +00:00
|
|
|
def main(args):
|
|
|
|
""" Set up and run a local playbook """
|
2012-10-12 14:35:21 +00:00
|
|
|
usage = "%prog [options] [playbook.yml]"
|
2012-10-05 21:31:13 +00:00
|
|
|
parser = OptionParser(usage=usage)
|
2012-10-12 14:53:56 +00:00
|
|
|
parser.add_option('--purge', default=False, action='store_true',
|
2013-06-27 01:57:31 +00:00
|
|
|
help='purge git checkout after playbook run')
|
2013-06-30 21:20:58 +00:00
|
|
|
parser.add_option('-o', '--only-if-changed', dest='ifchanged', default=False, action='store_true',
|
2013-06-30 23:31:37 +00:00
|
|
|
help='only run the playbook if the repository has been updated')
|
2012-04-30 21:27:57 +00:00
|
|
|
parser.add_option('-d', '--directory', dest='dest', default=None,
|
2013-06-27 01:57:31 +00:00
|
|
|
help='directory to clone the git repository to')
|
2012-10-05 21:31:13 +00:00
|
|
|
parser.add_option('-U', '--url', dest='url', default=None,
|
2013-06-27 01:57:31 +00:00
|
|
|
help='URL of the git repository')
|
2012-05-02 16:48:15 +00:00
|
|
|
parser.add_option('-C', '--checkout', dest='checkout',
|
|
|
|
default="HEAD",
|
2013-06-27 01:57:31 +00:00
|
|
|
help='branch/tag/commit to checkout; defaults to HEAD')
|
2013-05-21 05:25:01 +00:00
|
|
|
parser.add_option('-i', '--inventory-file', dest='inventory',
|
2013-06-27 01:57:31 +00:00
|
|
|
help="location of the inventory host file")
|
2012-04-30 21:27:57 +00:00
|
|
|
options, args = parser.parse_args(args)
|
|
|
|
|
2012-10-05 21:31:13 +00:00
|
|
|
if not options.dest:
|
2012-10-12 14:35:21 +00:00
|
|
|
parser.error("Missing required directory argument")
|
|
|
|
return 1
|
2012-10-05 21:31:13 +00:00
|
|
|
|
2013-07-05 17:57:01 +00:00
|
|
|
options.dest = os.path.abspath(options.dest)
|
|
|
|
|
2012-10-05 21:31:13 +00:00
|
|
|
if not options.url:
|
2012-10-12 14:35:21 +00:00
|
|
|
parser.error("URL for git repo not specified, use -h for help")
|
|
|
|
return 1
|
2012-10-05 21:31:13 +00:00
|
|
|
|
|
|
|
now = datetime.datetime.now()
|
2012-10-12 14:35:21 +00:00
|
|
|
print >>sys.stderr, now.strftime("Starting ansible-pull at %F %T")
|
2012-10-05 21:31:13 +00:00
|
|
|
|
2013-05-21 05:25:01 +00:00
|
|
|
inv_opts = 'localhost,'
|
2012-11-11 08:11:58 +00:00
|
|
|
limit_opts = 'localhost:%s:127.0.0.1' % socket.getfqdn()
|
2012-09-13 00:07:02 +00:00
|
|
|
git_opts = "repo=%s dest=%s version=%s" % (options.url, options.dest, options.checkout)
|
2013-05-21 05:25:01 +00:00
|
|
|
cmd = 'ansible all -c local -i "%s" --limit "%s" -m git -a "%s"' % (
|
|
|
|
inv_opts, limit_opts, git_opts
|
|
|
|
)
|
2013-06-30 21:20:58 +00:00
|
|
|
rc, out = _run(cmd)
|
2012-09-13 00:07:02 +00:00
|
|
|
if rc != 0:
|
|
|
|
return rc
|
2013-06-30 21:20:58 +00:00
|
|
|
elif options.ifchanged and '"changed": true' not in out:
|
|
|
|
print "Repository has not changed, quitting."
|
|
|
|
return 0
|
2012-09-13 00:07:02 +00:00
|
|
|
|
2012-10-12 14:35:21 +00:00
|
|
|
playbook = select_playbook(options.dest, args)
|
|
|
|
|
|
|
|
if playbook is None:
|
|
|
|
print >>sys.stderr, "Could not find a playbook to run."
|
|
|
|
return 1
|
2012-10-05 21:31:13 +00:00
|
|
|
|
2012-11-11 08:11:58 +00:00
|
|
|
cmd = 'ansible-playbook -c local --limit "%s" %s' % (limit_opts, playbook)
|
2013-05-21 05:25:01 +00:00
|
|
|
if options.inventory:
|
|
|
|
cmd += ' -i "%s"' % options.inventory
|
2012-04-30 21:27:57 +00:00
|
|
|
os.chdir(options.dest)
|
2013-06-30 21:20:58 +00:00
|
|
|
rc, out = _run(cmd)
|
2012-10-12 14:53:56 +00:00
|
|
|
|
|
|
|
if options.purge:
|
|
|
|
os.chdir('/')
|
|
|
|
try:
|
|
|
|
shutil.rmtree(options.dest)
|
|
|
|
except Exception, e:
|
|
|
|
print >>sys.stderr, "Failed to remove %s: %s" % (options.dest, str(e))
|
|
|
|
|
2012-05-02 16:48:15 +00:00
|
|
|
return rc
|
2012-04-30 21:27:57 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
sys.exit(main(sys.argv[1:]))
|
2012-05-02 16:48:15 +00:00
|
|
|
except KeyboardInterrupt, e:
|
|
|
|
print >>sys.stderr, "Exit on user request.\n"
|
2012-04-30 21:27:57 +00:00
|
|
|
sys.exit(1)
|