- 创建项目目录结构和主要文件 - 实现 Docker 和 Nginx 配置生成的基本功能 - 添加命令行参数解析和默认行为逻辑 - 实现配置文件读取和解析功能 - 添加 Jinja2 模板渲染功能 - 创建 Nginx 和 Docker Compose 配置模板
69 lines
2.4 KiB
Python
69 lines
2.4 KiB
Python
"""
|
|
this is the main file
|
|
"""
|
|
import argparse
|
|
import os
|
|
import subprocess
|
|
from pathlib import Path
|
|
from dotenv import load_dotenv
|
|
from jinja2_render import render_dataclass
|
|
from src.config_reader import nginx_config, docker_config, docker2nginx
|
|
|
|
load_dotenv()
|
|
|
|
def run_command(command):
|
|
try:
|
|
result = subprocess.run(command, shell=True, check=True,
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
print(result.stdout.decode())
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"Command failed with error: {e.stderr.decode()}")
|
|
|
|
def docker_compose_action():
|
|
config_path = Path(os.getenv('DOCKER_CONFIG_PATH', 'docker.ini'))
|
|
output_path = Path(os.getenv('DOCKER_COMPOSE_OUTPATH', 'docker-compose.conf'))
|
|
|
|
(auto_config, other) = docker_config(config_path)
|
|
render_dataclass(auto_config, Path('./src/docker-compose.j2'), output_path, other)
|
|
|
|
def inner_nginx_action():
|
|
config_path = Path(os.getenv('DOCKER_CONFIG_PATH', 'docker.ini'))
|
|
output_path = Path(os.getenv('INNER_NGINX_OUTPATH', 'inner-nginx.conf'))
|
|
|
|
(auto_config, other) = docker2nginx(config_path)
|
|
render_dataclass(auto_config, Path('./src/nginx-conf.j2'), output_path, other)
|
|
|
|
def outer_nginx_action():
|
|
config_path = Path(os.getenv('NGINX_CONFIG_PATH', 'docker.ini'))
|
|
output_path = Path(os.getenv('OUTER_NGINX_OUTPATH', 'outer-nginx.conf'))
|
|
|
|
(auto_config, other) = nginx_config(config_path)
|
|
render_dataclass(auto_config, Path('./src/nginx-conf.j2'), output_path, other)
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(description="Automate Docker and Nginx configuration.")
|
|
parser.add_argument('-c', '--docker-compose', action='store_true',
|
|
help='Generate Docker Compose')
|
|
parser.add_argument('-i', '--inner-nginx', action='store_true',
|
|
help='Generate Inner Nginx (docker network)')
|
|
parser.add_argument('-o', '--outer-nginx', action='store_true',
|
|
help='Generate Outer Nginx (host machine)')
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.docker_compose:
|
|
docker_compose_action()
|
|
elif args.inner_nginx:
|
|
inner_nginx_action()
|
|
elif args.outer_nginx:
|
|
outer_nginx_action()
|
|
else:
|
|
# Default behavior: perform all actions
|
|
docker_compose_action()
|
|
inner_nginx_action()
|
|
outer_nginx_action()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|