44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
import datetime
|
|
import subprocess
|
|
import time
|
|
from configurator import configurator
|
|
from mongo import mongo
|
|
from blob import minio
|
|
|
|
|
|
class Response:
|
|
code: int
|
|
out: str
|
|
err: str
|
|
|
|
|
|
def call(command: str) -> Response:
|
|
p = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
|
|
resp = p.wait()
|
|
response = Response()
|
|
response.code = resp
|
|
response.out, response.err = p.stdout.read().decode('utf-8'), p.stderr.read().decode('utf-8')
|
|
return response
|
|
|
|
|
|
def get_hosts() -> list[str]:
|
|
return list(set(configurator.get_config("hosts") + ["platform.chocomarsh.com"]))
|
|
|
|
|
|
def update_host(host: str):
|
|
gen_cert = call(f"docker exec $(docker ps -q -f name=infra_nginx) certbot --nginx --email emmtvv@gmail.com --agree-tos -d \"{host}\"")
|
|
if gen_cert.code != 0:
|
|
print("failed generating certificate")
|
|
return
|
|
|
|
|
|
|
|
while True:
|
|
now = datetime.datetime.now()
|
|
mongo_hosts = mongo.hosts
|
|
for host in get_hosts():
|
|
if now + datetime.timedelta(days=14) > mongo_hosts[host]["expire_time"]:
|
|
update_host(host)
|
|
print(f"Host {host} updated")
|
|
minio.put_object("certupdater", "nginx.conf", )
|
|
time.sleep(5 * 60) |