config refactor

This commit is contained in:
Zsolt Ero
2024-08-29 01:40:21 +02:00
parent 2fc91aa470
commit 939b782830
7 changed files with 59 additions and 61 deletions

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python3
import datetime
import subprocess
import sys
@@ -6,7 +7,7 @@ from pathlib import Path
import click
import requests
from http_host_lib import DEFAULT_ASSETS_DIR, DEFAULT_RUNS_DIR, HOST_CONFIG, MNT_DIR
from http_host_lib.config import config
from http_host_lib.download_assets import (
download_and_extract_asset_tar_gz,
download_sprites,
@@ -69,7 +70,7 @@ def download_tileset(area: str, version: str, list_versions: bool, runs_dir: Pat
selected_version = version
if not runs_dir:
runs_dir = DEFAULT_RUNS_DIR
runs_dir = config.default_runs_dir
if not runs_dir.parent.exists():
sys.exit("runs dir's parent doesn't exist")
@@ -91,7 +92,7 @@ def download_assets(assets_dir: Path):
print('running download_assets')
if not assets_dir:
assets_dir = DEFAULT_ASSETS_DIR
assets_dir = config.default_assets_dir
if not assets_dir.parent.exists():
sys.exit("asset dir's parent doesn't exist")
@@ -115,16 +116,16 @@ def mount():
assert_linux()
assert_sudo()
if not DEFAULT_RUNS_DIR.exists():
if not config.default_runs_dir.exists():
sys.exit(' download_tileset needs to be run first')
clean_up_mounts(MNT_DIR)
clean_up_mounts(config.mnt_dir)
create_fstab()
print(' running mount -a')
subprocess.run(['mount', '-a'], check=True)
clean_up_mounts(MNT_DIR)
clean_up_mounts(config.mnt_dir)
@cli.command()
@@ -142,7 +143,7 @@ def set_latest_versions():
assert_linux()
assert_sudo()
if not MNT_DIR.exists():
if not config.mnt_dir.exists():
sys.exit(' mount needs to be run first')
return set_tileset_versions()
@@ -159,7 +160,7 @@ def nginx_sync():
assert_linux()
assert_sudo()
if not MNT_DIR.exists():
if not config.mnt_dir.exists():
sys.exit(' mount needs to be run first')
write_nginx_config()
@@ -184,7 +185,7 @@ def sync(ctx, force):
download_done = False
download_done += ctx.invoke(download_tileset, area='monaco')
if not HOST_CONFIG.get('skip_planet'):
if not config.host_config.get('skip_planet'):
download_done += ctx.invoke(download_tileset, area='planet')
if download_done:
@@ -199,5 +200,5 @@ def sync(ctx, force):
if __name__ == '__main__':
# print(HOST_CONFIG)
# print(config.host_config)
cli()

View File

@@ -1,20 +0,0 @@
import json
from pathlib import Path
NGINX_DIR = Path(__file__).parent / 'nginx'
DEFAULT_RUNS_DIR = Path('/data/ofm/http_host/runs')
DEFAULT_ASSETS_DIR = Path('/data/ofm/http_host/assets')
MNT_DIR = Path('/mnt/ofm')
OFM_CONFIG_DIR = Path('/data/ofm/config')
HTTP_HOST_BIN_DIR = Path('/data/ofm/http_host/bin')
CERTS_DIR = Path('/data/nginx/certs')
try:
with open('/data/ofm/config/http_host.json') as fp:
HOST_CONFIG = json.load(fp)
except Exception:
HOST_CONFIG = {}

View File

@@ -0,0 +1,26 @@
import json
from pathlib import Path
class Configuration:
http_host_dir = Path('/data/ofm/http_host')
nginx_dir = Path(__file__).parent / 'nginx'
default_runs_dir = http_host_dir / 'runs'
default_assets_dir = http_host_dir / 'assets'
mnt_dir = Path('/mnt/ofm')
ofm_config_dir = Path('/data/ofm/config')
http_host_bin = http_host_dir / 'bin'
certs_dir = Path('/data/nginx/certs')
try:
with open('/data/ofm/config/http_host.json') as fp:
host_config = json.load(fp)
except Exception:
host_config = {}
config = Configuration()

View File

@@ -1,6 +1,5 @@
import shutil
import subprocess
import sys
from pathlib import Path
import click

View File

@@ -1,14 +1,14 @@
import subprocess
from pathlib import Path
from http_host_lib import DEFAULT_RUNS_DIR, MNT_DIR
from http_host_lib.config import config
def create_fstab():
fstab_new = []
for area in ['planet', 'monaco']:
area_dir = (DEFAULT_RUNS_DIR / area).resolve()
area_dir = (config.default_runs_dir / area).resolve()
if not area_dir.exists():
continue
@@ -19,14 +19,14 @@ def create_fstab():
if not btrfs_file.is_file():
continue
mnt_folder = MNT_DIR / f'{area}-{version_str}'
mnt_folder = config.mnt_dir / f'{area}-{version_str}'
mnt_folder.mkdir(exist_ok=True, parents=True)
fstab_new.append(f'{btrfs_file} {mnt_folder} btrfs loop,ro 0 0\n')
print(f' created fstab entry for {btrfs_file} -> {mnt_folder}')
with open('/etc/fstab') as fp:
fstab_orig = [l for l in fp.readlines() if f'{MNT_DIR}/' not in l]
fstab_orig = [l for l in fp.readlines() if f'{config.mnt_dir}/' not in l]
with open('/etc/fstab', 'w') as fp:
fp.writelines(fstab_orig + fstab_new)

View File

@@ -3,22 +3,14 @@ import subprocess
import sys
from pathlib import Path
from http_host_lib import (
CERTS_DIR,
DEFAULT_RUNS_DIR,
HOST_CONFIG,
HTTP_HOST_BIN_DIR,
MNT_DIR,
NGINX_DIR,
OFM_CONFIG_DIR,
)
from http_host_lib.config import config
def write_nginx_config():
curl_text_mix = ''
domain_le = HOST_CONFIG['domain_le']
domain_ledns = HOST_CONFIG['domain_ledns']
domain_le = config.host_config['domain_le']
domain_ledns = config.host_config['domain_ledns']
# remove old configs and certs
for file in Path('/data/nginx/sites').glob('ofm_*.conf'):
@@ -29,30 +21,30 @@ def write_nginx_config():
# processing Round Robin DNS config
if domain_ledns:
if not (OFM_CONFIG_DIR / 'rclone.conf').is_file():
if not (config.ofm_config_dir / 'rclone.conf').is_file():
sys.exit('rclone.conf missing')
# download the ledns certificate from bucket using rclone
write_ledns_reader_script(domain_ledns)
subprocess.run(['bash', HTTP_HOST_BIN_DIR / 'ledns_reader.sh'], check=True)
subprocess.run(['bash', config.http_host_bin / 'ledns_reader.sh'], check=True)
curl_text_mix += create_nginx_conf(
template_path=NGINX_DIR / 'ledns.conf',
template_path=config.nginx_dir / 'ledns.conf',
local='ofm_ledns',
domain=domain_ledns,
)
# processing Let's Encrypt config
if domain_le:
le_cert = CERTS_DIR / 'ofm_le.cert'
le_key = CERTS_DIR / 'ofm_le.key'
le_cert = config.certs_dir / 'ofm_le.cert'
le_key = config.certs_dir / 'ofm_le.key'
if not le_cert.is_file() or not le_key.is_file():
shutil.copyfile(Path('/etc/nginx/ssl/dummy.crt'), le_cert)
shutil.copyfile(Path('/etc/nginx/ssl/dummy.key'), le_key)
curl_text_mix += create_nginx_conf(
template_path=NGINX_DIR / 'le.conf',
template_path=config.nginx_dir / 'le.conf',
local='ofm_le',
domain=domain_le,
)
@@ -68,7 +60,7 @@ def write_nginx_config():
'--webroot-path=/data/nginx/acme-challenges',
'--noninteractive',
'-m',
HOST_CONFIG['le_email'],
config.host_config['le_email'],
'--agree-tos',
'--cert-name=ofm_le',
# '--staging',
@@ -121,7 +113,7 @@ def create_location_blocks(*, local, domain):
location_str = ''
curl_text = ''
for subdir in MNT_DIR.iterdir():
for subdir in config.mnt_dir.iterdir():
if not subdir.is_dir():
continue
area, version = subdir.name.split('-')
@@ -138,7 +130,7 @@ def create_location_blocks(*, local, domain):
location_str += create_latest_locations(local=local, domain=domain)
with open(NGINX_DIR / 'location_static.conf') as fp:
with open(config.nginx_dir / 'location_static.conf') as fp:
location_str += '\n' + fp.read()
return location_str, curl_text
@@ -147,7 +139,7 @@ def create_location_blocks(*, local, domain):
def create_version_location(
*, area: str, version: str, subdir: Path, local: str, domain: str
) -> str:
run_dir = DEFAULT_RUNS_DIR / area / version
run_dir = config.default_runs_dir / area / version
if not run_dir.is_dir():
print(f" {run_dir} doesn't exists, skipping")
return ''
@@ -204,14 +196,14 @@ def create_version_location(
def create_latest_locations(*, local: str, domain: str) -> str:
location_str = ''
local_version_files = OFM_CONFIG_DIR.glob('tileset_version_*.txt')
local_version_files = config.ofm_config_dir.glob('tileset_version_*.txt')
for file in local_version_files:
area = file.stem.split('_')[-1]
with open(file) as fp:
version = fp.read().strip()
print(f' setting latest version for {area}: {version}')
run_dir = DEFAULT_RUNS_DIR / area / version
run_dir = config.default_runs_dir / area / version
tilejson_path = run_dir / f'tilejson-{local}.json'
assert tilejson_path.is_file()
@@ -238,5 +230,5 @@ rclone copyto -v "remote:ofm-private/ledns/{domain_ledns}/ofm_ledns.cert" /data/
rclone copyto -v "remote:ofm-private/ledns/{domain_ledns}/ofm_ledns.key" /data/nginx/certs/ofm_ledns.key
""".strip()
with open(HTTP_HOST_BIN_DIR / 'ledns_reader.sh', 'w') as fp:
with open(config.http_host_bin / 'ledns_reader.sh', 'w') as fp:
fp.write(script)

View File

@@ -2,7 +2,7 @@ from pathlib import Path
import requests
from http_host_lib import OFM_CONFIG_DIR
from http_host_lib.config import config
def set_tileset_versions():
@@ -14,7 +14,7 @@ def set_tileset_versions():
remote_version = r.text.strip()
print(f' remote version for {area}: {remote_version}')
local_version_file = OFM_CONFIG_DIR / f'tileset_version_{area}.txt'
local_version_file = config.ofm_config_dir / f'tileset_version_{area}.txt'
if not local_version_file.exists():
local_version_start = None