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
This commit is contained in:
Zsolt Ero
2024-10-28 12:33:53 +01:00
parent 6fbe9f04e9
commit bd7f5fa740
4 changed files with 37 additions and 10 deletions

7
deploy-sync.sh Executable file
View File

@@ -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

View File

@@ -125,6 +125,16 @@ def loadbalancer(hostname, user, port, noninteractive):
setup_loadbalancer(c) 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() @cli.command()
@common_options @common_options
def debug(hostname, user, port, noninteractive): def debug(hostname, user, port, noninteractive):

View File

@@ -7,21 +7,26 @@ from http_host_lib.config import config
from http_host_lib.utils import download_file_aria2, download_if_size_differs from http_host_lib.utils import download_file_aria2, download_if_size_differs
def download_assets(): def download_assets() -> bool:
""" """
Downloads and extracts assets Downloads and extracts assets
""" """
download_and_extract_asset_tar_gz('fonts') changed = False
download_and_extract_asset_tar_gz('styles')
download_and_extract_asset_tar_gz('natural_earth')
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): def download_and_extract_asset_tar_gz(asset_kind):
""" """
Download and extract asset.tgz if the file size differ or not available locally Download and extract asset.tgz if the file size differ or not available locally
Returns True if modified
""" """
print(f'Downloading asset {asset_kind}') 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' local_file = asset_dir / 'ofm.tar.gz'
if not download_if_size_differs(url, local_file): if not download_if_size_differs(url, local_file):
print(f' skipping asset: {asset_kind}') print(f' skipping asset: {asset_kind}')
return return False
ofm_dir = asset_dir / 'ofm' ofm_dir = asset_dir / 'ofm'
ofm_dir_bak = asset_dir / 'ofm.bak' 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}') 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 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/')] sprites_remote = [l for l in r.text.splitlines() if l.startswith('sprites/')]
changed = False
for sprite in sprites_remote: for sprite in sprites_remote:
sprite_name = sprite.split('/')[1].replace('.tar.gz', '') sprite_name = sprite.split('/')[1].replace('.tar.gz', '')
@@ -81,3 +89,6 @@ def download_sprites():
) )
local_file.unlink() local_file.unlink()
print(f' downloaded sprite version: {sprite_name}') print(f' downloaded sprite version: {sprite_name}')
changed = True
return changed

View File

@@ -19,10 +19,9 @@ def full_sync(force=False):
assert_sudo() assert_sudo()
# start # start
versions_changed = fetch_version_files() versions_changed = fetch_version_files()
download_assets() assets_changed = download_assets()
btrfs_downloaded = False 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='latest')
btrfs_downloaded += download_area_version(area='planet', version='deployed') 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_clean_btrfs()
auto_mount() auto_mount()