From 3c6aaeef5752032c7dd86d680aa44dffbbf8437a Mon Sep 17 00:00:00 2001 From: Zsolt Ero Date: Sat, 24 Feb 2024 02:37:07 +0100 Subject: [PATCH] nginx work --- config/.env.sample | 2 +- init-server.py | 8 ++--- scripts/http_host/http_host_lib/nginx.py | 38 +++++++++++++++--------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/config/.env.sample b/config/.env.sample index 020695e..af4b7c3 100644 --- a/config/.env.sample +++ b/config/.env.sample @@ -2,7 +2,7 @@ SSH_PASSWD= # Domain to server directly, without CloudFlare -DOMAIN_DIRECT=direct.openfreemap.org +DOMAIN_LE=direct.openfreemap.org # Domain via CloudFlare, using origin certificates # Please put cf.key and cf.cert files in config/certs diff --git a/init-server.py b/init-server.py index 3540dda..d158394 100755 --- a/init-server.py +++ b/init-server.py @@ -90,14 +90,14 @@ def prepare_tile_gen(c): def upload_http_host_config(c): - domain_direct = dotenv_values(f'{CONFIG_DIR}/.env').get('DOMAIN_DIRECT', '').strip() + domain_le = dotenv_values(f'{CONFIG_DIR}/.env').get('DOMAIN_LE', '').strip() domain_cf = dotenv_values(f'{CONFIG_DIR}/.env').get('DOMAIN_CF', '').strip() skip_planet = ( dotenv_values(f'{CONFIG_DIR}/.env').get('SKIP_PLANET', '').lower().strip() == 'true' ) - if not (domain_direct or domain_cf): - sys.exit('Please specify DOMAIN_DIRECT or DOMAIN_CF in config/.env') + if not (domain_le or domain_cf): + sys.exit('Please specify DOMAIN_LE or DOMAIN_CF in config/.env') if domain_cf: if ( @@ -107,7 +107,7 @@ def upload_http_host_config(c): sys.exit('When using DOMAIN_CF, please put cf.key and cf.cert files in config/certs') host_config = { - 'domain_direct': domain_direct, + 'domain_le': domain_le, 'domain_cf': domain_cf, 'skip_planet': skip_planet, } diff --git a/scripts/http_host/http_host_lib/nginx.py b/scripts/http_host/http_host_lib/nginx.py index bcee045..a4323cc 100644 --- a/scripts/http_host/http_host_lib/nginx.py +++ b/scripts/http_host/http_host_lib/nginx.py @@ -6,31 +6,41 @@ from http_host_lib import DEFAULT_RUNS_DIR, HOST_CONFIG, MNT_DIR, NGINX_DIR, OFM def write_nginx_config(): - location_str, curl_text = create_location_blocks() curl_text_mix = '' if HOST_CONFIG['domain_cf']: - with open(NGINX_DIR / 'cf.conf') as fp: - cf_template = fp.read() - - cf_template = cf_template.replace('__LOCATION_BLOCKS__', location_str) - cf_template = cf_template.replace('__DOMAIN__', HOST_CONFIG['domain_cf']) - cf_template = cf_template.replace('__LOCAL__', 'ofm_cf') - - curl_text_mix += curl_text.replace('__DOMAIN__', HOST_CONFIG['domain_cf']).replace( - '__LOCAL__', 'ofm_cf' + curl_text_mix += create_nginx_conf( + template_path=NGINX_DIR / 'cf.conf', + local='ofm_cf', + domain=HOST_CONFIG['domain_cf'], ) - with open('/data/nginx/sites/cf.conf', 'w') as fp: - fp.write(cf_template) - print(' nginx config written') - subprocess.run(['nginx', '-t'], check=True) subprocess.run(['systemctl', 'reload', 'nginx'], check=True) print(curl_text_mix) +def create_nginx_conf(*, template_path, local, domain): + location_str, curl_text = create_location_blocks() + + with open(template_path) as fp: + template = fp.read() + + template = template.replace('__LOCATION_BLOCKS__', location_str) + template = template.replace('__LOCAL__', local) + template = template.replace('__DOMAIN__', domain) + + curl_text = curl_text.replace('__LOCAL__', local) + curl_text = curl_text.replace('__DOMAIN__', domain) + + with open(f'/data/nginx/sites/{local}.conf', 'w') as fp: + fp.write(template) + print(f' nginx config written: {domain} {local}') + + return curl_text + + def create_location_blocks(): location_str = '' curl_text = ''