diff --git a/init-server.py b/init-server.py index dba5518..1ca67eb 100755 --- a/init-server.py +++ b/init-server.py @@ -3,7 +3,7 @@ import click from fabric import Config, Connection -from ssh_lib import SCRIPTS_DIR, TILE_GEN_BIN, dotenv_val +from ssh_lib import SCRIPTS_DIR, dotenv_val from ssh_lib.tasks import ( prepare_http_host, prepare_shared, diff --git a/prepare-virtualenv.sh b/prepare-virtualenv.sh index 9c67b1c..cdd7319 100755 --- a/prepare-virtualenv.sh +++ b/prepare-virtualenv.sh @@ -13,6 +13,8 @@ pip install -U pip wheel setuptools pip install -e . pip install -e scripts/http_host pip install -e scripts/tile_gen +pip install -e scripts/loadbalancer +pip install -e scripts/setversion diff --git a/scripts/http_host/host_manager.py b/scripts/http_host/host_manager.py index 3a6f356..ff6523b 100755 --- a/scripts/http_host/host_manager.py +++ b/scripts/http_host/host_manager.py @@ -198,5 +198,5 @@ def sync(ctx): if __name__ == '__main__': - print(HOST_CONFIG) + # print(HOST_CONFIG) cli() diff --git a/scripts/http_host/http_host_lib/nginx.py b/scripts/http_host/http_host_lib/nginx.py index 3bd626e..9e60e13 100644 --- a/scripts/http_host/http_host_lib/nginx.py +++ b/scripts/http_host/http_host_lib/nginx.py @@ -20,6 +20,13 @@ def write_nginx_config(): domain_le = HOST_CONFIG['domain_le'] domain_ledns = HOST_CONFIG['domain_ledns'] + # remove old configs and certs + for file in Path('/data/nginx/sites').glob('ofm_*.conf'): + file.unlink() + + for file in Path('/data/nginx/certs').glob('ofm_*'): + file.unlink() + # processing Round Robin DNS config if domain_ledns: if not (OFM_CONFIG_DIR / 'rclone.conf').is_file(): diff --git a/scripts/setversion/setup.py b/scripts/setversion/setup.py new file mode 100644 index 0000000..66d3b58 --- /dev/null +++ b/scripts/setversion/setup.py @@ -0,0 +1,17 @@ +from setuptools import find_packages, setup + + +requirements = [ + 'click', + 'requests', + 'pycurl', + 'python-dotenv', + 'questionary', +] + + +setup( + python_requires='>=3.10', + install_requires=requirements, + packages=find_packages(), +) diff --git a/scripts/setversion/setversion.py b/scripts/setversion/setversion.py new file mode 100755 index 0000000..3352c5c --- /dev/null +++ b/scripts/setversion/setversion.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 + +import subprocess + +import click +import questionary +from setversion_lib import RCLONE_BIN, RCLONE_CONF + + +@click.group() +def cli(): + """ + Sets deployed reference versions + """ + + +@cli.command() +@click.argument('area', required=True) +def interactive(area): + versions = get_available_versions(area)[::-1] + + choices = [questionary.Choice(title=r, value=i) for i, r in enumerate(versions)] + answer = questionary.select(f'Select version for: {area}', choices=choices).ask() + + selected = versions[answer] + + set_version(area, selected) + + +def get_available_versions(area): + p = subprocess.run( + [ + RCLONE_BIN, + 'cat', + f'remote:ofm-{area}/dirs.txt', + ], + env=dict(RCLONE_CONFIG=RCLONE_CONF), + check=True, + capture_output=True, + text=True, + ) + versions = [l.strip() for l in p.stdout.strip().splitlines()] + versions.sort() + + return versions + + +def set_version(area, version): + subprocess.run( + [ + RCLONE_BIN, + 'rcat', + f'remote:ofm-assets/versions/deployed_{area}.txt', + ], + env=dict(RCLONE_CONFIG=RCLONE_CONF), + check=True, + input=version.encode(), + ) + + +if __name__ == '__main__': + cli() diff --git a/scripts/setversion/setversion_lib/__init__.py b/scripts/setversion/setversion_lib/__init__.py new file mode 100644 index 0000000..77672e6 --- /dev/null +++ b/scripts/setversion/setversion_lib/__init__.py @@ -0,0 +1,16 @@ +from pathlib import Path + + +if Path('/data/ofm/config').exists(): + OFM_CONFIG_DIR = Path('/data/ofm/config') +else: + OFM_CONFIG_DIR = Path(__file__).parent.parent.parent.parent / 'config' + +assert OFM_CONFIG_DIR.exists() + +RCLONE_CONF = OFM_CONFIG_DIR / 'rclone.conf' + +if Path('/opt/homebrew/bin/rclone').exists(): + RCLONE_BIN = '/opt/homebrew/bin/rclone' +else: + RCLONE_BIN = 'rclone' diff --git a/scripts/tile_gen/upload_manager.py b/scripts/tile_gen/upload_manager.py index 255981f..a83d0ab 100755 --- a/scripts/tile_gen/upload_manager.py +++ b/scripts/tile_gen/upload_manager.py @@ -155,43 +155,5 @@ def index(): make_indexes() -@cli.command() -def set_latest_versions(): - """ - Sets the latest version as the deployed one - """ - - for area in AREAS: - print(f'setting latest version for {area}') - - p = subprocess.run( - [ - 'rclone', - 'cat', - f'remote:ofm-{area}/dirs.txt', - ], - env=dict(RCLONE_CONFIG='/data/ofm/config/rclone.conf'), - check=True, - capture_output=True, - text=True, - ) - versions = [l.strip() for l in p.stdout.strip().splitlines()] - versions.sort(reverse=True) - - latest_version = versions[0] - print(latest_version) - - subprocess.run( - [ - 'rclone', - 'rcat', - f'remote:ofm-assets/versions/deployed_{area}.txt', - ], - env=dict(RCLONE_CONFIG='/data/ofm/config/rclone.conf'), - check=True, - input=latest_version.encode(), - ) - - if __name__ == '__main__': cli()