From cf87e1d9b2ae87f433ad5f800aca26a46f595cf1 Mon Sep 17 00:00:00 2001 From: Kevin Brebanov Date: Sun, 7 Aug 2016 15:43:40 -0400 Subject: [PATCH] apk: Add support for updating virtual packages (#2686) Fixes: #2389 --- .../modules/extras/packaging/os/apk.py | 58 ++++++++++++++----- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/lib/ansible/modules/extras/packaging/os/apk.py b/lib/ansible/modules/extras/packaging/os/apk.py index 191f3b39b09..120fc981382 100644 --- a/lib/ansible/modules/extras/packaging/os/apk.py +++ b/lib/ansible/modules/extras/packaging/os/apk.py @@ -42,7 +42,7 @@ options: choices: [ "present", "absent", "latest" ] update_cache: description: - - Update repository indexes. Can be run with other steps or on it's own. + - Update repository indexes. Can be run with other steps or on it's own. required: false default: no choices: [ "yes", "no" ] @@ -114,6 +114,23 @@ def query_latest(module, name): return False return True +def query_virtual(module, name): + cmd = "%s -v info --description %s" % (APK_PATH, name) + rc, stdout, stderr = module.run_command(cmd, check_rc=False) + search_pattern = "^%s: virtual meta package" % (name) + if re.search(search_pattern, stdout): + return True + return False + +def get_dependencies(module, name): + cmd = "%s -v info --depends %s" % (APK_PATH, name) + rc, stdout, stderr = module.run_command(cmd, check_rc=False) + dependencies = stdout.split() + if len(dependencies) > 1: + return dependencies[1:] + else: + return [] + def upgrade_packages(module): if module.check_mode: cmd = "%s upgrade --simulate" % (APK_PATH) @@ -128,29 +145,40 @@ def upgrade_packages(module): def install_packages(module, names, state): upgrade = False - uninstalled = [] + to_install = [] + to_upgrade = [] for name in names: - if not query_package(module, name): - uninstalled.append(name) - elif state == 'latest' and not query_latest(module, name): - upgrade = True - if not uninstalled and not upgrade: + # Check if virtual package + if query_virtual(module, name): + # Get virtual package dependencies + dependencies = get_dependencies(module, name) + for dependency in dependencies: + if state == 'latest' and not query_latest(module, dependency): + to_upgrade.append(dependency) + else: + if not query_package(module, name): + to_install.append(name) + elif state == 'latest' and not query_latest(module, name): + to_upgrade.append(name) + if to_upgrade: + upgrade = True + if not to_install and not upgrade: module.exit_json(changed=False, msg="package(s) already installed") - names = " ".join(uninstalled) + packages = " ".join(to_install) + " ".join(to_upgrade) if upgrade: if module.check_mode: - cmd = "%s add --upgrade --simulate %s" % (APK_PATH, names) + cmd = "%s add --upgrade --simulate %s" % (APK_PATH, packages) else: - cmd = "%s add --upgrade %s" % (APK_PATH, names) + cmd = "%s add --upgrade %s" % (APK_PATH, packages) else: if module.check_mode: - cmd = "%s add --simulate %s" % (APK_PATH, names) + cmd = "%s add --simulate %s" % (APK_PATH, packages) else: - cmd = "%s add %s" % (APK_PATH, names) + cmd = "%s add %s" % (APK_PATH, packages) rc, stdout, stderr = module.run_command(cmd, check_rc=False) if rc != 0: - module.fail_json(msg="failed to install %s" % (names)) - module.exit_json(changed=True, msg="installed %s package(s)" % (names)) + module.fail_json(msg="failed to install %s" % (packages)) + module.exit_json(changed=True, msg="installed %s package(s)" % (packages)) def remove_packages(module, names): installed = [] @@ -168,7 +196,7 @@ def remove_packages(module, names): if rc != 0: module.fail_json(msg="failed to remove %s package(s)" % (names)) module.exit_json(changed=True, msg="removed %s package(s)" % (names)) - + # ========================================== # Main control flow.