""" this is the main file """ import argparse import os import subprocess from pathlib import Path from dotenv import load_dotenv from src.jinja2_render import render_dataclass from src.config_reader import nginx_config, docker_config, docker2nginx load_dotenv() def run_command(command): "run a command and use stdout" try: result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(result.stdout.decode()) return True except subprocess.CalledProcessError as e: print(f"Command failed with error: {e.stderr.decode()}") return False def docker_compose_action(config_path, output_path): "读取并渲染配置文件" (auto_config, other) = docker_config(config_path) render_dataclass(auto_config, Path('./src/docker-compose.j2'), output_path, other=other) def inner_nginx_action(config_path, output_path): "读取并渲染配置文件" (auto_config, other) = docker2nginx(config_path) render_dataclass(auto_config, Path('./src/nginx-conf.j2'), output_path, other=other) def outer_nginx_action(config_path, output_path): "读取并渲染配置文件" (auto_config, other) = nginx_config(config_path) render_dataclass(auto_config, Path('./src/nginx-conf.j2'), output_path, other=other) def nginx_reload(): """reload nginx config""" res = True if res: res = run_command("docker exec -it nginx nginx -t") if res: res = run_command("docker exec -it nginx nginx -s reload") def docker_compose_restart(): "docker compose restart" run_command("docker compose restart") def docker_ps(): "list running docker containers by my format" cmd = "docker ps --format "\ "\"table {{.Names}}\t{{.Image}}\t{{.Command}}\t{{.CreatedAt}}\t{{.Status}}\"" run_command(cmd) def main(): "main function" parser = argparse.ArgumentParser(description="Automate Docker and Nginx configuration.") parser.add_argument('-a', '--all', action='store_true', default=True, help='Perform all actions') 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)') parser.add_argument('-r', '--nginx-reload', action='store_true', help='Reload Nginx configuration') parser.add_argument('-s', '--docker-compose-restart', action='store_true', help='Restart Docker Compose services') parser.add_argument('-p', '--docker-ps', action='store_true', help='List running Docker containers') parser.add_argument('--docker-config-path', type=Path, default=Path(os.getenv('DOCKER_INIPATH', 'docker.ini')), help='Path to the Docker INI configuration file') parser.add_argument('--docker-output-path', type=Path, default=Path(os.getenv('DOCKER_COMPOSE_OUTPATH', 'docker-compose.conf')), help='Path to the output Docker Compose configuration file') parser.add_argument('--inner-config-path', type=Path, default=Path(os.getenv('DOCKER_INIPATH', 'docker.ini')), help='Path to the Docker INI configuration file for inner Nginx') parser.add_argument('--inner-output-path', type=Path, default=Path(os.getenv('INNER_NGINX_OUTPATH', 'inner-nginx.conf')), help='Path to the output Inner Nginx configuration file') parser.add_argument('--outer-config-path', type=Path, default=Path(os.getenv('NGINX_INIPATH', 'nginx.ini')), help='Path to the Nginx INI configuration file for outer Nginx') parser.add_argument('--outer-output-path', type=Path, default=Path(os.getenv('OUTER_NGINX_OUTPATH', 'outer-nginx.conf')), help='Path to the output Outer Nginx configuration file') args = parser.parse_args() if args.all: docker_compose_action(args.docker_config_path, args.docker_output_path) inner_nginx_action(args.inner_config_path, args.inner_output_path) outer_nginx_action(args.outer_config_path, args.outer_output_path) nginx_reload() else: if args.docker_compose: docker_compose_action(args.docker_config_path, args.docker_output_path) if args.inner_nginx: inner_nginx_action(args.inner_config_path, args.inner_output_path) if args.outer_nginx: outer_nginx_action(args.outer_config_path, args.outer_output_path) if args.nginx_reload: nginx_reload() if args.docker_compose_restart: docker_compose_restart() if args.docker_ps: docker_ps() if __name__ == "__main__": main()