2012-08-15 15:04:17 +02:00
|
|
|
#!/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/>.
|
|
|
|
|
2012-09-29 16:28:55 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: subversion
|
|
|
|
short_description: Deploys a subversion repository.
|
|
|
|
description:
|
2012-11-28 10:02:20 +01:00
|
|
|
- This module is really simple: It checks out from the given branch of a repo or at a particular tag.
|
2012-09-29 16:28:55 +02:00
|
|
|
version_added: "0.7"
|
|
|
|
options:
|
|
|
|
repo:
|
|
|
|
description:
|
|
|
|
- The subversion URL to the repository.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
dest:
|
|
|
|
description:
|
|
|
|
- Absolute path where the repository should be deployed.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
force:
|
|
|
|
description:
|
2012-11-21 18:49:30 +01:00
|
|
|
- If C(yes), any modified files in the working repository will be discarded. If C(no), this module will fail if it encounters modified files.
|
2012-09-29 16:28:55 +02:00
|
|
|
required: false
|
|
|
|
default: yes
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
examples:
|
2012-10-23 15:14:01 +02:00
|
|
|
- code: "subversion: repo=svn+ssh://an.example.org/path/to/repo dest=/src/checkout"
|
2012-09-29 16:28:55 +02:00
|
|
|
description: Export subversion repository in a specified folder
|
|
|
|
notes:
|
2012-11-21 18:49:30 +01:00
|
|
|
- Requires I(subversion) and I(grep) on the client.
|
2012-09-29 16:28:55 +02:00
|
|
|
requirements: [ ]
|
|
|
|
author: Dane Summers
|
|
|
|
'''
|
2012-08-15 15:04:17 +02:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
def get_version(dest):
|
|
|
|
''' samples the version of the git repo '''
|
|
|
|
os.chdir(dest)
|
2012-08-23 05:06:03 +02:00
|
|
|
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]]
|
2012-08-15 15:04:17 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2012-08-23 05:06:03 +02:00
|
|
|
def switch(repo, dest):
|
|
|
|
''' makes a new svn repo if it does not already exist '''
|
|
|
|
cmd = "svn sw %s %s" % (repo, dest)
|
2012-08-15 15:04:17 +02:00
|
|
|
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(out, err) = cmd.communicate()
|
|
|
|
rc = cmd.returncode
|
|
|
|
return (rc, out, err)
|
|
|
|
|
2012-08-23 05:06:03 +02:00
|
|
|
def has_local_mods(dest):
|
|
|
|
os.chdir(dest)
|
|
|
|
cmd = "svn status"
|
2012-08-23 06:07:14 +02:00
|
|
|
lines = os.popen(cmd).read().splitlines()
|
|
|
|
filtered = filter(lambda c: re.search('^\\?.*$',c) == None,lines)
|
2012-08-23 05:06:03 +02:00
|
|
|
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,"","")
|
|
|
|
|
2012-08-15 15:04:17 +02:00
|
|
|
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']),
|
2012-08-23 05:06:03 +02:00
|
|
|
revision=dict(default='HEAD'),
|
2012-08-23 06:07:14 +02:00
|
|
|
force=dict(default='yes', choices=['yes', 'no'], aliases=['force'])
|
2012-08-15 15:04:17 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2012-09-04 02:06:47 +02:00
|
|
|
dest = os.path.expanduser(module.params['dest'])
|
2012-08-23 05:06:03 +02:00
|
|
|
repo = module.params['repo']
|
2012-08-15 15:04:17 +02:00
|
|
|
revision = module.params['revision']
|
2012-08-23 05:06:03 +02:00
|
|
|
force = module.boolean(module.params['force'])
|
2012-08-15 15:04:17 +02:00
|
|
|
|
|
|
|
rc, out, err, status = (0, None, None, None)
|
|
|
|
|
|
|
|
# if there is no .svn folder, do a checkout
|
|
|
|
# else update.
|
|
|
|
before = None
|
2012-08-23 05:06:03 +02:00
|
|
|
local_mods = False
|
2012-08-15 15:04:17 +02:00
|
|
|
if not os.path.exists("%s/.svn" % (dest)):
|
2012-08-23 05:06:03 +02:00
|
|
|
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)
|
2012-08-15 15:04:17 +02:00
|
|
|
else:
|
2012-08-23 05:06:03 +02:00
|
|
|
local_mods = has_local_mods(dest)
|
2012-08-15 15:04:17 +02:00
|
|
|
# else do an update
|
|
|
|
before = get_version(dest)
|
2012-08-23 05:06:03 +02:00
|
|
|
(rc, out, err) = reset(dest,force)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
(rc, out, err) = switch(repo, dest)
|
2012-08-15 15:04:17 +02:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
|
2012-08-23 05:06:03 +02:00
|
|
|
# handle errors from switch or pull
|
2012-08-15 15:04:17 +02:00
|
|
|
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
|
|
|
|
|
2012-08-23 05:06:03 +02:00
|
|
|
if before != after or local_mods:
|
2012-08-15 15:04:17 +02:00
|
|
|
changed = True
|
|
|
|
|
2012-08-23 05:06:03 +02:00
|
|
|
module.exit_json(changed=changed, before=before, after=after)
|
2012-08-15 15:04:17 +02:00
|
|
|
|
|
|
|
# include magic from lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|