2013-05-10 14:15:01 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, bleader
|
|
|
|
# Written by bleader <bleader@ratonland.org>
|
|
|
|
# Based on pkgin module written by Shaun Zinck <shaun.zinck at gmail.com>
|
|
|
|
# that was based on pacman module written by Afterburn <http://github.com/afterburn>
|
|
|
|
# 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: pkgng
|
|
|
|
short_description: Package manager for FreeBSD >= 9.0
|
|
|
|
description:
|
|
|
|
- Manage binary packages for FreeBSD using 'pkgng' which
|
|
|
|
is available in versions after 9.0.
|
|
|
|
version_added: "1.2"
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- name of package to install/remove
|
|
|
|
required: true
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- state of the package
|
|
|
|
choices: [ 'present', 'absent' ]
|
|
|
|
required: false
|
|
|
|
default: present
|
|
|
|
cached:
|
|
|
|
description:
|
|
|
|
- use local package base or try to fetch an updated one
|
|
|
|
choices: [ 'yes', 'no' ]
|
|
|
|
required: false
|
|
|
|
default: no
|
2014-03-11 20:24:32 +01:00
|
|
|
annotation:
|
|
|
|
description:
|
|
|
|
- a comma-separated list of keyvalue-pairs of the form
|
|
|
|
<+/-/:><key>[=<value>]. A '+' denotes adding an annotation, a
|
|
|
|
'-' denotes removing an annotation, and ':' denotes modifying an
|
|
|
|
annotation.
|
|
|
|
If setting or modifying annotations, a value must be provided.
|
|
|
|
required: false
|
2014-04-08 16:28:01 +02:00
|
|
|
version_added: "1.6"
|
2013-05-10 14:15:01 +02:00
|
|
|
pkgsite:
|
|
|
|
description:
|
2014-03-11 17:55:40 +01:00
|
|
|
- for pkgng versions before 1.1.4, specify packagesite to use
|
|
|
|
for downloading packages, if not specified, use settings from
|
|
|
|
/usr/local/etc/pkg.conf
|
|
|
|
for newer pkgng versions, specify a the name of a repository
|
|
|
|
configured in /usr/local/etc/pkg/repos
|
2013-05-10 14:15:01 +02:00
|
|
|
required: false
|
2015-04-21 14:44:58 +02:00
|
|
|
rootdir:
|
|
|
|
description:
|
|
|
|
- for pkgng versions 1.5 and later, pkg will install all packages
|
|
|
|
within the specified root directory
|
2016-04-11 08:25:30 +02:00
|
|
|
- can not be used together with 'chroot' option
|
|
|
|
required: false
|
|
|
|
chroot:
|
|
|
|
version_added: "2.1"
|
|
|
|
description:
|
|
|
|
- pkg will chroot in the specified environment
|
|
|
|
- can not be used together with 'rootdir' option
|
2015-04-21 14:44:58 +02:00
|
|
|
required: false
|
2016-05-27 15:49:54 +02:00
|
|
|
autoremove:
|
|
|
|
version_added: "2.2"
|
|
|
|
description:
|
|
|
|
- remove automatically installed packages which are no longer needed
|
|
|
|
required: false
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: no
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "bleader (@bleader)"
|
2013-05-10 14:15:01 +02:00
|
|
|
notes:
|
|
|
|
- When using pkgsite, be careful that already in cache packages won't be downloaded again.
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Install package foo
|
|
|
|
- pkgng: name=foo state=present
|
|
|
|
|
2014-03-11 20:24:32 +01:00
|
|
|
# Annotate package foo and bar
|
|
|
|
- pkgng: name=foo,bar annotation=+test1=baz,-test2,:test3=foobar
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
# Remove packages foo and bar
|
|
|
|
- pkgng: name=foo,bar state=absent
|
2013-05-10 14:15:01 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
2014-03-11 17:55:40 +01:00
|
|
|
import re
|
2016-05-31 14:38:43 +02:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2013-05-10 14:15:01 +02:00
|
|
|
|
2016-04-11 08:25:30 +02:00
|
|
|
def query_package(module, pkgng_path, name, dir_arg):
|
2013-05-10 14:15:01 +02:00
|
|
|
|
2016-04-11 08:25:30 +02:00
|
|
|
rc, out, err = module.run_command("%s %s info -g -e %s" % (pkgng_path, dir_arg, name))
|
2013-05-10 14:15:01 +02:00
|
|
|
|
|
|
|
if rc == 0:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2014-03-11 18:03:14 +01:00
|
|
|
def pkgng_older_than(module, pkgng_path, compare_version):
|
2014-03-11 17:55:40 +01:00
|
|
|
|
2014-03-11 18:03:14 +01:00
|
|
|
rc, out, err = module.run_command("%s -v" % pkgng_path)
|
2014-03-11 17:55:40 +01:00
|
|
|
version = map(lambda x: int(x), re.split(r'[\._]', out))
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
new_pkgng = True
|
|
|
|
while compare_version[i] == version[i]:
|
|
|
|
i += 1
|
|
|
|
if i == min(len(compare_version), len(version)):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if compare_version[i] > version[i]:
|
|
|
|
new_pkgng = False
|
|
|
|
return not new_pkgng
|
|
|
|
|
2013-05-10 14:15:01 +02:00
|
|
|
|
2016-04-11 08:25:30 +02:00
|
|
|
def remove_packages(module, pkgng_path, packages, dir_arg):
|
2016-05-27 15:49:54 +02:00
|
|
|
|
2013-05-10 14:15:01 +02:00
|
|
|
remove_c = 0
|
|
|
|
# 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-11 08:25:30 +02:00
|
|
|
if not query_package(module, pkgng_path, package, dir_arg):
|
2013-05-10 14:15:01 +02:00
|
|
|
continue
|
|
|
|
|
2013-09-25 17:51:17 +02:00
|
|
|
if not module.check_mode:
|
2016-04-11 08:25:30 +02:00
|
|
|
rc, out, err = module.run_command("%s %s delete -y %s" % (pkgng_path, dir_arg, package))
|
2013-05-10 14:15:01 +02:00
|
|
|
|
2016-04-11 08:25:30 +02:00
|
|
|
if not module.check_mode and query_package(module, pkgng_path, package, dir_arg):
|
2013-05-10 14:15:01 +02:00
|
|
|
module.fail_json(msg="failed to remove %s: %s" % (package, out))
|
2016-05-27 15:49:54 +02:00
|
|
|
|
2013-05-10 14:15:01 +02:00
|
|
|
remove_c += 1
|
|
|
|
|
|
|
|
if remove_c > 0:
|
|
|
|
|
2014-03-11 20:24:32 +01:00
|
|
|
return (True, "removed %s package(s)" % remove_c)
|
2013-05-10 14:15:01 +02:00
|
|
|
|
2014-03-11 20:24:32 +01:00
|
|
|
return (False, "package(s) already absent")
|
2013-05-10 14:15:01 +02:00
|
|
|
|
|
|
|
|
2016-04-11 08:25:30 +02:00
|
|
|
def install_packages(module, pkgng_path, packages, cached, pkgsite, dir_arg):
|
2013-05-10 14:15:01 +02:00
|
|
|
|
|
|
|
install_c = 0
|
|
|
|
|
2014-03-11 17:55:40 +01:00
|
|
|
# as of pkg-1.1.4, PACKAGESITE is deprecated in favor of repository definitions
|
|
|
|
# in /usr/local/etc/pkg/repos
|
2014-03-11 18:03:14 +01:00
|
|
|
old_pkgng = pkgng_older_than(module, pkgng_path, [1, 1, 4])
|
2014-03-18 21:55:05 +01:00
|
|
|
if pkgsite != "":
|
|
|
|
if old_pkgng:
|
|
|
|
pkgsite = "PACKAGESITE=%s" % (pkgsite)
|
|
|
|
else:
|
|
|
|
pkgsite = "-r %s" % (pkgsite)
|
2013-05-10 14:15:01 +02:00
|
|
|
|
2015-02-03 22:43:24 +01:00
|
|
|
batch_var = 'env BATCH=yes' # This environment variable skips mid-install prompts,
|
|
|
|
# setting them to their default values.
|
2015-02-03 06:10:23 +01:00
|
|
|
|
2014-04-15 16:41:27 +02:00
|
|
|
if not module.check_mode and not cached:
|
2014-03-11 17:55:40 +01:00
|
|
|
if old_pkgng:
|
2014-03-11 18:03:14 +01:00
|
|
|
rc, out, err = module.run_command("%s %s update" % (pkgsite, pkgng_path))
|
2014-03-11 17:55:40 +01:00
|
|
|
else:
|
2016-04-11 08:25:30 +02:00
|
|
|
rc, out, err = module.run_command("%s %s update" % (pkgng_path, dir_arg))
|
2013-05-10 14:15:01 +02:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Could not update catalogue")
|
|
|
|
|
|
|
|
for package in packages:
|
2016-04-11 08:25:30 +02:00
|
|
|
if query_package(module, pkgng_path, package, dir_arg):
|
2013-05-10 14:15:01 +02:00
|
|
|
continue
|
|
|
|
|
2013-09-25 17:51:17 +02:00
|
|
|
if not module.check_mode:
|
2014-03-11 17:55:40 +01:00
|
|
|
if old_pkgng:
|
2015-02-03 06:10:23 +01:00
|
|
|
rc, out, err = module.run_command("%s %s %s install -g -U -y %s" % (batch_var, pkgsite, pkgng_path, package))
|
2014-03-11 17:55:40 +01:00
|
|
|
else:
|
2016-04-11 08:25:30 +02:00
|
|
|
rc, out, err = module.run_command("%s %s %s install %s -g -U -y %s" % (batch_var, pkgng_path, dir_arg, pkgsite, package))
|
2013-05-10 14:15:01 +02:00
|
|
|
|
2016-04-11 08:25:30 +02:00
|
|
|
if not module.check_mode and not query_package(module, pkgng_path, package, dir_arg):
|
2013-11-12 16:37:27 +01:00
|
|
|
module.fail_json(msg="failed to install %s: %s" % (package, out), stderr=err)
|
2013-05-10 14:15:01 +02:00
|
|
|
|
|
|
|
install_c += 1
|
2016-05-27 15:49:54 +02:00
|
|
|
|
2013-05-10 14:15:01 +02:00
|
|
|
if install_c > 0:
|
2014-03-11 20:24:32 +01:00
|
|
|
return (True, "added %s package(s)" % (install_c))
|
2013-05-10 14:15:01 +02:00
|
|
|
|
2014-03-11 20:24:32 +01:00
|
|
|
return (False, "package(s) already present")
|
2013-05-10 14:15:01 +02:00
|
|
|
|
2016-04-11 08:25:30 +02:00
|
|
|
def annotation_query(module, pkgng_path, package, tag, dir_arg):
|
|
|
|
rc, out, err = module.run_command("%s %s info -g -A %s" % (pkgng_path, dir_arg, package))
|
2014-03-11 20:24:32 +01:00
|
|
|
match = re.search(r'^\s*(?P<tag>%s)\s*:\s*(?P<value>\w+)' % tag, out, flags=re.MULTILINE)
|
|
|
|
if match:
|
|
|
|
return match.group('value')
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2016-04-11 08:25:30 +02:00
|
|
|
def annotation_add(module, pkgng_path, package, tag, value, dir_arg):
|
|
|
|
_value = annotation_query(module, pkgng_path, package, tag, dir_arg)
|
2014-03-11 20:24:32 +01:00
|
|
|
if not _value:
|
|
|
|
# Annotation does not exist, add it.
|
2015-04-21 14:44:58 +02:00
|
|
|
rc, out, err = module.run_command('%s %s annotate -y -A %s %s "%s"'
|
2016-04-11 08:25:30 +02:00
|
|
|
% (pkgng_path, dir_arg, package, tag, value))
|
2014-03-11 20:24:32 +01:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json("could not annotate %s: %s"
|
|
|
|
% (package, out), stderr=err)
|
|
|
|
return True
|
|
|
|
elif _value != value:
|
|
|
|
# Annotation exists, but value differs
|
|
|
|
module.fail_json(
|
|
|
|
mgs="failed to annotate %s, because %s is already set to %s, but should be set to %s"
|
|
|
|
% (package, tag, _value, value))
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
# Annotation exists, nothing to do
|
|
|
|
return False
|
|
|
|
|
2016-04-11 08:25:30 +02:00
|
|
|
def annotation_delete(module, pkgng_path, package, tag, value, dir_arg):
|
|
|
|
_value = annotation_query(module, pkgng_path, package, tag, dir_arg)
|
2014-03-11 20:24:32 +01:00
|
|
|
if _value:
|
2015-04-21 14:44:58 +02:00
|
|
|
rc, out, err = module.run_command('%s %s annotate -y -D %s %s'
|
2016-04-11 08:25:30 +02:00
|
|
|
% (pkgng_path, dir_arg, package, tag))
|
2014-03-11 20:24:32 +01:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json("could not delete annotation to %s: %s"
|
|
|
|
% (package, out), stderr=err)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-04-11 08:25:30 +02:00
|
|
|
def annotation_modify(module, pkgng_path, package, tag, value, dir_arg):
|
|
|
|
_value = annotation_query(module, pkgng_path, package, tag, dir_arg)
|
2014-03-11 20:24:32 +01:00
|
|
|
if not value:
|
|
|
|
# No such tag
|
|
|
|
module.fail_json("could not change annotation to %s: tag %s does not exist"
|
|
|
|
% (package, tag))
|
|
|
|
elif _value == value:
|
|
|
|
# No change in value
|
|
|
|
return False
|
|
|
|
else:
|
2015-04-21 14:44:58 +02:00
|
|
|
rc,out,err = module.run_command('%s %s annotate -y -M %s %s "%s"'
|
2016-04-11 08:25:30 +02:00
|
|
|
% (pkgng_path, dir_arg, package, tag, value))
|
2014-03-11 20:24:32 +01:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json("could not change annotation annotation to %s: %s"
|
|
|
|
% (package, out), stderr=err)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2016-04-11 08:25:30 +02:00
|
|
|
def annotate_packages(module, pkgng_path, packages, annotation, dir_arg):
|
2014-03-11 20:24:32 +01:00
|
|
|
annotate_c = 0
|
|
|
|
annotations = map(lambda _annotation:
|
|
|
|
re.match(r'(?P<operation>[\+-:])(?P<tag>\w+)(=(?P<value>\w+))?',
|
|
|
|
_annotation).groupdict(),
|
|
|
|
re.split(r',', annotation))
|
|
|
|
|
|
|
|
operation = {
|
|
|
|
'+': annotation_add,
|
|
|
|
'-': annotation_delete,
|
|
|
|
':': annotation_modify
|
|
|
|
}
|
|
|
|
|
|
|
|
for package in packages:
|
|
|
|
for _annotation in annotations:
|
2015-05-08 23:36:15 +02:00
|
|
|
if operation[_annotation['operation']](module, pkgng_path, package, _annotation['tag'], _annotation['value']):
|
|
|
|
annotate_c += 1
|
2014-03-11 20:24:32 +01:00
|
|
|
|
|
|
|
if annotate_c > 0:
|
|
|
|
return (True, "added %s annotations." % annotate_c)
|
|
|
|
return (False, "changed no annotations")
|
2013-05-10 14:15:01 +02:00
|
|
|
|
2016-05-27 15:49:54 +02:00
|
|
|
def autoremove_packages(module, pkgng_path, dir_arg):
|
|
|
|
rc, out, err = module.run_command("%s %s autoremove -n" % (pkgng_path, dir_arg))
|
|
|
|
|
|
|
|
autoremove_c = 0
|
|
|
|
|
|
|
|
match = re.search('^Deinstallation has been requested for the following ([0-9]+) packages', out, re.MULTILINE)
|
|
|
|
if match:
|
|
|
|
autoremove_c = int(match.group(1))
|
|
|
|
|
|
|
|
if autoremove_c == 0:
|
|
|
|
return False, "no package(s) to autoremove"
|
|
|
|
|
|
|
|
if not module.check_mode:
|
|
|
|
rc, out, err = module.run_command("%s %s autoremove -y" % (pkgng_path, dir_arg))
|
|
|
|
|
|
|
|
return True, "autoremoved %d package(s)" % (autoremove_c)
|
|
|
|
|
2013-05-10 14:15:01 +02:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
2013-09-25 17:51:17 +02:00
|
|
|
argument_spec = dict(
|
2014-03-11 20:24:32 +01:00
|
|
|
state = dict(default="present", choices=["present","absent"], required=False),
|
2015-11-01 21:13:32 +01:00
|
|
|
name = dict(aliases=["pkg"], required=True, type='list'),
|
2013-11-12 05:23:56 +01:00
|
|
|
cached = dict(default=False, type='bool'),
|
2014-03-11 20:24:32 +01:00
|
|
|
annotation = dict(default="", required=False),
|
2015-04-21 14:44:58 +02:00
|
|
|
pkgsite = dict(default="", required=False),
|
2016-04-11 08:25:30 +02:00
|
|
|
rootdir = dict(default="", required=False, type='path'),
|
2016-05-27 15:49:54 +02:00
|
|
|
chroot = dict(default="", required=False, type='path'),
|
|
|
|
autoremove = dict(default=False, type='bool')),
|
2016-04-11 08:25:30 +02:00
|
|
|
supports_check_mode = True,
|
|
|
|
mutually_exclusive =[["rootdir", "chroot"]])
|
2013-05-10 14:15:01 +02:00
|
|
|
|
2014-03-11 18:03:14 +01:00
|
|
|
pkgng_path = module.get_bin_path('pkg', True)
|
2013-05-10 14:15:01 +02:00
|
|
|
|
|
|
|
p = module.params
|
|
|
|
|
2015-11-01 21:13:32 +01:00
|
|
|
pkgs = p["name"]
|
2013-05-10 14:15:01 +02:00
|
|
|
|
2014-03-11 20:24:32 +01:00
|
|
|
changed = False
|
|
|
|
msgs = []
|
2016-04-11 08:25:30 +02:00
|
|
|
dir_arg = ""
|
2015-04-21 14:44:58 +02:00
|
|
|
|
|
|
|
if p["rootdir"] != "":
|
|
|
|
old_pkgng = pkgng_older_than(module, pkgng_path, [1, 5, 0])
|
|
|
|
if old_pkgng:
|
|
|
|
module.fail_json(msg="To use option 'rootdir' pkg version must be 1.5 or greater")
|
|
|
|
else:
|
2016-04-11 08:25:30 +02:00
|
|
|
dir_arg = "--rootdir %s" % (p["rootdir"])
|
|
|
|
|
|
|
|
if p["chroot"] != "":
|
2016-05-27 15:49:54 +02:00
|
|
|
dir_arg = '--chroot %s' % (p["chroot"])
|
2014-03-11 20:24:32 +01:00
|
|
|
|
2013-05-10 14:15:01 +02:00
|
|
|
if p["state"] == "present":
|
2016-04-11 08:25:30 +02:00
|
|
|
_changed, _msg = install_packages(module, pkgng_path, pkgs, p["cached"], p["pkgsite"], dir_arg)
|
2014-03-11 20:24:32 +01:00
|
|
|
changed = changed or _changed
|
|
|
|
msgs.append(_msg)
|
2013-05-10 14:15:01 +02:00
|
|
|
|
|
|
|
elif p["state"] == "absent":
|
2016-04-11 08:25:30 +02:00
|
|
|
_changed, _msg = remove_packages(module, pkgng_path, pkgs, dir_arg)
|
2014-03-11 20:24:32 +01:00
|
|
|
changed = changed or _changed
|
|
|
|
msgs.append(_msg)
|
|
|
|
|
2016-05-27 15:49:54 +02:00
|
|
|
if p["autoremove"]:
|
|
|
|
_changed, _msg = autoremove_packages(module, pkgng_path, dir_arg)
|
|
|
|
changed = changed or _changed
|
|
|
|
msgs.append(_msg)
|
|
|
|
|
2014-03-11 20:24:32 +01:00
|
|
|
if p["annotation"]:
|
2016-04-11 08:25:30 +02:00
|
|
|
_changed, _msg = annotate_packages(module, pkgng_path, pkgs, p["annotation"], dir_arg)
|
2014-03-11 20:24:32 +01:00
|
|
|
changed = changed or _changed
|
|
|
|
msgs.append(_msg)
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, msg=", ".join(msgs))
|
|
|
|
|
|
|
|
|
2016-05-31 14:38:43 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|