1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/plugins/modules/macports.py

320 lines
9.6 KiB
Python
Raw Normal View History

2020-03-09 10:11:07 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2013, Jimmy Tang <jcftang@gmail.com>
2020-03-09 10:11:07 +01:00
# Based on okpg (Patrick Pelletier <pp.pelletier@gmail.com>), pacman
# (Afterburn) and pkgin (Shaun Zinck) modules
#
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
2020-03-09 10:11:07 +01:00
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: macports
author: "Jimmy Tang (@jcftang)"
short_description: Package manager for MacPorts
description:
- Manages MacPorts packages (ports)
options:
name:
description:
- A list of port names.
aliases: ['port']
type: list
elements: str
selfupdate:
description:
- Update Macports and the ports tree, either prior to installing ports or as a separate step.
- Equivalent to running C(port selfupdate).
aliases: ['update_cache', 'update_ports']
default: false
2020-03-09 10:11:07 +01:00
type: bool
state:
description:
- Indicates the desired state of the port.
choices: [ 'present', 'absent', 'active', 'inactive', 'installed', 'removed']
2020-03-09 10:11:07 +01:00
default: present
type: str
2020-03-09 10:11:07 +01:00
upgrade:
description:
- Upgrade all outdated ports, either prior to installing ports or as a separate step.
- Equivalent to running C(port upgrade outdated).
default: false
2020-03-09 10:11:07 +01:00
type: bool
variant:
description:
- A port variant specification.
- 'C(variant) is only supported with state: I(installed)/I(present).'
aliases: ['variants']
type: str
2020-03-09 10:11:07 +01:00
'''
EXAMPLES = '''
- name: Install the foo port
community.general.macports:
2020-03-09 10:11:07 +01:00
name: foo
- name: Install the universal, x11 variant of the foo port
community.general.macports:
2020-03-09 10:11:07 +01:00
name: foo
variant: +universal+x11
- name: Install a list of ports
community.general.macports:
2020-03-09 10:11:07 +01:00
name: "{{ ports }}"
vars:
ports:
- foo
- foo-tools
- name: Update Macports and the ports tree, then upgrade all outdated ports
community.general.macports:
selfupdate: true
upgrade: true
2020-03-09 10:11:07 +01:00
- name: Update Macports and the ports tree, then install the foo port
community.general.macports:
2020-03-09 10:11:07 +01:00
name: foo
selfupdate: true
2020-03-09 10:11:07 +01:00
- name: Remove the foo port
community.general.macports:
2020-03-09 10:11:07 +01:00
name: foo
state: absent
- name: Activate the foo port
community.general.macports:
2020-03-09 10:11:07 +01:00
name: foo
state: active
- name: Deactivate the foo port
community.general.macports:
2020-03-09 10:11:07 +01:00
name: foo
state: inactive
'''
import re
from ansible.module_utils.basic import AnsibleModule
def selfupdate(module, port_path):
""" Update Macports and the ports tree. """
rc, out, err = module.run_command("%s -v selfupdate" % port_path)
if rc == 0:
updated = any(
re.search(r'Total number of ports parsed:\s+[^0]', s.strip()) or
re.search(r'Installing new Macports release', s.strip())
for s in out.split('\n')
if s
)
if updated:
changed = True
msg = "Macports updated successfully"
else:
changed = False
msg = "Macports already up-to-date"
return (changed, msg, out, err)
2020-03-09 10:11:07 +01:00
else:
module.fail_json(msg="Failed to update Macports", stdout=out, stderr=err)
def upgrade(module, port_path):
""" Upgrade outdated ports. """
rc, out, err = module.run_command("%s upgrade outdated" % port_path)
# rc is 1 when nothing to upgrade so check stdout first.
if out.strip() == "Nothing to upgrade.":
changed = False
msg = "Ports already upgraded"
return (changed, msg, out, err)
2020-03-09 10:11:07 +01:00
elif rc == 0:
changed = True
msg = "Outdated ports upgraded successfully"
return (changed, msg, out, err)
2020-03-09 10:11:07 +01:00
else:
module.fail_json(msg="Failed to upgrade outdated ports", stdout=out, stderr=err)
def query_port(module, port_path, name, state="present"):
""" Returns whether a port is installed or not. """
if state == "present":
Fix macports package present/active detection (#1308) * Fix typo in redhat_subscription testcase * Fix macports state=present matching against the wrong package name Previous implementation returned true if the desired package name occurred anywhere in the list of all installed packages. For example as a substring of another package name, or even as a substring of a variant name for a different package. Instead: - request macports only list installed packages matching the desired package name, instead of all installed packages. - Note `port` exits with 0 regardless of whether any packages match the requested name. - pass `-q` flag: "Do not print the header line. This is useful when parsing the output of port installed in scripts for further processing." - eliminate `use_unsafe_shell=True` by searching stdout contents natively in python instead of using `grep`. This has the added benefit of eliminating any potential misinterpretation of characters in the package name as regex special characters. If there are zero matching installed packages, `out` is empty. If there are one or more matches (due to multiple installed versions), the output format is: https://github.com/macports/macports-base/blob/ec8a2bc682c7afbde198aee23a1920557721b508/src/port/port.tcl#L3320-L3323 Notably, two leading spaces, the package name, a space, and then other information. According to blame via github, those lines haven't changed in 11 years. * Update macports state=active to eliminate use_unsafe_shell Similar to previous commit (for macports state=present): - pass `-q` flag: "Do not print the header line. This is useful when parsing the output of port installed in scripts for further processing." - search stdout contents natively in python instead of using `grep`. - added parentheses to search string to eliminate false positives if the package name or variants contain the word `active`. Still could fail if they contain `(active)`, but that's less likely If there are zero matching installed packages, `out` is empty. If there are one or more matches (due to multiple installed versions), the output format is: https://github.com/macports/macports-base/blob/ec8a2bc682c7afbde198aee23a1920557721b508/src/port/port.tcl#L3320-L3323 For "state=active", we're looking for a line that contains `(active)` in the output. * Basic test case of query_port for present and active * Attempt to fix lint errors in test * Different mock module creation, changed test cases indentation/spacing - picked the wrong mock code to cargo-cult. Thanks to felixfontein for this suggestion - 4 space indentation on continuation line. I thought I had that originally, but it looks like my editor sabotaged me with mixed tabs/spaces - Remove leading newline on multi-line test cases. I don't think it would make a difference, but I'd read up on how the python syntax works and want to more accurately represent macports output. fingers crossed this addresses the known build errors * Add changelog fragment * Update tests/unit/plugins/modules/packaging/os/test_macports.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update changelogs/fragments/1307-macports-fix-status-check.yml Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2020-11-26 06:59:32 +01:00
rc, out, err = module.run_command([port_path, "-q", "installed", name])
if rc == 0 and out.strip().startswith(name + " "):
2020-03-09 10:11:07 +01:00
return True
return False
elif state == "active":
Fix macports package present/active detection (#1308) * Fix typo in redhat_subscription testcase * Fix macports state=present matching against the wrong package name Previous implementation returned true if the desired package name occurred anywhere in the list of all installed packages. For example as a substring of another package name, or even as a substring of a variant name for a different package. Instead: - request macports only list installed packages matching the desired package name, instead of all installed packages. - Note `port` exits with 0 regardless of whether any packages match the requested name. - pass `-q` flag: "Do not print the header line. This is useful when parsing the output of port installed in scripts for further processing." - eliminate `use_unsafe_shell=True` by searching stdout contents natively in python instead of using `grep`. This has the added benefit of eliminating any potential misinterpretation of characters in the package name as regex special characters. If there are zero matching installed packages, `out` is empty. If there are one or more matches (due to multiple installed versions), the output format is: https://github.com/macports/macports-base/blob/ec8a2bc682c7afbde198aee23a1920557721b508/src/port/port.tcl#L3320-L3323 Notably, two leading spaces, the package name, a space, and then other information. According to blame via github, those lines haven't changed in 11 years. * Update macports state=active to eliminate use_unsafe_shell Similar to previous commit (for macports state=present): - pass `-q` flag: "Do not print the header line. This is useful when parsing the output of port installed in scripts for further processing." - search stdout contents natively in python instead of using `grep`. - added parentheses to search string to eliminate false positives if the package name or variants contain the word `active`. Still could fail if they contain `(active)`, but that's less likely If there are zero matching installed packages, `out` is empty. If there are one or more matches (due to multiple installed versions), the output format is: https://github.com/macports/macports-base/blob/ec8a2bc682c7afbde198aee23a1920557721b508/src/port/port.tcl#L3320-L3323 For "state=active", we're looking for a line that contains `(active)` in the output. * Basic test case of query_port for present and active * Attempt to fix lint errors in test * Different mock module creation, changed test cases indentation/spacing - picked the wrong mock code to cargo-cult. Thanks to felixfontein for this suggestion - 4 space indentation on continuation line. I thought I had that originally, but it looks like my editor sabotaged me with mixed tabs/spaces - Remove leading newline on multi-line test cases. I don't think it would make a difference, but I'd read up on how the python syntax works and want to more accurately represent macports output. fingers crossed this addresses the known build errors * Add changelog fragment * Update tests/unit/plugins/modules/packaging/os/test_macports.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update changelogs/fragments/1307-macports-fix-status-check.yml Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2020-11-26 06:59:32 +01:00
rc, out, err = module.run_command([port_path, "-q", "installed", name])
2020-03-09 10:11:07 +01:00
Fix macports package present/active detection (#1308) * Fix typo in redhat_subscription testcase * Fix macports state=present matching against the wrong package name Previous implementation returned true if the desired package name occurred anywhere in the list of all installed packages. For example as a substring of another package name, or even as a substring of a variant name for a different package. Instead: - request macports only list installed packages matching the desired package name, instead of all installed packages. - Note `port` exits with 0 regardless of whether any packages match the requested name. - pass `-q` flag: "Do not print the header line. This is useful when parsing the output of port installed in scripts for further processing." - eliminate `use_unsafe_shell=True` by searching stdout contents natively in python instead of using `grep`. This has the added benefit of eliminating any potential misinterpretation of characters in the package name as regex special characters. If there are zero matching installed packages, `out` is empty. If there are one or more matches (due to multiple installed versions), the output format is: https://github.com/macports/macports-base/blob/ec8a2bc682c7afbde198aee23a1920557721b508/src/port/port.tcl#L3320-L3323 Notably, two leading spaces, the package name, a space, and then other information. According to blame via github, those lines haven't changed in 11 years. * Update macports state=active to eliminate use_unsafe_shell Similar to previous commit (for macports state=present): - pass `-q` flag: "Do not print the header line. This is useful when parsing the output of port installed in scripts for further processing." - search stdout contents natively in python instead of using `grep`. - added parentheses to search string to eliminate false positives if the package name or variants contain the word `active`. Still could fail if they contain `(active)`, but that's less likely If there are zero matching installed packages, `out` is empty. If there are one or more matches (due to multiple installed versions), the output format is: https://github.com/macports/macports-base/blob/ec8a2bc682c7afbde198aee23a1920557721b508/src/port/port.tcl#L3320-L3323 For "state=active", we're looking for a line that contains `(active)` in the output. * Basic test case of query_port for present and active * Attempt to fix lint errors in test * Different mock module creation, changed test cases indentation/spacing - picked the wrong mock code to cargo-cult. Thanks to felixfontein for this suggestion - 4 space indentation on continuation line. I thought I had that originally, but it looks like my editor sabotaged me with mixed tabs/spaces - Remove leading newline on multi-line test cases. I don't think it would make a difference, but I'd read up on how the python syntax works and want to more accurately represent macports output. fingers crossed this addresses the known build errors * Add changelog fragment * Update tests/unit/plugins/modules/packaging/os/test_macports.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update changelogs/fragments/1307-macports-fix-status-check.yml Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2020-11-26 06:59:32 +01:00
if rc == 0 and "(active)" in out:
2020-03-09 10:11:07 +01:00
return True
return False
def remove_ports(module, port_path, ports, stdout, stderr):
2020-03-09 10:11:07 +01:00
""" Uninstalls one or more ports if installed. """
remove_c = 0
# Using a for loop in case of error, we can report the port that failed
for port in ports:
# Query the port first, to see if we even need to remove
if not query_port(module, port_path, port):
continue
rc, out, err = module.run_command("%s uninstall %s" % (port_path, port))
stdout += out
stderr += err
2020-03-09 10:11:07 +01:00
if query_port(module, port_path, port):
module.fail_json(msg="Failed to remove %s: %s" % (port, err), stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
remove_c += 1
if remove_c > 0:
module.exit_json(changed=True, msg="Removed %s port(s)" % remove_c, stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
module.exit_json(changed=False, msg="Port(s) already absent", stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
def install_ports(module, port_path, ports, variant, stdout, stderr):
2020-03-09 10:11:07 +01:00
""" Installs one or more ports if not already installed. """
install_c = 0
for port in ports:
if query_port(module, port_path, port):
continue
rc, out, err = module.run_command("%s install %s %s" % (port_path, port, variant))
stdout += out
stderr += err
2020-03-09 10:11:07 +01:00
if not query_port(module, port_path, port):
module.fail_json(msg="Failed to install %s: %s" % (port, err), stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
install_c += 1
if install_c > 0:
module.exit_json(changed=True, msg="Installed %s port(s)" % (install_c), stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
module.exit_json(changed=False, msg="Port(s) already present", stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
def activate_ports(module, port_path, ports, stdout, stderr):
2020-03-09 10:11:07 +01:00
""" Activate a port if it's inactive. """
activate_c = 0
for port in ports:
if not query_port(module, port_path, port):
module.fail_json(msg="Failed to activate %s, port(s) not present" % (port), stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
if query_port(module, port_path, port, state="active"):
continue
rc, out, err = module.run_command("%s activate %s" % (port_path, port))
stdout += out
stderr += err
2020-03-09 10:11:07 +01:00
if not query_port(module, port_path, port, state="active"):
module.fail_json(msg="Failed to activate %s: %s" % (port, err), stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
activate_c += 1
if activate_c > 0:
module.exit_json(changed=True, msg="Activated %s port(s)" % (activate_c), stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
module.exit_json(changed=False, msg="Port(s) already active", stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
def deactivate_ports(module, port_path, ports, stdout, stderr):
2020-03-09 10:11:07 +01:00
""" Deactivate a port if it's active. """
deactivated_c = 0
for port in ports:
if not query_port(module, port_path, port):
module.fail_json(msg="Failed to deactivate %s, port(s) not present" % (port), stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
if not query_port(module, port_path, port, state="active"):
continue
rc, out, err = module.run_command("%s deactivate %s" % (port_path, port))
stdout += out
stderr += err
2020-03-09 10:11:07 +01:00
if query_port(module, port_path, port, state="active"):
module.fail_json(msg="Failed to deactivate %s: %s" % (port, err), stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
deactivated_c += 1
if deactivated_c > 0:
module.exit_json(changed=True, msg="Deactivated %s port(s)" % (deactivated_c), stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
module.exit_json(changed=False, msg="Port(s) already inactive", stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(type='list', elements='str', aliases=["port"]),
selfupdate=dict(aliases=["update_cache", "update_ports"], default=False, type='bool'),
state=dict(default="present", choices=["present", "installed", "absent", "removed", "active", "inactive"]),
upgrade=dict(default=False, type='bool'),
variant=dict(aliases=["variants"], default=None, type='str')
)
)
stdout = ""
stderr = ""
2020-03-09 10:11:07 +01:00
port_path = module.get_bin_path('port', True, ['/opt/local/bin'])
p = module.params
if p["selfupdate"]:
(changed, msg, out, err) = selfupdate(module, port_path)
stdout += out
stderr += err
2020-03-09 10:11:07 +01:00
if not (p["name"] or p["upgrade"]):
module.exit_json(changed=changed, msg=msg, stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
if p["upgrade"]:
(changed, msg, out, err) = upgrade(module, port_path)
stdout += out
stderr += err
2020-03-09 10:11:07 +01:00
if not p["name"]:
module.exit_json(changed=changed, msg=msg, stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
pkgs = p["name"]
variant = p["variant"]
if p["state"] in ["present", "installed"]:
install_ports(module, port_path, pkgs, variant, stdout, stderr)
2020-03-09 10:11:07 +01:00
elif p["state"] in ["absent", "removed"]:
remove_ports(module, port_path, pkgs, stdout, stderr)
2020-03-09 10:11:07 +01:00
elif p["state"] == "active":
activate_ports(module, port_path, pkgs, stdout, stderr)
2020-03-09 10:11:07 +01:00
elif p["state"] == "inactive":
deactivate_ports(module, port_path, pkgs, stdout, stderr)
2020-03-09 10:11:07 +01:00
if __name__ == '__main__':
main()