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

Fixes: #2389
This commit is contained in:
Kevin Brebanov 2016-08-07 15:43:40 -04:00 committed by Matt Clay
parent 4289354ce5
commit cf87e1d9b2

View file

@ -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.