|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (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/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: git
|
|
|
|
author: Michael DeHaan
|
|
|
|
version_added: 0.0.1
|
|
|
|
short_description: Deploy software (or files) from git checkouts
|
|
|
|
description:
|
|
|
|
- Manage I(git) checkouts of repositories to deploy files or software.
|
|
|
|
options:
|
|
|
|
repo:
|
|
|
|
required: true
|
|
|
|
aliases: [ name ]
|
|
|
|
description:
|
|
|
|
- git, SSH, or HTTP protocol address of the git repository.
|
|
|
|
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
|
|
|
|
git I(SHA), the literal string C(HEAD), a branch name, or a tag name.
|
|
|
|
remote:
|
|
|
|
required: false
|
|
|
|
default: "origin"
|
|
|
|
description:
|
|
|
|
- Name of the remote.
|
|
|
|
force:
|
|
|
|
required: false
|
|
|
|
default: "yes"
|
|
|
|
choices: [ yes, no ]
|
|
|
|
version_added: "0.7"
|
|
|
|
description:
|
|
|
|
- If C(yes), any modified files in the working
|
|
|
|
repository will be discarded. Prior to 0.7, this was always
|
|
|
|
'yes' and could not be disabled.
|
|
|
|
examples:
|
|
|
|
- code: "git: repo=git://foosball.example.org/path/to/repo.git dest=/srv/checkout version=release-0.22"
|
|
|
|
description: Example git checkout from Ansible Playbooks
|
|
|
|
'''
|
|
|
|
|
|
|
|
import re
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
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
|
|
|
|
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
12 years ago
|
|
|
def clone(module, repo, dest, remote):
|
|
|
|
''' makes a new git repo if it does not already exist '''
|
|
|
|
dest_dirname = os.path.dirname(dest)
|
|
|
|
try:
|
|
|
|
os.makedirs(dest_dirname)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
os.chdir(dest_dirname)
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
12 years ago
|
|
|
return module.run_command("git clone -o %s %s %s" % (remote, repo, dest),
|
|
|
|
fail_on_rc_non_zero=True)
|
|
|
|
|
|
|
|
def has_local_mods(dest):
|
|
|
|
os.chdir(dest)
|
|
|
|
cmd = "git status -s"
|
|
|
|
lines = os.popen(cmd).read().splitlines()
|
|
|
|
lines = filter(lambda c: re.search('^\\?\\?.*$',c) == None,lines)
|
|
|
|
return len(lines) > 0
|
|
|
|
|
|
|
|
def reset(module,dest,force):
|
|
|
|
'''
|
|
|
|
Resets the index and working tree to HEAD.
|
|
|
|
Discards any changes to tracked files in working
|
|
|
|
tree since that commit.
|
|
|
|
'''
|
|
|
|
os.chdir(dest)
|
|
|
|
if not force and has_local_mods(dest):
|
|
|
|
module.fail_json(msg="Local modifications exist in repository (force=no).")
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
12 years ago
|
|
|
return module.run_command("git reset --hard HEAD", fail_on_rc_non_zero=True)
|
|
|
|
|
|
|
|
def get_branches(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).
13 years ago
|
|
|
os.chdir(dest)
|
|
|
|
branches = []
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
12 years ago
|
|
|
(rc, out, err) = module.run_command("git branch -a")
|
|
|
|
if rc != 0:
|
|
|
|
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).
13 years ago
|
|
|
for line in out.split('\n'):
|
|
|
|
branches.append(line.strip())
|
|
|
|
return branches
|
|
|
|
|
|
|
|
def is_remote_branch(module, dest, remote, branch):
|
|
|
|
branches = get_branches(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).
13 years ago
|
|
|
rbranch = 'remotes/%s/%s' % (remote, branch)
|
|
|
|
if rbranch in branches:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_local_branch(module, dest, branch):
|
|
|
|
branches = get_branches(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).
13 years ago
|
|
|
lbranch = '%s' % branch
|
|
|
|
if lbranch in branches:
|
|
|
|
return True
|
|
|
|
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).
13 years ago
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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):
|
|
|
|
'''
|
|
|
|
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:
|
|
|
|
gitdir = yaml.load(open(repo_path)).get('gitdir')
|
|
|
|
# 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
|
|
|
|
|
|
|
|
def fetch(module, repo, dest, version, remote):
|
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).
13 years ago
|
|
|
''' updates repo from remote sources '''
|
|
|
|
os.chdir(dest)
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
12 years ago
|
|
|
(rc, out1, err1) = module.run_command("git fetch %s" % remote)
|
|
|
|
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).
13 years ago
|
|
|
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
12 years ago
|
|
|
(rc, out2, err2) = module.run_command("git fetch --tags %s" % remote)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Failed to download remote objects and refs")
|
|
|
|
return (rc, out1 + out2, err1 + err2)
|
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).
13 years ago
|
|
|
|
|
|
|
def switch_version(module, dest, remote, version):
|
|
|
|
''' 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).
13 years ago
|
|
|
os.chdir(dest)
|
|
|
|
cmd = ''
|
|
|
|
if version != 'HEAD':
|
|
|
|
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:
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
12 years ago
|
|
|
(rc, out, err) = module.run_command("git checkout --force %s" % version)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Failed to checkout branch %s" % version)
|
|
|
|
cmd = "git reset --hard %s/%s" % (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).
13 years ago
|
|
|
else:
|
|
|
|
cmd = "git checkout --force %s" % version
|
|
|
|
else:
|
|
|
|
branch = get_head_branch(module, dest, remote)
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
12 years ago
|
|
|
(rc, out, err) = module.run_command("git checkout --force %s" % branch)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Failed to checkout branch %s" % branch)
|
|
|
|
cmd = "git reset --hard %s" % remote
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
12 years ago
|
|
|
return module.run_command(cmd, fail_on_rc_non_zero=True)
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
dest=dict(required=True),
|
|
|
|
repo=dict(required=True, aliases=['name']),
|
|
|
|
version=dict(default='HEAD'),
|
|
|
|
remote=dict(default='origin'),
|
|
|
|
force=dict(default='yes', choices=['yes', 'no'], aliases=['force'])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
dest = os.path.expanduser(module.params['dest'])
|
|
|
|
repo = module.params['repo']
|
|
|
|
version = module.params['version']
|
|
|
|
remote = module.params['remote']
|
|
|
|
force = module.boolean(module.params['force'])
|
|
|
|
|
|
|
|
gitconfig = os.path.join(dest, '.git', 'config')
|
|
|
|
|
|
|
|
rc, out, err, status = (0, None, None, None)
|
|
|
|
|
|
|
|
# if there is no git configuration, do a clone operation
|
|
|
|
# else pull and switch the version
|
|
|
|
before = None
|
|
|
|
local_mods = False
|
|
|
|
if not os.path.exists(gitconfig):
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
12 years ago
|
|
|
(rc, out, err) = clone(module, repo, dest, remote)
|
|
|
|
else:
|
|
|
|
# else do a pull
|
|
|
|
local_mods = has_local_mods(dest)
|
|
|
|
before = get_version(dest)
|
|
|
|
(rc, out, err) = reset(module,dest,force)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
(rc, out, err) = fetch(module, repo, dest, version, remote)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
if before != after or local_mods:
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, before=before, after=after)
|
|
|
|
|
|
|
|
# include magic from lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|