Merge pull request #2355 from jcftang/macports

Teach the macports module to activate/deactivate packages
pull/2407/merge
Michael DeHaan 12 years ago
commit 917c11ebd7

@ -34,7 +34,7 @@ options:
state: state:
description: description:
- state of the package - state of the package
choices: [ 'present', 'absent' ] choices: [ 'present', 'absent', 'active', 'inactive' ]
required: false required: false
default: present default: present
update_cache: update_cache:
@ -49,7 +49,8 @@ EXAMPLES = '''
macports: name=foo state=present macports: name=foo state=present
macports: name=foo state=present update_cache=yes macports: name=foo state=present update_cache=yes
macports: name=foo state=absent macports: name=foo state=absent
macports: name=foo,bar state=absent macports: name=foo state=active
macports: name=foo state=inactive
''' '''
@ -73,6 +74,14 @@ def query_package(module, port_path, name, state="present"):
return False return False
elif state == "active":
rc, out, err = module.run_command("%s installed %s | grep -q active" % (port_path, name))
if rc == 0:
return True
return False
def remove_packages(module, port_path, packages): def remove_packages(module, port_path, packages):
""" Uninstalls one or more packages if installed. """ """ Uninstalls one or more packages if installed. """
@ -120,11 +129,61 @@ def install_packages(module, port_path, packages):
module.exit_json(changed=False, msg="package(s) already present") module.exit_json(changed=False, msg="package(s) already present")
def activate_packages(module, port_path, packages):
""" Activate a package if it's inactive. """
activate_c = 0
for package in packages:
if not query_package(module, port_path, package):
module.fail_json(msg="failed to activate %s, package(s) not present" % (package))
if query_package(module, port_path, package, state="active"):
continue
rc, out, err = module.run_command("%s activate %s" % (port_path, package))
if not query_package(module, port_path, package, state="active"):
module.fail_json(msg="failed to activate %s: %s" % (package, out))
activate_c += 1
if activate_c > 0:
module.exit_json(changed=True, msg="activated %s package(s)" % (activate_c))
module.exit_json(changed=False, msg="package(s) already active")
def deactivate_packages(module, port_path, packages):
""" Deactivate a package if it's active. """
deactivated_c = 0
for package in packages:
if not query_package(module, port_path, package):
module.fail_json(msg="failed to activate %s, package(s) not present" % (package))
if not query_package(module, port_path, package, state="active"):
continue
rc, out, err = module.run_command("%s deactivate %s" % (port_path, package))
if query_package(module, port_path, package, state="active"):
module.fail_json(msg="failed to deactivated %s: %s" % (package, out))
deactivated_c += 1
if deactivated_c > 0:
module.exit_json(changed=True, msg="deactivated %s package(s)" % (deactivated_c))
module.exit_json(changed=False, msg="package(s) already inactive")
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
name = dict(aliases=["pkg"], required=True), name = dict(aliases=["pkg"], required=True),
state = dict(default="present", choices=["present", "installed", "absent", "removed"]), state = dict(default="present", choices=["present", "installed", "absent", "removed", "active", "inactive"]),
update_cache = dict(default="no", aliases=["update-cache"], type='bool') update_cache = dict(default="no", aliases=["update-cache"], type='bool')
) )
) )
@ -144,6 +203,12 @@ def main():
elif p["state"] in ["absent", "removed"]: elif p["state"] in ["absent", "removed"]:
remove_packages(module, port_path, pkgs) remove_packages(module, port_path, pkgs)
elif p["state"] == "active":
activate_packages(module, port_path, pkgs)
elif p["state"] == "inactive":
deactivate_packages(module, port_path, pkgs)
# this is magic, see lib/ansible/module_common.py # this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>

Loading…
Cancel
Save