Skip to content

Commit

Permalink
Iterate only over available updates
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
DavidePrincipi committed Feb 9, 2024
1 parent 67320ab commit a37745c
Showing 1 changed file with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a37745c

Please sign in to comment.