Merge pull request #922 from dsummersl/subversion

Subversion
reviewable/pr18780/r1
Michael DeHaan 12 years ago
commit 3c86a43122

22
git

@ -45,13 +45,23 @@ def clone(repo, dest):
rc = cmd.returncode rc = cmd.returncode
return (rc, out, err) return (rc, out, err)
def reset(dest):
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. Resets the index and working tree to HEAD.
Discards any changes to tracked files in working Discards any changes to tracked files in working
tree since that commit. tree since that commit.
''' '''
os.chdir(dest) os.chdir(dest)
if not force and has_local_mods(dest):
module.fail_json(msg="Local modifications exist in repository (force=no).")
cmd = "git reset --hard HEAD" cmd = "git reset --hard HEAD"
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = cmd.communicate() (out, err) = cmd.communicate()
@ -140,7 +150,8 @@ def main():
dest=dict(required=True), dest=dict(required=True),
repo=dict(required=True, aliases=['name']), repo=dict(required=True, aliases=['name']),
version=dict(default='HEAD'), version=dict(default='HEAD'),
remote=dict(default='origin') remote=dict(default='origin'),
force=dict(default='yes', choices=['yes', 'no'], aliases=['force'])
) )
) )
@ -148,6 +159,7 @@ def main():
repo = module.params['repo'] repo = module.params['repo']
version = module.params['version'] version = module.params['version']
remote = module.params['remote'] remote = module.params['remote']
force = module.boolean(module.params['force'])
gitconfig = os.path.join(dest, '.git', 'config') gitconfig = os.path.join(dest, '.git', 'config')
@ -156,14 +168,16 @@ def main():
# if there is no git configuration, do a clone operation # if there is no git configuration, do a clone operation
# else pull and switch the version # else pull and switch the version
before = None before = None
local_mods = False
if not os.path.exists(gitconfig): if not os.path.exists(gitconfig):
(rc, out, err) = clone(repo, dest) (rc, out, err) = clone(repo, dest)
if rc != 0: if rc != 0:
module.fail_json(msg=err) module.fail_json(msg=err)
else: else:
# else do a pull # else do a pull
local_mods = has_local_mods(dest)
before = get_version(dest) before = get_version(dest)
(rc, out, err) = reset(dest) (rc, out, err) = reset(module,dest,force)
if rc != 0: if rc != 0:
module.fail_json(msg=err) module.fail_json(msg=err)
(rc, out, err) = pull(module, repo, dest, version) (rc, out, err) = pull(module, repo, dest, version)
@ -182,7 +196,7 @@ def main():
after = get_version(dest) after = get_version(dest)
changed = False changed = False
if before != after: if before != after or local_mods:
changed = True changed = True
module.exit_json(changed=changed, before=before, after=after) module.exit_json(changed=changed, before=before, after=after)

@ -0,0 +1,152 @@
#!/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/>.
# I wanted to keep this simple at first, so for now this checks out
# from the given branch of a repo at a particular SHA or
# tag. Latest is not supported, you should not be doing
# that. Contribs welcome! -- MPD
# requires subversion and grep on the client.
import re
def get_version(dest):
''' samples the version of the git repo '''
os.chdir(dest)
cmd = "svn info"
revision = filter(lambda l: re.search('Revision',l) != None,os.popen(cmd).read().splitlines())
url = filter(lambda l: re.search('^URL',l) != None,os.popen(cmd).read().splitlines())
return [revision[0],url[0]]
def checkout(repo, dest):
''' makes a new svn repo if it does not already exist '''
cmd = "svn co %s %s" % (repo, dest)
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = cmd.communicate()
rc = cmd.returncode
return (rc, out, err)
def switch(repo, dest):
''' makes a new svn repo if it does not already exist '''
cmd = "svn sw %s %s" % (repo, dest)
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = cmd.communicate()
rc = cmd.returncode
return (rc, out, err)
def has_local_mods(dest):
os.chdir(dest)
cmd = "svn status"
lines = os.popen(cmd).read().splitlines()
filtered = filter(lambda c: re.search('^\\?.*$',c) == None,lines)
return len(filtered) > 0
def reset(dest,force):
'''
Reset the repo:
force: if true, then remove any local modifications. Else, fail if there are local modifications
'''
if has_local_mods(dest):
if force:
cmd = "svn revert -R ."
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = cmd.communicate()
rc = cmd.returncode
return (rc, out, err)
else:
return (-1,"ERROR: modified files exist in the repository.","")
return (0,"","")
def update(module, dest, version):
''' update an existing svn repo '''
os.chdir(dest)
cmd = ''
if version != 'HEAD':
cmd = "svn up -r %s" % version
else:
cmd = "svn up"
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = cmd.communicate()
rc = cmd.returncode
return (rc, out, err)
# ===========================================
def main():
module = AnsibleModule(
argument_spec = dict(
dest=dict(required=True),
repo=dict(required=True, aliases=['name']),
revision=dict(default='HEAD'),
force=dict(default='yes', choices=['yes', 'no'], aliases=['force'])
)
)
dest = module.params['dest']
repo = module.params['repo']
revision = module.params['revision']
force = module.boolean(module.params['force'])
rc, out, err, status = (0, None, None, None)
# if there is no .svn folder, do a checkout
# else update.
before = None
local_mods = False
if not os.path.exists("%s/.svn" % (dest)):
if os.path.exists(dest):
module.fail_json(msg="%s folder already exists, but its not a subversion repository." % (dest))
else:
(rc, out, err) = checkout(repo, dest)
if rc != 0:
module.fail_json(msg=err)
else:
local_mods = has_local_mods(dest)
# else do an update
before = get_version(dest)
(rc, out, err) = reset(dest,force)
if rc != 0:
module.fail_json(msg=err)
(rc, out, err) = switch(repo, dest)
if rc != 0:
module.fail_json(msg=err)
# handle errors from switch or pull
if err.find('ERROR') != -1:
module.fail_json(msg=err)
# switch to version specified regardless of whether
# we cloned or pulled
(rc, out, err) = update(module, dest, revision)
if rc != 0:
module.fail_json(msg=err)
# 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()
Loading…
Cancel
Save