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

macports: add stdout and stderr to status (#3499) (#3500) (#3543)

* macports: add stdout and stderr to status (#3499)

* Add changelog fragment

* Update changelogs/fragments/3500-macports-add-stdout-and-stderr-to-status.yaml

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Aoife Finch <aoife@finch.ink>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 8ece0d3609)

Co-authored-by: Aoife Finch <aoife.github@finch.ink>
This commit is contained in:
patchback[bot] 2021-10-11 07:26:17 +02:00 committed by GitHub
parent f898279c8c
commit 5b9b99384f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 32 deletions

View file

@ -0,0 +1,3 @@
bugfixes:
- macports - add ``stdout`` and ``stderr`` to return values
(https://github.com/ansible-collections/community.general/issues/3499).

View file

@ -120,7 +120,7 @@ def selfupdate(module, port_path):
changed = False changed = False
msg = "Macports already up-to-date" msg = "Macports already up-to-date"
return (changed, msg) return (changed, msg, out, err)
else: else:
module.fail_json(msg="Failed to update Macports", stdout=out, stderr=err) module.fail_json(msg="Failed to update Macports", stdout=out, stderr=err)
@ -134,11 +134,11 @@ def upgrade(module, port_path):
if out.strip() == "Nothing to upgrade.": if out.strip() == "Nothing to upgrade.":
changed = False changed = False
msg = "Ports already upgraded" msg = "Ports already upgraded"
return (changed, msg) return (changed, msg, out, err)
elif rc == 0: elif rc == 0:
changed = True changed = True
msg = "Outdated ports upgraded successfully" msg = "Outdated ports upgraded successfully"
return (changed, msg) return (changed, msg, out, err)
else: else:
module.fail_json(msg="Failed to upgrade outdated ports", stdout=out, stderr=err) module.fail_json(msg="Failed to upgrade outdated ports", stdout=out, stderr=err)
@ -165,7 +165,7 @@ def query_port(module, port_path, name, state="present"):
return False return False
def remove_ports(module, port_path, ports): def remove_ports(module, port_path, ports, stdout, stderr):
""" Uninstalls one or more ports if installed. """ """ Uninstalls one or more ports if installed. """
remove_c = 0 remove_c = 0
@ -176,20 +176,21 @@ def remove_ports(module, port_path, ports):
continue continue
rc, out, err = module.run_command("%s uninstall %s" % (port_path, port)) rc, out, err = module.run_command("%s uninstall %s" % (port_path, port))
stdout += out
stderr += err
if query_port(module, port_path, port): if query_port(module, port_path, port):
module.fail_json(msg="Failed to remove %s: %s" % (port, err)) module.fail_json(msg="Failed to remove %s: %s" % (port, err), stdout=stdout, stderr=stderr)
remove_c += 1 remove_c += 1
if remove_c > 0: if remove_c > 0:
module.exit_json(changed=True, msg="Removed %s port(s)" % remove_c) module.exit_json(changed=True, msg="Removed %s port(s)" % remove_c, stdout=stdout, stderr=stderr)
module.exit_json(changed=False, msg="Port(s) already absent") module.exit_json(changed=False, msg="Port(s) already absent", stdout=stdout, stderr=stderr)
def install_ports(module, port_path, ports, variant): def install_ports(module, port_path, ports, variant, stdout, stderr):
""" Installs one or more ports if not already installed. """ """ Installs one or more ports if not already installed. """
install_c = 0 install_c = 0
@ -199,66 +200,70 @@ def install_ports(module, port_path, ports, variant):
continue continue
rc, out, err = module.run_command("%s install %s %s" % (port_path, port, variant)) rc, out, err = module.run_command("%s install %s %s" % (port_path, port, variant))
stdout += out
stderr += err
if not query_port(module, port_path, port): if not query_port(module, port_path, port):
module.fail_json(msg="Failed to install %s: %s" % (port, err)) module.fail_json(msg="Failed to install %s: %s" % (port, err), stdout=stdout, stderr=stderr)
install_c += 1 install_c += 1
if install_c > 0: if install_c > 0:
module.exit_json(changed=True, msg="Installed %s port(s)" % (install_c)) module.exit_json(changed=True, msg="Installed %s port(s)" % (install_c), stdout=stdout, stderr=stderr)
module.exit_json(changed=False, msg="Port(s) already present") module.exit_json(changed=False, msg="Port(s) already present", stdout=stdout, stderr=stderr)
def activate_ports(module, port_path, ports): def activate_ports(module, port_path, ports, stdout, stderr):
""" Activate a port if it's inactive. """ """ Activate a port if it's inactive. """
activate_c = 0 activate_c = 0
for port in ports: for port in ports:
if not query_port(module, port_path, port): if not query_port(module, port_path, port):
module.fail_json(msg="Failed to activate %s, port(s) not present" % (port)) module.fail_json(msg="Failed to activate %s, port(s) not present" % (port), stdout=stdout, stderr=stderr)
if query_port(module, port_path, port, state="active"): if query_port(module, port_path, port, state="active"):
continue continue
rc, out, err = module.run_command("%s activate %s" % (port_path, port)) rc, out, err = module.run_command("%s activate %s" % (port_path, port))
stdout += out
stderr += err
if not query_port(module, port_path, port, state="active"): if not query_port(module, port_path, port, state="active"):
module.fail_json(msg="Failed to activate %s: %s" % (port, err)) module.fail_json(msg="Failed to activate %s: %s" % (port, err), stdout=stdout, stderr=stderr)
activate_c += 1 activate_c += 1
if activate_c > 0: if activate_c > 0:
module.exit_json(changed=True, msg="Activated %s port(s)" % (activate_c)) module.exit_json(changed=True, msg="Activated %s port(s)" % (activate_c), stdout=stdout, stderr=stderr)
module.exit_json(changed=False, msg="Port(s) already active") module.exit_json(changed=False, msg="Port(s) already active", stdout=stdout, stderr=stderr)
def deactivate_ports(module, port_path, ports): def deactivate_ports(module, port_path, ports, stdout, stderr):
""" Deactivate a port if it's active. """ """ Deactivate a port if it's active. """
deactivated_c = 0 deactivated_c = 0
for port in ports: for port in ports:
if not query_port(module, port_path, port): if not query_port(module, port_path, port):
module.fail_json(msg="Failed to deactivate %s, port(s) not present" % (port)) module.fail_json(msg="Failed to deactivate %s, port(s) not present" % (port), stdout=stdout, stderr=stderr)
if not query_port(module, port_path, port, state="active"): if not query_port(module, port_path, port, state="active"):
continue continue
rc, out, err = module.run_command("%s deactivate %s" % (port_path, port)) rc, out, err = module.run_command("%s deactivate %s" % (port_path, port))
stdout += out
stderr += err
if query_port(module, port_path, port, state="active"): if query_port(module, port_path, port, state="active"):
module.fail_json(msg="Failed to deactivate %s: %s" % (port, err)) module.fail_json(msg="Failed to deactivate %s: %s" % (port, err), stdout=stdout, stderr=stderr)
deactivated_c += 1 deactivated_c += 1
if deactivated_c > 0: if deactivated_c > 0:
module.exit_json(changed=True, msg="Deactivated %s port(s)" % (deactivated_c)) module.exit_json(changed=True, msg="Deactivated %s port(s)" % (deactivated_c), stdout=stdout, stderr=stderr)
module.exit_json(changed=False, msg="Port(s) already inactive") module.exit_json(changed=False, msg="Port(s) already inactive", stdout=stdout, stderr=stderr)
def main(): def main():
@ -272,35 +277,42 @@ def main():
) )
) )
stdout = ""
stderr = ""
port_path = module.get_bin_path('port', True, ['/opt/local/bin']) port_path = module.get_bin_path('port', True, ['/opt/local/bin'])
p = module.params p = module.params
if p["selfupdate"]: if p["selfupdate"]:
(changed, msg) = selfupdate(module, port_path) (changed, msg, out, err) = selfupdate(module, port_path)
stdout += out
stderr += err
if not (p["name"] or p["upgrade"]): if not (p["name"] or p["upgrade"]):
module.exit_json(changed=changed, msg=msg) module.exit_json(changed=changed, msg=msg, stdout=stdout, stderr=stderr)
if p["upgrade"]: if p["upgrade"]:
(changed, msg) = upgrade(module, port_path) (changed, msg, out, err) = upgrade(module, port_path)
stdout += out
stderr += err
if not p["name"]: if not p["name"]:
module.exit_json(changed=changed, msg=msg) module.exit_json(changed=changed, msg=msg, stdout=stdout, stderr=stderr)
pkgs = p["name"] pkgs = p["name"]
variant = p["variant"] variant = p["variant"]
if p["state"] in ["present", "installed"]: if p["state"] in ["present", "installed"]:
install_ports(module, port_path, pkgs, variant) install_ports(module, port_path, pkgs, variant, stdout, stderr)
elif p["state"] in ["absent", "removed"]: elif p["state"] in ["absent", "removed"]:
remove_ports(module, port_path, pkgs) remove_ports(module, port_path, pkgs, stdout, stderr)
elif p["state"] == "active": elif p["state"] == "active":
activate_ports(module, port_path, pkgs) activate_ports(module, port_path, pkgs, stdout, stderr)
elif p["state"] == "inactive": elif p["state"] == "inactive":
deactivate_ports(module, port_path, pkgs) deactivate_ports(module, port_path, pkgs, stdout, stderr)
if __name__ == '__main__': if __name__ == '__main__':