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)

* 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>
This commit is contained in:
Aoife Finch 2021-10-10 21:55:09 -07:00 committed by GitHub
parent 905f4dcfa2
commit 8ece0d3609
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
msg = "Macports already up-to-date"
return (changed, msg)
return (changed, msg, out, err)
else:
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.":
changed = False
msg = "Ports already upgraded"
return (changed, msg)
return (changed, msg, out, err)
elif rc == 0:
changed = True
msg = "Outdated ports upgraded successfully"
return (changed, msg)
return (changed, msg, out, err)
else:
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
def remove_ports(module, port_path, ports):
def remove_ports(module, port_path, ports, stdout, stderr):
""" Uninstalls one or more ports if installed. """
remove_c = 0
@ -176,20 +176,21 @@ def remove_ports(module, port_path, ports):
continue
rc, out, err = module.run_command("%s uninstall %s" % (port_path, port))
stdout += out
stderr += err
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
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. """
install_c = 0
@ -199,66 +200,70 @@ def install_ports(module, port_path, ports, variant):
continue
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):
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
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_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))
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"):
continue
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"):
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
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. """
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))
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"):
continue
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"):
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
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():
@ -272,35 +277,42 @@ def main():
)
)
stdout = ""
stderr = ""
port_path = module.get_bin_path('port', True, ['/opt/local/bin'])
p = module.params
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"]):
module.exit_json(changed=changed, msg=msg)
module.exit_json(changed=changed, msg=msg, stdout=stdout, stderr=stderr)
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"]:
module.exit_json(changed=changed, msg=msg)
module.exit_json(changed=changed, msg=msg, stdout=stdout, stderr=stderr)
pkgs = p["name"]
variant = p["variant"]
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"]:
remove_ports(module, port_path, pkgs)
remove_ports(module, port_path, pkgs, stdout, stderr)
elif p["state"] == "active":
activate_ports(module, port_path, pkgs)
activate_ports(module, port_path, pkgs, stdout, stderr)
elif p["state"] == "inactive":
deactivate_ports(module, port_path, pkgs)
deactivate_ports(module, port_path, pkgs, stdout, stderr)
if __name__ == '__main__':