2013-03-11 23:40:59 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, André Paramés <git@andreparames.com>
|
|
|
|
# Based on the Git module by 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 = u'''
|
|
|
|
---
|
|
|
|
module: bzr
|
2015-05-14 15:37:00 +02:00
|
|
|
author: '"André Paramés (@andreparames)" <git@andreparames.com>'
|
2013-03-17 16:51:34 +01:00
|
|
|
version_added: "1.1"
|
2013-03-11 23:40:59 +01:00
|
|
|
short_description: Deploy software (or files) from bzr branches
|
|
|
|
description:
|
|
|
|
- Manage I(bzr) branches to deploy files or software.
|
|
|
|
options:
|
2013-03-17 16:53:27 +01:00
|
|
|
name:
|
2013-03-11 23:40:59 +01:00
|
|
|
required: true
|
2013-03-17 16:53:27 +01:00
|
|
|
aliases: [ 'parent' ]
|
2013-03-11 23:40:59 +01:00
|
|
|
description:
|
|
|
|
- SSH or HTTP protocol address of the parent branch.
|
|
|
|
dest:
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- Absolute path of where the branch should be cloned to.
|
|
|
|
version:
|
|
|
|
required: false
|
|
|
|
default: "head"
|
|
|
|
description:
|
|
|
|
- What version of the branch to clone. This can be the
|
|
|
|
bzr revno or revid.
|
|
|
|
force:
|
|
|
|
required: false
|
2015-01-30 03:21:21 +01:00
|
|
|
default: "no"
|
2013-06-01 18:15:19 +02:00
|
|
|
choices: [ 'yes', 'no' ]
|
2013-03-11 23:40:59 +01:00
|
|
|
description:
|
|
|
|
- If C(yes), any modified files in the working
|
2015-01-30 03:21:21 +01:00
|
|
|
tree will be discarded. Before 1.9 the default
|
|
|
|
value was "yes".
|
2013-09-26 23:26:46 +02:00
|
|
|
executable:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "1.4"
|
|
|
|
description:
|
|
|
|
- Path to bzr 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-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Example bzr checkout from Ansible Playbooks
|
|
|
|
- bzr: name=bzr+ssh://foosball.example.org/path/to/branch dest=/srv/checkout version=22
|
2013-03-11 23:40:59 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
import re
|
2013-09-26 23:26:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Bzr(object):
|
|
|
|
def __init__(self, module, parent, dest, version, bzr_path):
|
|
|
|
self.module = module
|
|
|
|
self.parent = parent
|
|
|
|
self.dest = dest
|
|
|
|
self.version = version
|
|
|
|
self.bzr_path = bzr_path
|
|
|
|
|
2014-03-10 22:11:24 +01:00
|
|
|
def _command(self, args_list, cwd=None, **kwargs):
|
2014-03-12 22:04:19 +01:00
|
|
|
(rc, out, err) = self.module.run_command([self.bzr_path] + args_list, cwd=cwd, **kwargs)
|
2013-09-26 23:26:46 +02:00
|
|
|
return (rc, out, err)
|
|
|
|
|
|
|
|
def get_version(self):
|
|
|
|
'''samples the version of the bzr branch'''
|
2014-03-10 22:11:24 +01:00
|
|
|
|
2013-09-26 23:26:46 +02:00
|
|
|
cmd = "%s revno" % self.bzr_path
|
2014-03-10 22:11:24 +01:00
|
|
|
rc, stdout, stderr = self.module.run_command(cmd, cwd=self.dest)
|
|
|
|
revno = stdout.strip()
|
2013-09-26 23:26:46 +02:00
|
|
|
return revno
|
|
|
|
|
|
|
|
def clone(self):
|
|
|
|
'''makes a new bzr branch if it does not already exist'''
|
|
|
|
dest_dirname = os.path.dirname(self.dest)
|
|
|
|
try:
|
|
|
|
os.makedirs(dest_dirname)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if self.version.lower() != 'head':
|
|
|
|
args_list = ["branch", "-r", self.version, self.parent, self.dest]
|
|
|
|
else:
|
|
|
|
args_list = ["branch", self.parent, self.dest]
|
2014-03-10 22:11:24 +01:00
|
|
|
return self._command(args_list, check_rc=True, cwd=dest_dirname)
|
2013-09-26 23:26:46 +02:00
|
|
|
|
|
|
|
def has_local_mods(self):
|
2014-03-10 22:11:24 +01:00
|
|
|
|
2013-09-26 23:26:46 +02:00
|
|
|
cmd = "%s status -S" % self.bzr_path
|
2014-03-10 22:11:24 +01:00
|
|
|
rc, stdout, stderr = self.module.run_command(cmd, cwd=self.dest)
|
|
|
|
lines = stdout.splitlines()
|
|
|
|
|
2013-09-26 23:26:46 +02:00
|
|
|
lines = filter(lambda c: not re.search('^\\?\\?.*$', c), lines)
|
|
|
|
return len(lines) > 0
|
|
|
|
|
|
|
|
def reset(self, force):
|
|
|
|
'''
|
|
|
|
Resets the index and working tree to head.
|
|
|
|
Discards any changes to tracked files in the working
|
|
|
|
tree since that commit.
|
|
|
|
'''
|
|
|
|
if not force and self.has_local_mods():
|
|
|
|
self.module.fail_json(msg="Local modifications exist in branch (force=no).")
|
2014-03-10 22:11:24 +01:00
|
|
|
return self._command(["revert"], check_rc=True, cwd=self.dest)
|
2013-09-26 23:26:46 +02:00
|
|
|
|
|
|
|
def fetch(self):
|
|
|
|
'''updates branch from remote sources'''
|
|
|
|
if self.version.lower() != 'head':
|
2014-03-10 22:11:24 +01:00
|
|
|
(rc, out, err) = self._command(["pull", "-r", self.version], cwd=self.dest)
|
2013-09-26 23:26:46 +02:00
|
|
|
else:
|
2014-03-10 22:11:24 +01:00
|
|
|
(rc, out, err) = self._command(["pull"], cwd=self.dest)
|
2013-09-26 23:26:46 +02:00
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg="Failed to pull")
|
|
|
|
return (rc, out, err)
|
|
|
|
|
|
|
|
def switch_version(self):
|
|
|
|
'''once pulled, switch to a particular revno or revid'''
|
|
|
|
if self.version.lower() != 'head':
|
|
|
|
args_list = ["revert", "-r", self.version]
|
|
|
|
else:
|
|
|
|
args_list = ["revert"]
|
2014-03-10 22:11:24 +01:00
|
|
|
return self._command(args_list, check_rc=True, cwd=self.dest)
|
2013-03-11 23:40:59 +01:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
dest=dict(required=True),
|
2013-03-17 16:52:48 +01:00
|
|
|
name=dict(required=True, aliases=['parent']),
|
2013-03-11 23:40:59 +01:00
|
|
|
version=dict(default='head'),
|
2015-01-30 03:21:21 +01:00
|
|
|
force=dict(default='no', type='bool'),
|
2013-09-26 23:26:46 +02:00
|
|
|
executable=dict(default=None),
|
2013-03-11 23:40:59 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
dest = os.path.abspath(os.path.expanduser(module.params['dest']))
|
2013-03-17 16:52:48 +01:00
|
|
|
parent = module.params['name']
|
2013-03-11 23:40:59 +01:00
|
|
|
version = module.params['version']
|
|
|
|
force = module.params['force']
|
2013-09-26 23:26:46 +02:00
|
|
|
bzr_path = module.params['executable'] or module.get_bin_path('bzr', True)
|
2013-03-11 23:40:59 +01:00
|
|
|
|
|
|
|
bzrconfig = os.path.join(dest, '.bzr', 'branch', 'branch.conf')
|
|
|
|
|
|
|
|
rc, out, err, status = (0, None, None, None)
|
|
|
|
|
2013-09-26 23:26:46 +02:00
|
|
|
bzr = Bzr(module, parent, dest, version, bzr_path)
|
|
|
|
|
2013-03-11 23:40:59 +01:00
|
|
|
# if there is no bzr configuration, do a branch operation
|
|
|
|
# else pull and switch the version
|
|
|
|
before = None
|
|
|
|
local_mods = False
|
|
|
|
if not os.path.exists(bzrconfig):
|
2013-09-26 23:26:46 +02:00
|
|
|
(rc, out, err) = bzr.clone()
|
|
|
|
|
2013-03-11 23:40:59 +01:00
|
|
|
else:
|
|
|
|
# else do a pull
|
2013-09-26 23:26:46 +02:00
|
|
|
local_mods = bzr.has_local_mods()
|
|
|
|
before = bzr.get_version()
|
|
|
|
(rc, out, err) = bzr.reset(force)
|
2013-03-11 23:40:59 +01:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
2013-09-26 23:26:46 +02:00
|
|
|
(rc, out, err) = bzr.fetch()
|
2013-03-11 23:40:59 +01:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
|
|
|
|
# switch to version specified regardless of whether
|
|
|
|
# we cloned or pulled
|
2013-09-26 23:26:46 +02:00
|
|
|
(rc, out, err) = bzr.switch_version()
|
2013-03-11 23:40:59 +01:00
|
|
|
|
|
|
|
# determine if we changed anything
|
2013-09-26 23:26:46 +02:00
|
|
|
after = bzr.get_version()
|
2013-03-11 23:40:59 +01:00
|
|
|
changed = False
|
|
|
|
|
|
|
|
if before != after or local_mods:
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, before=before, after=after)
|
|
|
|
|
2013-12-02 21:11:23 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2013-03-11 23:40:59 +01:00
|
|
|
main()
|