2012-02-27 04:17:31 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2012-02-29 01:08:09 +01:00
|
|
|
# (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/>.
|
|
|
|
|
2012-02-27 04:17:31 +01:00
|
|
|
# I wanted to keep this simple at first, so for now this checks out
|
2012-05-10 06:39:15 +02:00
|
|
|
# from the given branch of a repo at a particular SHA or
|
2012-02-27 04:17:31 +01:00
|
|
|
# tag. Latest is not supported, you should not be doing
|
2012-05-10 06:39:15 +02:00
|
|
|
# that. Contribs welcome! -- MPD
|
2012-02-27 04:17:31 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
import os
|
2012-05-08 22:20:08 +02:00
|
|
|
import re
|
2012-02-27 04:17:31 +01:00
|
|
|
import sys
|
|
|
|
import shlex
|
|
|
|
import subprocess
|
2012-05-10 00:08:45 +02:00
|
|
|
import syslog
|
2012-02-27 04:17:31 +01:00
|
|
|
|
2012-05-02 06:22:31 +02:00
|
|
|
# ===========================================
|
|
|
|
# Basic support methods
|
|
|
|
|
|
|
|
def exit_json(rc=0, **kwargs):
|
|
|
|
print json.dumps(kwargs)
|
|
|
|
sys.exit(rc)
|
|
|
|
|
|
|
|
def fail_json(**kwargs):
|
|
|
|
kwargs['failed'] = True
|
|
|
|
exit_json(rc=1, **kwargs)
|
|
|
|
|
2012-02-27 04:17:31 +01:00
|
|
|
# ===========================================
|
2012-02-27 04:31:42 +01:00
|
|
|
# convert arguments of form a=b c=d
|
2012-02-27 04:17:31 +01:00
|
|
|
# to a dictionary
|
|
|
|
# FIXME: make more idiomatic
|
|
|
|
|
2012-03-14 05:39:18 +01:00
|
|
|
if len(sys.argv) == 1:
|
2012-05-02 06:22:31 +02:00
|
|
|
fail_json(msg="the command module requires arguments (-a)")
|
2012-03-14 05:39:18 +01:00
|
|
|
|
|
|
|
argfile = sys.argv[1]
|
|
|
|
if not os.path.exists(argfile):
|
2012-05-02 06:22:31 +02:00
|
|
|
fail_json(msg="Argument file not found")
|
2012-03-14 05:39:18 +01:00
|
|
|
|
|
|
|
args = open(argfile, 'r').read()
|
2012-02-27 04:17:31 +01:00
|
|
|
items = shlex.split(args)
|
2012-05-10 00:08:45 +02:00
|
|
|
syslog.openlog('ansible-%s' % os.path.basename(__file__))
|
|
|
|
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args)
|
2012-03-14 05:39:18 +01:00
|
|
|
|
|
|
|
if not len(items):
|
2012-05-02 06:22:31 +02:00
|
|
|
fail_json(msg="the command module requires arguments (-a)")
|
2012-03-14 05:39:18 +01:00
|
|
|
|
2012-02-27 04:17:31 +01:00
|
|
|
params = {}
|
|
|
|
for x in items:
|
|
|
|
(k, v) = x.split("=")
|
|
|
|
params[k] = v
|
|
|
|
|
|
|
|
dest = params['dest']
|
|
|
|
repo = params['repo']
|
2012-05-08 22:20:08 +02:00
|
|
|
branch = params.get('branch')
|
2012-05-02 06:22:31 +02:00
|
|
|
version = params.get('version', 'HEAD')
|
2012-02-27 04:17:31 +01:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
|
|
|
|
def get_version(dest):
|
|
|
|
''' samples the version of the git repo '''
|
|
|
|
os.chdir(dest)
|
|
|
|
cmd = "git show --abbrev-commit"
|
|
|
|
sha = os.popen(cmd).read().split("\n")
|
|
|
|
sha = sha[0].split()[1]
|
|
|
|
return sha
|
|
|
|
|
2012-05-08 22:20:08 +02:00
|
|
|
def clone(repo, dest, branch):
|
2012-02-27 04:17:31 +01:00
|
|
|
''' makes a new git repo if it does not already exist '''
|
|
|
|
try:
|
|
|
|
os.makedirs(dest)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
cmd = "git clone %s %s" % (repo, dest)
|
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2012-05-08 22:20:08 +02:00
|
|
|
(out, err) = cmd.communicate()
|
|
|
|
rc = cmd.returncode
|
|
|
|
|
|
|
|
if branch is None or rc != 0:
|
|
|
|
return (out, err)
|
|
|
|
|
|
|
|
os.chdir(dest)
|
|
|
|
cmd = "git checkout -b %s origin/%s" % (branch, branch)
|
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2012-02-27 04:17:31 +01:00
|
|
|
return cmd.communicate()
|
|
|
|
|
2012-05-02 06:53:20 +02:00
|
|
|
def reset(dest):
|
|
|
|
'''
|
|
|
|
Resets the index and working tree to HEAD.
|
|
|
|
Discards any changes to tracked files in working
|
|
|
|
tree since that commit.
|
|
|
|
'''
|
|
|
|
os.chdir(dest)
|
|
|
|
cmd = "git reset --hard HEAD"
|
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(out, err) = cmd.communicate()
|
|
|
|
rc = cmd.returncode
|
|
|
|
return (rc, out, err)
|
|
|
|
|
2012-05-08 22:20:08 +02:00
|
|
|
def switchLocalBranch( branch ):
|
|
|
|
cmd = "git checkout %s" % branch
|
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
return cmd.communicate()
|
|
|
|
|
|
|
|
def pull(repo, dest, branch):
|
2012-02-27 04:17:31 +01:00
|
|
|
''' updates repo from remote sources '''
|
|
|
|
os.chdir(dest)
|
2012-05-08 22:20:08 +02:00
|
|
|
cmd = "git branch -a"
|
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(gbranch_out, gbranch_err) = cmd.communicate()
|
|
|
|
|
|
|
|
try:
|
|
|
|
m = re.search( '^\* (\S+|\(no branch\))$', gbranch_out, flags=re.M )
|
|
|
|
cur_branch = m.group(1)
|
|
|
|
m = re.search( '\s+remotes/origin/HEAD -> origin/(\S+)', gbranch_out, flags=re.M )
|
|
|
|
default_branch = m.group(1)
|
|
|
|
except:
|
|
|
|
fail_json(msg="could not determine branch data - received: %s" % gbranch_out)
|
|
|
|
|
|
|
|
if branch is None:
|
|
|
|
if cur_branch != default_branch:
|
|
|
|
(out, err) = switchLocalBranch( default_branch )
|
|
|
|
|
|
|
|
cmd = "git pull -u origin"
|
|
|
|
|
|
|
|
elif branch == cur_branch:
|
|
|
|
cmd = "git pull -u origin"
|
|
|
|
|
|
|
|
else:
|
|
|
|
m = re.search( '^\s+%s$' % branch, gbranch_out, flags=re.M ) #see if we've already checked it out
|
|
|
|
if m is None:
|
|
|
|
cmd = "git checkout -b %s origin/%s" % (branch, branch)
|
|
|
|
|
|
|
|
else:
|
|
|
|
(out, err) = switchLocalBranch( branch )
|
|
|
|
cmd = "git pull -u origin"
|
|
|
|
|
2012-02-27 04:17:31 +01:00
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
return cmd.communicate()
|
|
|
|
|
2012-05-08 22:24:29 +02:00
|
|
|
def switchver(version, dest):
|
2012-02-27 04:17:31 +01:00
|
|
|
''' once pulled, switch to a particular SHA or tag '''
|
|
|
|
os.chdir(dest)
|
|
|
|
if version != 'HEAD':
|
|
|
|
cmd = "git checkout %s --force" % version
|
|
|
|
else:
|
|
|
|
# is there a better way to do this?
|
|
|
|
cmd = "git rebase origin"
|
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(out, err) = cmd.communicate()
|
|
|
|
return (out, err)
|
|
|
|
|
|
|
|
|
|
|
|
gitconfig = os.path.join(dest, '.git', 'config')
|
|
|
|
|
|
|
|
out, err, status = (None, None, None)
|
|
|
|
|
|
|
|
# if there is no git configuration, do a clone operation
|
|
|
|
# else pull and switch the version
|
|
|
|
|
|
|
|
before = None
|
|
|
|
if not os.path.exists(gitconfig):
|
2012-05-08 22:20:08 +02:00
|
|
|
(out, err) = clone(repo, dest, branch)
|
2012-02-27 04:17:31 +01:00
|
|
|
else:
|
|
|
|
# else do a pull
|
|
|
|
before = get_version(dest)
|
2012-05-02 06:53:20 +02:00
|
|
|
(rc, out, err) = reset(dest)
|
|
|
|
if rc != 0:
|
|
|
|
fail_json(out=out, err=err)
|
2012-05-08 22:20:08 +02:00
|
|
|
(out, err) = pull(repo, dest, branch)
|
2012-02-27 04:17:31 +01:00
|
|
|
|
|
|
|
# handle errors from clone or pull
|
|
|
|
|
|
|
|
if out.find('error') != -1:
|
2012-05-02 06:22:31 +02:00
|
|
|
fail_json(out=out, err=err)
|
2012-02-27 04:17:31 +01:00
|
|
|
|
|
|
|
# switch to version specified regardless of whether
|
|
|
|
# we cloned or pulled
|
|
|
|
|
2012-05-08 22:24:29 +02:00
|
|
|
(out, err) = switchver(version, dest)
|
2012-02-27 04:17:31 +01:00
|
|
|
if err.find('error') != -1:
|
2012-05-02 06:22:31 +02:00
|
|
|
fail_json(out=out, err=err)
|
2012-02-27 04:17:31 +01:00
|
|
|
|
|
|
|
# determine if we changed anything
|
|
|
|
|
|
|
|
after = get_version(dest)
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
if before != after:
|
|
|
|
changed = True
|
|
|
|
|
2012-05-02 06:22:31 +02:00
|
|
|
exit_json(changed=changed, before=before, after=after)
|