From bd4c85338a5a8e3807bfbecb2ff28fecce34b7f2 Mon Sep 17 00:00:00 2001 From: Zsolt Ero Date: Thu, 12 Sep 2024 16:42:03 +0200 Subject: [PATCH] implemented relaxed mode for loadbalancer --- modules/http_host/http_host_lib/versions.py | 2 +- .../loadbalancer_lib/loadbalance.py | 21 ++++++++++++++++--- modules/tile_gen/tile_gen_lib/shared.py | 20 +++++++++++++++--- ssh_lib/tasks.py | 1 - 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/modules/http_host/http_host_lib/versions.py b/modules/http_host/http_host_lib/versions.py index 0d6c025..b1737a5 100644 --- a/modules/http_host/http_host_lib/versions.py +++ b/modules/http_host/http_host_lib/versions.py @@ -19,7 +19,7 @@ def fetch_version_files() -> bool: need_nginx_sync = False for area in config.areas: - deployed_version = get_deployed_version(area) + deployed_version = get_deployed_version(area)['version'] if not deployed_version: print(f' deployed version not found: {area}') continue diff --git a/modules/loadbalancer/loadbalancer_lib/loadbalance.py b/modules/loadbalancer/loadbalancer_lib/loadbalance.py index 2ff089d..48e4081 100644 --- a/modules/loadbalancer/loadbalancer_lib/loadbalance.py +++ b/modules/loadbalancer/loadbalancer_lib/loadbalance.py @@ -1,6 +1,8 @@ +from datetime import datetime, timedelta, timezone + from loadbalancer_lib.cloudflare import get_zone_id, set_records_round_robin from loadbalancer_lib.config import config -from loadbalancer_lib.shared import check_host_latest, get_deployed_version +from loadbalancer_lib.shared import check_host_latest, check_host_version, get_deployed_version from loadbalancer_lib.telegram_ import telegram_send_message @@ -46,18 +48,31 @@ def check_or_fix(fix=False): def run_area(area): - version = get_deployed_version(area) + deployed_data = get_deployed_version(area) + version = deployed_data['version'] + last_modified = deployed_data['last_modified'] + if not version: print(f' deployed version not found: {area}') return print(f' deployed version {area}: {version}') + # using relaxed mode for while the servers are still deploying + now = datetime.now(timezone.utc) + relaxed_mode = last_modified > now - timedelta(minutes=2) + results = {} for host_ip in config.http_host_list: try: - check_host_latest(config.domain_ledns, host_ip, area, version) + # don't check latest + if relaxed_mode: + print('using relaxed mode') + check_host_version(config.domain_ledns, host_ip, area, version) + else: + check_host_latest(config.domain_ledns, host_ip, area, version) + results[host_ip] = True except Exception as e: results[host_ip] = False diff --git a/modules/tile_gen/tile_gen_lib/shared.py b/modules/tile_gen/tile_gen_lib/shared.py index 84b9f58..c2f05f8 100644 --- a/modules/tile_gen/tile_gen_lib/shared.py +++ b/modules/tile_gen/tile_gen_lib/shared.py @@ -1,4 +1,5 @@ import json +from datetime import datetime, timezone from io import BytesIO from pathlib import Path @@ -27,11 +28,24 @@ def get_versions_for_area(area: str) -> list: return sorted(versions) -def get_deployed_version(area: str) -> str: +def get_deployed_version(area: str) -> dict: r = requests.get(f'https://assets.openfreemap.com/deployed_versions/{area}.txt', timeout=30) r.raise_for_status() - remote_version = r.text.strip() - return remote_version + version = r.text.strip() + + last_modified_str = r.headers.get('Last-Modified') + last_modified = parse_http_last_modified(last_modified_str) + + return dict( + version=version, + last_modified=last_modified, + ) + + +def parse_http_last_modified(date_string) -> datetime: + parsed_date = datetime.strptime(date_string, '%a, %d %b %Y %H:%M:%S GMT') + parsed_date = parsed_date.replace(tzinfo=timezone.utc) + return parsed_date def check_host_version(domain, host_ip, area, version): diff --git a/ssh_lib/tasks.py b/ssh_lib/tasks.py index aaf1a12..b834945 100644 --- a/ssh_lib/tasks.py +++ b/ssh_lib/tasks.py @@ -29,7 +29,6 @@ def prepare_shared(c): pkg_base(c) rclone(c) - c.sudo(f'rm -rf {REMOTE_CONFIG}') c.sudo(f'mkdir -p {REMOTE_CONFIG}') c.sudo(f'chown ofm:ofm {REMOTE_CONFIG}') c.sudo(f'chown ofm:ofm {OFM_DIR}')