2015-07-18 17:37:29 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2015, Kevin Brebanov <https://github.com/kbrebanov>
|
|
|
|
# Based on pacman (Afterburn <http://github.com/afterburn>, Aaron Bull Schaefer <aaron@elasticdog.com>)
|
|
|
|
# and apt (Matthew Williams <matthew@flowroute.com>>) modules.
|
|
|
|
#
|
|
|
|
# This module 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.
|
|
|
|
#
|
|
|
|
# This software 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 this software. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: apk
|
|
|
|
short_description: Manages apk packages
|
|
|
|
description:
|
|
|
|
- Manages I(apk) packages for Alpine Linux.
|
2015-08-03 22:28:02 +02:00
|
|
|
version_added: "2.0"
|
2015-07-18 17:37:29 +02:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2015-07-19 20:36:16 +02:00
|
|
|
- A package name, like C(foo), or mutliple packages, like C(foo, bar).
|
2015-07-18 17:37:29 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
state:
|
|
|
|
description:
|
2015-07-19 20:36:16 +02:00
|
|
|
- Indicates the desired package(s) state.
|
|
|
|
- C(present) ensures the package(s) is/are present.
|
|
|
|
- C(absent) ensures the package(s) is/are absent.
|
|
|
|
- C(latest) ensures the package(s) is/are present and the latest version(s).
|
2015-07-18 17:37:29 +02:00
|
|
|
required: false
|
|
|
|
default: present
|
|
|
|
choices: [ "present", "absent", "latest" ]
|
|
|
|
update_cache:
|
|
|
|
description:
|
|
|
|
- Update repository indexes. Can be run with other steps or on it's own.
|
|
|
|
required: false
|
|
|
|
default: no
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
upgrade:
|
|
|
|
description:
|
|
|
|
- Upgrade all installed packages to their latest version.
|
|
|
|
required: false
|
|
|
|
default: no
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Update repositories and install "foo" package
|
|
|
|
- apk: name=foo update_cache=yes
|
|
|
|
|
2015-07-19 20:33:35 +02:00
|
|
|
# Update repositories and install "foo" and "bar" packages
|
|
|
|
- apk: name=foo,bar update_cache=yes
|
|
|
|
|
2015-07-18 17:37:29 +02:00
|
|
|
# Remove "foo" package
|
|
|
|
- apk: name=foo state=absent
|
|
|
|
|
2015-07-19 19:47:17 +02:00
|
|
|
# Remove "foo" and "bar" packages
|
|
|
|
- apk: name=foo,bar state=absent
|
|
|
|
|
2015-07-18 17:37:29 +02:00
|
|
|
# Install the package "foo"
|
|
|
|
- apk: name=foo state=present
|
|
|
|
|
2015-07-19 20:33:35 +02:00
|
|
|
# Install the packages "foo" and "bar"
|
|
|
|
- apk: name=foo,bar state=present
|
|
|
|
|
2015-07-18 17:37:29 +02:00
|
|
|
# Update repositories and update package "foo" to latest version
|
|
|
|
- apk: name=foo state=latest update_cache=yes
|
|
|
|
|
2015-07-19 20:33:35 +02:00
|
|
|
# Update repositories and update packages "foo" and "bar" to latest versions
|
|
|
|
- apk: name=foo,bar state=latest update_cache=yes
|
|
|
|
|
2015-07-18 17:37:29 +02:00
|
|
|
# Update all installed packages to the latest versions
|
|
|
|
- apk: upgrade=yes
|
|
|
|
|
|
|
|
# Update repositories as a separate step
|
|
|
|
- apk: update_cache=yes
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
def update_package_db(module):
|
2015-08-03 22:14:09 +02:00
|
|
|
cmd = "%s update" % (APK_PATH)
|
2015-07-18 17:37:29 +02:00
|
|
|
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
|
|
|
if rc == 0:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="could not update package db")
|
|
|
|
|
|
|
|
def query_package(module, name):
|
2015-08-03 22:14:09 +02:00
|
|
|
cmd = "%s -v info --installed %s" % (APK_PATH, name)
|
2015-07-18 17:37:29 +02:00
|
|
|
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
|
|
|
if rc == 0:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def query_latest(module, name):
|
2015-08-03 22:14:09 +02:00
|
|
|
cmd = "%s version %s" % (APK_PATH, name)
|
2015-07-18 17:37:29 +02:00
|
|
|
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
|
|
|
search_pattern = "(%s)-[\d\.\w]+-[\d\w]+\s+(.)\s+[\d\.\w]+-[\d\w]+\s+" % (name)
|
|
|
|
match = re.search(search_pattern, stdout)
|
|
|
|
if match and match.group(2) == "<":
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def upgrade_packages(module):
|
|
|
|
if module.check_mode:
|
2015-08-03 22:14:09 +02:00
|
|
|
cmd = "%s upgrade --simulate" % (APK_PATH)
|
2015-07-18 17:37:29 +02:00
|
|
|
else:
|
2015-08-03 22:14:09 +02:00
|
|
|
cmd = "%s upgrade" % (APK_PATH)
|
2015-07-18 17:37:29 +02:00
|
|
|
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="failed to upgrade packages")
|
|
|
|
if re.search('^OK', stdout):
|
|
|
|
module.exit_json(changed=False, msg="packages already upgraded")
|
|
|
|
module.exit_json(changed=True, msg="upgraded packages")
|
|
|
|
|
2015-07-19 20:33:35 +02:00
|
|
|
def install_packages(module, names, state):
|
2015-07-18 17:37:29 +02:00
|
|
|
upgrade = False
|
2015-07-19 20:33:35 +02:00
|
|
|
uninstalled = []
|
|
|
|
for name in names:
|
|
|
|
if not query_package(module, name):
|
|
|
|
uninstalled.append(name)
|
|
|
|
elif state == 'latest' and not query_latest(module, name):
|
|
|
|
upgrade = True
|
|
|
|
if not uninstalled and not upgrade:
|
|
|
|
module.exit_json(changed=False, msg="package(s) already installed")
|
|
|
|
names = " ".join(uninstalled)
|
2015-07-18 17:37:29 +02:00
|
|
|
if upgrade:
|
|
|
|
if module.check_mode:
|
2015-08-03 22:14:09 +02:00
|
|
|
cmd = "%s add --upgrade --simulate %s" % (APK_PATH, names)
|
2015-07-18 17:37:29 +02:00
|
|
|
else:
|
2015-08-03 22:14:09 +02:00
|
|
|
cmd = "%s add --upgrade %s" % (APK_PATH, names)
|
2015-07-18 17:37:29 +02:00
|
|
|
else:
|
|
|
|
if module.check_mode:
|
2015-08-03 22:14:09 +02:00
|
|
|
cmd = "%s add --simulate %s" % (APK_PATH, names)
|
2015-07-18 17:37:29 +02:00
|
|
|
else:
|
2015-08-03 22:14:09 +02:00
|
|
|
cmd = "%s add %s" % (APK_PATH, names)
|
2015-07-18 17:37:29 +02:00
|
|
|
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
|
|
|
if rc != 0:
|
2015-07-19 20:33:35 +02:00
|
|
|
module.fail_json(msg="failed to install %s" % (names))
|
|
|
|
module.exit_json(changed=True, msg="installed %s package(s)" % (names))
|
2015-07-18 17:37:29 +02:00
|
|
|
|
2015-07-19 19:47:17 +02:00
|
|
|
def remove_packages(module, names):
|
|
|
|
installed = []
|
|
|
|
for name in names:
|
|
|
|
if query_package(module, name):
|
|
|
|
installed.append(name)
|
2015-07-18 17:37:29 +02:00
|
|
|
if not installed:
|
2015-07-19 19:47:17 +02:00
|
|
|
module.exit_json(changed=False, msg="package(s) already removed")
|
|
|
|
names = " ".join(installed)
|
2015-07-18 17:37:29 +02:00
|
|
|
if module.check_mode:
|
2015-08-03 22:14:09 +02:00
|
|
|
cmd = "%s del --purge --simulate %s" % (APK_PATH, names)
|
2015-07-18 17:37:29 +02:00
|
|
|
else:
|
2015-08-03 22:14:09 +02:00
|
|
|
cmd = "%s del --purge %s" % (APK_PATH, names)
|
2015-07-18 17:37:29 +02:00
|
|
|
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
|
|
|
if rc != 0:
|
2015-07-19 19:47:17 +02:00
|
|
|
module.fail_json(msg="failed to remove %s package(s)" % (names))
|
|
|
|
module.exit_json(changed=True, msg="removed %s package(s)" % (names))
|
|
|
|
|
2015-07-18 17:37:29 +02:00
|
|
|
# ==========================================
|
|
|
|
# Main control flow.
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
state = dict(default='present', choices=['present', 'installed', 'absent', 'removed', 'latest']),
|
2015-07-29 22:22:32 +02:00
|
|
|
name = dict(type='list'),
|
2015-07-18 17:37:29 +02:00
|
|
|
update_cache = dict(default='no', choices=BOOLEANS, type='bool'),
|
|
|
|
upgrade = dict(default='no', choices=BOOLEANS, type='bool'),
|
|
|
|
),
|
|
|
|
required_one_of = [['name', 'update_cache', 'upgrade']],
|
|
|
|
supports_check_mode = True
|
|
|
|
)
|
|
|
|
|
2015-08-03 22:14:09 +02:00
|
|
|
global APK_PATH
|
|
|
|
APK_PATH = module.get_bin_path('apk', required=True)
|
2015-07-18 17:37:29 +02:00
|
|
|
|
|
|
|
p = module.params
|
|
|
|
|
|
|
|
# normalize the state parameter
|
|
|
|
if p['state'] in ['present', 'installed']:
|
|
|
|
p['state'] = 'present'
|
|
|
|
if p['state'] in ['absent', 'removed']:
|
|
|
|
p['state'] = 'absent'
|
|
|
|
|
|
|
|
if p['update_cache']:
|
|
|
|
update_package_db(module)
|
|
|
|
if not p['name']:
|
|
|
|
module.exit_json(changed=True, msg='updated repository indexes')
|
|
|
|
|
|
|
|
if p['upgrade']:
|
|
|
|
upgrade_packages(module)
|
|
|
|
|
|
|
|
if p['state'] in ['present', 'latest']:
|
2015-07-29 22:22:32 +02:00
|
|
|
install_packages(module, p['name'], p['state'])
|
2015-07-18 17:37:29 +02:00
|
|
|
elif p['state'] == 'absent':
|
2015-07-29 22:22:32 +02:00
|
|
|
remove_packages(module, p['name'])
|
2015-07-18 17:37:29 +02:00
|
|
|
|
|
|
|
# Import module snippets.
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|