2012-04-30 23:27:57 +02: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 23:31:13 +02: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 23:27:57 +02:00
|
|
|
|
|
|
|
import os
|
2012-05-02 18:48:15 +02:00
|
|
|
import subprocess
|
2012-04-30 23:27:57 +02:00
|
|
|
import sys
|
2012-10-05 23:31:13 +02:00
|
|
|
import datetime
|
2012-09-13 08:42:54 +02:00
|
|
|
import socket
|
2012-05-02 18:48:15 +02:00
|
|
|
from optparse import OptionParser
|
2012-04-30 23:27:57 +02:00
|
|
|
|
|
|
|
DEFAULT_PLAYBOOK = 'local.yml'
|
|
|
|
|
2012-05-02 18:48:15 +02:00
|
|
|
def _run(cmd):
|
|
|
|
cmd = subprocess.Popen(cmd, shell=True,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(out, err) = cmd.communicate()
|
|
|
|
print out
|
|
|
|
if cmd.returncode != 0:
|
|
|
|
print err
|
|
|
|
return cmd.returncode
|
|
|
|
|
2012-04-30 23:27:57 +02:00
|
|
|
def main(args):
|
|
|
|
""" Set up and run a local playbook """
|
2012-10-05 23:31:13 +02:00
|
|
|
usage = "%prog [options] [path/playbook.yml]"
|
|
|
|
parser = OptionParser(usage=usage)
|
2012-04-30 23:27:57 +02:00
|
|
|
parser.add_option('-d', '--directory', dest='dest', default=None,
|
2012-10-05 23:31:13 +02:00
|
|
|
help='Directory to checkout git repository to')
|
|
|
|
parser.add_option('-U', '--url', dest='url', default=None,
|
2012-04-30 23:27:57 +02:00
|
|
|
help='URL of git repository')
|
2012-05-02 18:48:15 +02:00
|
|
|
parser.add_option('-C', '--checkout', dest='checkout',
|
|
|
|
default="HEAD",
|
2012-04-30 23:27:57 +02:00
|
|
|
help='Branch/Tag/Commit to checkout. Defaults to HEAD.')
|
|
|
|
options, args = parser.parse_args(args)
|
|
|
|
|
2012-10-05 23:31:13 +02:00
|
|
|
if not options.dest:
|
|
|
|
parser.error("Missing required directory argument")
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if not options.url:
|
|
|
|
parser.error("URL for git repo not specified, use -h for help")
|
|
|
|
return 1
|
|
|
|
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
print now.strftime("ansible-pull_started: %Y%m%d-%H%M-%S"), "\n"
|
|
|
|
|
2012-09-13 02:07:02 +02:00
|
|
|
git_opts = "repo=%s dest=%s version=%s" % (options.url, options.dest, options.checkout)
|
|
|
|
cmd = 'ansible all -c local -m git -a "%s"' % git_opts
|
|
|
|
print "cmd=%s" % cmd, "\n"
|
|
|
|
rc = _run(cmd)
|
|
|
|
if rc != 0:
|
|
|
|
return rc
|
|
|
|
|
2012-09-13 08:42:54 +02:00
|
|
|
hostname = "%s.yml" % socket.getfqdn()
|
2012-10-05 23:31:13 +02:00
|
|
|
|
|
|
|
if not args:
|
|
|
|
try:
|
|
|
|
with open('%s/%s' % (options.dest, hostname)) as f: pass
|
|
|
|
playbook = hostname
|
|
|
|
print 'using playbook %s/%s' % (options.dest, hostname)
|
|
|
|
except IOError as e:
|
|
|
|
print 'playbook %s/%s does not exist, falling back to %s' % (options.dest, hostname, DEFAULT_PLAYBOOK)
|
|
|
|
try:
|
|
|
|
with open('%s/%s' % (options.dest, DEFAULT_PLAYBOOK)) as f: pass
|
|
|
|
playbook = DEFAULT_PLAYBOOK
|
|
|
|
print 'using playbook %s/%s' % (options.dest, DEFAULT_PLAYBOOK)
|
|
|
|
except IOError as e:
|
|
|
|
print 'playbook %s/%s does not exist, no playbooks to run; use -h for help' % (options.dest, DEFAULT_PLAYBOOK)
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
with open('%s/%s' % (options.dest, args[0])) as f: pass
|
|
|
|
playbook = args[0]
|
|
|
|
print 'using playbook %s/%s' % (options.dest, args[0])
|
|
|
|
except IOError as e:
|
|
|
|
print 'playbook %s/%s does not exist, falling back to %s' % (options.dest, args[0], hostname)
|
|
|
|
try:
|
|
|
|
with open('%s/%s' % (options.dest, hostname)) as f: pass
|
|
|
|
playbook = hostname
|
|
|
|
print 'using playbook %s/%s' % (options.dest, hostname)
|
|
|
|
except IOError as e:
|
|
|
|
print 'playbook %s/%s does not exist, falling back to %s' % (options.dest, hostname, DEFAULT_PLAYBOOK)
|
|
|
|
try:
|
|
|
|
with open('%s/%s' % (options.dest, DEFAULT_PLAYBOOK)) as f: pass
|
|
|
|
playbook = DEFAULT_PLAYBOOK
|
|
|
|
print 'using playbook %s/%s' % (options.dest, DEFAULT_PLAYBOOK)
|
|
|
|
except IOError as e:
|
|
|
|
print 'playbook %s/%s does not exist, no playbooks to run; use -h for help' % (options.dest, DEFAULT_PLAYBOOK)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
print
|
|
|
|
|
|
|
|
cmd = 'ansible-playbook -c local %s' % playbook
|
|
|
|
print "cmd=%s" % cmd
|
2012-04-30 23:27:57 +02:00
|
|
|
os.chdir(options.dest)
|
2012-05-02 18:48:15 +02:00
|
|
|
rc = _run(cmd)
|
|
|
|
return rc
|
2012-04-30 23:27:57 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
sys.exit(main(sys.argv[1:]))
|
2012-05-02 18:48:15 +02:00
|
|
|
except KeyboardInterrupt, e:
|
|
|
|
print >>sys.stderr, "Exit on user request.\n"
|
2012-04-30 23:27:57 +02:00
|
|
|
sys.exit(1)
|