mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Use module.get_bin_path for pkgin module instead of hardcoding paths
This commit is contained in:
parent
d8d1f2cd7a
commit
28cf95e585
1 changed files with 13 additions and 17 deletions
|
@ -55,13 +55,11 @@ import shlex
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
PKGIN_PATH = "/opt/local/bin/pkgin"
|
def query_package(module, pkgin_path, name, state="present"):
|
||||||
|
|
||||||
def query_package(module, name, state="present"):
|
|
||||||
|
|
||||||
if state == "present":
|
if state == "present":
|
||||||
|
|
||||||
rc, out, err = module.run_command("%s list | grep ^%s" % (PKGIN_PATH, name))
|
rc, out, err = module.run_command("%s list | grep ^%s" % (pkgin_path, name))
|
||||||
|
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
return True
|
return True
|
||||||
|
@ -69,18 +67,18 @@ def query_package(module, name, state="present"):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def remove_packages(module, packages):
|
def remove_packages(module, pkgin_path, packages):
|
||||||
|
|
||||||
remove_c = 0
|
remove_c = 0
|
||||||
# Using a for loop incase of error, we can report the package that failed
|
# Using a for loop incase of error, we can report the package that failed
|
||||||
for package in packages:
|
for package in packages:
|
||||||
# Query the package first, to see if we even need to remove
|
# Query the package first, to see if we even need to remove
|
||||||
if not query_package(module, package):
|
if not query_package(module, pkgin_path, package):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
rc, out, err = module.run_command("%s -y remove %s" % (PKGIN_PATH, package))
|
rc, out, err = module.run_command("%s -y remove %s" % (pkgin_path, package))
|
||||||
|
|
||||||
if query_package(module, package):
|
if query_package(module, pkgin_path, package):
|
||||||
module.fail_json(msg="failed to remove %s: %s" % (package, out))
|
module.fail_json(msg="failed to remove %s: %s" % (package, out))
|
||||||
|
|
||||||
remove_c += 1
|
remove_c += 1
|
||||||
|
@ -92,17 +90,17 @@ def remove_packages(module, packages):
|
||||||
module.exit_json(changed=False, msg="package(s) already absent")
|
module.exit_json(changed=False, msg="package(s) already absent")
|
||||||
|
|
||||||
|
|
||||||
def install_packages(module, packages):
|
def install_packages(module, pkgin_path, packages):
|
||||||
|
|
||||||
install_c = 0
|
install_c = 0
|
||||||
|
|
||||||
for package in packages:
|
for package in packages:
|
||||||
if query_package(module, package):
|
if query_package(module, pkgin_path, package):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
rc, out, err = module.run_command("%s -y install %s" % (PKGIN_PATH, package))
|
rc, out, err = module.run_command("%s -y install %s" % (pkgin_path, package))
|
||||||
|
|
||||||
if not query_package(module, package):
|
if not query_package(module, pkgin_path, package):
|
||||||
module.fail_json(msg="failed to install %s: %s" % (package, out))
|
module.fail_json(msg="failed to install %s: %s" % (package, out))
|
||||||
|
|
||||||
install_c += 1
|
install_c += 1
|
||||||
|
@ -120,19 +118,17 @@ def main():
|
||||||
state = dict(default="present", choices=["present","absent"]),
|
state = dict(default="present", choices=["present","absent"]),
|
||||||
name = dict(aliases=["pkg"], required=True)))
|
name = dict(aliases=["pkg"], required=True)))
|
||||||
|
|
||||||
|
pkgin_path = module.get_bin_path('pkgin', True, ['/opt/local/bin'])
|
||||||
if not os.path.exists(PKGIN_PATH):
|
|
||||||
module.fail_json(msg="cannot find pkgin, looking for %s" % (PKGIN_PATH))
|
|
||||||
|
|
||||||
p = module.params
|
p = module.params
|
||||||
|
|
||||||
pkgs = p["name"].split(",")
|
pkgs = p["name"].split(",")
|
||||||
|
|
||||||
if p["state"] == "present":
|
if p["state"] == "present":
|
||||||
install_packages(module, pkgs)
|
install_packages(module, pkgin_path, pkgs)
|
||||||
|
|
||||||
elif p["state"] == "absent":
|
elif p["state"] == "absent":
|
||||||
remove_packages(module, pkgs)
|
remove_packages(module, pkgin_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…
Reference in a new issue