2013-02-27 12:28:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Johan Wiren <johan.wiren.se@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 = '''
|
|
|
|
---
|
|
|
|
module: gem
|
|
|
|
short_description: Manage Ruby gems
|
2014-01-30 01:09:28 +01:00
|
|
|
description:
|
2013-02-27 12:28:07 +01:00
|
|
|
- Manage installation and uninstallation of Ruby gems.
|
|
|
|
version_added: "1.1"
|
|
|
|
options:
|
|
|
|
name:
|
2013-03-30 20:53:28 +01:00
|
|
|
description:
|
|
|
|
- The name of the gem to be managed.
|
2013-02-27 12:28:07 +01:00
|
|
|
required: true
|
|
|
|
state:
|
2014-01-30 01:09:28 +01:00
|
|
|
description:
|
2013-03-05 12:28:04 +01:00
|
|
|
- The desired state of the gem. C(latest) ensures that the latest version is installed.
|
2014-02-12 18:31:11 +01:00
|
|
|
required: false
|
2013-02-27 12:28:07 +01:00
|
|
|
choices: [present, absent, latest]
|
2014-02-12 18:31:11 +01:00
|
|
|
default: present
|
2013-03-01 23:55:13 +01:00
|
|
|
gem_source:
|
2013-03-30 20:53:28 +01:00
|
|
|
description:
|
|
|
|
- The path to a local gem used as installation source.
|
2013-02-27 12:28:07 +01:00
|
|
|
required: false
|
2013-03-01 23:55:13 +01:00
|
|
|
include_dependencies:
|
2013-03-30 20:53:28 +01:00
|
|
|
description:
|
2014-08-03 14:52:24 +02:00
|
|
|
- Whether to include dependencies or not.
|
2013-02-27 12:28:07 +01:00
|
|
|
required: false
|
2013-03-12 13:18:12 +01:00
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "yes"
|
2013-02-27 12:28:07 +01:00
|
|
|
repository:
|
2013-03-30 20:53:28 +01:00
|
|
|
description:
|
|
|
|
- The repository from which the gem will be installed
|
2013-02-27 12:28:07 +01:00
|
|
|
required: false
|
|
|
|
aliases: [source]
|
2013-07-01 15:20:46 +02:00
|
|
|
user_install:
|
|
|
|
description:
|
|
|
|
- Install gem in user's local gems cache or for all users
|
|
|
|
required: false
|
|
|
|
default: "yes"
|
2013-07-21 17:12:04 +02:00
|
|
|
version_added: "1.3"
|
2013-10-15 09:08:33 +02:00
|
|
|
executable:
|
|
|
|
description:
|
|
|
|
- Override the path to the gem executable
|
|
|
|
required: false
|
|
|
|
version_added: "1.4"
|
2013-02-27 12:28:07 +01:00
|
|
|
version:
|
2013-03-30 20:53:28 +01:00
|
|
|
description:
|
|
|
|
- Version of the gem to be installed/removed.
|
2013-02-27 12:28:07 +01:00
|
|
|
required: false
|
2014-03-28 18:08:46 +01:00
|
|
|
pre_release:
|
2013-12-18 20:34:08 +01:00
|
|
|
description:
|
2014-03-28 18:08:46 +01:00
|
|
|
- Allow installation of pre-release versions of the gem.
|
2013-12-18 20:34:08 +01:00
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
version_added: "1.6"
|
2015-03-18 19:10:54 +01:00
|
|
|
build_flags:
|
|
|
|
description:
|
|
|
|
- Allow adding build flags for gem compilation
|
|
|
|
required: false
|
|
|
|
version_added: "2.0"
|
2015-06-15 21:53:30 +02:00
|
|
|
author:
|
|
|
|
- "Ansible Core Team"
|
|
|
|
- "Johan Wiren"
|
2013-02-27 12:28:07 +01:00
|
|
|
'''
|
2013-03-01 23:55:13 +01:00
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Installs version 1.0 of vagrant.
|
2013-06-17 08:53:46 +02:00
|
|
|
- gem: name=vagrant version=1.0 state=present
|
2013-03-01 23:55:13 +01:00
|
|
|
|
|
|
|
# Installs latest available version of rake.
|
2013-06-17 08:53:46 +02:00
|
|
|
- gem: name=rake state=latest
|
2013-03-01 23:55:13 +01:00
|
|
|
|
|
|
|
# Installs rake version 1.0 from a local gem on disk.
|
2013-06-17 08:53:46 +02:00
|
|
|
- gem: name=rake gem_source=/path/to/gems/rake-1.0.gem state=present
|
2013-03-01 23:55:13 +01:00
|
|
|
'''
|
|
|
|
|
2013-02-27 12:28:07 +01:00
|
|
|
import re
|
|
|
|
|
2013-10-15 09:08:33 +02:00
|
|
|
def get_rubygems_path(module):
|
|
|
|
if module.params['executable']:
|
2014-06-17 11:58:10 +02:00
|
|
|
return module.params['executable'].split(' ')
|
2013-10-15 09:08:33 +02:00
|
|
|
else:
|
2014-06-17 11:58:10 +02:00
|
|
|
return [ module.get_bin_path('gem', True) ]
|
2013-10-15 09:08:33 +02:00
|
|
|
|
2013-03-16 11:01:02 +01:00
|
|
|
def get_rubygems_version(module):
|
2014-06-17 11:58:10 +02:00
|
|
|
cmd = get_rubygems_path(module) + [ '--version' ]
|
2013-03-16 11:01:02 +01:00
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True)
|
|
|
|
|
|
|
|
match = re.match(r'^(\d+)\.(\d+)\.(\d+)', out)
|
|
|
|
if not match:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return tuple(int(x) for x in match.groups())
|
|
|
|
|
2013-03-01 23:55:13 +01:00
|
|
|
def get_installed_versions(module, remote=False):
|
|
|
|
|
2014-06-17 11:58:10 +02:00
|
|
|
cmd = get_rubygems_path(module)
|
2013-02-27 12:28:07 +01:00
|
|
|
cmd.append('query')
|
|
|
|
if remote:
|
|
|
|
cmd.append('--remote')
|
|
|
|
if module.params['repository']:
|
|
|
|
cmd.extend([ '--source', module.params['repository'] ])
|
|
|
|
cmd.append('-n')
|
|
|
|
cmd.append('^%s$' % module.params['name'])
|
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True)
|
2013-03-01 23:55:13 +01:00
|
|
|
installed_versions = []
|
2013-02-27 12:28:07 +01:00
|
|
|
for line in out.splitlines():
|
|
|
|
match = re.match(r"\S+\s+\((.+)\)", line)
|
|
|
|
if match:
|
|
|
|
versions = match.group(1)
|
|
|
|
for version in versions.split(', '):
|
2013-10-02 23:55:30 +02:00
|
|
|
installed_versions.append(version.split()[0])
|
2013-03-01 23:55:13 +01:00
|
|
|
return installed_versions
|
2013-02-27 12:28:07 +01:00
|
|
|
|
|
|
|
def exists(module):
|
2013-03-01 23:55:13 +01:00
|
|
|
|
2013-02-27 12:28:07 +01:00
|
|
|
if module.params['state'] == 'latest':
|
2013-03-01 23:55:13 +01:00
|
|
|
remoteversions = get_installed_versions(module, remote=True)
|
2013-02-27 12:28:07 +01:00
|
|
|
if remoteversions:
|
|
|
|
module.params['version'] = remoteversions[0]
|
2013-03-01 23:55:13 +01:00
|
|
|
installed_versions = get_installed_versions(module)
|
2013-02-27 12:28:07 +01:00
|
|
|
if module.params['version']:
|
2013-03-01 23:55:13 +01:00
|
|
|
if module.params['version'] in installed_versions:
|
2013-02-27 12:28:07 +01:00
|
|
|
return True
|
|
|
|
else:
|
2013-03-01 23:55:13 +01:00
|
|
|
if installed_versions:
|
2013-02-27 12:28:07 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def uninstall(module):
|
2013-03-01 23:55:13 +01:00
|
|
|
|
2013-02-27 12:28:07 +01:00
|
|
|
if module.check_mode:
|
|
|
|
return
|
2014-06-17 11:58:10 +02:00
|
|
|
cmd = get_rubygems_path(module)
|
2013-02-27 12:28:07 +01:00
|
|
|
cmd.append('uninstall')
|
|
|
|
if module.params['version']:
|
|
|
|
cmd.extend([ '--version', module.params['version'] ])
|
|
|
|
else:
|
|
|
|
cmd.append('--all')
|
2014-01-30 01:09:28 +01:00
|
|
|
cmd.append('--executable')
|
2013-02-27 12:28:07 +01:00
|
|
|
cmd.append(module.params['name'])
|
|
|
|
module.run_command(cmd, check_rc=True)
|
|
|
|
|
|
|
|
def install(module):
|
2013-03-01 23:55:13 +01:00
|
|
|
|
2013-02-27 12:28:07 +01:00
|
|
|
if module.check_mode:
|
|
|
|
return
|
2013-03-16 11:01:02 +01:00
|
|
|
|
|
|
|
ver = get_rubygems_version(module)
|
2013-03-26 11:45:47 +01:00
|
|
|
if ver:
|
|
|
|
major = ver[0]
|
|
|
|
else:
|
|
|
|
major = None
|
2013-03-16 11:01:02 +01:00
|
|
|
|
2014-06-17 11:58:10 +02:00
|
|
|
cmd = get_rubygems_path(module)
|
2013-02-27 12:28:07 +01:00
|
|
|
cmd.append('install')
|
|
|
|
if module.params['version']:
|
|
|
|
cmd.extend([ '--version', module.params['version'] ])
|
|
|
|
if module.params['repository']:
|
|
|
|
cmd.extend([ '--source', module.params['repository'] ])
|
2013-03-16 11:01:02 +01:00
|
|
|
if not module.params['include_dependencies']:
|
|
|
|
cmd.append('--ignore-dependencies')
|
|
|
|
else:
|
|
|
|
if major and major < 2:
|
|
|
|
cmd.append('--include-dependencies')
|
2013-08-24 22:48:58 +02:00
|
|
|
if module.params['user_install']:
|
|
|
|
cmd.append('--user-install')
|
|
|
|
else:
|
2013-10-15 09:08:33 +02:00
|
|
|
cmd.append('--no-user-install')
|
2014-03-28 18:08:46 +01:00
|
|
|
if module.params['pre_release']:
|
2013-12-18 20:34:08 +01:00
|
|
|
cmd.append('--pre')
|
2013-02-27 12:28:07 +01:00
|
|
|
cmd.append('--no-rdoc')
|
|
|
|
cmd.append('--no-ri')
|
2013-03-01 23:55:13 +01:00
|
|
|
cmd.append(module.params['gem_source'])
|
2015-03-18 19:10:54 +01:00
|
|
|
if module.params['build_flags']:
|
|
|
|
cmd.extend([ '--', module.params['build_flags'] ])
|
2013-02-27 12:28:07 +01:00
|
|
|
module.run_command(cmd, check_rc=True)
|
|
|
|
|
|
|
|
def main():
|
2013-03-01 23:55:13 +01:00
|
|
|
|
2013-02-27 12:28:07 +01:00
|
|
|
module = AnsibleModule(
|
2013-03-01 23:55:13 +01:00
|
|
|
argument_spec = dict(
|
2013-10-15 09:08:33 +02:00
|
|
|
executable = dict(required=False, type='str'),
|
2013-03-01 23:55:13 +01:00
|
|
|
gem_source = dict(required=False, type='str'),
|
|
|
|
include_dependencies = dict(required=False, default=True, type='bool'),
|
|
|
|
name = dict(required=True, type='str'),
|
|
|
|
repository = dict(required=False, aliases=['source'], type='str'),
|
2014-02-12 18:31:11 +01:00
|
|
|
state = dict(required=False, default='present', choices=['present','absent','latest'], type='str'),
|
2013-07-01 15:20:46 +02:00
|
|
|
user_install = dict(required=False, default=True, type='bool'),
|
2015-03-18 19:10:54 +01:00
|
|
|
pre_release = dict(required=False, default=False, type='bool'),
|
2013-03-01 23:55:13 +01:00
|
|
|
version = dict(required=False, type='str'),
|
2015-03-18 19:10:54 +01:00
|
|
|
build_flags = dict(required=False, type='str'),
|
2013-03-01 23:55:13 +01:00
|
|
|
),
|
2013-02-27 12:28:07 +01:00
|
|
|
supports_check_mode = True,
|
2013-03-01 23:55:13 +01:00
|
|
|
mutually_exclusive = [ ['gem_source','repository'], ['gem_source','version'] ],
|
|
|
|
)
|
2013-02-27 12:28:07 +01:00
|
|
|
|
|
|
|
if module.params['version'] and module.params['state'] == 'latest':
|
|
|
|
module.fail_json(msg="Cannot specify version when state=latest")
|
2013-03-01 23:55:13 +01:00
|
|
|
if module.params['gem_source'] and module.params['state'] == 'latest':
|
2013-02-27 12:28:07 +01:00
|
|
|
module.fail_json(msg="Cannot maintain state=latest when installing from local source")
|
|
|
|
|
2013-03-07 01:58:57 +01:00
|
|
|
if not module.params['gem_source']:
|
2013-03-01 23:55:13 +01:00
|
|
|
module.params['gem_source'] = module.params['name']
|
2013-02-27 12:28:07 +01:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
2013-03-01 23:55:13 +01:00
|
|
|
if module.params['state'] in [ 'present', 'latest']:
|
2013-02-27 12:28:07 +01:00
|
|
|
if not exists(module):
|
|
|
|
install(module)
|
|
|
|
changed = True
|
|
|
|
elif module.params['state'] == 'absent':
|
|
|
|
if exists(module):
|
|
|
|
uninstall(module)
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
result['name'] = module.params['name']
|
|
|
|
result['state'] = module.params['state']
|
|
|
|
if module.params['version']:
|
|
|
|
result['version'] = module.params['version']
|
|
|
|
result['changed'] = changed
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2013-12-02 21:11:23 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2013-02-27 12:28:07 +01:00
|
|
|
main()
|