2020-03-09 10:11:07 +01: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 <https://github.com/afterburn>
|
|
|
|
# that was based on apt module written by Matthew Williams <matthew@flowroute.com>
|
|
|
|
#
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name or list of names of packages to install/remove.
|
2020-07-07 23:04:35 +02:00
|
|
|
- "With I(name=*), I(state: latest) will operate, but I(state: present) and I(state: absent) will be noops."
|
|
|
|
- >
|
|
|
|
Warning: In Ansible 2.9 and earlier this module had a misfeature
|
|
|
|
where I(name=*) with I(state: latest) or I(state: present) would
|
|
|
|
install every package from every package repository, filling up
|
|
|
|
the machines disk. Avoid using them unless you are certain that
|
|
|
|
your role will only be used with newer versions.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
2020-06-26 15:17:11 +02:00
|
|
|
aliases: [pkg]
|
|
|
|
type: list
|
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State of the package.
|
|
|
|
- 'Note: "latest" added in 2.7'
|
|
|
|
choices: [ 'present', 'latest', 'absent' ]
|
|
|
|
required: false
|
|
|
|
default: present
|
2020-06-26 15:17:11 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
cached:
|
|
|
|
description:
|
|
|
|
- Use local package base instead of fetching an updated one.
|
|
|
|
type: bool
|
|
|
|
required: false
|
|
|
|
default: no
|
|
|
|
annotation:
|
|
|
|
description:
|
|
|
|
- A comma-separated list of keyvalue-pairs of the form
|
|
|
|
C(<+/-/:><key>[=<value>]). A C(+) denotes adding an annotation, a
|
|
|
|
C(-) denotes removing an annotation, and C(:) denotes modifying an
|
|
|
|
annotation.
|
|
|
|
If setting or modifying annotations, a value must be provided.
|
|
|
|
required: false
|
2020-06-26 15:17:11 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
pkgsite:
|
|
|
|
description:
|
|
|
|
- For pkgng versions before 1.1.4, specify packagesite to use
|
|
|
|
for downloading packages. If not specified, use settings from
|
|
|
|
C(/usr/local/etc/pkg.conf).
|
|
|
|
- For newer pkgng versions, specify a the name of a repository
|
|
|
|
configured in C(/usr/local/etc/pkg/repos).
|
|
|
|
required: false
|
2020-06-26 15:17:11 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
rootdir:
|
|
|
|
description:
|
|
|
|
- For pkgng versions 1.5 and later, pkg will install all packages
|
|
|
|
within the specified root directory.
|
|
|
|
- Can not be used together with I(chroot) or I(jail) options.
|
|
|
|
required: false
|
2020-06-26 15:17:11 +02:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
chroot:
|
|
|
|
description:
|
|
|
|
- Pkg will chroot in the specified environment.
|
|
|
|
- Can not be used together with I(rootdir) or I(jail) options.
|
|
|
|
required: false
|
2020-06-26 15:17:11 +02:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
jail:
|
|
|
|
description:
|
|
|
|
- Pkg will execute in the given jail name or id.
|
|
|
|
- Can not be used together with I(chroot) or I(rootdir) options.
|
2020-06-26 15:17:11 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
autoremove:
|
|
|
|
description:
|
|
|
|
- Remove automatically installed packages which are no longer needed.
|
|
|
|
required: false
|
|
|
|
type: bool
|
|
|
|
default: no
|
2020-11-17 07:17:16 +01:00
|
|
|
ignore_osver:
|
|
|
|
description:
|
|
|
|
- Ignore FreeBSD OS version check, useful on -STABLE and -CURRENT branches.
|
|
|
|
- Defines the C(IGNORE_OSVERSION) environment variable.
|
|
|
|
required: false
|
|
|
|
type: bool
|
|
|
|
default: no
|
|
|
|
version_added: 1.3.0
|
2020-03-09 10:11:07 +01:00
|
|
|
author: "bleader (@bleader)"
|
|
|
|
notes:
|
|
|
|
- When using pkgsite, be careful that already in cache packages won't be downloaded again.
|
|
|
|
- When used with a `loop:` each package will be processed individually,
|
|
|
|
it is much more efficient to pass the list directly to the `name` option.
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Install package foo
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pkgng:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: foo
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Annotate package foo and bar
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pkgng:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: foo,bar
|
|
|
|
annotation: '+test1=baz,-test2,:test3=foobar'
|
|
|
|
|
|
|
|
- name: Remove packages foo and bar
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pkgng:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: foo,bar
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
# "latest" support added in 2.7
|
|
|
|
- name: Upgrade package baz
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pkgng:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: baz
|
|
|
|
state: latest
|
2020-07-07 23:04:35 +02:00
|
|
|
|
|
|
|
- name: Upgrade all installed packages (see warning for the name option first!)
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pkgng:
|
2020-07-07 23:04:35 +02:00
|
|
|
name: "*"
|
|
|
|
state: latest
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
Pkgng many packages one command (#3393) (#3529)
* pkgng: join package list into one command
Change the pkgng module so all packages being
installed (or upgraded) are acted on in one
command (per action). This will make installs
and upgrades a bit faster, because pkg will be
invoked fewer times per module run. More important,
module actions will be more atomic, making it less
likely that some packages are acted on because they
appear earlier in the argument list.
This change also improves the status reporting of
packages acted on, specifying the number of packages
for each action (install or upgrade).
* pkgng: make upgrade check lazily evaluated
Make upgrade_available an inner function so that the
if statement that checks whether installed packages
are up-to-date only runs the upgrade check on packages
that are already installed. This gets lazily evaluated
because of boolean operator short-circuiting:
https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not
Previously, the module would always check for upgrades,
even for not-installed packages, when running with
`state=latest`.
* pkgng: add changelog fragment
* pkgng: Apply changelog suggestions from code review
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: resolve pep8 style issue
Remove inline function. It's purpose would be confusing for
future maintainers, and someone refactoring it to a variable,
with good intentions, would introduce a performance regression.
Including the `query_update()` call in the if expression makes
the intent more legible and still ensures lazy evaluation of the
function call if the first `and` is `False`.
* pkgng: Fix changelog fragment syntax issue
Need to escape quotes so YAML doesn't eat them
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: Improve output message English grammar
Make word "package" plural only if reporting on more than one package
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 293c7a9fb386e357bb7e9da512b1ff951fe88fe5)
Co-authored-by: Ross Williams <ross@ross-williams.net>
2021-10-08 07:57:27 +02:00
|
|
|
from collections import defaultdict
|
2020-03-09 10:11:07 +01:00
|
|
|
import re
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
def query_package(module, pkgng_path, name, dir_arg):
|
|
|
|
|
|
|
|
rc, out, err = module.run_command("%s %s info -g -e %s" % (pkgng_path, dir_arg, name))
|
|
|
|
|
|
|
|
if rc == 0:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def query_update(module, pkgng_path, name, dir_arg, old_pkgng, pkgsite):
|
|
|
|
|
|
|
|
# Check to see if a package upgrade is available.
|
|
|
|
# rc = 0, no updates available or package not installed
|
|
|
|
# rc = 1, updates available
|
|
|
|
if old_pkgng:
|
|
|
|
rc, out, err = module.run_command("%s %s upgrade -g -n %s" % (pkgsite, pkgng_path, name))
|
|
|
|
else:
|
|
|
|
rc, out, err = module.run_command("%s %s upgrade %s -g -n %s" % (pkgng_path, dir_arg, pkgsite, name))
|
|
|
|
|
|
|
|
if rc == 1:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def pkgng_older_than(module, pkgng_path, compare_version):
|
|
|
|
|
|
|
|
rc, out, err = module.run_command("%s -v" % pkgng_path)
|
|
|
|
version = [int(x) for x in 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
|
|
|
|
|
|
|
|
|
2020-07-07 23:04:35 +02:00
|
|
|
def upgrade_packages(module, pkgng_path, dir_arg):
|
|
|
|
# Run a 'pkg upgrade', updating all packages.
|
|
|
|
upgraded_c = 0
|
|
|
|
|
|
|
|
cmd = "%s %s upgrade -y" % (pkgng_path, dir_arg)
|
|
|
|
if module.check_mode:
|
|
|
|
cmd += " -n"
|
|
|
|
rc, out, err = module.run_command(cmd)
|
|
|
|
|
|
|
|
match = re.search('^Number of packages to be upgraded: ([0-9]+)', out, re.MULTILINE)
|
|
|
|
if match:
|
|
|
|
upgraded_c = int(match.group(1))
|
|
|
|
|
|
|
|
if upgraded_c > 0:
|
|
|
|
return (True, "updated %s package(s)" % upgraded_c, out, err)
|
|
|
|
return (False, "no packages need upgrades", out, err)
|
|
|
|
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def remove_packages(module, pkgng_path, packages, dir_arg):
|
|
|
|
remove_c = 0
|
2020-06-23 19:55:40 +02:00
|
|
|
stdout = ""
|
|
|
|
stderr = ""
|
2020-03-09 10:11:07 +01:00
|
|
|
# Using a for loop in case 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
|
|
|
|
if not query_package(module, pkgng_path, package, dir_arg):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not module.check_mode:
|
|
|
|
rc, out, err = module.run_command("%s %s delete -y %s" % (pkgng_path, dir_arg, package))
|
2020-06-23 19:55:40 +02:00
|
|
|
stdout += out
|
|
|
|
stderr += err
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if not module.check_mode and query_package(module, pkgng_path, package, dir_arg):
|
2020-06-23 19:55:40 +02:00
|
|
|
module.fail_json(msg="failed to remove %s: %s" % (package, out), stdout=stdout, stderr=stderr)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
remove_c += 1
|
|
|
|
|
|
|
|
if remove_c > 0:
|
2020-06-23 19:55:40 +02:00
|
|
|
return (True, "removed %s package(s)" % remove_c, stdout, stderr)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-06-23 19:55:40 +02:00
|
|
|
return (False, "package(s) already absent", stdout, stderr)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2020-11-17 07:17:16 +01:00
|
|
|
def install_packages(module, pkgng_path, packages, cached, pkgsite, dir_arg, state, ignoreosver):
|
Pkgng many packages one command (#3393) (#3529)
* pkgng: join package list into one command
Change the pkgng module so all packages being
installed (or upgraded) are acted on in one
command (per action). This will make installs
and upgrades a bit faster, because pkg will be
invoked fewer times per module run. More important,
module actions will be more atomic, making it less
likely that some packages are acted on because they
appear earlier in the argument list.
This change also improves the status reporting of
packages acted on, specifying the number of packages
for each action (install or upgrade).
* pkgng: make upgrade check lazily evaluated
Make upgrade_available an inner function so that the
if statement that checks whether installed packages
are up-to-date only runs the upgrade check on packages
that are already installed. This gets lazily evaluated
because of boolean operator short-circuiting:
https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not
Previously, the module would always check for upgrades,
even for not-installed packages, when running with
`state=latest`.
* pkgng: add changelog fragment
* pkgng: Apply changelog suggestions from code review
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: resolve pep8 style issue
Remove inline function. It's purpose would be confusing for
future maintainers, and someone refactoring it to a variable,
with good intentions, would introduce a performance regression.
Including the `query_update()` call in the if expression makes
the intent more legible and still ensures lazy evaluation of the
function call if the first `and` is `False`.
* pkgng: Fix changelog fragment syntax issue
Need to escape quotes so YAML doesn't eat them
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: Improve output message English grammar
Make word "package" plural only if reporting on more than one package
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 293c7a9fb386e357bb7e9da512b1ff951fe88fe5)
Co-authored-by: Ross Williams <ross@ross-williams.net>
2021-10-08 07:57:27 +02:00
|
|
|
action_queue = defaultdict(list)
|
|
|
|
action_count = defaultdict(int)
|
2020-06-23 19:55:40 +02:00
|
|
|
stdout = ""
|
|
|
|
stderr = ""
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# as of pkg-1.1.4, PACKAGESITE is deprecated in favor of repository definitions
|
|
|
|
# in /usr/local/etc/pkg/repos
|
|
|
|
old_pkgng = pkgng_older_than(module, pkgng_path, [1, 1, 4])
|
|
|
|
if pkgsite != "":
|
|
|
|
if old_pkgng:
|
|
|
|
pkgsite = "PACKAGESITE=%s" % (pkgsite)
|
|
|
|
else:
|
|
|
|
pkgsite = "-r %s" % (pkgsite)
|
|
|
|
|
|
|
|
# This environment variable skips mid-install prompts,
|
|
|
|
# setting them to their default values.
|
|
|
|
batch_var = 'env BATCH=yes'
|
|
|
|
|
2020-11-17 07:17:16 +01:00
|
|
|
if ignoreosver:
|
|
|
|
# Ignore FreeBSD OS version check,
|
|
|
|
# useful on -STABLE and -CURRENT branches.
|
|
|
|
batch_var = batch_var + ' IGNORE_OSVERSION=yes'
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
if not module.check_mode and not cached:
|
|
|
|
if old_pkgng:
|
|
|
|
rc, out, err = module.run_command("%s %s update" % (pkgsite, pkgng_path))
|
|
|
|
else:
|
2020-11-17 07:17:16 +01:00
|
|
|
rc, out, err = module.run_command("%s %s %s update" % (batch_var, pkgng_path, dir_arg))
|
2020-06-23 19:55:40 +02:00
|
|
|
stdout += out
|
|
|
|
stderr += err
|
2020-03-09 10:11:07 +01:00
|
|
|
if rc != 0:
|
2020-06-23 19:55:40 +02:00
|
|
|
module.fail_json(msg="Could not update catalogue [%d]: %s %s" % (rc, out, err), stdout=stdout, stderr=stderr)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
for package in packages:
|
|
|
|
already_installed = query_package(module, pkgng_path, package, dir_arg)
|
|
|
|
if already_installed and state == "present":
|
|
|
|
continue
|
|
|
|
|
Pkgng many packages one command (#3393) (#3529)
* pkgng: join package list into one command
Change the pkgng module so all packages being
installed (or upgraded) are acted on in one
command (per action). This will make installs
and upgrades a bit faster, because pkg will be
invoked fewer times per module run. More important,
module actions will be more atomic, making it less
likely that some packages are acted on because they
appear earlier in the argument list.
This change also improves the status reporting of
packages acted on, specifying the number of packages
for each action (install or upgrade).
* pkgng: make upgrade check lazily evaluated
Make upgrade_available an inner function so that the
if statement that checks whether installed packages
are up-to-date only runs the upgrade check on packages
that are already installed. This gets lazily evaluated
because of boolean operator short-circuiting:
https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not
Previously, the module would always check for upgrades,
even for not-installed packages, when running with
`state=latest`.
* pkgng: add changelog fragment
* pkgng: Apply changelog suggestions from code review
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: resolve pep8 style issue
Remove inline function. It's purpose would be confusing for
future maintainers, and someone refactoring it to a variable,
with good intentions, would introduce a performance regression.
Including the `query_update()` call in the if expression makes
the intent more legible and still ensures lazy evaluation of the
function call if the first `and` is `False`.
* pkgng: Fix changelog fragment syntax issue
Need to escape quotes so YAML doesn't eat them
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: Improve output message English grammar
Make word "package" plural only if reporting on more than one package
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 293c7a9fb386e357bb7e9da512b1ff951fe88fe5)
Co-authored-by: Ross Williams <ross@ross-williams.net>
2021-10-08 07:57:27 +02:00
|
|
|
if (
|
|
|
|
already_installed and state == "latest"
|
|
|
|
and not query_update(module, pkgng_path, package, dir_arg, old_pkgng, pkgsite)
|
|
|
|
):
|
2020-03-09 10:11:07 +01:00
|
|
|
continue
|
|
|
|
|
Pkgng many packages one command (#3393) (#3529)
* pkgng: join package list into one command
Change the pkgng module so all packages being
installed (or upgraded) are acted on in one
command (per action). This will make installs
and upgrades a bit faster, because pkg will be
invoked fewer times per module run. More important,
module actions will be more atomic, making it less
likely that some packages are acted on because they
appear earlier in the argument list.
This change also improves the status reporting of
packages acted on, specifying the number of packages
for each action (install or upgrade).
* pkgng: make upgrade check lazily evaluated
Make upgrade_available an inner function so that the
if statement that checks whether installed packages
are up-to-date only runs the upgrade check on packages
that are already installed. This gets lazily evaluated
because of boolean operator short-circuiting:
https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not
Previously, the module would always check for upgrades,
even for not-installed packages, when running with
`state=latest`.
* pkgng: add changelog fragment
* pkgng: Apply changelog suggestions from code review
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: resolve pep8 style issue
Remove inline function. It's purpose would be confusing for
future maintainers, and someone refactoring it to a variable,
with good intentions, would introduce a performance regression.
Including the `query_update()` call in the if expression makes
the intent more legible and still ensures lazy evaluation of the
function call if the first `and` is `False`.
* pkgng: Fix changelog fragment syntax issue
Need to escape quotes so YAML doesn't eat them
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: Improve output message English grammar
Make word "package" plural only if reporting on more than one package
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 293c7a9fb386e357bb7e9da512b1ff951fe88fe5)
Co-authored-by: Ross Williams <ross@ross-williams.net>
2021-10-08 07:57:27 +02:00
|
|
|
if already_installed:
|
|
|
|
action_queue["upgrade"].append(package)
|
|
|
|
else:
|
|
|
|
action_queue["install"].append(package)
|
|
|
|
|
|
|
|
if not module.check_mode:
|
|
|
|
# install/upgrade all named packages with one pkg command
|
|
|
|
for (action, package_list) in action_queue.items():
|
|
|
|
packages = ' '.join(package_list)
|
2020-03-09 10:11:07 +01:00
|
|
|
if old_pkgng:
|
Pkgng many packages one command (#3393) (#3529)
* pkgng: join package list into one command
Change the pkgng module so all packages being
installed (or upgraded) are acted on in one
command (per action). This will make installs
and upgrades a bit faster, because pkg will be
invoked fewer times per module run. More important,
module actions will be more atomic, making it less
likely that some packages are acted on because they
appear earlier in the argument list.
This change also improves the status reporting of
packages acted on, specifying the number of packages
for each action (install or upgrade).
* pkgng: make upgrade check lazily evaluated
Make upgrade_available an inner function so that the
if statement that checks whether installed packages
are up-to-date only runs the upgrade check on packages
that are already installed. This gets lazily evaluated
because of boolean operator short-circuiting:
https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not
Previously, the module would always check for upgrades,
even for not-installed packages, when running with
`state=latest`.
* pkgng: add changelog fragment
* pkgng: Apply changelog suggestions from code review
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: resolve pep8 style issue
Remove inline function. It's purpose would be confusing for
future maintainers, and someone refactoring it to a variable,
with good intentions, would introduce a performance regression.
Including the `query_update()` call in the if expression makes
the intent more legible and still ensures lazy evaluation of the
function call if the first `and` is `False`.
* pkgng: Fix changelog fragment syntax issue
Need to escape quotes so YAML doesn't eat them
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: Improve output message English grammar
Make word "package" plural only if reporting on more than one package
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 293c7a9fb386e357bb7e9da512b1ff951fe88fe5)
Co-authored-by: Ross Williams <ross@ross-williams.net>
2021-10-08 07:57:27 +02:00
|
|
|
rc, out, err = module.run_command("%s %s %s %s -g -U -y %s" % (batch_var, pkgsite, pkgng_path, action, packages))
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
Pkgng many packages one command (#3393) (#3529)
* pkgng: join package list into one command
Change the pkgng module so all packages being
installed (or upgraded) are acted on in one
command (per action). This will make installs
and upgrades a bit faster, because pkg will be
invoked fewer times per module run. More important,
module actions will be more atomic, making it less
likely that some packages are acted on because they
appear earlier in the argument list.
This change also improves the status reporting of
packages acted on, specifying the number of packages
for each action (install or upgrade).
* pkgng: make upgrade check lazily evaluated
Make upgrade_available an inner function so that the
if statement that checks whether installed packages
are up-to-date only runs the upgrade check on packages
that are already installed. This gets lazily evaluated
because of boolean operator short-circuiting:
https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not
Previously, the module would always check for upgrades,
even for not-installed packages, when running with
`state=latest`.
* pkgng: add changelog fragment
* pkgng: Apply changelog suggestions from code review
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: resolve pep8 style issue
Remove inline function. It's purpose would be confusing for
future maintainers, and someone refactoring it to a variable,
with good intentions, would introduce a performance regression.
Including the `query_update()` call in the if expression makes
the intent more legible and still ensures lazy evaluation of the
function call if the first `and` is `False`.
* pkgng: Fix changelog fragment syntax issue
Need to escape quotes so YAML doesn't eat them
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: Improve output message English grammar
Make word "package" plural only if reporting on more than one package
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 293c7a9fb386e357bb7e9da512b1ff951fe88fe5)
Co-authored-by: Ross Williams <ross@ross-williams.net>
2021-10-08 07:57:27 +02:00
|
|
|
rc, out, err = module.run_command("%s %s %s %s %s -g -U -y %s" % (batch_var, pkgng_path, dir_arg, action, pkgsite, packages))
|
2020-06-23 19:55:40 +02:00
|
|
|
stdout += out
|
|
|
|
stderr += err
|
2020-03-09 10:11:07 +01:00
|
|
|
|
Pkgng many packages one command (#3393) (#3529)
* pkgng: join package list into one command
Change the pkgng module so all packages being
installed (or upgraded) are acted on in one
command (per action). This will make installs
and upgrades a bit faster, because pkg will be
invoked fewer times per module run. More important,
module actions will be more atomic, making it less
likely that some packages are acted on because they
appear earlier in the argument list.
This change also improves the status reporting of
packages acted on, specifying the number of packages
for each action (install or upgrade).
* pkgng: make upgrade check lazily evaluated
Make upgrade_available an inner function so that the
if statement that checks whether installed packages
are up-to-date only runs the upgrade check on packages
that are already installed. This gets lazily evaluated
because of boolean operator short-circuiting:
https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not
Previously, the module would always check for upgrades,
even for not-installed packages, when running with
`state=latest`.
* pkgng: add changelog fragment
* pkgng: Apply changelog suggestions from code review
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: resolve pep8 style issue
Remove inline function. It's purpose would be confusing for
future maintainers, and someone refactoring it to a variable,
with good intentions, would introduce a performance regression.
Including the `query_update()` call in the if expression makes
the intent more legible and still ensures lazy evaluation of the
function call if the first `and` is `False`.
* pkgng: Fix changelog fragment syntax issue
Need to escape quotes so YAML doesn't eat them
Co-authored-by: Felix Fontein <felix@fontein.de>
* pkgng: Improve output message English grammar
Make word "package" plural only if reporting on more than one package
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 293c7a9fb386e357bb7e9da512b1ff951fe88fe5)
Co-authored-by: Ross Williams <ross@ross-williams.net>
2021-10-08 07:57:27 +02:00
|
|
|
# individually verify packages are in requested state
|
|
|
|
for package in package_list:
|
|
|
|
verified = False
|
|
|
|
if action == 'install':
|
|
|
|
verified = query_package(module, pkgng_path, package, dir_arg)
|
|
|
|
elif action == 'upgrade':
|
|
|
|
verified = not query_update(module, pkgng_path, package, dir_arg, old_pkgng, pkgsite)
|
|
|
|
|
|
|
|
if verified:
|
|
|
|
action_count[action] += 1
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="failed to %s %s" % (action, package), stdout=stdout, stderr=stderr)
|
|
|
|
|
|
|
|
if sum(action_count.values()) > 0:
|
|
|
|
past_tense = {'install': 'installed', 'upgrade': 'upgraded'}
|
|
|
|
messages = []
|
|
|
|
for (action, count) in action_count.items():
|
|
|
|
messages.append("%s %s package%s" % (past_tense.get(action, action), count, "s" if count != 1 else ""))
|
|
|
|
|
|
|
|
return (True, '; '.join(messages), stdout, stderr)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-06-23 19:55:40 +02:00
|
|
|
return (False, "package(s) already %s" % (state), stdout, stderr)
|
2020-03-09 10:11:07 +01: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))
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def annotation_add(module, pkgng_path, package, tag, value, dir_arg):
|
|
|
|
_value = annotation_query(module, pkgng_path, package, tag, dir_arg)
|
|
|
|
if not _value:
|
|
|
|
# Annotation does not exist, add it.
|
|
|
|
rc, out, err = module.run_command('%s %s annotate -y -A %s %s "%s"'
|
|
|
|
% (pkgng_path, dir_arg, package, tag, value))
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="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
|
|
|
|
|
|
|
|
|
|
|
|
def annotation_delete(module, pkgng_path, package, tag, value, dir_arg):
|
|
|
|
_value = annotation_query(module, pkgng_path, package, tag, dir_arg)
|
|
|
|
if _value:
|
|
|
|
rc, out, err = module.run_command('%s %s annotate -y -D %s %s'
|
|
|
|
% (pkgng_path, dir_arg, package, tag))
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="could not delete annotation to %s: %s"
|
|
|
|
% (package, out), stderr=err)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def annotation_modify(module, pkgng_path, package, tag, value, dir_arg):
|
|
|
|
_value = annotation_query(module, pkgng_path, package, tag, dir_arg)
|
|
|
|
if not value:
|
|
|
|
# No such tag
|
|
|
|
module.fail_json(msg="could not change annotation to %s: tag %s does not exist"
|
|
|
|
% (package, tag))
|
|
|
|
elif _value == value:
|
|
|
|
# No change in value
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
rc, out, err = module.run_command('%s %s annotate -y -M %s %s "%s"'
|
|
|
|
% (pkgng_path, dir_arg, package, tag, value))
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="could not change annotation annotation to %s: %s"
|
|
|
|
% (package, out), stderr=err)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def annotate_packages(module, pkgng_path, packages, annotation, dir_arg):
|
|
|
|
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:
|
|
|
|
if operation[_annotation['operation']](module, pkgng_path, package, _annotation['tag'], _annotation['value']):
|
|
|
|
annotate_c += 1
|
|
|
|
|
|
|
|
if annotate_c > 0:
|
|
|
|
return (True, "added %s annotations." % annotate_c)
|
|
|
|
return (False, "changed no annotations")
|
|
|
|
|
|
|
|
|
|
|
|
def autoremove_packages(module, pkgng_path, dir_arg):
|
2020-06-23 19:55:40 +02:00
|
|
|
stdout = ""
|
|
|
|
stderr = ""
|
2020-03-09 10:11:07 +01:00
|
|
|
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:
|
2020-06-23 19:55:40 +02:00
|
|
|
return (False, "no package(s) to autoremove", stdout, stderr)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if not module.check_mode:
|
|
|
|
rc, out, err = module.run_command("%s %s autoremove -y" % (pkgng_path, dir_arg))
|
2020-06-23 19:55:40 +02:00
|
|
|
stdout += out
|
|
|
|
stderr += err
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-06-23 19:55:40 +02:00
|
|
|
return (True, "autoremoved %d package(s)" % (autoremove_c), stdout, stderr)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
state=dict(default="present", choices=["present", "latest", "absent"], required=False),
|
2020-06-26 15:17:11 +02:00
|
|
|
name=dict(aliases=["pkg"], required=True, type='list', elements='str'),
|
2020-03-09 10:11:07 +01:00
|
|
|
cached=dict(default=False, type='bool'),
|
2020-11-17 07:17:16 +01:00
|
|
|
ignore_osver=dict(default=False, required=False, type='bool'),
|
2020-03-09 10:11:07 +01:00
|
|
|
annotation=dict(default="", required=False),
|
|
|
|
pkgsite=dict(default="", required=False),
|
|
|
|
rootdir=dict(default="", required=False, type='path'),
|
|
|
|
chroot=dict(default="", required=False, type='path'),
|
|
|
|
jail=dict(default="", required=False, type='str'),
|
|
|
|
autoremove=dict(default=False, type='bool')),
|
|
|
|
supports_check_mode=True,
|
|
|
|
mutually_exclusive=[["rootdir", "chroot", "jail"]])
|
|
|
|
|
|
|
|
pkgng_path = module.get_bin_path('pkg', True)
|
|
|
|
|
|
|
|
p = module.params
|
|
|
|
|
|
|
|
pkgs = p["name"]
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
msgs = []
|
2020-06-23 19:55:40 +02:00
|
|
|
stdout = ""
|
|
|
|
stderr = ""
|
2020-03-09 10:11:07 +01:00
|
|
|
dir_arg = ""
|
|
|
|
|
|
|
|
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:
|
|
|
|
dir_arg = "--rootdir %s" % (p["rootdir"])
|
|
|
|
|
2020-11-17 07:17:16 +01:00
|
|
|
if p["ignore_osver"]:
|
|
|
|
old_pkgng = pkgng_older_than(module, pkgng_path, [1, 11, 0])
|
|
|
|
if old_pkgng:
|
|
|
|
module.fail_json(msg="To use option 'ignore_osver' pkg version must be 1.11 or greater")
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
if p["chroot"] != "":
|
|
|
|
dir_arg = '--chroot %s' % (p["chroot"])
|
|
|
|
|
|
|
|
if p["jail"] != "":
|
|
|
|
dir_arg = '--jail %s' % (p["jail"])
|
|
|
|
|
2020-07-07 23:04:35 +02:00
|
|
|
if pkgs == ['*'] and p["state"] == 'latest':
|
|
|
|
# Operate on all installed packages. Only state: latest makes sense here.
|
|
|
|
_changed, _msg, _stdout, _stderr = upgrade_packages(module, pkgng_path, dir_arg)
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = changed or _changed
|
2020-06-23 19:55:40 +02:00
|
|
|
stdout += _stdout
|
|
|
|
stderr += _stderr
|
2020-03-09 10:11:07 +01:00
|
|
|
msgs.append(_msg)
|
|
|
|
|
2020-07-07 23:04:35 +02:00
|
|
|
# Operate on named packages
|
|
|
|
named_packages = [pkg for pkg in pkgs if pkg != '*']
|
|
|
|
if p["state"] in ("present", "latest") and named_packages:
|
2020-11-17 07:17:16 +01:00
|
|
|
_changed, _msg, _out, _err = install_packages(module, pkgng_path, named_packages,
|
|
|
|
p["cached"], p["pkgsite"], dir_arg,
|
|
|
|
p["state"], p["ignore_osver"])
|
2020-07-07 23:04:35 +02:00
|
|
|
stdout += _out
|
|
|
|
stderr += _err
|
|
|
|
changed = changed or _changed
|
|
|
|
msgs.append(_msg)
|
|
|
|
|
|
|
|
elif p["state"] == "absent" and named_packages:
|
|
|
|
_changed, _msg, _out, _err = remove_packages(module, pkgng_path, named_packages, dir_arg)
|
|
|
|
stdout += _out
|
|
|
|
stderr += _err
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = changed or _changed
|
|
|
|
msgs.append(_msg)
|
|
|
|
|
|
|
|
if p["autoremove"]:
|
2020-06-23 19:55:40 +02:00
|
|
|
_changed, _msg, _stdout, _stderr = autoremove_packages(module, pkgng_path, dir_arg)
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = changed or _changed
|
2020-06-23 19:55:40 +02:00
|
|
|
stdout += _stdout
|
|
|
|
stderr += _stderr
|
2020-03-09 10:11:07 +01:00
|
|
|
msgs.append(_msg)
|
|
|
|
|
|
|
|
if p["annotation"]:
|
|
|
|
_changed, _msg = annotate_packages(module, pkgng_path, pkgs, p["annotation"], dir_arg)
|
|
|
|
changed = changed or _changed
|
|
|
|
msgs.append(_msg)
|
|
|
|
|
2020-06-23 19:55:40 +02:00
|
|
|
module.exit_json(changed=changed, msg=", ".join(msgs), stdout=stdout, stderr=stderr)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|