scripts -> modules

This commit is contained in:
Zsolt Ero
2024-08-29 16:33:59 +02:00
parent 7196e15837
commit 66d0bdc515
54 changed files with 65 additions and 52 deletions

View File

@@ -56,7 +56,7 @@ There is no cloud, just dedicated servers. The HTTPS server is nginx on Ubuntu.
Production-quality hosting of 300 million tiny files is hard. The average file size is just 450 byte. Dozens of tile servers have been written to tackle this problem, but they all have their limitations.
The original idea of this project is to avoid using tile servers altogether. Instead, the tiles are directly served from Btrfs partition images + hard links using an optimised nginx config. I wrote [extract_mbtiles](scripts/tile_gen/scripts/extract_mbtiles.py) and [shrink_btrfs](scripts/tile_gen/scripts/shrink_btrfs.py) scripts for this very purpose.
The original idea of this project is to avoid using tile servers altogether. Instead, the tiles are directly served from Btrfs partition images + hard links using an optimised nginx config. I wrote [extract_mbtiles](modules/tile_gen/scripts/extract_mbtiles.py) and [shrink_btrfs](modules/tile_gen/scripts/shrink_btrfs.py) scripts for this very purpose.
This replaces a running service with a pure, file-system-level implementation. Since the Linux kernel's file caching is among the highest-performing and most thoroughly tested codes ever written, it delivers serious performance.
@@ -70,7 +70,7 @@ The project has the following parts
This sets up everything on a clean Ubuntu server. You run it locally and it sets up the server via SSH.
#### HTTP host - scripts/http_host
#### HTTP host - modules/http_host
Inside `http_host`, all work is done by `host_manager.py`.
@@ -85,13 +85,13 @@ It does the following:
You can run `./host_manager.py --help` to see which options are available. Some commands can be run locally, including on non-linux machines.
#### tile generation - scripts/tile_gen
#### tile generation - modules/tile_gen
_note: Tile generation is 100% optional, as we are providing the processed full planet files for public download._
The `tile_gen` script downloads a full planet OSM extract and runs it through Planetiler.
The created .mbtiles file is then extracted into a Btrfs partition image using the custom [extract_mbtiles](scripts/tile_gen/scripts/extract_mbtiles.py) script. The partition is shrunk using the [shrink_btrfs](scripts/tile_gen/scripts/shrink_btrfs.py) script.
The created .mbtiles file is then extracted into a Btrfs partition image using the custom [extract_mbtiles](modules/tile_gen/scripts/extract_mbtiles.py) script. The partition is shrunk using the [shrink_btrfs](modules/tile_gen/scripts/shrink_btrfs.py) script.
Finally, it's uploaded to a public Cloudflare R2 bucket using rclone.
@@ -99,7 +99,7 @@ Finally, it's uploaded to a public Cloudflare R2 bucket using rclone.
A very important part, probably needs the most work in the long term future.
#### load balancer script - scripts/loadbalancer
#### load balancer script - modules/loadbalancer
A Round Robin DNS based load balancer script for health checking and updating records. It pushes status messages to a Telegram bot.

View File

