This commit is contained in:
Zsolt Ero
2025-10-07 18:24:21 +02:00
parent 17d580023b
commit a36e830416
7 changed files with 46 additions and 22 deletions

View File

@@ -3,7 +3,7 @@ from pathlib import Path
class Configuration:
# Local paths relative to this file
# local paths relative to this file
local_assets_dir = Path(__file__).parent / 'assets'
local_config_dir = Path(__file__).parent.parent / 'config'
local_modules_dir = Path(__file__).parent.parent / 'modules'
@@ -14,7 +14,9 @@ class Configuration:
else:
local_config_jsonc = local_config_dir / f'config.{ENV}.jsonc'
# remote paths (always Linux /, not using pathlib)
config_schema_json = local_config_dir / 'config.schema.json'
# remote paths (always forward / on Linux - not using pathlib)
ofm_dir = '/data/ofm'
remote_config = f'{ofm_dir}/config'
venv_bin = f'{ofm_dir}/venv/bin'

40
ssh_lib/slugify.py Normal file
View File

@@ -0,0 +1,40 @@
import re
import unicodedata
# Pre-compiled patterns for better performance
_RE_INVALID = re.compile(r'[^a-z0-9_-]+')
_RE_SEPARATORS = re.compile(r'[-_]+')
def slugify(
value: str | bytes | int | float | None,
*,
separator: str = '-',
) -> str:
if value in (None, ''):
return ''
if separator not in ('-', '_'):
raise ValueError(f"separator must be '-' or '_', got {repr(separator)}")
# 1. Normalize value to string
if isinstance(value, bytes):
value = value.decode('utf-8', 'ignore')
else:
value = str(value)
# 2. Unicode → ASCII, then lowercase
value = unicodedata.normalize('NFKD', value)
value = value.encode('ascii', 'ignore').decode('ascii').lower()
# 3. Replace invalid characters with separator
value = _RE_INVALID.sub(separator, value)
# 4. Collapse multiple separators
value = _RE_SEPARATORS.sub(separator, value)
# 5. Strip separators from edges
value = value.strip('-_')
return value

View File

@@ -1,17 +1,20 @@
import json
import json5
from ssh_lib.benchmark import c1000k, wrk
from ssh_lib.config import config
from ssh_lib.kernel import kernel_limits1m, kernel_somaxconn65k
from ssh_lib.nginx import certbot, nginx
from ssh_lib.utils import put, put_dir, sudo_cmd
from ssh_lib.slugify import slugify
from ssh_lib.utils import put, put_dir, put_str, sudo_cmd
def prepare_http_host(c):
kernel_somaxconn65k(c)
kernel_limits1m(c)
upload_config_json(c)
upload_config_and_certs(c)
nginx(c)
certbot(c)
@@ -29,20 +32,40 @@ def prepare_http_host(c):
c.sudo(f'{config.venv_bin}/pip install -e {config.http_host_bin} --use-pep517')
def upload_config_json(c):
def upload_config_and_certs(c):
if not config.local_config_jsonc.is_file():
print(f'{config.local_config_jsonc} not found. Make sure it exists in the /config dir')
return
# validate using json5 + jsonschema
config_data = json.loads(config.local_config_jsonc.read_text())
# use config.config_schema_json
config_data = json5.loads(config.local_config_jsonc.read_text())
# if ok, upload the file
put(
c,
config.local_config_jsonc,
f'{config.remote_config}/config.jsonc',
)
# pre-generate all the slugs
for domain_data in config_data['domains']:
domain_data['slug'] = slugify(domain_data['domain'], separator='_')
if domain_data['cert']['type'] == 'upload':
local_cert_path = domain_data['cert']['cert_path']
cert_basename = local_cert_path.basename
local_key_path = local_cert_path.parent / f'{cert_basename}.key'
if not local_cert_path.is_file() or local_key_path.is_file():
print(
f'cert or key file for {domain_data["domain"]} is not found. Make sure these files exists: {local_cert_path} {local_key_path}'
)
remote_cert_path = f'/data/nginx/certs/ofm-{domain_data["slug"]}.cert'
remote_key_path = f'/data/nginx/certs/ofm-{domain_data["slug"]}.key'
# TODO fix permissions
put(c, local_cert_path, remote_cert_path)
put(c, local_key_path, remote_key_path)
# generate a normal JSON and upload it
config_str = json.dumps(config_data, indent=2, ensure_ascii=False)
put_str(c, f'{config.remote_config}/config.json', config_str)
def upload_http_host_files(c):
@@ -52,7 +75,9 @@ def upload_http_host_files(c):
put_dir(c, config.local_modules_dir / 'http_host', config.http_host_bin, file_permissions='755')
for dirname in ['http_host_lib', 'scripts']:
put_dir(c, config.local_modules_dir / 'http_host' / dirname, f'{config.http_host_bin}/{dirname}')
put_dir(
c, config.local_modules_dir / 'http_host' / dirname, f'{config.http_host_bin}/{dirname}'
)
put_dir(
c,