bzr 6.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#!/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
author: André Paramés
26
version_added: "1.1"
27 28 29 30
short_description: Deploy software (or files) from bzr branches
description:
    - Manage I(bzr) branches to deploy files or software.
options:
31
    name:
32
        required: true
33
        aliases: [ 'parent' ]
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        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
        default: "yes"
Michael DeHaan committed
49
        choices: [ 'yes', 'no' ]
50 51 52
        description:
            - If C(yes), any modified files in the working
              tree will be discarded.
53 54 55 56 57 58
    executable:
        required: false
        default: null
        version_added: "1.4"
        description:
            - Path to bzr executable to use. If not supplied,
59
              the normal mechanism for resolving binary paths will be used.
60 61 62 63 64
'''

EXAMPLES = '''
# Example bzr checkout from Ansible Playbooks
- bzr: name=bzr+ssh://foosball.example.org/path/to/branch dest=/srv/checkout version=22
65 66 67
'''

import re
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140


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

    def _command(self, args_list, **kwargs):
        (rc, out, err) = self.module.run_command(
            [self.bzr_path] + args_list, **kwargs)
        return (rc, out, err)

    def get_version(self):
        '''samples the version of the bzr branch'''
        os.chdir(self.dest)
        cmd = "%s revno" % self.bzr_path
        revno = os.popen(cmd).read().strip()
        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
        os.chdir(dest_dirname)
        if self.version.lower() != 'head':
            args_list = ["branch", "-r", self.version, self.parent, self.dest]
        else:
            args_list = ["branch", self.parent, self.dest]
        return self._command(args_list, check_rc=True)

    def has_local_mods(self):
        os.chdir(self.dest)
        cmd = "%s status -S" % self.bzr_path
        lines = os.popen(cmd).read().splitlines()
        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.
        '''
        os.chdir(self.dest)
        if not force and self.has_local_mods():
            self.module.fail_json(msg="Local modifications exist in branch (force=no).")
        return self._command(["revert"], check_rc=True)

    def fetch(self):
        '''updates branch from remote sources'''
        os.chdir(self.dest)
        if self.version.lower() != 'head':
            (rc, out, err) = self._command(["pull", "-r", self.version])
        else:
            (rc, out, err) = self._command(["pull"])
        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'''
        os.chdir(self.dest)
        if self.version.lower() != 'head':
            args_list = ["revert", "-r", self.version]
        else:
            args_list = ["revert"]
        return self._command(args_list, check_rc=True)
141 142 143 144 145 146 147

# ===========================================

def main():
    module = AnsibleModule(
        argument_spec = dict(
            dest=dict(required=True),
148
            name=dict(required=True, aliases=['parent']),
149
            version=dict(default='head'),
150 151
            force=dict(default='yes', type='bool'),
            executable=dict(default=None),
152 153 154 155
        )
    )

    dest    = os.path.abspath(os.path.expanduser(module.params['dest']))
156
    parent  = module.params['name']
157 158
    version = module.params['version']
    force   = module.params['force']
159
    bzr_path = module.params['executable'] or module.get_bin_path('bzr', True)
160 161 162 163 164

    bzrconfig = os.path.join(dest, '.bzr', 'branch', 'branch.conf')

    rc, out, err, status = (0, None, None, None)

165 166
    bzr = Bzr(module, parent, dest, version, bzr_path)

167 168 169 170 171
    # 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):
172 173
        (rc, out, err) = bzr.clone()

174 175
    else:
        # else do a pull
176 177 178
        local_mods = bzr.has_local_mods()
        before = bzr.get_version()
        (rc, out, err) = bzr.reset(force)
179 180
        if rc != 0:
            module.fail_json(msg=err)
181
        (rc, out, err) = bzr.fetch()
182 183 184 185 186
        if rc != 0:
            module.fail_json(msg=err)

    # switch to version specified regardless of whether
    # we cloned or pulled
187
    (rc, out, err) = bzr.switch_version()
188 189

    # determine if we changed anything
190
    after = bzr.get_version()
191 192 193 194 195 196 197
    changed = False

    if before != after or local_mods:
        changed = True

    module.exit_json(changed=changed, before=before, after=after)

198 199
# import module snippets
from ansible.module_utils.basic import *
200
main()