Skip to content

Commit

Permalink
Refactor of list_core_modules()
Browse files Browse the repository at this point in the history
The list is built starting from installed instances (instead of comparing
available instances from repositories to what is installed).

This is necessary to always capture the "core" module as installed.
  • Loading branch information
DavidePrincipi committed Jun 14, 2024
1 parent 3414c43 commit baae9a6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 44 deletions.
72 changes: 30 additions & 42 deletions core/imageroot/usr/local/agent/pypkg/cluster/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,47 +357,35 @@ def list_updates(rdb, skip_core_modules = False):

return updates

def list_core_modules():
rdb = agent.redis_connect(privileged=True)
modules = []
installed = list_installed_core(rdb)
def list_core_modules(rdb):
"""List core modules and if they can be updated."""
core_modules = {}
available = list_available(rdb, skip_core_modules = False)

for module in available:
if not 'core' in module['categories']:
continue

if module["source"] not in installed.keys():
continue

newest_version = None
for version in module["versions"]:
v = semver.VersionInfo.parse(version["tag"])
# Skip testing versions if testing is disabled
testing = rdb.hget(f'cluster/repository/{module["repository"]}', 'testing')
if int(testing) == 0 and not v.prerelease is None:
continue

newest_version = version["tag"]
break

# Handle multiple instances of the same module
instances = []
for instance in installed[module["source"]]:
del(instance['module'])
instance["update"] = ""
instances.append(instance)
try:
cur = semver.VersionInfo.parse(instance["version"])
except:
# skip installed instanced with dev version
continue

# Version are already sorted
# First match is the newest release
if v > cur:
instance["update"] = version["tag"]

modules.append({"name": module['name'], "instances": instances})

return modules
def _calc_update(image_name, cur):
# Lookup module information from repositories
for module in available:
if module["id"] == image_name:
break
else:
return ""
try:
vupdate = module["versions"][0]['tag']
vinfo = semver.VersionInfo.parse(vupdate)
if vupdate > cur:
return vupdate
except:
pass
return ""

for module_source, instances in list_installed_core(rdb).items():
_, image_name = module_source.rsplit("/", 1)
core_modules.setdefault(image_name, {"name": image_name, "instances": []})
for instance in instances:
core_modules[image_name]['instances'].append({
"id": instance["id"],
"version": instance["version"],
"update": _calc_update(image_name, instance["version"]),
})

return list(core_modules.values())
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
import sys
import json
import cluster.modules
import agent

json.dump(cluster.modules.list_core_modules(), fp=sys.stdout)
rdb = agent.redis_connect(privileged=True)

json.dump(cluster.modules.list_core_modules(rdb), fp=sys.stdout)
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ rdb = agent.redis_connect(privileged=True)

# Update all core modules
instances = dict()
for oimage in cluster.modules.list_core_modules():
for oimage in cluster.modules.list_core_modules(rdb):
image_id = oimage['name']
if image_id == 'core':
continue # skip core: it is handled by another action step
Expand Down

0 comments on commit baae9a6

Please sign in to comment.