2013-01-26 04:51:20 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
#-*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Yeukhon Wong <yeukhon@acm.org>
|
|
|
|
#
|
|
|
|
# This module was originally inspired by Brad Olson's ansible-module-mercurial
|
2013-01-31 08:11:28 +01:00
|
|
|
# <https://github.com/bradobro/ansible-module-mercurial>. This module tends
|
|
|
|
# to follow the git module implementation.
|
2013-01-26 04:51:20 +01:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
import ConfigParser
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: hg
|
|
|
|
short_description: Manages Mercurial (hg) repositories.
|
|
|
|
description:
|
|
|
|
- Manages Mercurial (hg) repositories. Supports SSH, HTTP/S and local address.
|
|
|
|
version_added: "1.0"
|
|
|
|
author: Yeukhon Wong
|
|
|
|
options:
|
|
|
|
repo:
|
|
|
|
description:
|
2013-01-31 04:40:26 +01:00
|
|
|
- The repository address.
|
2013-01-26 04:51:20 +01:00
|
|
|
required: true
|
|
|
|
default: null
|
2013-07-23 19:16:20 +02:00
|
|
|
aliases: [ name ]
|
2013-01-26 04:51:20 +01:00
|
|
|
dest:
|
|
|
|
description:
|
|
|
|
- Absolute path of where the repository should be cloned to.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
revision:
|
|
|
|
description:
|
2013-01-31 08:11:28 +01:00
|
|
|
- Equivalent C(-r) option in hg command which could be the changeset, revision number,
|
2013-10-10 17:15:04 +02:00
|
|
|
branch name or even tag.
|
2013-01-26 04:51:20 +01:00
|
|
|
required: false
|
|
|
|
default: "default"
|
2013-07-23 19:16:20 +02:00
|
|
|
aliases: [ version ]
|
2013-01-26 04:51:20 +01:00
|
|
|
force:
|
|
|
|
description:
|
2013-07-03 21:23:08 +02:00
|
|
|
- Discards uncommitted changes. Runs C(hg update -C).
|
2013-01-26 04:51:20 +01:00
|
|
|
required: false
|
|
|
|
default: "yes"
|
2013-03-12 13:18:12 +01:00
|
|
|
choices: [ "yes", "no" ]
|
2013-01-31 08:11:28 +01:00
|
|
|
purge:
|
|
|
|
description:
|
2013-12-03 08:50:33 +01:00
|
|
|
- Deletes untracked files. Runs C(hg purge).
|
2013-01-31 08:11:28 +01:00
|
|
|
required: false
|
|
|
|
default: "no"
|
2013-03-12 13:18:12 +01:00
|
|
|
choices: [ "yes", "no" ]
|
2013-09-26 23:27:16 +02:00
|
|
|
executable:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "1.4"
|
|
|
|
description:
|
|
|
|
- Path to hg 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-01-26 04:51:20 +01:00
|
|
|
notes:
|
2013-11-01 01:00:11 +01:00
|
|
|
- "If the task seems to be hanging, first verify remote host is in C(known_hosts).
|
2013-10-31 23:00:08 +01:00
|
|
|
SSH will prompt user to authorize the first contact with a remote host. To avoid this prompt,
|
|
|
|
one solution is to add the remote host public key in C(/etc/ssh/ssh_known_hosts) before calling
|
2013-11-01 01:00:11 +01:00
|
|
|
the hg module, with the following command: ssh-keyscan remote_host.com >> /etc/ssh/ssh_known_hosts."
|
2013-01-26 04:51:20 +01:00
|
|
|
requirements: [ ]
|
|
|
|
'''
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Ensure the current working copy is inside the stable branch and deletes untracked files if any.
|
|
|
|
- hg: repo=https://bitbucket.org/user/repo1 dest=/home/user/repo1 revision=stable purge=yes
|
|
|
|
'''
|
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
class Hg(object):
|
|
|
|
|
|
|
|
def __init__(self, module, dest, repo, revision, hg_path):
|
|
|
|
self.module = module
|
|
|
|
self.dest = dest
|
|
|
|
self.repo = repo
|
|
|
|
self.revision = revision
|
2013-10-10 17:46:10 +02:00
|
|
|
self.hg_path = hg_path
|
2013-09-26 23:27:16 +02:00
|
|
|
|
|
|
|
def _command(self, args_list):
|
|
|
|
(rc, out, err) = self.module.run_command([self.hg_path] + args_list)
|
|
|
|
return (rc, out, err)
|
|
|
|
|
|
|
|
def _list_untracked(self):
|
2013-12-03 08:50:33 +01:00
|
|
|
args = ['purge', '--config', 'extensions.purge=', '-R', self.dest, '--print']
|
|
|
|
return self._command(args)
|
2013-09-26 23:27:16 +02:00
|
|
|
|
|
|
|
def get_revision(self):
|
|
|
|
"""
|
|
|
|
hg id -b -i -t returns a string in the format:
|
|
|
|
"<changeset>[+] <branch_name> <tag>"
|
|
|
|
This format lists the state of the current working copy,
|
|
|
|
and indicates whether there are uncommitted changes by the
|
|
|
|
plus sign. Otherwise, the sign is omitted.
|
|
|
|
|
|
|
|
Read the full description via hg id --help
|
|
|
|
"""
|
|
|
|
(rc, out, err) = self._command(['id', '-b', '-i', '-t', '-R', self.dest])
|
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
|
|
|
else:
|
|
|
|
return out.strip('\n')
|
2013-01-26 04:51:20 +01:00
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
def has_local_mods(self):
|
|
|
|
now = self.get_revision()
|
|
|
|
if '+' in now:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def discard(self):
|
|
|
|
before = self.has_local_mods()
|
|
|
|
if not before:
|
|
|
|
return False
|
2013-01-31 08:11:28 +01:00
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
(rc, out, err) = self._command(['update', '-C', '-R', self.dest])
|
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
2013-01-26 04:51:20 +01:00
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
after = self.has_local_mods()
|
|
|
|
if before != after and not after: # no more local modification
|
|
|
|
return True
|
2013-10-10 17:15:04 +02:00
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
def purge(self):
|
|
|
|
# before purge, find out if there are any untracked files
|
|
|
|
(rc1, out1, err1) = self._list_untracked()
|
|
|
|
if rc1 != 0:
|
|
|
|
self.module.fail_json(msg=err1)
|
2013-10-10 17:15:04 +02:00
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
# there are some untrackd files
|
|
|
|
if out1 != '':
|
2013-12-03 08:50:33 +01:00
|
|
|
args = ['purge', '--config', 'extensions.purge=', '-R', self.dest]
|
|
|
|
(rc2, out2, err2) = self._command(args)
|
|
|
|
if rc2 != 0:
|
2013-09-26 23:27:16 +02:00
|
|
|
self.module.fail_json(msg=err2)
|
|
|
|
return True
|
2013-01-31 08:11:28 +01:00
|
|
|
else:
|
2013-09-26 23:27:16 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def cleanup(self, force, purge):
|
|
|
|
discarded = False
|
|
|
|
purged = False
|
2013-01-26 04:51:20 +01:00
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
if force:
|
|
|
|
discarded = self.discard()
|
|
|
|
if purge:
|
|
|
|
purged = self.purge()
|
|
|
|
if discarded or purged:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def pull(self):
|
|
|
|
return self._command(
|
2014-01-23 05:56:36 +01:00
|
|
|
['pull', '-R', self.dest, self.repo])
|
2013-01-26 04:51:20 +01:00
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
def update(self):
|
|
|
|
return self._command(['update', '-R', self.dest])
|
2013-01-26 04:51:20 +01:00
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
def clone(self):
|
|
|
|
return self._command(['clone', self.repo, self.dest, '-r', self.revision])
|
2013-01-26 04:51:20 +01:00
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
def switch_version(self):
|
|
|
|
return self._command(['update', '-r', self.revision, '-R', self.dest])
|
2013-01-31 08:11:28 +01:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
|
2013-01-26 04:51:20 +01:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
2013-07-23 19:16:20 +02:00
|
|
|
repo = dict(required=True, aliases=['name']),
|
2013-01-26 04:51:20 +01:00
|
|
|
dest = dict(required=True),
|
2013-07-23 19:16:20 +02:00
|
|
|
revision = dict(default="default", aliases=['version']),
|
2013-02-23 22:56:45 +01:00
|
|
|
force = dict(default='yes', type='bool'),
|
2013-09-26 23:27:16 +02:00
|
|
|
purge = dict(default='no', type='bool'),
|
|
|
|
executable = dict(default=None),
|
2013-01-26 04:51:20 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
repo = module.params['repo']
|
2013-08-03 01:03:20 +02:00
|
|
|
dest = os.path.expanduser(module.params['dest'])
|
2013-01-26 04:51:20 +01:00
|
|
|
revision = module.params['revision']
|
2013-02-23 19:59:52 +01:00
|
|
|
force = module.params['force']
|
|
|
|
purge = module.params['purge']
|
2013-10-10 17:15:04 +02:00
|
|
|
hg_path = module.params['executable'] or module.get_bin_path('hg', True)
|
2013-01-29 23:29:35 +01:00
|
|
|
hgrc = os.path.join(dest, '.hg/hgrc')
|
2013-10-10 17:15:04 +02:00
|
|
|
|
2013-01-31 08:11:28 +01:00
|
|
|
# initial states
|
|
|
|
before = ''
|
|
|
|
changed = False
|
|
|
|
cleaned = False
|
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
hg = Hg(module, dest, repo, revision, hg_path)
|
|
|
|
|
2013-01-29 23:29:35 +01:00
|
|
|
# If there is no hgrc file, then assume repo is absent
|
|
|
|
# and perform clone. Otherwise, perform pull and update.
|
|
|
|
if not os.path.exists(hgrc):
|
2013-09-26 23:27:16 +02:00
|
|
|
(rc, out, err) = hg.clone()
|
2013-01-29 23:29:35 +01:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
else:
|
|
|
|
# get the current state before doing pulling
|
2013-09-26 23:27:16 +02:00
|
|
|
before = hg.get_revision()
|
2013-01-29 23:29:35 +01:00
|
|
|
|
2013-01-31 08:11:28 +01:00
|
|
|
# can perform force and purge
|
2013-09-26 23:27:16 +02:00
|
|
|
cleaned = hg.cleanup(force, purge)
|
2013-01-29 23:29:35 +01:00
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
(rc, out, err) = hg.pull()
|
2013-01-29 23:29:35 +01:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
(rc, out, err) = hg.update()
|
2013-01-29 23:29:35 +01:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
|
2013-09-26 23:27:16 +02:00
|
|
|
hg.switch_version()
|
|
|
|
after = hg.get_revision()
|
2013-01-31 08:11:28 +01:00
|
|
|
if before != after or cleaned:
|
|
|
|
changed = True
|
|
|
|
module.exit_json(before=before, after=after, changed=changed, cleaned=cleaned)
|
2013-01-26 04:51:20 +01:00
|
|
|
|
2013-12-02 21:11:23 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2013-01-26 04:51:20 +01:00
|
|
|
main()
|