deploy_tileset_version

This commit is contained in:
Zsolt Ero
2024-01-03 16:37:30 +01:00
parent 5d0b165c99
commit 395447bbbd
3 changed files with 66 additions and 82 deletions

View File

@@ -1,81 +0,0 @@
#!/usr/bin/env python3
import datetime
import os
import subprocess
import sys
from pathlib import Path
import click
import requests
@click.command()
def cli():
"""
Deploys the version of tiles specified by https://assets.openfreemap.com/versions/deployed_tiles_planet.txt
1. Checking if the given version is present on the disk and mounted
2. Writing to a version file
3. Calling nginx_sync to update the /planet location block
"""
print(datetime.datetime.now(tz=datetime.timezone.utc))
if not Path('/etc/fstab').exists():
sys.exit('Needs to be run on Linux')
if os.geteuid() != 0:
sys.exit('Needs sudo')
if not Path('/mnt/ofm').exists():
sys.exit('mounter.py needs to be run first')
need_nginx_sync = False
for area in ['planet', 'monaco']:
r = requests.get(f'https://assets.openfreemap.com/versions/deployed_tiles_{area}.txt')
r.raise_for_status()
version_str = r.text.strip()
print(f'remote version for {area}: {version_str}')
local_version_file = Path(f'/data/ofm/config/deployed_tiles_{area}.txt')
if not local_version_file.exists():
local_version_start = None
else:
with open(local_version_file) as fp:
local_version_start = fp.read()
if not version_str:
print(' remote version not specified')
if local_version_start is not None:
local_version_file.unlink()
need_nginx_sync = True
continue
mnt_file = Path(f'/mnt/ofm/{area}-{version_str}/metadata.json')
if not mnt_file.exists():
print(' local version does not exist')
if local_version_start is not None:
local_version_file.unlink()
need_nginx_sync = True
continue
if version_str != local_version_start:
with open(local_version_file, 'w') as fp:
fp.write(version_str)
need_nginx_sync = True
if need_nginx_sync:
print('running nginx_sync.py')
subprocess.run(
[sys.executable, Path(__file__).parent / 'nginx_sync' / 'nginx_sync.py'],
check=True,
)
print('\n\n\n')
if __name__ == '__main__':
cli()

View File

@@ -7,6 +7,7 @@ from pathlib import Path
import click
import requests
from http_host_lib import DEFAULT_ASSETS_DIR, DEFAULT_RUNS_DIR, MNT_DIR
from http_host_lib.deploy_tileset import deploy_tileset
from http_host_lib.download_fonts import download_fonts
from http_host_lib.download_tileset import download_and_extract_tileset
from http_host_lib.mount import clean_up_mounts, create_fstab
@@ -110,23 +111,44 @@ def mount():
clean_up_mounts(MNT_DIR)
@cli.command()
def deploy_tileset_version():
"""
Deploys the tileset version specified by
https://assets.openfreemap.com/versions/deployed_planet.txt
1. Check if the given version is present on the disk and is mounted
2. Write to a version file
"""
assert_linux()
assert_sudo()
if not MNT_DIR.exists():
sys.exit('mount needs to be run first')
return deploy_tileset()
@cli.command()
@click.pass_context
def sync(ctx):
"""
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(datetime.datetime.now(tz=datetime.timezone.utc))
downloaded = False
downloaded += ctx.invoke(download_tileset, area='monaco')
# d2 = ctx.invoke(download_tileset, area='planet')
if downloaded:
ctx.invoke(mount)
ctx.invoke(download_assets)
ctx.invoke(deploy_tileset_version)
if __name__ == '__main__':
# TODO

View File

@@ -0,0 +1,43 @@
from pathlib import Path
import requests
def deploy_tileset():
need_nginx_sync = False
for area in ['planet', 'monaco']:
r = requests.get(f'https://assets.openfreemap.com/versions/deployed_tiles_{area}.txt')
r.raise_for_status()
remote_version = r.text.strip()
print(f' remote version for {area}: {remote_version}')
local_version_file = Path(f'/data/ofm/config/deployed_tiles_{area}.txt')
if not local_version_file.exists():
local_version_start = None
else:
with open(local_version_file) as fp:
local_version_start = fp.read()
if not remote_version:
print(' remote version not specified')
if local_version_start is not None:
local_version_file.unlink()
need_nginx_sync = True
continue
mnt_file = Path(f'/mnt/ofm/{area}-{remote_version}/metadata.json')
if not mnt_file.exists():
print(' local version does not exist')
if local_version_start is not None:
local_version_file.unlink()
need_nginx_sync = True
continue
if remote_version != local_version_start:
with open(local_version_file, 'w') as fp:
fp.write(remote_version)
need_nginx_sync = True
return need_nginx_sync