mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
339 lines
12 KiB
Plaintext
339 lines
12 KiB
Plaintext
13 years ago
|
#!/usr/bin/python
|
||
12 years ago
|
# -*- coding: utf-8 -*-
|
||
13 years ago
|
|
||
13 years ago
|
# (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/>.
|
||
|
|
||
12 years ago
|
DOCUMENTATION = '''
|
||
|
---
|
||
|
module: git
|
||
|
author: Michael DeHaan
|
||
12 years ago
|
version_added: "0.0.1"
|
||
12 years ago
|
short_description: Deploy software (or files) from git checkouts
|
||
|
description:
|
||
12 years ago
|
- Manage I(git) checkouts of repositories to deploy files or software.
|
||
12 years ago
|
options:
|
||
|
repo:
|
||
|
required: true
|
||
|
aliases: [ name ]
|
||
|
description:
|
||
12 years ago
|
- git, SSH, or HTTP protocol address of the git repository.
|
||
12 years ago
|
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
|
||
12 years ago
|
git I(SHA), the literal string C(HEAD), a branch name, or a tag name.
|
||
12 years ago
|
remote:
|
||
|
required: false
|
||
|
default: "origin"
|
||
|
description:
|
||
12 years ago
|
- Name of the remote.
|
||
12 years ago
|
force:
|
||
|
required: false
|
||
|
default: "yes"
|
||
12 years ago
|
choices: [ "yes", "no" ]
|
||
12 years ago
|
version_added: "0.7"
|
||
12 years ago
|
description:
|
||
12 years ago
|
- If C(yes), any modified files in the working
|
||
12 years ago
|
repository will be discarded. Prior to 0.7, this was always
|
||
|
'yes' and could not be disabled.
|
||
12 years ago
|
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.
|
||
12 years ago
|
examples:
|
||
12 years ago
|
- code: "git: repo=git://foosball.example.org/path/to/repo.git dest=/srv/checkout version=release-0.22"
|
||
12 years ago
|
description: Example git checkout from Ansible Playbooks
|
||
12 years ago
|
- code: "git: repo=ssh://git@github.com/mylogin/hello.git dest=/home/mylogin/hello"
|
||
|
description: Example read-write git checkout from github
|
||
12 years ago
|
'''
|
||
|
|
||
13 years ago
|
import re
|
||
12 years ago
|
import tempfile
|
||
13 years ago
|
|
||
|
def get_version(dest):
|
||
13 years ago
|
''' 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
|
||
13 years ago
|
|
||
12 years ago
|
def clone(module, repo, dest, remote, depth):
|
||
13 years ago
|
''' makes a new git repo if it does not already exist '''
|
||
12 years ago
|
dest_dirname = os.path.dirname(dest)
|
||
13 years ago
|
try:
|
||
12 years ago
|
os.makedirs(dest_dirname)
|
||
13 years ago
|
except:
|
||
|
pass
|
||
12 years ago
|
os.chdir(dest_dirname)
|
||
12 years ago
|
cmd = [ module.get_bin_path('git', True), 'clone', '-o', remote ]
|
||
|
if depth:
|
||
|
cmd.extend([ '--depth', str(depth) ])
|
||
|
cmd.extend([ repo, dest ])
|
||
|
return module.run_command(cmd, check_rc=True)
|
||
12 years ago
|
|
||
|
def has_local_mods(dest):
|
||
|
os.chdir(dest)
|
||
|
cmd = "git status -s"
|
||
|
lines = os.popen(cmd).read().splitlines()
|
||
12 years ago
|
lines = filter(lambda c: not re.search('^\\?\\?.*$', c), lines)
|
||
12 years ago
|
return len(lines) > 0
|
||
|
|
||
12 years ago
|
def reset(module, dest, force):
|
||
13 years ago
|
'''
|
||
|
Resets the index and working tree to HEAD.
|
||
|
Discards any changes to tracked files in working
|
||
|
tree since that commit.
|
||
|
'''
|
||
|
os.chdir(dest)
|
||
12 years ago
|
if not force and has_local_mods(dest):
|
||
|
module.fail_json(msg="Local modifications exist in repository (force=no).")
|
||
12 years ago
|
return module.run_command("git reset --hard HEAD", check_rc=True)
|
||
13 years ago
|
|
||
12 years ago
|
def get_remote_head(module, dest, version, remote):
|
||
12 years ago
|
cmd = ''
|
||
|
os.chdir(dest)
|
||
12 years ago
|
if version == 'HEAD':
|
||
|
version = get_head_branch(module, dest, remote)
|
||
12 years ago
|
if is_remote_branch(module, dest, remote, version):
|
||
|
cmd = 'git ls-remote %s -h refs/heads/%s' % (remote, version)
|
||
|
elif is_remote_tag(module, dest, remote, version):
|
||
|
cmd = 'git ls-remote %s -t refs/tags/%s' % (remote, version)
|
||
|
else:
|
||
|
# appears to be a sha1. return as-is since it appears
|
||
|
# cannot check for a specific sha1 on remote
|
||
|
return version
|
||
12 years ago
|
(rc, out, err) = module.run_command(cmd, check_rc=True)
|
||
|
if len(out) < 1:
|
||
|
module.fail_json(msg="Could not determine remote revision for %s" % version)
|
||
|
rev = out.split()[0]
|
||
|
return rev
|
||
|
|
||
12 years ago
|
def is_remote_tag(module, dest, remote, version):
|
||
|
os.chdir(dest)
|
||
|
cmd = 'git ls-remote %s -t refs/tags/%s' % (remote, version)
|
||
|
(rc, out, err) = module.run_command(cmd)
|
||
|
if version in out:
|
||
|
return True
|
||
|
else:
|
||
|
return False
|
||
|
|
||
13 years ago
|
def get_branches(module, dest):
|
||
13 years ago
|
os.chdir(dest)
|
||
|
branches = []
|
||
12 years ago
|
(rc, out, err) = module.run_command("git branch -a")
|
||
12 years ago
|
if rc != 0:
|
||
13 years ago
|
module.fail_json(msg="Could not determine branch data - received %s" % out)
|
||
13 years ago
|
for line in out.split('\n'):
|
||
|
branches.append(line.strip())
|
||
|
return branches
|
||
|
|
||
13 years ago
|
def is_remote_branch(module, dest, remote, branch):
|
||
|
branches = get_branches(module, dest)
|
||
13 years ago
|
rbranch = 'remotes/%s/%s' % (remote, branch)
|
||
|
if rbranch in branches:
|
||
|
return True
|
||
|
else:
|
||
|
return False
|
||
|
|
||
13 years ago
|
def is_local_branch(module, dest, branch):
|
||
|
branches = get_branches(module, dest)
|
||
13 years ago
|
lbranch = '%s' % branch
|
||
|
if lbranch in branches:
|
||
|
return True
|
||
13 years ago
|
elif '* %s' % branch in branches:
|
||
|
return True
|
||
13 years ago
|
else:
|
||
|
return False
|
||
|
|
||
13 years ago
|
def is_current_branch(module, dest, branch):
|
||
|
branches = get_branches(module, dest)
|
||
|
for b in branches:
|
||
|
if b.startswith('* '):
|
||
|
cur_branch = b
|
||
|
if branch == cur_branch or '* %s' % branch == cur_branch:
|
||
|
return True
|
||
|
else:
|
||
|
return True
|
||
|
|
||
12 years ago
|
def is_not_a_branch(module, dest):
|
||
|
branches = get_branches(module, dest)
|
||
|
for b in branches:
|
||
|
if b.startswith('* ') and 'no branch' in b:
|
||
|
return True
|
||
|
return False
|
||
|
|
||
|
def get_head_branch(module, dest, remote):
|
||
12 years ago
|
'''
|
||
|
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.
|
||
|
'''
|
||
|
repo_path = os.path.join(dest, '.git')
|
||
|
# 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:
|
||
12 years ago
|
gitdir = yaml.safe_load(open(repo_path)).get('gitdir')
|
||
12 years ago
|
# 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"))
|
||
|
if is_not_a_branch(module, dest):
|
||
|
f.close()
|
||
|
f = open(os.path.join(repo_path, 'refs', 'remotes', remote, 'HEAD'))
|
||
|
branch = f.readline().split('/')[-1].rstrip("\n")
|
||
|
f.close()
|
||
|
return branch
|
||
12 years ago
|
|
||
12 years ago
|
def fetch(module, repo, dest, version, remote):
|
||
13 years ago
|
''' updates repo from remote sources '''
|
||
|
os.chdir(dest)
|
||
12 years ago
|
(rc, out1, err1) = module.run_command("git fetch %s" % remote)
|
||
12 years ago
|
if rc != 0:
|
||
|
module.fail_json(msg="Failed to download remote objects and refs")
|
||
13 years ago
|
|
||
12 years ago
|
(rc, out2, err2) = module.run_command("git fetch --tags %s" % remote)
|
||
12 years ago
|
if rc != 0:
|
||
|
module.fail_json(msg="Failed to download remote objects and refs")
|
||
|
return (rc, out1 + out2, err1 + err2)
|
||
13 years ago
|
|
||
13 years ago
|
def switch_version(module, dest, remote, version):
|
||
|
''' once pulled, switch to a particular SHA, tag, or branch '''
|
||
13 years ago
|
os.chdir(dest)
|
||
|
cmd = ''
|
||
|
if version != 'HEAD':
|
||
12 years ago
|
if is_remote_branch(module, dest, remote, version):
|
||
|
if not is_local_branch(module, dest, version):
|
||
|
cmd = "git checkout --track -b %s %s/%s" % (version, remote, version)
|
||
|
else:
|
||
12 years ago
|
(rc, out, err) = module.run_command("git checkout --force %s" % version)
|
||
12 years ago
|
if rc != 0:
|
||
|
module.fail_json(msg="Failed to checkout branch %s" % version)
|
||
|
cmd = "git reset --hard %s/%s" % (remote, version)
|
||
13 years ago
|
else:
|
||
|
cmd = "git checkout --force %s" % version
|
||
|
else:
|
||
12 years ago
|
branch = get_head_branch(module, dest, remote)
|
||
12 years ago
|
(rc, out, err) = module.run_command("git checkout --force %s" % branch)
|
||
12 years ago
|
if rc != 0:
|
||
|
module.fail_json(msg="Failed to checkout branch %s" % branch)
|
||
12 years ago
|
cmd = "git reset --hard %s" % remote
|
||
12 years ago
|
return module.run_command(cmd, check_rc=True)
|
||
13 years ago
|
|
||
13 years ago
|
# ===========================================
|
||
13 years ago
|
|
||
13 years ago
|
def main():
|
||
|
module = AnsibleModule(
|
||
|
argument_spec = dict(
|
||
|
dest=dict(required=True),
|
||
12 years ago
|
repo=dict(required=True, aliases=['name']),
|
||
13 years ago
|
version=dict(default='HEAD'),
|
||
12 years ago
|
remote=dict(default='origin'),
|
||
12 years ago
|
force=dict(default='yes', type='bool'),
|
||
|
depth=dict(default=None, type='int'),
|
||
12 years ago
|
),
|
||
|
supports_check_mode=True
|
||
13 years ago
|
)
|
||
|
|
||
12 years ago
|
dest = os.path.abspath(os.path.expanduser(module.params['dest']))
|
||
13 years ago
|
repo = module.params['repo']
|
||
|
version = module.params['version']
|
||
|
remote = module.params['remote']
|
||
12 years ago
|
force = module.params['force']
|
||
12 years ago
|
depth = module.params['depth']
|
||
13 years ago
|
|
||
|
gitconfig = os.path.join(dest, '.git', 'config')
|
||
|
|
||
12 years ago
|
rc, out, err, status = (0, None, None, None)
|
||
13 years ago
|
|
||
|
# if there is no git configuration, do a clone operation
|
||
|
# else pull and switch the version
|
||
|
before = None
|
||
12 years ago
|
local_mods = False
|
||
13 years ago
|
if not os.path.exists(gitconfig):
|
||
12 years ago
|
if module.check_mode:
|
||
|
module.exit_json(changed=True)
|
||
12 years ago
|
(rc, out, err) = clone(module, repo, dest, remote, depth)
|
||
13 years ago
|
else:
|
||
12 years ago
|
# else do a pull
|
||
12 years ago
|
local_mods = has_local_mods(dest)
|
||
13 years ago
|
before = get_version(dest)
|
||
12 years ago
|
# if force, do a reset
|
||
|
if local_mods and module.check_mode:
|
||
|
module.exit_json(changed=True, msg='Local modifications exist')
|
||
12 years ago
|
(rc, out, err) = reset(module,dest,force)
|
||
13 years ago
|
if rc != 0:
|
||
12 years ago
|
module.fail_json(msg=err)
|
||
12 years ago
|
# check or get changes from remote
|
||
|
remote_head = get_remote_head(module, dest, version, remote)
|
||
|
if module.check_mode:
|
||
12 years ago
|
changed = False
|
||
|
if remote_head == version:
|
||
|
# get_remote_head returned version as-is
|
||
|
# were given a sha1 object, see if it is present
|
||
|
(rc, out, err) = module.run_command("git show %s" % version)
|
||
|
if version in out:
|
||
|
changed = False
|
||
|
else:
|
||
|
changed = True
|
||
12 years ago
|
else:
|
||
12 years ago
|
remote_head = remote_head[0:7]
|
||
|
if before != remote_head:
|
||
|
changed = True
|
||
|
else:
|
||
|
changed = False
|
||
|
module.exit_json(changed=changed, before=before, after=remote_head)
|
||
12 years ago
|
(rc, out, err) = fetch(module, repo, dest, version, remote)
|
||
12 years ago
|
if rc != 0:
|
||
|
module.fail_json(msg=err)
|
||
13 years ago
|
|
||
|
# switch to version specified regardless of whether
|
||
|
# we cloned or pulled
|
||
|
(rc, out, err) = switch_version(module, dest, remote, version)
|
||
|
|
||
|
# determine if we changed anything
|
||
|
after = get_version(dest)
|
||
|
changed = False
|
||
|
|
||
12 years ago
|
if before != after or local_mods:
|
||
13 years ago
|
changed = True
|
||
|
|
||
|
module.exit_json(changed=changed, before=before, after=after)
|
||
|
|
||
|
# include magic from lib/ansible/module_common.py
|
||
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||
|
main()
|