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"
|
2015-06-15 21:53:30 +02:00
|
|
|
author: "Dane Summers (@dsummersl) <njharman@gmail.com>"
|
2012-12-11 03:34:26 +01:00
|
|
|
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.
|
2015-01-29 01:25:56 +01:00
|
|
|
Prior to 1.9 the default was `yes`.
|
2012-09-29 16:28:55 +02:00
|
|
|
required: false
|
2015-01-29 01:19:00 +01:00
|
|
|
default: "no"
|
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
|
2014-05-27 23:20:10 +02:00
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
2014-04-08 17:25:44 +02:00
|
|
|
version_added: "1.6"
|
2013-12-13 09:51:55 +01:00
|
|
|
description:
|
2014-05-27 23:20:10 +02:00
|
|
|
- If C(yes), do export instead of checkout/update.
|
2015-06-29 13:45:08 +02:00
|
|
|
switch:
|
|
|
|
required: false
|
|
|
|
default: "yes"
|
|
|
|
choices: [ "yes", "no" ]
|
2015-07-07 13:38:19 +02:00
|
|
|
version_added: "2.0"
|
2015-06-29 13:45:08 +02:00
|
|
|
description:
|
|
|
|
- If C(no), do not call svn switch before 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
|
|
|
|
2015-06-29 13:45:08 +02:00
|
|
|
def _exec(self, args, check_rc=True):
|
2015-07-07 13:38:19 +02:00
|
|
|
'''Execute a subversion command, and return output. If check_rc is False, returns the return code instead of the output.'''
|
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)
|
2015-07-07 13:38:19 +02:00
|
|
|
rc, out, err = self.module.run_command(bits, check_rc)
|
2015-06-29 13:45:08 +02:00
|
|
|
if check_rc:
|
|
|
|
return out.splitlines()
|
|
|
|
else:
|
|
|
|
return rc
|
|
|
|
|
|
|
|
def is_svn_repo(self):
|
|
|
|
'''Checks if path is a SVN Repo.'''
|
|
|
|
rc = self._exec(["info", self.dest], check_rc=False)
|
2015-07-07 13:38:19 +02:00
|
|
|
return rc == 0
|
2012-12-11 03:34:26 +01:00
|
|
|
|
|
|
|
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])
|
2015-06-30 23:23:51 +02:00
|
|
|
|
2013-12-13 09:51:55 +01:00
|
|
|
def export(self, force=False):
|
|
|
|
'''Export svn repo to directory'''
|
2014-12-13 22:05:40 +01:00
|
|
|
cmd = ["export"]
|
|
|
|
if force:
|
|
|
|
cmd.append("--force")
|
|
|
|
cmd.extend(["-r", self.revision, self.repo, self.dest])
|
|
|
|
|
|
|
|
self._exec(cmd)
|
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.'''
|
2015-06-29 13:45:08 +02:00
|
|
|
lines = self._exec(["status", "--quiet", "--ignore-externals", self.dest])
|
2015-02-13 15:06:15 +01:00
|
|
|
# The --quiet option will return only modified files.
|
2015-06-29 13:45:08 +02:00
|
|
|
|
2012-12-11 03:34:26 +01:00
|
|
|
# Has local mods if more than 0 modifed revisioned files.
|
2015-02-13 15:06:15 +01:00
|
|
|
return len(filter(len, lines)) > 0
|
2012-12-11 03:34:26 +01:00
|
|
|
|
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']),
|
2014-12-13 22:05:40 +01:00
|
|
|
force=dict(default='no', 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'),
|
2015-06-29 13:45:08 +02:00
|
|
|
switch=dict(default=True, 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']
|
2015-06-29 13:45:08 +02:00
|
|
|
switch = module.params['switch']
|
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
|
|
|
|
2014-12-13 22:07:41 +01:00
|
|
|
if export or not os.path.exists(dest):
|
2012-12-11 03:34:26 +01:00
|
|
|
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:
|
2014-12-13 22:05:40 +01:00
|
|
|
svn.export(force=force)
|
2015-06-29 13:45:08 +02:00
|
|
|
elif svn.is_svn_repo():
|
2012-12-11 03:34:26 +01:00
|
|
|
# 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()
|
2015-06-29 13:45:08 +02:00
|
|
|
if switch:
|
|
|
|
svn.switch()
|
2012-12-11 03:34:26 +01:00
|
|
|
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
|
|
|
|
2014-12-13 22:07:41 +01:00
|
|
|
if export:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
else:
|
|
|
|
after = svn.get_revision()
|
|
|
|
changed = before != after or local_mods
|
|
|
|
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()
|