apk: Add support for updating virtual packages (#2686)

Fixes: #2389
pull/18777/head
Kevin Brebanov 8 years ago committed by Matt Clay
parent 4289354ce5
commit cf87e1d9b2

@ -42,7 +42,7 @@ options:
choices: [ "present", "absent", "latest" ] choices: [ "present", "absent", "latest" ]
update_cache: update_cache:
description: 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 required: false
default: no default: no
choices: [ "yes", "no" ] choices: [ "yes", "no" ]
@ -114,6 +114,23 @@ def query_latest(module, name):
return False return False
return True 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): def upgrade_packages(module):
if module.check_mode: if module.check_mode:
cmd = "%s upgrade --simulate" % (APK_PATH) cmd = "%s upgrade --simulate" % (APK_PATH)
@ -128,29 +145,40 @@ def upgrade_packages(module):
def install_packages(module, names, state): def install_packages(module, names, state):
upgrade = False upgrade = False
uninstalled = [] to_install = []
to_upgrade = []
for name in names: for name in names:
if not query_package(module, name): # Check if virtual package
uninstalled.append(name) if query_virtual(module, name):
elif state == 'latest' and not query_latest(module, name): # Get virtual package dependencies
upgrade = True dependencies = get_dependencies(module, name)
if not uninstalled and not upgrade: 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") module.exit_json(changed=False, msg="package(s) already installed")
names = " ".join(uninstalled) packages = " ".join(to_install) + " ".join(to_upgrade)
if upgrade: if upgrade:
if module.check_mode: if module.check_mode:
cmd = "%s add --upgrade --simulate %s" % (APK_PATH, names) cmd = "%s add --upgrade --simulate %s" % (APK_PATH, packages)
else: else:
cmd = "%s add --upgrade %s" % (APK_PATH, names) cmd = "%s add --upgrade %s" % (APK_PATH, packages)
else: else:
if module.check_mode: if module.check_mode:
cmd = "%s add --simulate %s" % (APK_PATH, names) cmd = "%s add --simulate %s" % (APK_PATH, packages)
else: 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) rc, stdout, stderr = module.run_command(cmd, check_rc=False)
if rc != 0: if rc != 0:
module.fail_json(msg="failed to install %s" % (names)) module.fail_json(msg="failed to install %s" % (packages))
module.exit_json(changed=True, msg="installed %s package(s)" % (names)) module.exit_json(changed=True, msg="installed %s package(s)" % (packages))
def remove_packages(module, names): def remove_packages(module, names):
installed = [] installed = []
@ -168,7 +196,7 @@ def remove_packages(module, names):
if rc != 0: if rc != 0:
module.fail_json(msg="failed to remove %s package(s)" % (names)) module.fail_json(msg="failed to remove %s package(s)" % (names))
module.exit_json(changed=True, msg="removed %s package(s)" % (names)) module.exit_json(changed=True, msg="removed %s package(s)" % (names))
# ========================================== # ==========================================
# Main control flow. # Main control flow.

Loading…
Cancel
Save