manticore conf 파일에 환경변수가 들어가는데 python 스크립트로 env들을 치환해주려고 했다.
문제는 docker compose로 컨테이너를 띄우는 도중에 바꿔주는 게 어려웠다.
이 방법은 github action 실행할 때 썼지만 지금 상황과는 맞지 않았고 불편했다.
"""
manticore_{SERVER_ENV}.conf 파일에 .env의 환경 변수를 replace해주는 스크립트
"""
import os
from dotenv import load_dotenv
from shutil import copyfile
SERVER_ENV = os.getenv("SERVER_ENV")
load_dotenv(verbose=True, dotenv_path=f".env.{SERVER_ENV}")
# Get the secret from .env
DB_HOST = os.getenv("DB_HOST")
DB_PORT = os.getenv("DB_PORT")
DB_USER = os.getenv("DB_USER")
DB_PASSWORD = os.getenv("DB_PASSWORD")
DB_NAME = os.getenv("DB_NAME")
def read_file():
with open(f"./manticore/manticore_{SERVER_ENV}.conf", "r") as f:
config_contents = f.read()
return config_contents
def open_and_modify_file(config_contents):
config_contents = config_contents.replace("DB_HOST", DB_HOST)
config_contents = config_contents.replace("DB_PORT", DB_PORT)
config_contents = config_contents.replace("DB_USER", DB_USER)
config_contents = config_contents.replace("DB_PASSWORD", DB_PASSWORD)
config_contents = config_contents.replace("DB_NAME", DB_NAME)
write_env(config_contents)
copyfile(f"./manticore/manticore_{SERVER_ENV}.conf", "./manticore/manticore.conf")
def write_env(config_contents):
with open(f"./manticore/manticore_{SERVER_ENV}.conf", "w") as f:
f.write(config_contents)
def restore_original_file():
copyfile(
"./manticore/manticore_tmp.conf", f"./manticore/manticore_{SERVER_ENV}.conf"
)
def replace_manticore_conf():
copyfile(
f"./manticore/manticore_{SERVER_ENV}.conf", "./manticore/manticore_tmp.conf"
)
config_contents = read_file()
open_and_modify_file(config_contents)
# restore_original_file()
os.remove("./manticore/manticore_tmp.conf")
python 스크립트 대신 envsubst 사용
envsubst를 사용하기 위해 dockerfile에 설치
# syntax=docker/dockerfile:experimental
FROM python:3.9-slim as python-base
RUN apt-get update \
&& apt-get install default-mysql-client gdal-bin libgdal-dev docker.io gettext -yqq
...
RUN touch ./manticore/_manticore.conf
manticore
source real_estate_source {
type = pgsql
sql_host = $DB_HOST
sql_port = $DB_PORT
sql_user = $DB_USER
sql_pass = $DB_PASSWORD
sql_db = $DB_NAME
sql_query = select id, lot_number, road_name_address, name from real_estate
sql_field_string = lot_number
sql_field_string = road_name_address
sql_field_string = name
}
.env엔 DB_HOST 등이 있어야 함
version: '3.7'
services:
django:
container_name: django
build:
context: .
dockerfile: ./dockerfile-testing
restart: always
env_file: .env.testing
ports:
- "8000:8000"
command:
- sh
- -c
- |
envsubst < ./manticore/manticore.conf.template > ./manticore/_manticore.conf
export SERVER_ENV=testing
gunicorn xxx.wsgi --bind 0.0.0.0:8000 --workers 4 --threads 4
volumes:
- .:/xxx
- static_volume:/xxx/static
network_mode: "host"
manticore:
container_name: manticore
image: manticoresearch/manticore
environment:
- EXTRA=1
env_file: .env.testing
restart: always
ports:
- 127.0.0.1:9306:9306
- 127.0.0.1:9308:9308
ulimits:
nproc: 65535
nofile:
soft: 65535
hard: 65535
memlock:
soft: -1
hard: -1
volumes:
- ./manticore/data:/var/lib/manticore
- ./manticore/_manticore.conf:/etc/manticoresearch/_manticore.conf
- ./manticore/log:/var/log/manticore
- ./manticore/index:/var/index/manticore
network_mode: "host"
depends_on:
- django
template 파일 내 ${}를 환경변수로 치환해서 표준 출력함
envsubst < ./manticore/manticore.conf.template
표준 출력된 내용을 실제 사용될 conf 파일에 씀
envsubst < ./manticore/manticore.conf.template > ./manticore/_manticore.conf