From a37745ceb4ed86c7eba3daf2a20076f0bbd9b5a4 Mon Sep 17 00:00:00 2001 From: Davide Principi Date: Fri, 9 Feb 2024 17:58:24 +0100 Subject: [PATCH] Iterate only over available updates - Do not iterate over all modules, use the core library function to get the list of modules that can be updated, as done by the `list-updates` action. - Fix the task progress logic to produce a value between 0 and 100. --- .../cluster/actions/update-modules/50update | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/core/imageroot/var/lib/nethserver/cluster/actions/update-modules/50update b/core/imageroot/var/lib/nethserver/cluster/actions/update-modules/50update index cafb51f796..44dddc4098 100755 --- a/core/imageroot/var/lib/nethserver/cluster/actions/update-modules/50update +++ b/core/imageroot/var/lib/nethserver/cluster/actions/update-modules/50update @@ -24,35 +24,31 @@ import agent import sys import os import agent.tasks -import cluster.modules as lmods +import cluster.modules -rdb = agent.redis_connect() +rdb = agent.redis_connect(privileged=True) +updates = cluster.modules.list_updates(rdb, skip_core_modules = True) -modules = [] -for mf in rdb.scan_iter('module/*/flags'): - flags = rdb.smembers(mf) - if 'core_module' in flags: - continue +if len(updates) == 0: + print(agent.SD_INFO, "No updates available for the installed modules", file=sys.stderr) + sys.exit(0) - module_id = mf.removeprefix('module/').removesuffix('/flags') - modules.append(module_id) - -step = len(modules) -start = 5 +ucnt = len(updates) +ustep = 100 / ucnt +uidx = 0 errors = 0 -for module in modules: - image_name = agent.get_image_name_from_url(rdb.hget(f'module/{module}/environment', 'IMAGE_URL')) - try: - image_url = lmods.get_latest_module(image_name, rdb) - except lmods.LatestModuleLookupError: - continue +for oupdate in updates: + module = oupdate['id'] + image_url = f"{oupdate['source']}:{oupdate['update']}" + start = int(uidx * ustep) + end = start + max(ustep, 1) upd_module_result = agent.tasks.run("cluster", "update-module", data={"instances": [module], "image_url": image_url}, endpoint="redis://cluster-leader", - progress_callback=agent.get_progress_callback(start, start + step) + progress_callback=agent.get_progress_callback(start, end) ) - start = start + step + 1 + uidx += 1 if upd_module_result['exit_code'] != 0: print(agent.SD_ERR + f"Failed update-module for instance {module} from image {image_url}", file=sys.stderr) errors += 1