diff --git a/init-server.py b/init-server.py index bf20c12..0f04cf5 100755 --- a/init-server.py +++ b/init-server.py @@ -107,7 +107,8 @@ def upload_https_host_files(c): c.sudo(f'mkdir -p {HTTP_HOST_BIN}') for file in [ - 'downloader.py', + 'download_assets.py', + 'download_tiles.py', 'mounter.py', 'metadata_to_tilejson.py', ]: @@ -129,8 +130,14 @@ def upload_https_host_files(c): create_parent_dir=True, ) + put(c, SCRIPTS_DIR / 'http_host' / 'cron.d' / 'ofm_http_host', '/etc/cron.d/') + c.sudo('chown -R ofm:ofm /data/ofm/http_host') + c.sudo('rm -rf /data/ofm/http_host/logs') + c.sudo('mkdir -p /data/ofm/http_host/logs') + c.sudo('chown root:root /data/ofm/http_host/logs') + def upload_certificates(c): for file in (CONFIG_DIR / 'certs').iterdir(): diff --git a/scripts/http_host/cron.d/ofm_http_host b/scripts/http_host/cron.d/ofm_http_host new file mode 100644 index 0000000..554a2c6 --- /dev/null +++ b/scripts/http_host/cron.d/ofm_http_host @@ -0,0 +1,4 @@ +PYTHON=/data/ofm/venv/bin/python + +# every minute download_asset.py +* * * * * root $PYTHON /data/ofm/http_host/bin/download_assets.py > /data/ofm/http_host/logs/download_assets.log 2>&1 diff --git a/scripts/http_host/download_assets.py b/scripts/http_host/download_assets.py index b8ff5bf..245cbae 100755 --- a/scripts/http_host/download_assets.py +++ b/scripts/http_host/download_assets.py @@ -32,18 +32,49 @@ def cli(assets_dir): def download_fonts(assets_dir): + """ + Download and extract font assets if their file differ. + Making updates atomic, with extract to temp + move instead of extracting in place. + """ + fonts_dir = assets_dir / 'fonts' fonts_dir.mkdir(exist_ok=True, parents=True) + fonts_temp = assets_dir / 'fonts_temp' + for font in ['ml', 'omt', 'pm']: url = f'https://assets.openfreemap.com/fonts/{font}.tgz' local_file = fonts_dir / f'{font}.tgz' - download_if_size_differs(url, local_file) + if not download_if_size_differs(url, local_file): + continue + + shutil.rmtree(fonts_temp, ignore_errors=True) + fonts_temp.mkdir() + + subprocess.run( + ['tar', '-xzf', local_file, '-C', fonts_temp], + check=True, + ) + + target_dir = fonts_dir / font + target_dir_renamed = fonts_dir / f'{font}.bak' + temp_dir = fonts_temp / font + + if target_dir.exists(): + target_dir.rename(target_dir_renamed) + temp_dir.rename(target_dir) + + shutil.rmtree(target_dir_renamed, ignore_errors=True) + + shutil.rmtree(fonts_temp, ignore_errors=True) def download_if_size_differs(url: str, local_file: Path): if not local_file.exists() or local_file.stat().st_size != get_remote_file_size(url): download_file(url, local_file) + return True + + return False def get_remote_file_size(url: str): @@ -61,8 +92,10 @@ def download_file(url, local_file): '--split=8', '--max-connection-per-server=8', '--file-allocation=none', + '-d', + local_file.parent, '-o', - local_file, + local_file.name, url, ], check=True,