From bd7f5fa740b17dd9c33ebb70670baab79459264c Mon Sep 17 00:00:00 2001 From: Zsolt Ero Date: Mon, 28 Oct 2024 12:33:53 +0100 Subject: [PATCH] feat(deploy): add deploy-sync script for production environment setup feat(init-server.py): implement http_host_sync command for server initialization refactor(http_host_lib): enhance asset downloading functions to return status of changes made --- deploy-sync.sh | 7 +++++++ init-server.py | 10 +++++++++ modules/http_host/http_host_lib/assets.py | 25 ++++++++++++++++------- modules/http_host/http_host_lib/sync.py | 5 ++--- 4 files changed, 37 insertions(+), 10 deletions(-) create mode 100755 deploy-sync.sh diff --git a/deploy-sync.sh b/deploy-sync.sh new file mode 100755 index 0000000..b1ee970 --- /dev/null +++ b/deploy-sync.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +export ENV=prod + +./init-server.py http-host-sync ofm-h-fi-1 -y +./init-server.py http-host-sync ofm-h-de-2 -y + diff --git a/init-server.py b/init-server.py index b186b8a..b9c9514 100755 --- a/init-server.py +++ b/init-server.py @@ -125,6 +125,16 @@ def loadbalancer(hostname, user, port, noninteractive): setup_loadbalancer(c) +@cli.command() +@common_options +def http_host_sync(hostname, user, port, noninteractive): + if not noninteractive and not click.confirm(f'Run script on {hostname}?'): + return + + c = get_connection(hostname, user, port) + run_http_host_sync(c) + + @cli.command() @common_options def debug(hostname, user, port, noninteractive): diff --git a/modules/http_host/http_host_lib/assets.py b/modules/http_host/http_host_lib/assets.py index 6ef0d4c..01c425d 100644 --- a/modules/http_host/http_host_lib/assets.py +++ b/modules/http_host/http_host_lib/assets.py @@ -7,21 +7,26 @@ from http_host_lib.config import config from http_host_lib.utils import download_file_aria2, download_if_size_differs -def download_assets(): +def download_assets() -> bool: """ Downloads and extracts assets """ - download_and_extract_asset_tar_gz('fonts') - download_and_extract_asset_tar_gz('styles') - download_and_extract_asset_tar_gz('natural_earth') + changed = False - download_sprites() + changed += download_and_extract_asset_tar_gz('fonts') + changed += download_and_extract_asset_tar_gz('styles') + changed += download_and_extract_asset_tar_gz('natural_earth') + + changed += download_sprites() + + return changed def download_and_extract_asset_tar_gz(asset_kind): """ Download and extract asset.tgz if the file size differ or not available locally + Returns True if modified """ print(f'Downloading asset {asset_kind}') @@ -33,7 +38,7 @@ def download_and_extract_asset_tar_gz(asset_kind): local_file = asset_dir / 'ofm.tar.gz' if not download_if_size_differs(url, local_file): print(f' skipping asset: {asset_kind}') - return + return False ofm_dir = asset_dir / 'ofm' ofm_dir_bak = asset_dir / 'ofm.bak' @@ -47,9 +52,10 @@ def download_and_extract_asset_tar_gz(asset_kind): ) print(f' downloaded asset: {asset_kind}') + return True -def download_sprites(): +def download_sprites() -> bool: """ Sprites are special assets, as we have to keep the old versions indefinitely """ @@ -64,6 +70,8 @@ def download_sprites(): sprites_remote = [l for l in r.text.splitlines() if l.startswith('sprites/')] + changed = False + for sprite in sprites_remote: sprite_name = sprite.split('/')[1].replace('.tar.gz', '') @@ -81,3 +89,6 @@ def download_sprites(): ) local_file.unlink() print(f' downloaded sprite version: {sprite_name}') + changed = True + + return changed diff --git a/modules/http_host/http_host_lib/sync.py b/modules/http_host/http_host_lib/sync.py index a57c095..7180147 100644 --- a/modules/http_host/http_host_lib/sync.py +++ b/modules/http_host/http_host_lib/sync.py @@ -19,10 +19,9 @@ def full_sync(force=False): assert_sudo() # start - versions_changed = fetch_version_files() - download_assets() + assets_changed = download_assets() btrfs_downloaded = False @@ -35,7 +34,7 @@ def full_sync(force=False): btrfs_downloaded += download_area_version(area='planet', version='latest') btrfs_downloaded += download_area_version(area='planet', version='deployed') - if btrfs_downloaded or versions_changed or force: + if btrfs_downloaded or versions_changed or assets_changed or force: auto_clean_btrfs() auto_mount()