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:
|
2013-09-24 18:10:09 +02:00
|
|
|
- Deploy given repository URL / revision to dest. If dest exists, update to the specified revision, otherwise perform a checkout.
|
2012-09-29 16:28:55 +02:00
|
|
|
version_added: "0.7"
|
2012-12-11 03:34:26 +01:00
|
|
|
author: Dane Summers, njharman@gmail.com
|
|
|
|
notes:
|
2014-04-03 14:34:43 +02:00
|
|
|
- Requires I(svn) to be installed on the client.
|
2012-12-11 03:34:26 +01:00
|
|
|
requirements: []
|
2012-09-29 16:28:55 +02:00
|
|
|
options:
|
|
|
|
repo:
|
|
|
|
description:
|
|
|
|
- The subversion URL to the repository.
|
|
|
|
required: true
|
2013-07-23 19:16:20 +02:00
|
|
|
aliases: [ name, repository ]
|
2012-09-29 16:28:55 +02:00
|
|
|
default: null
|
|
|
|
dest:
|
|
|
|
description:
|
|
|
|
- Absolute path where the repository should be deployed.
|
|
|
|
required: true
|
|
|
|
default: null
|
2012-12-11 03:34:26 +01:00
|
|
|
revision:
|
|
|
|
description:
|
|
|
|
- Specific revision to checkout.
|
|
|
|
required: false
|
|
|
|
default: HEAD
|
2013-07-23 19:16:20 +02:00
|
|
|
aliases: [ version ]
|
2012-09-29 16:28:55 +02:00
|
|
|
force:
|
|
|
|
description:
|
2012-12-11 03:34:26 +01:00
|
|
|
- If C(yes), modified files will be discarded. If C(no), module will fail if it encounters modified files.
|
2012-09-29 16:28:55 +02:00
|
|
|
required: false
|
2013-03-12 13:18:12 +01:00
|
|
|
default: "yes"
|
2012-09-29 16:28:55 +02:00
|
|
|
choices: [ "yes", "no" ]
|
2012-12-11 03:34:26 +01:00
|
|
|
username:
|
|
|
|
description:
|
|
|
|
- --username parameter passed to svn.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
password:
|
|
|
|
description:
|
|
|
|
- --password parameter passed to svn.
|
|
|
|
required: false
|
|
|
|
default: null
|
2013-09-26 23:27:40 +02:00
|
|
|
executable:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "1.4"
|
|
|
|
description:
|
|
|
|
- Path to svn 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-12-13 09:51:55 +01:00
|
|
|
export:
|
|
|
|
required: false
|
|
|
|
default: False
|
2014-04-08 17:25:44 +02:00
|
|
|
version_added: "1.6"
|
2013-12-13 09:51:55 +01:00
|
|
|
description:
|
|
|
|
- If True, do export instead of checkout/update.
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Checkout subversion repository to specified folder.
|
|
|
|
- subversion: repo=svn+ssh://an.example.org/path/to/repo dest=/src/checkout
|
2014-02-10 15:04:17 +01:00
|
|
|
|
|
|
|
# Export subversion directory to folder
|
|
|
|
- subversion: repo=svn+ssh://an.example.org/path/to/repo dest=/src/export export=True
|
2012-09-29 16:28:55 +02:00
|
|
|
'''
|
2012-08-15 15:04:17 +02:00
|
|
|
|
|
|
|
import re
|
2013-09-26 23:27:40 +02:00
|
|
|
import tempfile
|
2012-08-15 15:04:17 +02:00
|
|
|
|
2012-12-11 03:34:26 +01:00
|
|
|
|
|
|
|
class Subversion(object):
|
2013-09-26 23:27:40 +02:00
|
|
|
def __init__(
|
|
|
|
self, module, dest, repo, revision, username, password, svn_path):
|
2012-12-12 23:55:30 +01:00
|
|
|
self.module = module
|
2012-12-11 03:34:26 +01:00
|
|
|
self.dest = dest
|
|
|
|
self.repo = repo
|
|
|
|
self.revision = revision
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
2013-09-26 23:27:40 +02:00
|
|
|
self.svn_path = svn_path
|
2012-12-11 03:34:26 +01:00
|
|
|
|
|
|
|
def _exec(self, args):
|
2013-09-26 23:27:40 +02:00
|
|
|
bits = [
|
|
|
|
self.svn_path,
|
|
|
|
'--non-interactive',
|
|
|
|
'--trust-server-cert',
|
|
|
|
'--no-auth-cache',
|
|
|
|
]
|
2012-12-11 03:34:26 +01:00
|
|
|
if self.username:
|
2014-02-27 04:23:14 +01:00
|
|
|
bits.extend(["--username", self.username])
|
2012-12-11 03:34:26 +01:00
|
|
|
if self.password:
|
2014-02-27 04:23:14 +01:00
|
|
|
bits.extend(["--password", self.password])
|
|
|
|
bits.extend(args)
|
|
|
|
rc, out, err = self.module.run_command(bits, check_rc=True)
|
2012-12-11 03:34:26 +01:00
|
|
|
return out.splitlines()
|
|
|
|
|
|
|
|
def checkout(self):
|
|
|
|
'''Creates new svn working directory if it does not already exist.'''
|
2014-02-27 04:23:14 +01:00
|
|
|
self._exec(["checkout", "-r", self.revision, self.repo, self.dest])
|
2013-12-13 09:51:55 +01:00
|
|
|
|
|
|
|
def export(self, force=False):
|
|
|
|
'''Export svn repo to directory'''
|
2014-03-26 03:25:50 +01:00
|
|
|
self._exec(["export", "-r", self.revision, self.repo, self.dest])
|
2012-12-11 03:34:26 +01:00
|
|
|
|
|
|
|
def switch(self):
|
|
|
|
'''Change working directory's repo.'''
|
|
|
|
# switch to ensure we are pointing at correct repo.
|
2014-02-27 04:23:14 +01:00
|
|
|
self._exec(["switch", self.repo, self.dest])
|
2012-12-11 03:34:26 +01:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
'''Update existing svn working directory.'''
|
2014-02-27 04:23:14 +01:00
|
|
|
self._exec(["update", "-r", self.revision, self.dest])
|
2012-12-11 03:34:26 +01:00
|
|
|
|
|
|
|
def revert(self):
|
|
|
|
'''Revert svn working directory.'''
|
2014-02-27 04:23:14 +01:00
|
|
|
self._exec(["revert", "-R", self.dest])
|
2012-12-11 03:34:26 +01:00
|
|
|
|
|
|
|
def get_revision(self):
|
|
|
|
'''Revision and URL of subversion working directory.'''
|
2014-02-27 04:23:14 +01:00
|
|
|
text = '\n'.join(self._exec(["info", self.dest]))
|
2012-12-11 03:34:26 +01:00
|
|
|
rev = re.search(r'^Revision:.*$', text, re.MULTILINE).group(0)
|
|
|
|
url = re.search(r'^URL:.*$', text, re.MULTILINE).group(0)
|
|
|
|
return rev, url
|
|
|
|
|
|
|
|
def has_local_mods(self):
|
|
|
|
'''True if revisioned files have been added or modified. Unrevisioned files are ignored.'''
|
2014-02-27 04:23:14 +01:00
|
|
|
lines = self._exec(["status", self.dest])
|
2012-12-11 03:34:26 +01:00
|
|
|
# Match only revisioned files, i.e. ignore status '?'.
|
|
|
|
regex = re.compile(r'^[^?]')
|
|
|
|
# Has local mods if more than 0 modifed revisioned files.
|
|
|
|
return len(filter(regex.match, lines)) > 0
|
|
|
|
|
2013-03-06 19:23:35 +01:00
|
|
|
def needs_update(self):
|
|
|
|
curr, url = self.get_revision()
|
2014-02-27 04:23:14 +01:00
|
|
|
out2 = '\n'.join(self._exec(["info", "-r", "HEAD", self.dest]))
|
2013-03-06 19:23:35 +01:00
|
|
|
head = re.search(r'^Revision:.*$', out2, re.MULTILINE).group(0)
|
|
|
|
rev1 = int(curr.split(':')[1].strip())
|
|
|
|
rev2 = int(head.split(':')[1].strip())
|
|
|
|
change = False
|
|
|
|
if rev1 < rev2:
|
|
|
|
change = True
|
|
|
|
return change, curr, head
|
|
|
|
|
2012-08-15 15:04:17 +02:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
2012-12-11 03:34:26 +01:00
|
|
|
argument_spec=dict(
|
2012-08-15 15:04:17 +02:00
|
|
|
dest=dict(required=True),
|
2012-12-11 03:34:26 +01:00
|
|
|
repo=dict(required=True, aliases=['name', 'repository']),
|
2013-07-23 19:16:20 +02:00
|
|
|
revision=dict(default='HEAD', aliases=['rev', 'version']),
|
2013-02-23 22:56:45 +01:00
|
|
|
force=dict(default='yes', type='bool'),
|
2012-12-11 03:34:26 +01:00
|
|
|
username=dict(required=False),
|
|
|
|
password=dict(required=False),
|
2013-09-26 23:27:40 +02:00
|
|
|
executable=dict(default=None),
|
2014-05-27 21:09:50 +02:00
|
|
|
export=dict(default=False, required=False, type='bool'),
|
2013-02-27 21:23:35 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=True
|
2012-08-15 15:04:17 +02:00
|
|
|
)
|
|
|
|
|
2012-12-11 03:34:26 +01:00
|
|
|
dest = os.path.expanduser(module.params['dest'])
|
|
|
|
repo = module.params['repo']
|
2012-08-15 15:04:17 +02:00
|
|
|
revision = module.params['revision']
|
2013-02-23 19:59:52 +01:00
|
|
|
force = module.params['force']
|
2012-12-11 03:34:26 +01:00
|
|
|
username = module.params['username']
|
|
|
|
password = module.params['password']
|
2013-09-26 23:27:40 +02:00
|
|
|
svn_path = module.params['executable'] or module.get_bin_path('svn', True)
|
2013-12-13 09:51:55 +01:00
|
|
|
export = module.params['export']
|
2012-12-11 03:34:26 +01:00
|
|
|
|
2014-02-27 04:23:14 +01:00
|
|
|
os.environ['LANG'] = 'C'
|
2013-09-26 23:27:40 +02:00
|
|
|
svn = Subversion(module, dest, repo, revision, username, password, svn_path)
|
2012-12-11 03:34:26 +01:00
|
|
|
|
|
|
|
if not os.path.exists(dest):
|
|
|
|
before = None
|
|
|
|
local_mods = False
|
2013-02-27 21:23:35 +01:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
2013-12-13 09:51:55 +01:00
|
|
|
if not export:
|
|
|
|
svn.checkout()
|
|
|
|
else:
|
|
|
|
svn.export()
|
2012-12-11 03:34:26 +01:00
|
|
|
elif os.path.exists("%s/.svn" % (dest, )):
|
|
|
|
# Order matters. Need to get local mods before switch to avoid false
|
|
|
|
# positives. Need to switch before revert to ensure we are reverting to
|
|
|
|
# correct repo.
|
2013-02-27 21:23:35 +01:00
|
|
|
if module.check_mode:
|
2013-03-06 19:23:35 +01:00
|
|
|
check, before, after = svn.needs_update()
|
|
|
|
module.exit_json(changed=check, before=before, after=after)
|
2012-12-11 03:34:26 +01:00
|
|
|
before = svn.get_revision()
|
|
|
|
local_mods = svn.has_local_mods()
|
|
|
|
svn.switch()
|
|
|
|
if local_mods:
|
|
|
|
if force:
|
|
|
|
svn.revert()
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="ERROR: modified files exist in the repository.")
|
|
|
|
svn.update()
|
2012-08-15 15:04:17 +02:00
|
|
|
else:
|
2012-12-11 03:34:26 +01:00
|
|
|
module.fail_json(msg="ERROR: %s folder already exists, but its not a subversion repository." % (dest, ))
|
2012-08-15 15:04:17 +02:00
|
|
|
|
2012-12-11 03:34:26 +01:00
|
|
|
after = svn.get_revision()
|
|
|
|
changed = before != after or local_mods
|
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
|
|
|
|
2013-12-02 21:11:23 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2012-08-15 15:04:17 +02:00
|
|
|
main()
|