forked from openwrt/asu
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Paul Spooren <[email protected]>
- Loading branch information
Showing
5 changed files
with
103 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
|
||
import redis | ||
|
||
from . import __version__ | ||
|
||
|
||
def get_branch(version): | ||
if version.endswith("-SNAPSHOT"): | ||
|
@@ -381,14 +383,15 @@ def get_targets_upstream(config: dict, version: str) -> list: | |
return list(req.json().keys()) | ||
|
||
|
||
def update_targets(config: dict, branch: dict) -> list: | ||
def update_targets(config: dict, version) -> list: | ||
branch = config["BRANCHES"][get_branch(version)] | ||
version_path = branch["path"].format(version=branch["versions"][0]) | ||
|
||
targets = requests.get( | ||
config["UPSTREAM_URL"] + f"/{version_path}/.targets.json" | ||
).json() | ||
|
||
logging.warning(f"{branch['name']}: Found {len(targets)} targets") | ||
logging.info(f"{branch['name']}: Found {len(targets)} targets") | ||
pipeline = get_redis_client(config).pipeline(True) | ||
pipeline.delete(f"targets:{branch['name']}") | ||
pipeline.hset(f"targets:{branch['name']}", mapping=targets) | ||
|
@@ -407,7 +410,7 @@ def update_profiles(config, version: str, target: str) -> str: | |
""" | ||
branch = config["BRANCHES"][get_branch(version)] | ||
version_path = branch["path"].format(version=version) | ||
logging.info(f"{version}/{target}: Update profiles") | ||
logging.debug(f"{version}/{target}: Update profiles") | ||
|
||
r = get_redis_client(config) | ||
|
||
|
@@ -426,13 +429,13 @@ def update_profiles(config, version: str, target: str) -> str: | |
|
||
metadata = req.json() | ||
profiles = metadata.pop("profiles", {}) | ||
logging.info(f"{version}/{target}: Found {len(profiles)} profiles") | ||
|
||
r.set( | ||
f"revision:{version}:{target}", | ||
metadata["version_code"], | ||
) | ||
logging.info(f"{version}/{target}: Found revision {metadata['version_code']}") | ||
logging.info(f"{version}/{target}: Found {len(profiles)} profiles") | ||
|
||
pipeline = r.pipeline(True) | ||
pipeline.delete(f"profiles:{branch['name']}:{version}:{target}") | ||
|
@@ -472,4 +475,67 @@ def update_profiles(config, version: str, target: str) -> str: | |
data["target"] = target | ||
|
||
pipeline.execute() | ||
return profiles | ||
|
||
|
||
def update_meta_json(config): | ||
latest = list( | ||
map( | ||
lambda b: b["versions"][0], | ||
filter( | ||
lambda b: b.get("enabled"), | ||
config["BRANCHES"].values(), | ||
), | ||
) | ||
) | ||
|
||
branches = dict( | ||
map( | ||
lambda b: ( | ||
b["name"], | ||
{ | ||
**b, | ||
"targets": dict( | ||
map( | ||
lambda a: (a[0].decode(), a[1].decode()), | ||
get_redis_client(config) | ||
.hgetall(f"architecture:{b['name']}") | ||
.items(), | ||
) | ||
), | ||
}, | ||
), | ||
filter( | ||
lambda b: b.get("enabled"), | ||
config["BRANCHES"].values(), | ||
), | ||
) | ||
) | ||
|
||
config["OVERVIEW"] = { | ||
"latest": latest, | ||
"branches": branches, | ||
"server": { | ||
"version": __version__, | ||
"contact": "[email protected]", | ||
"allow_defaults": config["ALLOW_DEFAULTS"], | ||
"repository_allow_list": config["REPOSITORY_ALLOW_LIST"], | ||
}, | ||
} | ||
|
||
config["JSON_PATH"].mkdir(exist_ok=True, parents=True) | ||
|
||
(config["JSON_PATH"] / "overview.json").write_text( | ||
json.dumps(config["OVERVIEW"], indent=2, sort_keys=False, default=str) | ||
) | ||
|
||
(config["JSON_PATH"] / "branches.json").write_text( | ||
json.dumps(list(branches.values()), indent=2, sort_keys=False, default=str) | ||
) | ||
|
||
(config["JSON_PATH"] / "latest.json").write_text(json.dumps({"latest": latest})) | ||
|
||
|
||
def update(config, version: str, target: str): | ||
update_targets(config, version) | ||
update_profiles(config, version, target) | ||
update_meta_json(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,53 @@ | ||
import json | ||
import logging | ||
from datetime import timedelta | ||
|
||
import requests | ||
from flask import Blueprint | ||
from rq import Queue | ||
|
||
from asu import __version__ | ||
from asu.common import get_redis_client, update_profiles, update_targets | ||
from asu.common import ( | ||
get_redis_client, | ||
update_meta_json, | ||
update_profiles, | ||
update_targets, | ||
) | ||
|
||
bp = Blueprint("janitor", __name__) | ||
|
||
session = requests.Session() | ||
|
||
|
||
def update_branch(config, branch): | ||
logging.warning(f"Update {branch['name']}") | ||
logging.debug(f"Update {branch['name']}") | ||
|
||
targets = list(update_targets(config, branch).keys()) | ||
targets = list(update_targets(config, branch["versions"][0]).keys()) | ||
|
||
logging.warning(f"Targets: {targets}") | ||
logging.debug(f"Targets: {targets}") | ||
|
||
if not targets: | ||
logging.warning(f"No targets found for {branch['name']}") | ||
return | ||
|
||
for version in branch["versions"]: | ||
logging.warning(f"Update {branch['name']}/{version}") | ||
logging.info(f"Update {branch['name']}/{version}") | ||
|
||
for target in targets: | ||
logging.warning(f"Update {branch['name']}/{version}/{target}") | ||
logging.info(f"Update {branch['name']}/{version}/{target}") | ||
update_profiles(config, version, target) | ||
|
||
|
||
def update_meta_json(config): | ||
latest = list( | ||
map( | ||
lambda b: b["versions"][0], | ||
filter( | ||
lambda b: b.get("enabled"), | ||
config["BRANCHES"].values(), | ||
), | ||
) | ||
) | ||
|
||
branches = dict( | ||
map( | ||
lambda b: ( | ||
b["name"], | ||
{ | ||
**b, | ||
"targets": dict( | ||
map( | ||
lambda a: (a[0].decode(), a[1].decode()), | ||
get_redis_client(config) | ||
.hgetall(f"architecture:{b['name']}") | ||
.items(), | ||
) | ||
), | ||
}, | ||
), | ||
filter( | ||
lambda b: b.get("enabled"), | ||
config["BRANCHES"].values(), | ||
), | ||
) | ||
) | ||
|
||
config["OVERVIEW"] = { | ||
"latest": latest, | ||
"branches": branches, | ||
"server": { | ||
"version": __version__, | ||
"contact": "[email protected]", | ||
"allow_defaults": config["ALLOW_DEFAULTS"], | ||
"repository_allow_list": config["REPOSITORY_ALLOW_LIST"], | ||
}, | ||
} | ||
|
||
config["JSON_PATH"].mkdir(exist_ok=True, parents=True) | ||
|
||
(config["JSON_PATH"] / "overview.json").write_text( | ||
json.dumps(config["OVERVIEW"], indent=2, sort_keys=False, default=str) | ||
) | ||
|
||
(config["JSON_PATH"] / "branches.json").write_text( | ||
json.dumps(list(branches.values()), indent=2, sort_keys=False, default=str) | ||
) | ||
|
||
(config["JSON_PATH"] / "latest.json").write_text(json.dumps({"latest": latest})) | ||
|
||
|
||
def update(config): | ||
def update_branches(config): | ||
"""Update the data required to run the server | ||
For this all available profiles for all enabled versions is | ||
downloaded and stored in the Redis database. | ||
""" | ||
|
||
if not config["BRANCHES"]: | ||
logging.error("No BRANCHES defined in config, nothing to do, exiting") | ||
return | ||
|
||
try: | ||
if not config["BRANCHES"]: | ||
logging.error("No BRANCHES defined in config, nothing to do, exiting") | ||
return | ||
for branch in config["BRANCHES"].values(): | ||
if not branch.get("enabled"): | ||
logging.info(f"{branch['name']}: Skip disabled branch") | ||
|
@@ -112,12 +57,12 @@ def update(config): | |
update_branch(config, branch) | ||
|
||
update_meta_json(config) | ||
except Exception: | ||
logging.exception("Failed to update") | ||
except Exception as e: | ||
logging.exception(e) | ||
|
||
Queue(connection=get_redis_client(config)).enqueue_in( | ||
timedelta(minutes=10), | ||
update, | ||
timedelta(hours=1), | ||
update_branches, | ||
config, | ||
job_timeout="10m", | ||
job_timeout="15m", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters