| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2013, berenddeboer |
| 4 |
# Written by berenddeboer <[email protected]> |
| 5 |
# Based on pkgng module written by bleader <bleader at ratonland.org> |
| 6 |
# |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: portinstall |
| 14 |
short_description: Installing packages from FreeBSD's ports system |
| 15 |
description: |
| 16 |
- Manage packages for FreeBSD using C(portinstall). |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: none |
| 22 |
diff_mode: |
| 23 |
support: none |
| 24 |
options: |
| 25 |
name: |
| 26 |
description: |
| 27 |
- Name of package to install/remove. |
| 28 |
aliases: [pkg] |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
state: |
| 32 |
description: |
| 33 |
- State of the package. |
| 34 |
choices: ['present', 'absent'] |
| 35 |
default: present |
| 36 |
type: str |
| 37 |
use_packages: |
| 38 |
description: |
| 39 |
- Use packages instead of ports whenever available. |
| 40 |
type: bool |
| 41 |
default: true |
| 42 |
author: "berenddeboer (@berenddeboer)" |
| 43 |
""" |
| 44 |
|
| 45 |
EXAMPLES = r""" |
| 46 |
- name: Install package foo |
| 47 |
community.general.portinstall: |
| 48 |
name: foo |
| 49 |
state: present |
| 50 |
|
| 51 |
- name: Install package security/cyrus-sasl2-saslauthd |
| 52 |
community.general.portinstall: |
| 53 |
name: security/cyrus-sasl2-saslauthd |
| 54 |
state: present |
| 55 |
|
| 56 |
- name: Remove packages foo and bar |
| 57 |
community.general.portinstall: |
| 58 |
name: foo,bar |
| 59 |
state: absent |
| 60 |
""" |
| 61 |
|
| 62 |
import re |
| 63 |
from shlex import quote as shlex_quote |
| 64 |
|
| 65 |
from ansible.module_utils.basic import AnsibleModule |
| 66 |
|
| 67 |
|
| 68 |
def query_package(module, name): |
| 69 |
pkg_info_path = module.get_bin_path("pkg_info", False) |
| 70 |
|
| 71 |
# Assume that if we have pkg_info, we haven't upgraded to pkgng |
| 72 |
if pkg_info_path: |
| 73 |
module.get_bin_path("pkg_glob", True) |
| 74 |
# TODO: convert run_comand() argument to list! |
| 75 |
rc, out, err = module.run_command(f"{pkg_info_path} -e `pkg_glob {shlex_quote(name)}`", use_unsafe_shell=True) |
| 76 |
pkg_info_path = [pkg_info_path] |
| 77 |
else: |
| 78 |
pkg_info_path = [module.get_bin_path("pkg", True), "info"] |
| 79 |
rc, out, err = module.run_command(pkg_info_path + [name]) |
| 80 |
|
| 81 |
found = rc == 0 |
| 82 |
|
| 83 |
if not found: |
| 84 |
# databases/mysql55-client installs as mysql-client, so try solving |
| 85 |
# that the ugly way. Pity FreeBSD doesn't have a fool proof way of checking |
| 86 |
# some package is installed |
| 87 |
name_without_digits = re.sub("[0-9]", "", name) |
| 88 |
if name != name_without_digits: |
| 89 |
rc, out, err = module.run_command(pkg_info_path + [name_without_digits]) |
| 90 |
|
| 91 |
found = rc == 0 |
| 92 |
|
| 93 |
return found |
| 94 |
|
| 95 |
|
| 96 |
def matching_packages(module, name): |
| 97 |
ports_glob_path = module.get_bin_path("ports_glob", True) |
| 98 |
rc, out, err = module.run_command([ports_glob_path, name]) |
| 99 |
# counts the number of packages found |
| 100 |
occurrences = out.count("\n") |
| 101 |
if occurrences == 0: |
| 102 |
name_without_digits = re.sub("[0-9]", "", name) |
| 103 |
if name != name_without_digits: |
| 104 |
rc, out, err = module.run_command([ports_glob_path, name_without_digits]) |
| 105 |
occurrences = out.count("\n") |
| 106 |
return occurrences |
| 107 |
|
| 108 |
|
| 109 |
def remove_packages(module, packages): |
| 110 |
remove_c = 0 |
| 111 |
pkg_glob_path = module.get_bin_path("pkg_glob", True) |
| 112 |
|
| 113 |
# If pkg_delete not found, we assume pkgng |
| 114 |
pkg_delete_path = module.get_bin_path("pkg_delete", False) |
| 115 |
if not pkg_delete_path: |
| 116 |
pkg_delete_path = module.get_bin_path("pkg", True) |
| 117 |
pkg_delete_path = f"{pkg_delete_path} delete -y" |
| 118 |
|
| 119 |
# Using a for loop in case of error, we can report the package that failed |
| 120 |
for package in packages: |
| 121 |
# Query the package first, to see if we even need to remove |
| 122 |
if not query_package(module, package): |
| 123 |
continue |
| 124 |
|
| 125 |
# TODO: convert run_comand() argument to list! |
| 126 |
rc, out, err = module.run_command( |
| 127 |
f"{pkg_delete_path} `{pkg_glob_path} {shlex_quote(package)}`", use_unsafe_shell=True |
| 128 |
) |
| 129 |
|
| 130 |
if query_package(module, package): |
| 131 |
name_without_digits = re.sub("[0-9]", "", package) |
| 132 |
# TODO: convert run_comand() argument to list! |
| 133 |
rc, out, err = module.run_command( |
| 134 |
f"{pkg_delete_path} `{pkg_glob_path} {shlex_quote(name_without_digits)}`", use_unsafe_shell=True |
| 135 |
) |
| 136 |
if query_package(module, package): |
| 137 |
module.fail_json(msg=f"failed to remove {package}: {out}") |
| 138 |
|
| 139 |
remove_c += 1 |
| 140 |
|
| 141 |
if remove_c > 0: |
| 142 |
module.exit_json(changed=True, msg=f"removed {remove_c} package(s)") |
| 143 |
|
| 144 |
module.exit_json(changed=False, msg="package(s) already absent") |
| 145 |
|
| 146 |
|
| 147 |
def install_packages(module, packages, use_packages): |
| 148 |
install_c = 0 |
| 149 |
|
| 150 |
# If portinstall not found, automagically install |
| 151 |
portinstall_path = module.get_bin_path("portinstall", False) |
| 152 |
if not portinstall_path: |
| 153 |
pkg_path = module.get_bin_path("pkg", False) |
| 154 |
if pkg_path: |
| 155 |
module.run_command([pkg_path, "install", "-y", "portupgrade"]) |
| 156 |
portinstall_path = module.get_bin_path("portinstall", True) |
| 157 |
|
| 158 |
if use_packages: |
| 159 |
portinstall_params = ["--use-packages"] |
| 160 |
else: |
| 161 |
portinstall_params = [] |
| 162 |
|
| 163 |
for package in packages: |
| 164 |
if query_package(module, package): |
| 165 |
continue |
| 166 |
|
| 167 |
# TODO: check how many match |
| 168 |
matches = matching_packages(module, package) |
| 169 |
if matches == 1: |
| 170 |
rc, out, err = module.run_command([portinstall_path, "--batch"] + portinstall_params + [package]) |
| 171 |
if not query_package(module, package): |
| 172 |
module.fail_json(msg=f"failed to install {package}: {out}") |
| 173 |
elif matches == 0: |
| 174 |
module.fail_json(msg=f"no matches for package {package}") |
| 175 |
else: |
| 176 |
module.fail_json(msg=f"{matches} matches found for package name {package}") |
| 177 |
|
| 178 |
install_c += 1 |
| 179 |
|
| 180 |
if install_c > 0: |
| 181 |
module.exit_json(changed=True, msg=f"present {install_c} package(s)") |
| 182 |
|
| 183 |
module.exit_json(changed=False, msg="package(s) already present") |
| 184 |
|
| 185 |
|
| 186 |
def main(): |
| 187 |
module = AnsibleModule( |
| 188 |
argument_spec=dict( |
| 189 |
state=dict(default="present", choices=["present", "absent"]), |
| 190 |
name=dict(aliases=["pkg"], required=True), |
| 191 |
use_packages=dict(type="bool", default=True), |
| 192 |
) |
| 193 |
) |
| 194 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 195 |
|
| 196 |
p = module.params |
| 197 |
|
| 198 |
pkgs = p["name"].split(",") |
| 199 |
|
| 200 |
if p["state"] == "present": |
| 201 |
install_packages(module, pkgs, p["use_packages"]) |
| 202 |
|
| 203 |
elif p["state"] == "absent": |
| 204 |
remove_packages(module, pkgs) |
| 205 |
|
| 206 |
|
| 207 |
if __name__ == "__main__": |
| 208 |
main() |