diff options
| -rwxr-xr-x | tool/aio.py | 34 | ||||
| -rwxr-xr-x | tool/modules/nginx.py | 30 | 
2 files changed, 31 insertions, 33 deletions
| diff --git a/tool/aio.py b/tool/aio.py index 23540bb..f7f7612 100755 --- a/tool/aio.py +++ b/tool/aio.py @@ -109,24 +109,24 @@ def generate_nginx_config(domain: str) -> None:  if args.action == 'domain':      domain = check_domain_is_defined() -    match args.domain_action: -        case 'list': -            domains = list_domains(domain) -            for domain in domains: -                console.print(domain) -        case 'certbot': -            console.print( -                "Here is some commands you can use to do certbot related work.") -            is_test = args.test -            if is_test: -                console.print( -                    "Note you specified --test, so the commands are for test use.", style="yellow") -            console.print( -                f"To create certs for init:\n[code]{certbot_command_gen(domain, 'create', test=is_test)}[/]") +    domain_action = args.domain_action +    if domain_action == 'list': +        domains = list_domains(domain) +        for domain in domains: +            console.print(domain) +    elif domain_action == 'certbot': +        console.print( +            "Here is some commands you can use to do certbot related work.") +        is_test = args.test +        if is_test:              console.print( -                f"To renew certs previously created:\n[code]{certbot_command_gen(domain, 'renew', test=is_test)}[/]") -        case 'nginx': -            generate_nginx_config(domain) +                "Note you specified --test, so the commands are for test use.", style="yellow") +        console.print( +            f"To create certs for init:\n[code]{certbot_command_gen(domain, 'create', test=is_test)}[/]") +        console.print( +            f"To renew certs previously created:\n[code]{certbot_command_gen(domain, 'renew', test=is_test)}[/]") +    elif domain_action == 'nginx': +        generate_nginx_config(domain)      exit(0) diff --git a/tool/modules/nginx.py b/tool/modules/nginx.py index 6cb918c..fac7214 100755 --- a/tool/modules/nginx.py +++ b/tool/modules/nginx.py @@ -44,29 +44,27 @@ def nginx_config_gen(domain: str, dest: str) -> None:          subdomain = site["subdomain"]          local_config = config.copy()          local_config['CRUPEST_NGINX_SUBDOMAIN'] = subdomain -        match site["type"]: -            case 'static-file': -                template = static_file_template -                local_config['CRUPEST_NGINX_ROOT'] = site["root"] -            case 'reverse-proxy': -                template = reverse_proxy_template -                local_config['CRUPEST_NGINX_UPSTREAM_NAME'] = site["upstream"]["name"] -                local_config['CRUPEST_NGINX_UPSTREAM_SERVER'] = site["upstream"]["server"] +        if site["type"] == 'static-file': +            template = static_file_template +            local_config['CRUPEST_NGINX_ROOT'] = site["root"] +        elif site["type"] == 'reverse-proxy': +            template = reverse_proxy_template +            local_config['CRUPEST_NGINX_UPSTREAM_NAME'] = site["upstream"]["name"] +            local_config['CRUPEST_NGINX_UPSTREAM_SERVER'] = site["upstream"]["server"]          with open(os.path.join(dest, f'{subdomain}.{domain}.conf'), 'w') as f:              f.write(template.generate(local_config))  def list_domains(domain: str) -> list[str]: -    return [domain, *server.sites.map(lambda s: f"{s.subdomain}.{domain}")] +    return [domain, *[f"{s['subdomain']}.{domain}" for s in server["sites"]]]  def certbot_command_gen(domain: str, action, test=False) -> str:      domains = list_domains(domain) -    match action: -        case 'create': -            # create with standalone mode -            return f'docker run -it --name certbot -v "./data/certbot/certs:/etc/letsencrypt" -v "./data/certbot/data:/var/lib/letsencrypt" certbot/certbot certonly --standalone -d {" -d ".join(domains)}{ " --test-cert" if test else "" }' -        case 'renew': -            # renew with webroot mode -            return f'docker run -it --name certbot -v "./data/certbot/certs:/etc/letsencrypt" -v "./data/certbot/data:/var/lib/letsencrypt" -v "./data/certbot/webroot:/var/www/certbot" certbot/certbot renew --webroot -w /var/www/certbot' +    if action == 'create': +        # create with standalone mode +        return f'docker run -it --name certbot -v "./data/certbot/certs:/etc/letsencrypt" -v "./data/certbot/data:/var/lib/letsencrypt" certbot/certbot certonly --standalone -d {" -d ".join(domains)}{ " --test-cert" if test else "" }' +    elif action == 'renew': +        # renew with webroot mode +        return f'docker run -it --name certbot -v "./data/certbot/certs:/etc/letsencrypt" -v "./data/certbot/data:/var/lib/letsencrypt" -v "./data/certbot/webroot:/var/www/certbot" certbot/certbot renew --webroot -w /var/www/certbot'      raise ValueError('Invalid action') | 
