From f38bf04212e32c561b0b4b261ca75f2b635e06c3 Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Thu, 12 Jun 2025 01:02:26 +0300 Subject: [PATCH] fix --- nginx/nginx-dev/prepare.py | 42 ++++++++++++++++++++++--------------- nginx/nginx-prod/prepare.py | 29 ++++++++++++++----------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/nginx/nginx-dev/prepare.py b/nginx/nginx-dev/prepare.py index fd77ad5..1f4671b 100644 --- a/nginx/nginx-dev/prepare.py +++ b/nginx/nginx-dev/prepare.py @@ -9,26 +9,28 @@ minio_client = Minio( "minio.develop.sprinthub.ru:9000", access_key="serviceminioadmin", secret_key=os.getenv("MINIO_SECRET_KEY", "minioadmin"), - secure=False + secure=False, ) + def get(url): with urlopen(url) as response: - data = response.read().decode('utf-8') + data = response.read().decode("utf-8") return loads(data) + try: - response = get('http://configurator/api/v1/fetch?project=certupdater&stage=development') - hosts = response['configs']['hosts'] + response = get( + "http://configurator/api/v1/fetch?project=certupdater&stage=development" + ) + hosts = response["configs"]["hosts"] except Exception as e: print(f"Error fetching config: {e}", file=sys.stderr) sys.exit(1) -hosts = {**hosts, 'platform.develop.sprinthub.ru': {'host': 'platform-nginx', 'port': 1238}} - -config = '' +config = "" for host, params in hosts.items(): - config += ''' + config += """ server {{ listen 443 ssl http2; listen [::]:443 ssl http2; @@ -48,27 +50,33 @@ for host, params in hosts.items(): proxy_pass http://{target_host}:{port}$request_uri; }} }}\n\n - '''.format(host=host, target_host=params['host'], port=params['port']) + """.format( + host=host, target_host=params["host"], port=params["port"] + ) try: - fullchain = minio_client.get_object("certupdater", f'certificates/{host}/fullchain.pem') - privkey = minio_client.get_object("certupdater", f'certificates/{host}/privkey.pem') + fullchain = minio_client.get_object( + "certupdater", f"certificates/{host}/fullchain.pem" + ) + privkey = minio_client.get_object( + "certupdater", f"certificates/{host}/privkey.pem" + ) try: - os.makedirs(f'/etc/nginx/{host}', exist_ok=True) + os.makedirs(f"/etc/nginx/{host}", exist_ok=True) except OSError as e: print(f"Error creating directory: {e}", file=sys.stderr) continue - - with open(f"/etc/nginx/{host}/fullchain.pem", 'wb') as fp: + + with open(f"/etc/nginx/{host}/fullchain.pem", "wb") as fp: fp.write(fullchain.data) - with open(f"/etc/nginx/{host}/privkey.pem", 'wb') as fp: + with open(f"/etc/nginx/{host}/privkey.pem", "wb") as fp: fp.write(privkey.data) except Exception as e: print(f"Error processing host {host}: {e}", file=sys.stderr) continue try: - with open('/etc/nginx/hosts.conf', 'w') as fp: + with open("/etc/nginx/hosts.conf", "w") as fp: fp.write(config) except Exception as e: print(f"Error writing config file: {e}", file=sys.stderr) - sys.exit(1) \ No newline at end of file + sys.exit(1) diff --git a/nginx/nginx-prod/prepare.py b/nginx/nginx-prod/prepare.py index dd8b902..a1bfa61 100644 --- a/nginx/nginx-prod/prepare.py +++ b/nginx/nginx-prod/prepare.py @@ -7,16 +7,17 @@ minio_client = Minio( "minio.sprinthub.ru:9000", access_key="serviceminioadmin", secret_key=os.getenv("MINIO_SECRET_KEY", "minioadmin"), - secure=False + secure=False, ) -hosts = get('http://configurator/api/v1/fetch?project=certupdater&stage=production').json()['configs']['hosts'] -hosts = {**hosts, 'platform.sprinthub.ru': {'host': 'platform-nginx', 'port': 1238}} +hosts = get( + "http://configurator/api/v1/fetch?project=certupdater&stage=production" +).json()["configs"]["hosts"] -config = '' +config = "" for host, params in hosts.items(): - config += ''' + config += """ server {{ listen 443 ssl http2; listen [::]:443 ssl http2; @@ -36,17 +37,21 @@ for host, params in hosts.items(): proxy_pass http://{target_host}:{port}$request_uri; }} }}\n\n - '''.format(host=host, target_host=params['host'], port=params['port']) - fullchain = minio_client.get_object("certupdater", f'certificates/{host}/fullchain.pem') - privkey = minio_client.get_object("certupdater", f'certificates/{host}/privkey.pem') + """.format( + host=host, target_host=params["host"], port=params["port"] + ) + fullchain = minio_client.get_object( + "certupdater", f"certificates/{host}/fullchain.pem" + ) + privkey = minio_client.get_object("certupdater", f"certificates/{host}/privkey.pem") try: - os.mkdir(f'/etc/nginx/{host}') + os.mkdir(f"/etc/nginx/{host}") except FileExistsError: ... - with open(f"/etc/nginx/{host}/fullchain.pem", 'wb') as fp: + with open(f"/etc/nginx/{host}/fullchain.pem", "wb") as fp: fp.write(fullchain.data) - with open(f"/etc/nginx/{host}/privkey.pem", 'wb') as fp: + with open(f"/etc/nginx/{host}/privkey.pem", "wb") as fp: fp.write(privkey.data) -with open('/etc/nginx/hosts.conf', 'w') as fp: +with open("/etc/nginx/hosts.conf", "w") as fp: fp.write(config)