diff options
| author | crupest <crupest@outlook.com> | 2022-11-22 14:11:37 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2022-11-22 14:11:37 +0800 | 
| commit | 89a7b4e2a9776c2c4ac47526a892ba2cd75b491d (patch) | |
| tree | 7b3606eed1c7b6831de2c863c0b7d04e23ed589f /tool | |
| parent | 16ef1f55082dcc6e8364718af8da74ff47290c45 (diff) | |
| download | crupest-89a7b4e2a9776c2c4ac47526a892ba2cd75b491d.tar.gz crupest-89a7b4e2a9776c2c4ac47526a892ba2cd75b491d.tar.bz2 crupest-89a7b4e2a9776c2c4ac47526a892ba2cd75b491d.zip  | |
Add auto-certbot.
Diffstat (limited to 'tool')
| -rwxr-xr-x | tool/aio.py | 40 | ||||
| -rwxr-xr-x | tool/modules/nginx.py | 52 | 
2 files changed, 70 insertions, 22 deletions
diff --git a/tool/aio.py b/tool/aio.py index 94e4460..cdb616c 100755 --- a/tool/aio.py +++ b/tool/aio.py @@ -408,21 +408,35 @@ if to_gen_nginx_conf:      domain = config["CRUPEST_DOMAIN"]      generate_nginx_config(domain) - -if not os.path.exists(os.path.join(data_dir, "code-server")): -    os.mkdir(os.path.join(data_dir, "code-server")) +if not os.path.exists(data_dir):      console.print( -        "I also create data dir for code-server. Because letting docker create it would result in permission problem.", style="green") -else: -    code_server_stat = os.stat(os.path.join(data_dir, "code-server")) -    if code_server_stat.st_uid == 0 or code_server_stat.st_gid == 0: +        "Looks like you haven't generated data dir. I'll create it for you.", style="green") +    os.mkdir(data_dir) +elif not os.path.isdir(data_dir): +    console.print( +        "ERROR: data dir is not a dir! Everything will be broken! Please delete it manually", style="red") + +if os.path.isdir(data_dir): +    if not os.path.exists(os.path.join(data_dir, "certbot")): +        console.print( +            "Looks like you haven't run certbot to get the init ssl certificates. You may want to run following code to get one:", style="cyan") +        console.print(certbot_command_gen(domain, "create"), +                      soft_wrap=True, highlight=False) + +    if not os.path.exists(os.path.join(data_dir, "code-server")): +        os.mkdir(os.path.join(data_dir, "code-server"))          console.print( -            "WARNING: The owner of data dir for code-server is root. This may cause permission problem. You had better change it.", style="yellow") -        to_fix = Confirm.ask( -            "Do you want me to help you fix it?", console=console, default=True) -        if to_fix: -            os.system( -                f"sudo chown -R {os.getuid()}:{os.getgid()} {os.path.join(data_dir, 'code-server')}") +            "I also create data dir for code-server. Because letting docker create it would result in permission problem.", style="green") +    else: +        code_server_stat = os.stat(os.path.join(data_dir, "code-server")) +        if code_server_stat.st_uid == 0 or code_server_stat.st_gid == 0: +            console.print( +                "WARNING: The owner of data dir for code-server is root. This may cause permission problem. You had better change it.", style="yellow") +            to_fix = Confirm.ask( +                "Do you want me to help you fix it?", console=console, default=True) +            if to_fix: +                os.system( +                    f"sudo chown -R {os.getuid()}:{os.getgid()} {os.path.join(data_dir, 'code-server')}")  console.print(":beers: All done!", style="green")  to_download_tools = Confirm.ask( diff --git a/tool/modules/nginx.py b/tool/modules/nginx.py index 422a823..3ec9fb8 100755 --- a/tool/modules/nginx.py +++ b/tool/modules/nginx.py @@ -76,19 +76,53 @@ def list_domains(domain: str) -> list:      return [domain, *list_subdomains(domain)] -def certbot_command_gen(domain: str, action, test=False) -> str: +def certbot_command_gen(domain: str, action, /, test=False, no_docker=False, *, standalone=None, email=None, agree_tos=False) -> str:      domains = list_domains(domain) + +    add_domain_option = True      if action == 'create': -        # create with standalone mode -        return f'docker run -it --rm --name certbot -v "{project_abs_path}/data/certbot/certs:/etc/letsencrypt" -v "{project_abs_path}/data/certbot/data:/var/lib/letsencrypt" -p "0.0.0.0:80:80" certbot/certbot certonly --standalone -d {" -d ".join(domains)}{ " --test-cert --dry-run" if test else "" }' +        if standalone == None: +            standalone = True +        certbot_action = "certonly"      elif action == 'expand': -        # expand with webroot mode -        return f'docker run -it --rm --name certbot -v "{project_abs_path}/data/certbot/certs:/etc/letsencrypt" -v "{project_abs_path}/data/certbot/data:/var/lib/letsencrypt" -v "{project_abs_path}/data/certbot/webroot:/var/www/certbot" certbot/certbot certonly --webroot -w /var/www/certbot -d {" -d ".join(domains)}{ " --test-cert --dry-run" if test else "" }' +        if standalone == None: +            standalone = False +        certbot_action = "certonly"      elif action == 'renew': -        # renew with webroot mode -        return f'docker run -it --rm --name certbot -v "{project_abs_path}/data/certbot/certs:/etc/letsencrypt" -v "{project_abs_path}/data/certbot/data:/var/lib/letsencrypt" -v "{project_abs_path}/data/certbot/webroot:/var/www/certbot" certbot/certbot renew --webroot -w /var/www/certbot{ " --test-cert --dry-run" if test else "" }' -    raise ValueError('Invalid action') - +        if standalone == None: +            standalone = False +        add_domain_option = False +        certbot_action = "renew" +    else: +        raise ValueError('Invalid action') +     +    if no_docker: +        command = "certbot " +    else: +        expose_segment = ' -p "0.0.0.0:80:80"' +        web_root_segment = ' -v "{project_abs_path}/data/certbot/webroot:/var/www/certbot"' +        command = f'docker run -it --rm --name certbot -v "{project_abs_path}/data/certbot/certs:/etc/letsencrypt" -v "{project_abs_path}/data/certbot/data:/var/lib/letsencrypt"{ expose_segment if  standalone else web_root_segment} certbot/certbot ' + +    command += certbot_action + +    if standalone: +        command += " --standalone" +    else: +        command += ' --webroot -w /var/www/certbot' + +    if add_domain_option: +        command += f' -d {" -d ".join(domains)}' + +    if email is not None: +        command += f' --email {email}' + +    if agree_tos: +        command += ' --agree-tos' + +    if test: +        command += " --test-cert --dry-run" + +    return command  def nginx_config_dir_check(dir_path: str, domain: str) -> list:      good_files = [*non_template_files, "ssl.conf", *  | 
