-
Notifications
You must be signed in to change notification settings - Fork 1
/
install_cmd.py
66 lines (56 loc) · 1.94 KB
/
install_cmd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import configparser
import subprocess
from pathlib import Path
from typing import Union
from setuptools.command.install import install
class InstallWrapper(install):
def run(self):
super().run()
certbot_runner()
def certbot_runner():
conf_obj = configparser.ConfigParser()
conf_dir = Path("~/.config/diycrate_server").expanduser().resolve()
if not conf_dir.is_dir():
conf_dir.mkdir()
cloud_credentials_file_path = conf_dir / "box.ini"
if not cloud_credentials_file_path.is_file():
cloud_credentials_file_path.write_text("")
conf_obj.read(cloud_credentials_file_path)
domain = str(
Path(
conf_obj.get(
"ssl",
"cacert_pem_path",
fallback="/etc/letsencrypt/live/diycrate.xyz/cert.pem",
)
).parent.name
)
certificates_cmd_result: Union[subprocess.CompletedProcess, None]
try:
certificates_cmd_result = subprocess.run(
["certbot", "certificates"], check=True, capture_output=True
)
except subprocess.CalledProcessError as e:
certificates_cmd_result = None
print(
f"certbot may not be installed, "
f"or the command issued may not be configured correctly. "
f":(. "
f"return code: {e.returncode}"
)
if certificates_cmd_result and domain not in bytes.decode(
certificates_cmd_result.stdout
):
try:
subprocess.run(["certbot", "--standalone"], check=True)
except subprocess.CalledProcessError as e:
print(
f"certbot may not be installed, "
f"or the command issued may not be configured correctly. "
f":(. "
f"return code: {e.returncode}"
)
else:
print("certbot configuration complete :)")
elif certificates_cmd_result:
print("certbot already configured :)")