""" 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()