2013-03-19 17:07:36 +01:00
|
|
|
#!/usr/bin/python
|
2013-01-24 05:09:07 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-03-06 01:52:20 +01:00
|
|
|
# Copyright (c) 2013 Shaun Zinck <shaun.zinck at gmail.com>
|
|
|
|
# Copyright (c) 2015 Lawrence Leonard Gilbert <larry@L2G.to>
|
2016-04-13 11:02:42 +02:00
|
|
|
# Copyright (c) 2016 Jasper Lievisse Adriaanse <j at jasper.la>
|
2015-03-06 01:52:20 +01:00
|
|
|
#
|
|
|
|
# Written by Shaun Zinck
|
2013-07-24 16:18:14 +02:00
|
|
|
# Based on pacman module written by Afterburn <http://github.com/afterburn>
|
2013-01-24 05:09:07 +01:00
|
|
|
# that was based on apt module written by Matthew Williams <matthew@flowroute.com>
|
|
|
|
#
|
|
|
|
# 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: pkgin
|
2015-03-06 01:52:20 +01:00
|
|
|
short_description: Package manager for SmartOS, NetBSD, et al.
|
2013-01-24 05:09:07 +01:00
|
|
|
description:
|
2015-03-06 01:52:20 +01:00
|
|
|
- "The standard package manager for SmartOS, but also usable on NetBSD
|
|
|
|
or any OS that uses C(pkgsrc). (Home: U(http://pkgin.net/))"
|
2013-01-24 05:09:07 +01:00
|
|
|
version_added: "1.0"
|
2015-05-15 21:12:26 +02:00
|
|
|
author:
|
2015-06-16 20:32:39 +02:00
|
|
|
- "Larry Gilbert (L2G)"
|
|
|
|
- "Shaun Zinck (@szinck)"
|
2016-04-13 11:02:42 +02:00
|
|
|
- "Jasper Lievisse Adriaanse (@jasperla)"
|
2015-03-06 01:52:20 +01:00
|
|
|
notes:
|
|
|
|
- "Known bug with pkgin < 0.8.0: if a package is removed and another
|
|
|
|
package depends on it, the other package will be silently removed as
|
|
|
|
well. New to Ansible 1.9: check-mode support."
|
2013-01-24 05:09:07 +01:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2015-03-06 01:52:20 +01:00
|
|
|
- Name of package to install/remove;
|
|
|
|
- multiple names may be given, separated by commas
|
2016-03-10 09:37:21 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
2013-01-24 05:09:07 +01:00
|
|
|
state:
|
|
|
|
description:
|
2015-03-06 01:52:20 +01:00
|
|
|
- Intended state of the package
|
2013-01-26 18:44:43 +01:00
|
|
|
choices: [ 'present', 'absent' ]
|
2013-01-24 05:09:07 +01:00
|
|
|
required: false
|
2013-01-26 18:44:43 +01:00
|
|
|
default: present
|
2016-03-10 09:37:21 +01:00
|
|
|
update_cache:
|
|
|
|
description:
|
|
|
|
- Update repository database. Can be run with other steps or on it's own.
|
|
|
|
required: false
|
|
|
|
default: no
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
version_added: "2.1"
|
2016-04-13 11:02:42 +02:00
|
|
|
upgrade:
|
|
|
|
description:
|
|
|
|
- Upgrade main packages to their newer versions
|
|
|
|
required: false
|
|
|
|
default: no
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
version_added: "2.1"
|
|
|
|
full_upgrade:
|
|
|
|
description:
|
|
|
|
- Upgrade all packages to their newer versions
|
|
|
|
required: false
|
|
|
|
default: no
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
version_added: "2.1"
|
|
|
|
clean:
|
|
|
|
description:
|
|
|
|
- Clean packages cache
|
|
|
|
required: false
|
|
|
|
default: no
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
version_added: "2.1"
|
|
|
|
force:
|
|
|
|
description:
|
|
|
|
- Force package reinstall
|
|
|
|
required: false
|
|
|
|
default: no
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
version_added: "2.1"
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2015-03-06 01:52:20 +01:00
|
|
|
# install package foo
|
2013-06-14 11:53:43 +02:00
|
|
|
- pkgin: name=foo state=present
|
|
|
|
|
2016-03-10 09:37:21 +01:00
|
|
|
# Update database and install "foo" package
|
|
|
|
- pkgin: name=foo update_cache=yes
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
# remove package foo
|
|
|
|
- pkgin: name=foo state=absent
|
|
|
|
|
2013-07-24 16:18:14 +02:00
|
|
|
# remove packages foo and bar
|
2013-06-14 11:53:43 +02:00
|
|
|
- pkgin: name=foo,bar state=absent
|
2016-03-10 09:37:21 +01:00
|
|
|
|
|
|
|
# Update repositories as a separate step
|
|
|
|
- pkgin: update_cache=yes
|
2016-04-13 11:02:42 +02:00
|
|
|
|
|
|
|
# Upgrade main packages (equivalent to C(pkgin upgrade))
|
|
|
|
- pkgin: upgrade=yes
|
|
|
|
|
|
|
|
# Upgrade all packages (equivalent to C(pkgin full-upgrade))
|
|
|
|
- pkgin: full_upgrade=yes
|
|
|
|
|
|
|
|
# Force-upgrade all packages (equivalent to C(pkgin -F full-upgrade))
|
|
|
|
- pkgin: full_upgrade=yes force=yes
|
|
|
|
|
|
|
|
# clean packages cache (equivalent to C(pkgin clean))
|
|
|
|
- pkgin: clean=yes
|
2013-01-24 05:09:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
2015-08-18 22:32:17 +02:00
|
|
|
import re
|
2013-07-24 16:18:14 +02:00
|
|
|
|
2016-04-13 11:02:42 +02:00
|
|
|
def query_package(module, name):
|
2015-03-06 01:52:20 +01:00
|
|
|
"""Search for the package by name.
|
|
|
|
|
|
|
|
Possible return values:
|
|
|
|
* "present" - installed, no upgrade needed
|
|
|
|
* "outdated" - installed, but can be upgraded
|
|
|
|
* False - not installed or not found
|
|
|
|
"""
|
|
|
|
|
2015-05-06 20:41:12 +02:00
|
|
|
# test whether '-p' (parsable) flag is supported.
|
2016-04-13 11:02:42 +02:00
|
|
|
rc, out, err = module.run_command("%s -p -v" % PKGIN_PATH)
|
2015-05-06 20:41:12 +02:00
|
|
|
|
|
|
|
if rc == 0:
|
|
|
|
pflag = '-p'
|
|
|
|
splitchar = ';'
|
|
|
|
else:
|
|
|
|
pflag = ''
|
|
|
|
splitchar = ' '
|
|
|
|
|
2015-03-06 01:52:20 +01:00
|
|
|
# Use "pkgin search" to find the package. The regular expression will
|
|
|
|
# only match on the complete name.
|
2016-04-13 11:02:42 +02:00
|
|
|
rc, out, err = module.run_command("%s %s search \"^%s$\"" % (PKGIN_PATH, pflag, name))
|
2015-03-06 01:52:20 +01:00
|
|
|
|
|
|
|
# rc will not be 0 unless the search was a success
|
|
|
|
if rc == 0:
|
|
|
|
|
2015-08-18 22:32:17 +02:00
|
|
|
# Search results may contain more than one line (e.g., 'emacs'), so iterate
|
|
|
|
# through each line to see if we have a match.
|
|
|
|
packages = out.split('\n')
|
|
|
|
|
|
|
|
for package in packages:
|
|
|
|
|
|
|
|
# Break up line at spaces. The first part will be the package with its
|
|
|
|
# version (e.g. 'gcc47-libs-4.7.2nb4'), and the second will be the state
|
|
|
|
# of the package:
|
|
|
|
# '' - not installed
|
|
|
|
# '<' - installed but out of date
|
|
|
|
# '=' - installed and up to date
|
|
|
|
# '>' - installed but newer than the repository version
|
|
|
|
pkgname_with_version, raw_state = package.split(splitchar)[0:2]
|
|
|
|
|
|
|
|
# Search for package, stripping version
|
|
|
|
# (results in sth like 'gcc47-libs' or 'emacs24-nox11')
|
|
|
|
pkg_search_obj = re.search(r'^([a-zA-Z]+[0-9]*[\-]*\w*)-[0-9]', pkgname_with_version, re.M)
|
|
|
|
|
|
|
|
# Do not proceed unless we have a match
|
|
|
|
if not pkg_search_obj:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Grab matched string
|
|
|
|
pkgname_without_version = pkg_search_obj.group(1)
|
|
|
|
|
|
|
|
if name != pkgname_without_version:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# The package was found; now return its state
|
|
|
|
if raw_state == '<':
|
|
|
|
return 'outdated'
|
|
|
|
elif raw_state == '=' or raw_state == '>':
|
|
|
|
return 'present'
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
# no fall-through
|
|
|
|
|
|
|
|
# No packages were matched, so return False
|
|
|
|
return False
|
2013-07-24 16:18:14 +02:00
|
|
|
|
|
|
|
|
2015-03-06 01:52:20 +01:00
|
|
|
def format_action_message(module, action, count):
|
|
|
|
vars = { "actioned": action,
|
|
|
|
"count": count }
|
2013-07-24 16:18:14 +02:00
|
|
|
|
2015-03-06 01:52:20 +01:00
|
|
|
if module.check_mode:
|
|
|
|
message = "would have %(actioned)s %(count)d package" % vars
|
|
|
|
else:
|
|
|
|
message = "%(actioned)s %(count)d package" % vars
|
2013-07-24 16:18:14 +02:00
|
|
|
|
2015-03-06 01:52:20 +01:00
|
|
|
if count == 1:
|
|
|
|
return message
|
|
|
|
else:
|
|
|
|
return message + "s"
|
2013-01-24 05:09:07 +01:00
|
|
|
|
2015-03-06 01:52:20 +01:00
|
|
|
|
2016-04-13 11:02:42 +02:00
|
|
|
def format_pkgin_command(module, command, package=None):
|
2016-03-10 09:37:21 +01:00
|
|
|
# Not all commands take a package argument, so cover this up by passing
|
|
|
|
# an empty string. Some commands (e.g. 'update') will ignore extra
|
|
|
|
# arguments, however this behaviour cannot be relied on for others.
|
|
|
|
if package is None:
|
|
|
|
package = ""
|
|
|
|
|
2016-04-13 11:02:42 +02:00
|
|
|
if module.params["force"]:
|
|
|
|
force = "-F"
|
|
|
|
else:
|
|
|
|
force = ""
|
|
|
|
|
|
|
|
vars = { "pkgin": PKGIN_PATH,
|
2015-03-06 01:52:20 +01:00
|
|
|
"command": command,
|
2016-04-13 11:02:42 +02:00
|
|
|
"package": package,
|
|
|
|
"force": force}
|
2015-03-06 01:52:20 +01:00
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
return "%(pkgin)s -n %(command)s %(package)s" % vars
|
|
|
|
else:
|
2016-04-13 11:02:42 +02:00
|
|
|
return "%(pkgin)s -y %(force)s %(command)s %(package)s" % vars
|
2013-01-24 05:09:07 +01:00
|
|
|
|
|
|
|
|
2016-04-13 11:02:42 +02:00
|
|
|
def remove_packages(module, packages):
|
2013-07-24 16:18:14 +02:00
|
|
|
|
2013-01-24 05:09:07 +01:00
|
|
|
remove_c = 0
|
2015-03-06 01:52:20 +01:00
|
|
|
|
2013-01-24 05:09:07 +01:00
|
|
|
# Using a for loop incase of error, we can report the package that failed
|
|
|
|
for package in packages:
|
|
|
|
# Query the package first, to see if we even need to remove
|
2016-04-13 11:02:42 +02:00
|
|
|
if not query_package(module, package):
|
2013-01-24 05:09:07 +01:00
|
|
|
continue
|
|
|
|
|
2015-03-06 01:52:20 +01:00
|
|
|
rc, out, err = module.run_command(
|
2016-04-13 11:02:42 +02:00
|
|
|
format_pkgin_command(module, "remove", package))
|
2013-01-24 05:09:07 +01:00
|
|
|
|
2016-04-13 11:02:42 +02:00
|
|
|
if not module.check_mode and query_package(module, package):
|
2013-01-25 23:58:29 +01:00
|
|
|
module.fail_json(msg="failed to remove %s: %s" % (package, out))
|
2013-07-24 16:18:14 +02:00
|
|
|
|
2013-01-24 05:09:07 +01:00
|
|
|
remove_c += 1
|
|
|
|
|
|
|
|
if remove_c > 0:
|
2015-03-06 01:52:20 +01:00
|
|
|
module.exit_json(changed=True, msg=format_action_message(module, "removed", remove_c))
|
2013-01-24 05:09:07 +01:00
|
|
|
|
|
|
|
module.exit_json(changed=False, msg="package(s) already absent")
|
|
|
|
|
|
|
|
|
2016-04-13 11:02:42 +02:00
|
|
|
def install_packages(module, packages):
|
2013-01-24 05:09:07 +01:00
|
|
|
|
|
|
|
install_c = 0
|
|
|
|
|
|
|
|
for package in packages:
|
2016-04-13 11:02:42 +02:00
|
|
|
if query_package(module, package):
|
2013-01-24 05:09:07 +01:00
|
|
|
continue
|
|
|
|
|
2015-03-06 01:52:20 +01:00
|
|
|
rc, out, err = module.run_command(
|
2016-04-13 11:02:42 +02:00
|
|
|
format_pkgin_command(module, "install", package))
|
2013-01-24 05:09:07 +01:00
|
|
|
|
2016-04-13 11:02:42 +02:00
|
|
|
if not module.check_mode and not query_package(module, package):
|
2013-01-25 23:58:29 +01:00
|
|
|
module.fail_json(msg="failed to install %s: %s" % (package, out))
|
2013-01-24 05:09:07 +01:00
|
|
|
|
|
|
|
install_c += 1
|
2013-07-24 16:18:14 +02:00
|
|
|
|
2013-01-24 05:09:07 +01:00
|
|
|
if install_c > 0:
|
2015-03-06 01:52:20 +01:00
|
|
|
module.exit_json(changed=True, msg=format_action_message(module, "installed", install_c))
|
2013-01-24 05:09:07 +01:00
|
|
|
|
2013-01-26 18:44:43 +01:00
|
|
|
module.exit_json(changed=False, msg="package(s) already present")
|
2013-01-24 05:09:07 +01:00
|
|
|
|
2016-04-13 11:02:42 +02:00
|
|
|
def update_package_db(module):
|
2016-03-10 09:37:21 +01:00
|
|
|
rc, out, err = module.run_command(
|
2016-04-13 11:02:42 +02:00
|
|
|
format_pkgin_command(module, "update"))
|
2016-03-10 09:37:21 +01:00
|
|
|
|
|
|
|
if rc == 0:
|
2016-04-13 11:02:42 +02:00
|
|
|
if re.search('database for.*is up-to-date\n$', out):
|
|
|
|
return False, "datebase is up-to-date"
|
|
|
|
else:
|
|
|
|
return True, "updated repository database"
|
2016-03-10 09:37:21 +01:00
|
|
|
else:
|
|
|
|
module.fail_json(msg="could not update package db")
|
2013-01-24 05:09:07 +01:00
|
|
|
|
2016-04-13 11:02:42 +02:00
|
|
|
def do_upgrade_packages(module, full=False):
|
|
|
|
if full:
|
|
|
|
cmd = "full-upgrade"
|
|
|
|
else:
|
|
|
|
cmd = "upgrade"
|
|
|
|
|
|
|
|
rc, out, err = module.run_command(
|
|
|
|
format_pkgin_command(module, cmd))
|
|
|
|
|
|
|
|
if rc == 0:
|
|
|
|
if re.search('^nothing to do.\n$', out):
|
|
|
|
module.exit_json(changed=False, msg="nothing left to upgrade")
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="could not %s packages" % cmd)
|
|
|
|
|
|
|
|
def upgrade_packages(module):
|
|
|
|
do_upgrade_packages(module)
|
|
|
|
|
|
|
|
def full_upgrade_packages(module):
|
|
|
|
do_upgrade_packages(module, True)
|
|
|
|
|
|
|
|
def clean_cache(module):
|
|
|
|
rc, out, err = module.run_command(
|
|
|
|
format_pkgin_command(module, "clean"))
|
|
|
|
|
|
|
|
if rc == 0:
|
|
|
|
# There's no indication if 'clean' actually removed anything,
|
|
|
|
# so assume it did.
|
|
|
|
module.exit_json(changed=True, msg="cleaned caches")
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="could not clean package cache")
|
2013-01-24 05:09:07 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
2013-01-26 18:44:43 +01:00
|
|
|
state = dict(default="present", choices=["present","absent"]),
|
2016-03-10 09:37:21 +01:00
|
|
|
name = dict(aliases=["pkg"], type='list'),
|
2016-04-13 11:02:42 +02:00
|
|
|
update_cache = dict(default='no', type='bool'),
|
|
|
|
upgrade = dict(default='no', type='bool'),
|
|
|
|
full_upgrade = dict(default='no', type='bool'),
|
|
|
|
clean = dict(default='no', type='bool'),
|
|
|
|
force = dict(default='no', type='bool')),
|
|
|
|
required_one_of = [['name', 'update_cache', 'upgrade', 'full_upgrade', 'clean']],
|
2015-03-06 01:52:20 +01:00
|
|
|
supports_check_mode = True)
|
2013-01-24 05:09:07 +01:00
|
|
|
|
2016-04-13 11:02:42 +02:00
|
|
|
global PKGIN_PATH
|
|
|
|
PKGIN_PATH = module.get_bin_path('pkgin', True, ['/opt/local/bin'])
|
2013-01-24 05:09:07 +01:00
|
|
|
|
2016-04-13 11:02:42 +02:00
|
|
|
module.run_command_environ_update = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C', LC_CTYPE='C')
|
2013-01-24 05:09:07 +01:00
|
|
|
|
2016-04-13 11:02:42 +02:00
|
|
|
p = module.params
|
2013-01-24 05:09:07 +01:00
|
|
|
|
2016-03-10 09:37:21 +01:00
|
|
|
if p["update_cache"]:
|
2016-04-13 11:02:42 +02:00
|
|
|
c, msg = update_package_db(module)
|
|
|
|
if not (p['name'] or p["upgrade"] or p["full_upgrade"]):
|
|
|
|
module.exit_json(changed=c, msg=msg)
|
|
|
|
|
|
|
|
if p["upgrade"]:
|
|
|
|
upgrade_packages(module)
|
2016-03-10 09:37:21 +01:00
|
|
|
if not p['name']:
|
2016-04-13 11:02:42 +02:00
|
|
|
module.exit_json(changed=True, msg='upgraded packages')
|
|
|
|
|
|
|
|
if p["full_upgrade"]:
|
|
|
|
full_upgrade_packages(module)
|
|
|
|
if not p['name']:
|
|
|
|
module.exit_json(changed=True, msg='upgraded all packages')
|
|
|
|
|
|
|
|
if p["clean"]:
|
|
|
|
clean_cache(module)
|
|
|
|
if not p['name']:
|
|
|
|
module.exit_json(changed=True, msg='cleaned caches')
|
|
|
|
|
|
|
|
pkgs = p["name"]
|
2016-03-10 09:37:21 +01:00
|
|
|
|
2013-01-26 18:44:43 +01:00
|
|
|
if p["state"] == "present":
|
2016-04-13 11:02:42 +02:00
|
|
|
install_packages(module, pkgs)
|
2013-01-24 05:09:07 +01:00
|
|
|
|
|
|
|
elif p["state"] == "absent":
|
2016-04-13 11:02:42 +02:00
|
|
|
remove_packages(module, pkgs)
|
2013-01-24 05:09:07 +01:00
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2013-07-24 16:18:14 +02:00
|
|
|
|
2016-03-10 09:37:21 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|