@@ -3,7 +3,7 @@
import click
from fabric import Config, Connection
from ssh_lib import SCRIPTS_DIR, dotenv_val
from ssh_lib import MODULES_DIR, dotenv_val
from ssh_lib.tasks import (
prepare_http_host,
prepare_shared,
@@ -89,7 +89,7 @@ def http_host_autoupdate(hostname, user, port):
run_http_host_sync(c)
put(c, SCRIPTS_DIR / 'http_host' / 'cron.d' / 'ofm_http_host', '/etc/cron.d/')
put(c, MODULES_DIR / 'http_host' / 'cron.d' / 'ofm_http_host', '/etc/cron.d/')
@cli.command()

View File

@@ -1,24 +1,18 @@
#!/usr/bin/env python3
import datetime
import subprocess
import sys
from pathlib import Path
import click
import requests
from http_host_lib.assets import (
download_and_extract_asset_tar_gz,
download_assets,
download_sprites,
)
from http_host_lib.btrfs import (
download_and_extract_btrfs,
download_area_version,
get_versions_for_area,
)
from http_host_lib.config import config
from http_host_lib.mount import clean_up_mounts, create_fstab
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
@@ -28,8 +22,8 @@ from http_host_lib.utils import assert_linux, assert_sudo
def cli():
"""
Manages OpenFreeMap HTTP hosts, including:\n
- Downloading btrfs images\n
- Downloading assets\n
- Downloading tilesets\n
- Mounting directories\n
- Updating nginx config\n
- Getting the deployed versions of tilesets\n
@@ -68,21 +62,7 @@ def mount():
When finished, /mnt/ofm dir will have all the present tiles.btrfs files mounted in a read-only way.
"""
print('running mount')
assert_linux()
assert_sudo()
if not config.runs_dir.exists():
sys.exit(' download_tileset needs to be run first')
clean_up_mounts(config.mnt_dir)
create_fstab()
print(' running mount -a')
subprocess.run(['mount', '-a'], check=True)
clean_up_mounts(config.mnt_dir)
auto_mount_unmount()
@cli.command()
@@ -140,10 +120,10 @@ def sync(ctx, force):
assert_sudo()
download_done = False
download_done += ctx.invoke(download_tileset, area='monaco')
download_done += ctx.invoke(download_btrfs, area='monaco')
if not config.host_config.get('skip_planet'):
download_done += ctx.invoke(download_tileset, area='planet')
download_done += ctx.invoke(download_btrfs, area='planet')
if download_done:
ctx.invoke(mount)

View File

@@ -8,6 +8,10 @@ from http_host_lib.utils import download_file_aria2, download_if_size_differs
def download_assets():
"""
Downloads and extracts assets
"""
download_and_extract_asset_tar_gz('fonts')
download_and_extract_asset_tar_gz('styles')
download_and_extract_asset_tar_gz('natural_earth')

View File

@@ -9,6 +9,10 @@ from http_host_lib.utils import download_file_aria2, get_remote_file_size
def download_area_version(area: str, version: str):
"""
Downloads and uncompresses tiles.btrfs files from the btrfs bucket
"""
if area not in config.areas:
sys.exit(f' please specify area: {config.areas}')

View File

@@ -1,7 +1,32 @@
import subprocess
import sys
from pathlib import Path
from http_host_lib.config import config
from http_host_lib.utils import assert_linux, assert_sudo
def auto_mount_unmount():
"""
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.
"""
print('running mount')
assert_linux()
assert_sudo()
if not config.runs_dir.exists():
sys.exit(' download-btrfs needs to be run first')
clean_up_mounts(config.mnt_dir)
create_fstab()
print(' running mount -a')
subprocess.run(['mount', '-a'], check=True)
clean_up_mounts(config.mnt_dir)
def create_fstab():

View File

@@ -11,10 +11,10 @@ source venv/bin/activate
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
pip install -e modules/http_host
pip install -e modules/tile_gen
pip install -e modules/loadbalancer
pip install -e modules/set_version

View File

@@ -5,7 +5,7 @@ from dotenv import dotenv_values
ASSETS_DIR = Path(__file__).parent / 'assets'
CONFIG_DIR = Path(__file__).parent.parent / 'config'
SCRIPTS_DIR = Path(__file__).parent.parent / 'scripts'
MODULES_DIR = Path(__file__).parent.parent / 'modules'
OFM_DIR = '/data/ofm'
REMOTE_CONFIG = f'{OFM_DIR}/config'

View File

@@ -1,4 +1,4 @@
from ssh_lib import SCRIPTS_DIR
from ssh_lib import MODULES_DIR
from ssh_lib.utils import apt_get_install, exists, put
@@ -20,4 +20,4 @@ def c1000k(c):
def wrk(c):
apt_get_install(c, 'wrk')
c.sudo('mkdir -p /data/ofm/benchmark')
put(c, f'{SCRIPTS_DIR}/http_host/benchmark/wrk_custom_list.lua', '/data/ofm/benchmark')
put(c, f'{MODULES_DIR}/http_host/benchmark/wrk_custom_list.lua', '/data/ofm/benchmark')

View File

@@ -6,7 +6,7 @@ from ssh_lib import (
HTTP_HOST_BIN,
OFM_DIR,
REMOTE_CONFIG,
SCRIPTS_DIR,
MODULES_DIR,
TILE_GEN_BIN,
VENV_BIN,
dotenv_val,
@@ -39,7 +39,7 @@ def prepare_shared(c):
def prepare_venv(c):
put(
c,
SCRIPTS_DIR / 'prepare-virtualenv.sh',
MODULES_DIR / 'prepare-virtualenv.sh',
OFM_DIR,
permissions='755',
user='ofm',
@@ -54,10 +54,10 @@ def prepare_tile_gen(c):
c.sudo(f'rm -rf {TILE_GEN_BIN}')
put_dir(c, SCRIPTS_DIR / 'tile_gen', TILE_GEN_BIN, file_permissions='755')
put_dir(c, MODULES_DIR / 'tile_gen', TILE_GEN_BIN, file_permissions='755')
for dirname in ['tile_gen_lib', 'scripts']:
put_dir(c, SCRIPTS_DIR / 'tile_gen' / dirname, f'{TILE_GEN_BIN}/{dirname}')
put_dir(c, MODULES_DIR / 'tile_gen' / dirname, f'{TILE_GEN_BIN}/{dirname}')
if (CONFIG_DIR / 'rclone.conf').exists():
put(
@@ -75,7 +75,7 @@ def prepare_tile_gen(c):
c.sudo('chown ofm:ofm /data/ofm/tile_gen/{,*}')
c.sudo(f'chown ofm:ofm -R {TILE_GEN_BIN}')
put(c, SCRIPTS_DIR / 'tile_gen' / 'cron.d' / 'ofm_tile_gen', '/etc/cron.d/')
put(c, MODULES_DIR / 'tile_gen' / 'cron.d' / 'ofm_tile_gen', '/etc/cron.d/')
def upload_http_host_config(c):
@@ -109,7 +109,7 @@ def upload_http_host_config(c):
f'{REMOTE_CONFIG}/rclone.conf',
permissions=400,
)
put(c, SCRIPTS_DIR / 'http_host' / 'cron.d' / 'ofm_ledns_reader', '/etc/cron.d/')
put(c, MODULES_DIR / 'http_host' / 'cron.d' / 'ofm_ledns_reader', '/etc/cron.d/')
def prepare_http_host(c):
@@ -140,14 +140,14 @@ def run_http_host_sync(c):
def upload_http_host_files(c):
c.sudo(f'mkdir -p {HTTP_HOST_BIN}')
put_dir(c, SCRIPTS_DIR / 'http_host', HTTP_HOST_BIN, file_permissions='755')
put_dir(c, MODULES_DIR / 'http_host', HTTP_HOST_BIN, file_permissions='755')
for dirname in ['http_host_lib', 'scripts']:
put_dir(c, SCRIPTS_DIR / 'http_host' / dirname, f'{HTTP_HOST_BIN}/{dirname}')
put_dir(c, MODULES_DIR / 'http_host' / dirname, f'{HTTP_HOST_BIN}/{dirname}')
put_dir(
c,
SCRIPTS_DIR / 'http_host' / 'http_host_lib' / 'nginx_confs',
MODULES_DIR / 'http_host' / 'http_host_lib' / 'nginx_confs',
f'{HTTP_HOST_BIN}/http_host_lib/nginx_confs',
)
@@ -198,7 +198,7 @@ def setup_ledns_writer(c):
put(
c,
SCRIPTS_DIR / 'ledns' / 'rclone_write.sh',
MODULES_DIR / 'ledns' / 'rclone_write.sh',
'/data/ofm/ledns/rclone_write.sh',
create_parent_dir=True,
permissions=500,
@@ -248,16 +248,16 @@ def setup_loadbalancer(c):
)
c.sudo('rm -rf /data/ofm/loadbalancer')
put_dir(c, SCRIPTS_DIR / 'loadbalancer', '/data/ofm/loadbalancer')
put_dir(c, MODULES_DIR / 'loadbalancer', '/data/ofm/loadbalancer')
put_dir(
c,
SCRIPTS_DIR / 'loadbalancer' / 'loadbalancer_lib',
MODULES_DIR / 'loadbalancer' / 'loadbalancer_lib',
'/data/ofm/loadbalancer/loadbalancer_lib',
)
c.sudo(f'{VENV_BIN}/pip install -e /data/ofm/loadbalancer --use-pep517')
c.sudo('mkdir -p /data/ofm/loadbalancer/logs')
put(c, SCRIPTS_DIR / 'loadbalancer' / 'cron.d' / 'ofm_loadbalancer', '/etc/cron.d/')
put(c, MODULES_DIR / 'loadbalancer' / 'cron.d' / 'ofm_loadbalancer', '/etc/cron.d/')
c.sudo('chown -R ofm:ofm /data/ofm/loadbalancer')