2012-02-27 04:17:31 +01:00
|
|
|
#!/usr/bin/python
|
2012-08-03 03:29:10 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-02-27 04:17:31 +01:00
|
|
|
|
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-09-30 08:50:25 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: git
|
|
|
|
author: Michael DeHaan
|
2013-03-30 20:44:34 +01:00
|
|
|
version_added: "0.0.1"
|
2012-09-30 08:50:25 +02:00
|
|
|
short_description: Deploy software (or files) from git checkouts
|
|
|
|
description:
|
2012-11-21 18:49:30 +01:00
|
|
|
- Manage I(git) checkouts of repositories to deploy files or software.
|
2012-09-30 08:50:25 +02:00
|
|
|
options:
|
|
|
|
repo:
|
|
|
|
required: true
|
|
|
|
aliases: [ name ]
|
|
|
|
description:
|
2012-11-21 18:49:30 +01:00
|
|
|
- git, SSH, or HTTP protocol address of the git repository.
|
2012-09-30 08:50:25 +02:00
|
|
|
dest:
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- Absolute path of where the repository should be checked out to.
|
|
|
|
version:
|
|
|
|
required: false
|
|
|
|
default: "HEAD"
|
|
|
|
description:
|
|
|
|
- What version of the repository to check out. This can be the
|
2013-10-16 08:16:25 +02:00
|
|
|
full 40-character I(SHA-1) hash, the literal string C(HEAD), a
|
|
|
|
branch name, or a tag name.
|
2014-01-11 16:35:33 +01:00
|
|
|
accept_hostkey:
|
|
|
|
required: false
|
2014-01-11 17:02:01 +01:00
|
|
|
default: false
|
2014-01-11 16:35:33 +01:00
|
|
|
version_added: "1.5"
|
|
|
|
description:
|
|
|
|
- Add the hostkey for the repo url if not already added.
|
2014-01-28 17:49:35 +01:00
|
|
|
If ssh_args contains "-o StrictHostKeyChecking=no", this
|
|
|
|
parameter is ignored.
|
2014-01-23 22:44:17 +01:00
|
|
|
ssh_opts:
|
2014-01-23 22:22:43 +01:00
|
|
|
required: false
|
|
|
|
default: None
|
|
|
|
version_added: "1.5"
|
|
|
|
description:
|
|
|
|
- Creates a wrapper script and exports the path as GIT_SSH
|
|
|
|
which git then automatically uses to override ssh arguments.
|
|
|
|
An example value could be "-o StrictHostKeyChecking=no"
|
2014-01-23 22:44:17 +01:00
|
|
|
key_file:
|
2014-01-23 22:22:43 +01:00
|
|
|
requird: false
|
|
|
|
default: None
|
|
|
|
version_added: "1.5"
|
|
|
|
description:
|
2014-01-23 22:44:17 +01:00
|
|
|
- Uses the same wrapper method as ssh_opts to pass
|
|
|
|
"-i <key_file>" to the ssh arguments used by git
|
2013-10-28 02:22:10 +01:00
|
|
|
reference:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "1.4"
|
|
|
|
description:
|
|
|
|
- Reference repository (see "git clone --reference ...")
|
2012-09-30 08:50:25 +02:00
|
|
|
remote:
|
|
|
|
required: false
|
|
|
|
default: "origin"
|
|
|
|
description:
|
2013-01-03 05:56:01 +01:00
|
|
|
- Name of the remote.
|
2012-09-30 08:50:25 +02:00
|
|
|
force:
|
|
|
|
required: false
|
|
|
|
default: "yes"
|
2013-03-12 13:18:12 +01:00
|
|
|
choices: [ "yes", "no" ]
|
2012-11-21 18:49:30 +01:00
|
|
|
version_added: "0.7"
|
2012-09-30 08:50:25 +02:00
|
|
|
description:
|
2012-11-21 18:49:30 +01:00
|
|
|
- If C(yes), any modified files in the working
|
2012-09-30 08:50:25 +02:00
|
|
|
repository will be discarded. Prior to 0.7, this was always
|
|
|
|
'yes' and could not be disabled.
|
2013-04-11 18:40:15 +02:00
|
|
|
depth:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "1.2"
|
|
|
|
description:
|
|
|
|
- Create a shallow clone with a history truncated to the specified
|
|
|
|
number or revisions. The minimum possible value is C(1), otherwise
|
|
|
|
ignored.
|
2013-05-16 07:14:17 +02:00
|
|
|
update:
|
|
|
|
required: false
|
|
|
|
default: "yes"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
version_added: "1.2"
|
|
|
|
description:
|
|
|
|
- If C(yes), repository will be updated using the supplied
|
|
|
|
remote. Otherwise the repo will be left untouched.
|
|
|
|
Prior to 1.2, this was always 'yes' and could not be disabled.
|
2013-09-26 23:15:28 +02:00
|
|
|
executable:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "1.4"
|
|
|
|
description:
|
|
|
|
- Path to git executable to use. If not supplied,
|
2013-10-01 22:46:17 +02:00
|
|
|
the normal mechanism for resolving binary paths will be used.
|
2013-10-28 02:22:10 +01:00
|
|
|
bare:
|
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
version_added: "1.4"
|
|
|
|
description:
|
|
|
|
- if C(yes), repository will be created as a bare repo, otherwise
|
|
|
|
it will be a standard repo with a workspace.
|
2013-08-07 22:33:29 +02:00
|
|
|
notes:
|
2013-11-01 01:00:11 +01:00
|
|
|
- "If the task seems to be hanging, first verify remote host is in C(known_hosts).
|
2013-10-31 23:00:08 +01:00
|
|
|
SSH will prompt user to authorize the first contact with a remote host. To avoid this prompt,
|
|
|
|
one solution is to add the remote host public key in C(/etc/ssh/ssh_known_hosts) before calling
|
2013-11-01 01:00:11 +01:00
|
|
|
the git module, with the following command: ssh-keyscan remote_host.com >> /etc/ssh/ssh_known_hosts."
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Example git checkout from Ansible Playbooks
|
|
|
|
- git: repo=git://foosball.example.org/path/to/repo.git
|
|
|
|
dest=/srv/checkout
|
|
|
|
version=release-0.22
|
|
|
|
|
|
|
|
# Example read-write git checkout from github
|
|
|
|
- git: repo=ssh://git@github.com/mylogin/hello.git dest=/home/mylogin/hello
|
|
|
|
|
|
|
|
# Example just ensuring the repo checkout exists
|
|
|
|
- git: repo=git://foosball.example.org/path/to/repo.git dest=/srv/checkout update=no
|
2012-09-30 08:50:25 +02:00
|
|
|
'''
|
|
|
|
|
2012-05-08 22:20:08 +02:00
|
|
|
import re
|
2012-08-28 11:28:50 +02:00
|
|
|
import tempfile
|
2012-02-27 04:17:31 +01:00
|
|
|
|
2014-01-23 22:22:43 +01:00
|
|
|
def write_ssh_wrapper():
|
2014-01-31 01:39:39 +01:00
|
|
|
fd, wrapper_path = tempfile.mkstemp()
|
|
|
|
fh = os.fdopen(fd, 'w+b')
|
2014-01-23 22:22:43 +01:00
|
|
|
template = """#!/bin/sh
|
|
|
|
if [ -z "$GIT_SSH_OPTS" ]; then
|
|
|
|
BASEOPTS=""
|
|
|
|
else
|
|
|
|
BASEOPTS=$GIT_SSH_OPTS
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$GIT_KEY" ]; then
|
|
|
|
ssh $BASEOPTS "$@"
|
|
|
|
else
|
|
|
|
ssh -i "$GIT_KEY" $BASEOPTS "$@"
|
|
|
|
fi
|
|
|
|
"""
|
|
|
|
fh.write(template)
|
|
|
|
fh.close()
|
|
|
|
st = os.stat(wrapper_path)
|
|
|
|
os.chmod(wrapper_path, st.st_mode | stat.S_IEXEC)
|
|
|
|
return wrapper_path
|
|
|
|
|
|
|
|
def set_git_ssh(ssh_wrapper, key_file, ssh_opts):
|
|
|
|
|
|
|
|
if os.environ.get("GIT_SSH"):
|
|
|
|
del os.environ["GIT_SSH"]
|
|
|
|
os.environ["GIT_SSH"] = ssh_wrapper
|
|
|
|
|
|
|
|
if os.environ.get("GIT_KEY"):
|
|
|
|
del os.environ["GIT_KEY"]
|
|
|
|
|
|
|
|
if key_file:
|
|
|
|
os.environ["GIT_KEY"] = key_file
|
|
|
|
|
|
|
|
if os.environ.get("GIT_SSH_OPTS"):
|
|
|
|
del os.environ["GIT_SSH_OPTS"]
|
|
|
|
|
|
|
|
if ssh_opts:
|
|
|
|
os.environ["GIT_SSH_OPTS"] = ssh_opts
|
2014-01-11 16:35:33 +01:00
|
|
|
|
2013-10-28 02:22:10 +01:00
|
|
|
def get_version(git_path, dest, ref="HEAD"):
|
2012-07-30 07:33:16 +02:00
|
|
|
''' samples the version of the git repo '''
|
|
|
|
os.chdir(dest)
|
2013-10-28 02:22:10 +01:00
|
|
|
cmd = "%s rev-parse %s" % (git_path, ref)
|
|
|
|
sha = os.popen(cmd).read().rstrip("\n")
|
2012-07-30 07:33:16 +02:00
|
|
|
return sha
|
2012-02-27 04:17:31 +01:00
|
|
|
|
2013-10-28 02:22:10 +01:00
|
|
|
def clone(git_path, module, repo, dest, remote, depth, version, bare, reference):
|
2012-07-30 07:33:16 +02:00
|
|
|
''' makes a new git repo if it does not already exist '''
|
2013-01-13 13:07:22 +01:00
|
|
|
dest_dirname = os.path.dirname(dest)
|
2012-07-30 07:33:16 +02:00
|
|
|
try:
|
2013-01-13 13:07:22 +01:00
|
|
|
os.makedirs(dest_dirname)
|
2012-07-30 07:33:16 +02:00
|
|
|
except:
|
|
|
|
pass
|
2013-01-13 13:07:22 +01:00
|
|
|
os.chdir(dest_dirname)
|
2013-10-28 02:22:10 +01:00
|
|
|
cmd = [ git_path, 'clone' ]
|
|
|
|
if bare:
|
|
|
|
cmd.append('--bare')
|
|
|
|
else:
|
|
|
|
cmd.extend([ '--origin', remote, '--recursive' ])
|
|
|
|
if is_remote_branch(git_path, module, dest, repo, version) \
|
|
|
|
or is_remote_tag(git_path, module, dest, repo, version):
|
|
|
|
cmd.extend([ '--branch', version ])
|
2013-04-11 18:40:15 +02:00
|
|
|
if depth:
|
|
|
|
cmd.extend([ '--depth', str(depth) ])
|
2013-10-28 02:22:10 +01:00
|
|
|
if reference:
|
|
|
|
cmd.extend([ '--reference', str(reference) ])
|
2013-04-11 18:40:15 +02:00
|
|
|
cmd.extend([ repo, dest ])
|
2013-10-28 02:22:10 +01:00
|
|
|
module.run_command(cmd, check_rc=True)
|
|
|
|
if bare:
|
|
|
|
os.chdir(dest)
|
|
|
|
if remote != 'origin':
|
|
|
|
module.run_command([git_path, 'remote', 'add', remote, repo], check_rc=True)
|
|
|
|
|
|
|
|
def has_local_mods(git_path, dest, bare):
|
|
|
|
if bare:
|
|
|
|
return False
|
2012-08-23 06:07:14 +02:00
|
|
|
os.chdir(dest)
|
2013-05-30 22:50:37 +02:00
|
|
|
cmd = "%s status -s" % (git_path,)
|
2012-08-23 06:07:14 +02:00
|
|
|
lines = os.popen(cmd).read().splitlines()
|
2013-02-10 00:49:54 +01:00
|
|
|
lines = filter(lambda c: not re.search('^\\?\\?.*$', c), lines)
|
2012-08-23 06:07:14 +02:00
|
|
|
return len(lines) > 0
|
|
|
|
|
2013-10-16 14:08:52 +02:00
|
|
|
def reset(git_path, module, dest):
|
2012-07-30 07:33:16 +02:00
|
|
|
'''
|
|
|
|
Resets the index and working tree to HEAD.
|
|
|
|
Discards any changes to tracked files in working
|
|
|
|
tree since that commit.
|
|
|
|
'''
|
|
|
|
os.chdir(dest)
|
2013-05-30 22:50:37 +02:00
|
|
|
cmd = "%s reset --hard HEAD" % (git_path,)
|
|
|
|
return module.run_command(cmd, check_rc=True)
|
2012-05-02 06:53:20 +02:00
|
|
|
|
2013-10-28 02:22:10 +01:00
|
|
|
def get_remote_head(git_path, module, dest, version, remote, bare):
|
2013-10-16 13:02:11 +02:00
|
|
|
cloning = False
|
|
|
|
if remote == module.params['repo']:
|
|
|
|
cloning = True
|
|
|
|
else:
|
|
|
|
os.chdir(dest)
|
2013-03-15 06:29:04 +01:00
|
|
|
if version == 'HEAD':
|
2013-10-16 13:02:11 +02:00
|
|
|
if cloning:
|
|
|
|
# cloning the repo, just get the remote's HEAD version
|
|
|
|
cmd = '%s ls-remote %s -h HEAD' % (git_path, remote)
|
|
|
|
else:
|
2013-10-28 02:22:10 +01:00
|
|
|
head_branch = get_head_branch(git_path, module, dest, remote, bare)
|
2013-10-16 13:02:11 +02:00
|
|
|
cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, head_branch)
|
|
|
|
elif is_remote_branch(git_path, module, dest, remote, version):
|
2013-05-30 22:50:37 +02:00
|
|
|
cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, version)
|
|
|
|
elif is_remote_tag(git_path, module, dest, remote, version):
|
|
|
|
cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version)
|
2013-03-20 22:28:06 +01:00
|
|
|
else:
|
|
|
|
# appears to be a sha1. return as-is since it appears
|
|
|
|
# cannot check for a specific sha1 on remote
|
|
|
|
return version
|
2014-01-23 22:22:43 +01:00
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True )
|
2013-03-15 06:29:04 +01:00
|
|
|
if len(out) < 1:
|
|
|
|
module.fail_json(msg="Could not determine remote revision for %s" % version)
|
|
|
|
rev = out.split()[0]
|
|
|
|
return rev
|
|
|
|
|
2013-05-30 22:50:37 +02:00
|
|
|
def is_remote_tag(git_path, module, dest, remote, version):
|
|
|
|
cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version)
|
2013-10-16 14:18:01 +02:00
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True)
|
2013-03-20 22:28:06 +01:00
|
|
|
if version in out:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2013-05-30 22:50:37 +02:00
|
|
|
def get_branches(git_path, module, dest):
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 09:22:30 +02:00
|
|
|
os.chdir(dest)
|
|
|
|
branches = []
|
2013-05-30 22:50:37 +02:00
|
|
|
cmd = '%s branch -a' % (git_path,)
|
|
|
|
(rc, out, err) = module.run_command(cmd)
|
2012-10-23 08:08:27 +02:00
|
|
|
if rc != 0:
|
2012-07-30 07:33:16 +02:00
|
|
|
module.fail_json(msg="Could not determine branch data - received %s" % out)
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 09:22:30 +02:00
|
|
|
for line in out.split('\n'):
|
|
|
|
branches.append(line.strip())
|
|
|
|
return branches
|
|
|
|
|
2013-10-16 13:02:11 +02:00
|
|
|
def is_remote_branch(git_path, module, dest, remote, version):
|
|
|
|
cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, version)
|
2013-10-16 14:18:01 +02:00
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True)
|
2013-10-16 13:02:11 +02:00
|
|
|
if version in out:
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 09:22:30 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2013-05-30 22:50:37 +02:00
|
|
|
def is_local_branch(git_path, module, dest, branch):
|
|
|
|
branches = get_branches(git_path, module, dest)
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 09:22:30 +02:00
|
|
|
lbranch = '%s' % branch
|
|
|
|
if lbranch in branches:
|
|
|
|
return True
|
2012-07-30 07:33:16 +02:00
|
|
|
elif '* %s' % branch in branches:
|
|
|
|
return True
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 09:22:30 +02:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2013-05-30 22:50:37 +02:00
|
|
|
def is_not_a_branch(git_path, module, dest):
|
|
|
|
branches = get_branches(git_path, module, dest)
|
2012-10-23 01:03:35 +02:00
|
|
|
for b in branches:
|
|
|
|
if b.startswith('* ') and 'no branch' in b:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2013-10-28 02:22:10 +01:00
|
|
|
def get_head_branch(git_path, module, dest, remote, bare=False):
|
2012-11-08 01:33:22 +01:00
|
|
|
'''
|
|
|
|
Determine what branch HEAD is associated with. This is partly
|
|
|
|
taken from lib/ansible/utils/__init__.py. It finds the correct
|
|
|
|
path to .git/HEAD and reads from that file the branch that HEAD is
|
|
|
|
associated with. In the case of a detached HEAD, this will look
|
|
|
|
up the branch in .git/refs/remotes/<remote>/HEAD.
|
|
|
|
'''
|
2013-10-28 02:22:10 +01:00
|
|
|
if bare:
|
|
|
|
repo_path = dest
|
|
|
|
else:
|
|
|
|
repo_path = os.path.join(dest, '.git')
|
2012-11-08 01:33:22 +01:00
|
|
|
# Check if the .git is a file. If it is a file, it means that we are in a submodule structure.
|
|
|
|
if os.path.isfile(repo_path):
|
|
|
|
try:
|
2013-02-23 19:34:14 +01:00
|
|
|
gitdir = yaml.safe_load(open(repo_path)).get('gitdir')
|
2012-11-08 01:33:22 +01:00
|
|
|
# There is a posibility the .git file to have an absolute path.
|
|
|
|
if os.path.isabs(gitdir):
|
|
|
|
repo_path = gitdir
|
|
|
|
else:
|
|
|
|
repo_path = os.path.join(repo_path.split('.git')[0], gitdir)
|
|
|
|
except (IOError, AttributeError):
|
|
|
|
return ''
|
|
|
|
# Read .git/HEAD for the name of the branch.
|
|
|
|
# If we're in a detached HEAD state, look up the branch associated with
|
|
|
|
# the remote HEAD in .git/refs/remotes/<remote>/HEAD
|
|
|
|
f = open(os.path.join(repo_path, "HEAD"))
|
2013-05-30 22:50:37 +02:00
|
|
|
if is_not_a_branch(git_path, module, dest):
|
2012-11-08 01:33:22 +01:00
|
|
|
f.close()
|
|
|
|
f = open(os.path.join(repo_path, 'refs', 'remotes', remote, 'HEAD'))
|
|
|
|
branch = f.readline().split('/')[-1].rstrip("\n")
|
|
|
|
f.close()
|
|
|
|
return branch
|
2012-10-23 01:03:35 +02:00
|
|
|
|
2013-10-28 02:22:10 +01:00
|
|
|
def fetch(git_path, module, repo, dest, version, remote, bare):
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 09:22:30 +02:00
|
|
|
''' updates repo from remote sources '''
|
|
|
|
os.chdir(dest)
|
2013-10-28 02:22:10 +01:00
|
|
|
if bare:
|
|
|
|
(rc, out1, err1) = module.run_command([git_path, 'fetch', remote, '+refs/heads/*:refs/heads/*'])
|
|
|
|
else:
|
|
|
|
(rc, out1, err1) = module.run_command("%s fetch %s" % (git_path, remote))
|
2012-10-23 08:08:27 +02:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Failed to download remote objects and refs")
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 09:22:30 +02:00
|
|
|
|
2013-10-28 02:22:10 +01:00
|
|
|
if bare:
|
|
|
|
(rc, out2, err2) = module.run_command([git_path, 'fetch', remote, '+refs/tags/*:refs/tags/*'])
|
|
|
|
else:
|
|
|
|
(rc, out2, err2) = module.run_command("%s fetch --tags %s" % (git_path, remote))
|
2012-10-23 08:08:27 +02:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Failed to download remote objects and refs")
|
2013-07-04 02:19:14 +02:00
|
|
|
(rc, out3, err3) = submodule_update(git_path, module, dest)
|
2013-06-20 23:48:39 +02:00
|
|
|
return (rc, out1 + out2 + out3, err1 + err2 + err3)
|
|
|
|
|
2013-07-04 02:19:14 +02:00
|
|
|
def submodule_update(git_path, module, dest):
|
2013-06-20 23:48:39 +02:00
|
|
|
''' init and update any submodules '''
|
|
|
|
os.chdir(dest)
|
|
|
|
# skip submodule commands if .gitmodules is not present
|
|
|
|
if not os.path.exists(os.path.join(dest, '.gitmodules')):
|
|
|
|
return (0, '', '')
|
|
|
|
cmd = [ git_path, 'submodule', 'sync' ]
|
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True)
|
|
|
|
cmd = [ git_path, 'submodule', 'update', '--init', '--recursive' ]
|
|
|
|
(rc, out, err) = module.run_command(cmd)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Failed to init/update submodules")
|
|
|
|
return (rc, out, err)
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 09:22:30 +02:00
|
|
|
|
2013-05-30 22:50:37 +02:00
|
|
|
def switch_version(git_path, module, dest, remote, version):
|
2012-07-30 07:33:16 +02:00
|
|
|
''' once pulled, switch to a particular SHA, tag, or branch '''
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 09:22:30 +02:00
|
|
|
os.chdir(dest)
|
|
|
|
cmd = ''
|
|
|
|
if version != 'HEAD':
|
2013-05-30 22:50:37 +02:00
|
|
|
if is_remote_branch(git_path, module, dest, remote, version):
|
|
|
|
if not is_local_branch(git_path, module, dest, version):
|
|
|
|
cmd = "%s checkout --track -b %s %s/%s" % (git_path, version, remote, version)
|
2012-12-05 02:02:54 +01:00
|
|
|
else:
|
2013-05-30 22:50:37 +02:00
|
|
|
(rc, out, err) = module.run_command("%s checkout --force %s" % (git_path, version))
|
2012-12-05 02:02:54 +01:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Failed to checkout branch %s" % version)
|
2013-05-30 22:50:37 +02:00
|
|
|
cmd = "%s reset --hard %s/%s" % (git_path, remote, version)
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 09:22:30 +02:00
|
|
|
else:
|
2013-05-30 22:50:37 +02:00
|
|
|
cmd = "%s checkout --force %s" % (git_path, version)
|
Update git module to handle branches better
This drops the branch option. The version option is overloaded
to mean either a sha1, branch, or tag. This also adds the option
'remote' which defaults to 'origin'.
clone() was simplified by removing the checkout operation. That
happens later when switch_version() is called.
Added the methods get_branches(), is_remote_branch(), and
is_local_branch(). get_branches() returns an array listing all
of the branches for the git repository. is_remote_branch() checks
whether the arguments supplied correspond to a remote branch.
Similarly, is_local_branch() checks for a local branch.
The pull() method now checks to see if it is on the desired branch.
If not, it checks out the requested branch and then does a pull.
This should keep issue #604 still fixed.
switch_version(), formerly switchver(), looks to see if it is
checking out a branch. If a branch, it checks it out with the --track
option. This type of checkout was in pull() before.
Updated pull, clone, and switch_version to return (rc, out, err).
2012-07-26 09:22:30 +02:00
|
|
|
else:
|
2013-05-30 22:50:37 +02:00
|
|
|
branch = get_head_branch(git_path, module, dest, remote)
|
|
|
|
(rc, out, err) = module.run_command("%s checkout --force %s" % (git_path, branch))
|
2012-10-24 08:34:53 +02:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Failed to checkout branch %s" % branch)
|
2013-05-30 22:50:37 +02:00
|
|
|
cmd = "%s reset --hard %s" % (git_path, remote)
|
2013-07-04 02:19:14 +02:00
|
|
|
(rc, out1, err1) = module.run_command(cmd)
|
|
|
|
if rc != 0:
|
2013-08-02 12:53:08 +02:00
|
|
|
if version != 'HEAD':
|
|
|
|
module.fail_json(msg="Failed to checkout %s" % (version))
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Failed to checkout branch %s" % (branch))
|
2013-07-04 02:19:14 +02:00
|
|
|
(rc, out2, err2) = submodule_update(git_path, module, dest)
|
|
|
|
return (rc, out1 + out2, err1 + err2)
|
2012-02-27 04:17:31 +01:00
|
|
|
|
2012-07-30 07:33:16 +02:00
|
|
|
# ===========================================
|
2012-02-27 04:17:31 +01:00
|
|
|
|
2012-07-30 07:33:16 +02:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
dest=dict(required=True),
|
2012-08-01 06:21:36 +02:00
|
|
|
repo=dict(required=True, aliases=['name']),
|
2012-07-30 07:33:16 +02:00
|
|
|
version=dict(default='HEAD'),
|
2012-08-23 06:07:14 +02:00
|
|
|
remote=dict(default='origin'),
|
2013-10-28 02:22:10 +01:00
|
|
|
reference=dict(default=None),
|
2013-04-11 18:40:15 +02:00
|
|
|
force=dict(default='yes', type='bool'),
|
|
|
|
depth=dict(default=None, type='int'),
|
2013-05-16 07:14:17 +02:00
|
|
|
update=dict(default='yes', type='bool'),
|
2014-01-11 17:02:01 +01:00
|
|
|
accept_hostkey=dict(default='no', type='bool'),
|
2014-01-23 22:44:17 +01:00
|
|
|
key_file=dict(default=None, required=False),
|
|
|
|
ssh_opts=dict(default=None, required=False),
|
2013-09-26 23:15:28 +02:00
|
|
|
executable=dict(default=None),
|
2013-10-28 02:22:10 +01:00
|
|
|
bare=dict(default='no', type='bool'),
|
2013-03-15 06:29:04 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=True
|
2012-07-30 07:33:16 +02:00
|
|
|
)
|
|
|
|
|
2013-10-28 02:22:10 +01:00
|
|
|
dest = os.path.abspath(os.path.expanduser(module.params['dest']))
|
|
|
|
repo = module.params['repo']
|
|
|
|
version = module.params['version']
|
|
|
|
remote = module.params['remote']
|
|
|
|
force = module.params['force']
|
|
|
|
depth = module.params['depth']
|
|
|
|
update = module.params['update']
|
|
|
|
bare = module.params['bare']
|
|
|
|
reference = module.params['reference']
|
|
|
|
git_path = module.params['executable'] or module.get_bin_path('git', True)
|
2014-01-23 22:22:43 +01:00
|
|
|
|
2014-01-23 22:44:17 +01:00
|
|
|
key_file = module.params['key_file']
|
|
|
|
ssh_opts = module.params['ssh_opts']
|
2014-01-23 22:22:43 +01:00
|
|
|
|
|
|
|
# create a wrapper script and export
|
|
|
|
# GIT_SSH=<path> as an environment variable
|
|
|
|
# for git to use the wrapper script
|
|
|
|
ssh_wrapper = None
|
|
|
|
if key_file or ssh_opts:
|
|
|
|
ssh_wrapper = write_ssh_wrapper()
|
|
|
|
set_git_ssh(ssh_wrapper, key_file, ssh_opts)
|
2013-10-28 02:22:10 +01:00
|
|
|
|
2014-01-11 16:35:33 +01:00
|
|
|
# add the git repo's hostkey
|
2014-01-28 17:49:35 +01:00
|
|
|
if module.params['ssh_opts'] is not None:
|
|
|
|
if not "-o StrictHostKeyChecking=no" in module.params['ssh_opts']:
|
|
|
|
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
|
|
|
|
else:
|
|
|
|
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
|
2014-01-11 16:35:33 +01:00
|
|
|
|
2013-10-28 02:22:10 +01:00
|
|
|
if bare:
|
|
|
|
gitconfig = os.path.join(dest, 'config')
|
|
|
|
else:
|
|
|
|
gitconfig = os.path.join(dest, '.git', 'config')
|
2012-07-30 07:33:16 +02:00
|
|
|
|
2012-08-07 20:20:14 +02:00
|
|
|
rc, out, err, status = (0, None, None, None)
|
2012-07-30 07:33:16 +02:00
|
|
|
|
|
|
|
# if there is no git configuration, do a clone operation
|
|
|
|
# else pull and switch the version
|
|
|
|
before = None
|
2012-08-23 06:07:14 +02:00
|
|
|
local_mods = False
|
2012-07-30 07:33:16 +02:00
|
|
|
if not os.path.exists(gitconfig):
|
2013-03-15 06:29:04 +01:00
|
|
|
if module.check_mode:
|
2013-11-25 15:55:05 +01:00
|
|
|
remote_head = get_remote_head(git_path, module, dest, version, repo, bare)
|
2013-10-16 14:08:52 +02:00
|
|
|
module.exit_json(changed=True, before=before, after=remote_head)
|
2013-10-28 02:22:10 +01:00
|
|
|
clone(git_path, module, repo, dest, remote, depth, version, bare, reference)
|
2013-05-16 07:14:17 +02:00
|
|
|
elif not update:
|
|
|
|
# Just return having found a repo already in the dest path
|
|
|
|
# this does no checking that the repo is the actual repo
|
|
|
|
# requested.
|
2013-05-30 22:50:37 +02:00
|
|
|
before = get_version(git_path, dest)
|
2013-05-16 07:14:17 +02:00
|
|
|
module.exit_json(changed=False, before=before, after=before)
|
2012-07-30 07:33:16 +02:00
|
|
|
else:
|
2012-08-07 02:07:02 +02:00
|
|
|
# else do a pull
|
2013-10-28 02:22:10 +01:00
|
|
|
local_mods = has_local_mods(git_path, dest, bare)
|
2013-05-30 22:50:37 +02:00
|
|
|
before = get_version(git_path, dest)
|
2013-10-16 14:08:52 +02:00
|
|
|
if local_mods:
|
|
|
|
# failure should happen regardless of check mode
|
|
|
|
if not force:
|
|
|
|
module.fail_json(msg="Local modifications exist in repository (force=no).")
|
|
|
|
# if force and in non-check mode, do a reset
|
|
|
|
if not module.check_mode:
|
|
|
|
reset(git_path, module, dest)
|
|
|
|
# exit if already at desired sha version
|
2013-10-28 02:22:10 +01:00
|
|
|
remote_head = get_remote_head(git_path, module, dest, version, remote, bare)
|
2013-10-16 14:08:52 +02:00
|
|
|
if before == remote_head:
|
|
|
|
if local_mods:
|
|
|
|
module.exit_json(changed=True, before=before, after=remote_head,
|
|
|
|
msg="Local modifications exist")
|
2013-03-15 06:29:04 +01:00
|
|
|
else:
|
2013-10-16 14:08:52 +02:00
|
|
|
module.exit_json(changed=False, before=before, after=remote_head)
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True, before=before, after=remote_head)
|
2013-10-28 02:22:10 +01:00
|
|
|
fetch(git_path, module, repo, dest, version, remote, bare)
|
2012-07-30 07:33:16 +02:00
|
|
|
|
|
|
|
# switch to version specified regardless of whether
|
|
|
|
# we cloned or pulled
|
2013-10-28 02:22:10 +01:00
|
|
|
if not bare:
|
|
|
|
switch_version(git_path, module, dest, remote, version)
|
2012-07-30 07:33:16 +02:00
|
|
|
|
|
|
|
# determine if we changed anything
|
2013-05-30 22:50:37 +02:00
|
|
|
after = get_version(git_path, dest)
|
2012-07-30 07:33:16 +02:00
|
|
|
changed = False
|
|
|
|
|
2012-08-23 06:07:14 +02:00
|
|
|
if before != after or local_mods:
|
2012-07-30 07:33:16 +02:00
|
|
|
changed = True
|
|
|
|
|
2014-01-23 22:22:43 +01:00
|
|
|
# cleanup the wrapper script
|
|
|
|
if ssh_wrapper:
|
|
|
|
os.remove(ssh_wrapper)
|
|
|
|
|
2012-07-30 07:33:16 +02:00
|
|
|
module.exit_json(changed=changed, before=before, after=after)
|
|
|
|
|
2013-12-02 21:11:23 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2014-01-11 16:35:33 +01:00
|
|
|
from ansible.module_utils.known_hosts import *
|
|
|
|
|
2012-07-30 07:33:16 +02:00
|
|
|
main()
|