#!/usr/bin/env python3 import datetime import sys import click from http_host_lib.assets import ( download_assets, ) from http_host_lib.btrfs import ( download_area_version, get_versions_for_area, ) from http_host_lib.config import config from http_host_lib.mount import auto_mount_unmount from http_host_lib.nginx import write_nginx_config from http_host_lib.set_tileset_versions import set_tileset_versions from http_host_lib.utils import assert_linux, assert_sudo @click.group() def cli(): """ Manages OpenFreeMap HTTP hosts, including:\n - Downloading btrfs images\n - Downloading assets\n - Mounting directories\n - Updating nginx config\n - Getting the deployed versions of tilesets\n - Running the sync cron task (called every minute) """ @cli.command() @click.argument('area', required=False) @click.option( '--version', default='latest', help='Optional version string, like "20231227_043106_pt"' ) def download_btrfs(area: str, version: str): """ Downloads and uncompresses tiles.btrfs files from the btrfs bucket Version can be "latest" (default) or specified, like "20231227_043106_pt" Use --version=1 to list all available versions """ return download_area_version(area, version) @cli.command(name='download-assets') def download_assets_(): """ Downloads and extracts assets """ download_assets() @cli.command() def mount(): """ Mounts/unmounts the btrfs images from /data/ofm/http_host/runs automatically. When finished, /mnt/ofm dir will have all the present tiles.btrfs files mounted in a read-only way. """ auto_mount_unmount() @cli.command() def set_latest_versions(): """ Sets the latest version of the tilesets to the version specified by https://assets.openfreemap.com/versions/deployed_planet.txt 1. Checks if the given version is present on the disk and is mounted 2. Writes to a version file """ print('running set_latest_versions') assert_linux() assert_sudo() if not config.mnt_dir.exists(): sys.exit(' mount needs to be run first') return set_tileset_versions() @cli.command() def nginx_sync(): """ Syncs the nginx config to the state of the system """ print('running nginx_sync') assert_linux() assert_sudo() if not config.mnt_dir.exists(): sys.exit(' mount needs to be run first') write_nginx_config() @cli.command() @click.option('--force', is_flag=True, help='Force nginx sync run') @click.pass_context def sync(ctx, force): """ Runs the sync task, normally called by cron every minute On a new server this also takes care of everything, no need to run anything manually. """ print('---') print('running sync') print(datetime.datetime.now(tz=datetime.timezone.utc)) assert_linux() assert_sudo() download_done = False download_done += ctx.invoke(download_btrfs, area='monaco') if not config.host_config.get('skip_planet'): download_done += ctx.invoke(download_btrfs, area='planet') if download_done: ctx.invoke(mount) ctx.invoke(download_assets) deploy_done = ctx.invoke(set_latest_versions) if download_done or deploy_done or force: ctx.invoke(nginx_sync) @cli.command() def debug(): versions = get_versions_for_area('monaco') print(versions) if __name__ == '__main__': # print(config.host_config) cli()