From 88535421820ceff678aeca42c92f03379c1f493b Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sat, 7 Jun 2025 13:23:40 +0300 Subject: [PATCH 01/11] fix --- main.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index 8497e15..240a036 100644 --- a/main.py +++ b/main.py @@ -42,22 +42,22 @@ def update_host(host: str) -> bool: if not container_name: print("No nginx container") return False - + gen_command = f"docker exec {container_name} certbot --nginx --email emmtvv@gmail.com --agree-tos --non-interactive -d \"{host}\"" print(gen_command) - + gen_cert = call(gen_command) if gen_cert.code != 0: print(f"failed generating certificate: {gen_cert.err}") print("Here is the log") print(call(f"docker exec {container_name} cat /var/log/letsencrypt/letsencrypt.log").out) return False - + fullchain_command = call(f"docker exec {container_name} cat /etc/letsencrypt/live/{host}/fullchain.pem") if fullchain_command.code != 0: print(f"failed getting fullchain: {fullchain_command.err}") return True - + privkey_command = call(f"docker exec {container_name} cat /etc/letsencrypt/live/{host}/privkey.pem") if privkey_command.code != 0: print(f"failed getting fullchain: {privkey_command.err}") @@ -83,10 +83,9 @@ while True: updated = True if updated: if os.getenv("STAGE") == "development": - container_id_run = call(f"echo $(docker ps -q -f name=infra-development_nginx)") - result = call(f"docker restart {container_id_run.out}") + container_id_run = call(f"echo $(docker ps -q -f name=infra-development_nginx)").out else: - result = call("docker service update --force infra_nginx") + container_id_run = call(f"echo $(docker ps -q -f name=infra_nginx)").out + print(call("docker exec {container_id_run} python3 prepare.py && nginx -s reload").out) - print(result.err, result.out) time.sleep(30) -- 2.45.2 From 00c307fd1e3cc10633caee5ac8d5323794c8beba Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sun, 8 Jun 2025 10:58:58 +0300 Subject: [PATCH 02/11] fix --- main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 240a036..5c49be5 100644 --- a/main.py +++ b/main.py @@ -86,6 +86,8 @@ while True: container_id_run = call(f"echo $(docker ps -q -f name=infra-development_nginx)").out else: container_id_run = call(f"echo $(docker ps -q -f name=infra_nginx)").out - print(call("docker exec {container_id_run} python3 prepare.py && nginx -s reload").out) + + restart = call("docker exec {container_id_run} python3 prepare.py && nginx -s reload") + print(restart.out + restart.err) time.sleep(30) -- 2.45.2 From 7ef998d635080da892df488132d3d8664f2f725e Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sun, 8 Jun 2025 11:01:53 +0300 Subject: [PATCH 03/11] fix --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 5c49be5..8397502 100644 --- a/main.py +++ b/main.py @@ -87,7 +87,7 @@ while True: else: container_id_run = call(f"echo $(docker ps -q -f name=infra_nginx)").out - restart = call("docker exec {container_id_run} python3 prepare.py && nginx -s reload") + restart = call(f"docker exec {container_id_run} python3 prepare.py && nginx -s reload") print(restart.out + restart.err) time.sleep(30) -- 2.45.2 From 66eefeb324484b51b583b9b0021f0ea6f93a95ef Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sun, 8 Jun 2025 11:05:13 +0300 Subject: [PATCH 04/11] fix --- main.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 8397502..8133ff2 100644 --- a/main.py +++ b/main.py @@ -87,7 +87,8 @@ while True: else: container_id_run = call(f"echo $(docker ps -q -f name=infra_nginx)").out - restart = call(f"docker exec {container_id_run} python3 prepare.py && nginx -s reload") - print(restart.out + restart.err) + restart = call(f"docker exec {container_id_run} python3 prepare.py") + restart2 = call(f"docker exec {container_id_run} nginx -s reload") + print(restart.out, restart.err, restart2.out, restart2.err) time.sleep(30) -- 2.45.2 From 9b89423d95038461a76098da09be67a360dfa242 Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sun, 8 Jun 2025 11:38:06 +0300 Subject: [PATCH 05/11] fix --- main.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 8133ff2..223839d 100644 --- a/main.py +++ b/main.py @@ -83,9 +83,11 @@ while True: updated = True if updated: if os.getenv("STAGE") == "development": - container_id_run = call(f"echo $(docker ps -q -f name=infra-development_nginx)").out + container_id_run = call("echo $(docker ps -q -f name=infra-development_nginx)") else: - container_id_run = call(f"echo $(docker ps -q -f name=infra_nginx)").out + container_id_run = call("echo $(docker ps -q -f name=infra_nginx)") + + print(container_id_run.code, container_id_run.out, container_id_run.err) restart = call(f"docker exec {container_id_run} python3 prepare.py") restart2 = call(f"docker exec {container_id_run} nginx -s reload") -- 2.45.2 From fa0fc6f3bc114fe493953bf922223b55e418f159 Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sun, 8 Jun 2025 11:49:18 +0300 Subject: [PATCH 06/11] fix --- main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 223839d..57e12d4 100644 --- a/main.py +++ b/main.py @@ -89,8 +89,7 @@ while True: print(container_id_run.code, container_id_run.out, container_id_run.err) - restart = call(f"docker exec {container_id_run} python3 prepare.py") - restart2 = call(f"docker exec {container_id_run} nginx -s reload") - print(restart.out, restart.err, restart2.out, restart2.err) + restart = call(f"docker exec {container_id_run} ./refre.sh") + print(restart.code, restart.out, restart.err) time.sleep(30) -- 2.45.2 From 4baf1c94fa3b4c133d6a9dcf97ac24bb6158dfb7 Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Tue, 10 Jun 2025 01:48:47 +0300 Subject: [PATCH 07/11] fix --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 57e12d4..90a4aee 100644 --- a/main.py +++ b/main.py @@ -86,10 +86,10 @@ while True: container_id_run = call("echo $(docker ps -q -f name=infra-development_nginx)") else: container_id_run = call("echo $(docker ps -q -f name=infra_nginx)") - + print(container_id_run.code, container_id_run.out, container_id_run.err) - restart = call(f"docker exec {container_id_run} ./refre.sh") + restart = call(f"docker exec {container_id_run.out} ./refre.sh") print(restart.code, restart.out, restart.err) time.sleep(30) -- 2.45.2 From f7b2b7b7ee5776b5e7ce0db6055eb248b1a3ed39 Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Tue, 10 Jun 2025 02:02:10 +0300 Subject: [PATCH 08/11] fix --- main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 90a4aee..e3303bf 100644 --- a/main.py +++ b/main.py @@ -89,7 +89,10 @@ while True: print(container_id_run.code, container_id_run.out, container_id_run.err) - restart = call(f"docker exec {container_id_run.out} ./refre.sh") + command = f"docker exec {container_id_run.out} ./refre.sh" + print(command) + + restart = call(command) print(restart.code, restart.out, restart.err) time.sleep(30) -- 2.45.2 From cab3256c10df65d5e21fe8f89f632c0bd94feeda Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Tue, 10 Jun 2025 02:04:24 +0300 Subject: [PATCH 09/11] fix --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index e3303bf..ab1a9ad 100644 --- a/main.py +++ b/main.py @@ -89,7 +89,7 @@ while True: print(container_id_run.code, container_id_run.out, container_id_run.err) - command = f"docker exec {container_id_run.out} ./refre.sh" + command = f"docker exec {container_id_run.out.strip()} ./refre.sh" print(command) restart = call(command) -- 2.45.2 From 32377d93fe6f25e9f03f4da681fe578698ed1c1c Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Thu, 12 Jun 2025 00:54:10 +0300 Subject: [PATCH 10/11] fix --- .deploy/deploy-dev.yaml | 3 ++ main.py | 104 +++++++++++++++++++++++++++++----------- 2 files changed, 78 insertions(+), 29 deletions(-) diff --git a/.deploy/deploy-dev.yaml b/.deploy/deploy-dev.yaml index aa87cc6..efb07b6 100644 --- a/.deploy/deploy-dev.yaml +++ b/.deploy/deploy-dev.yaml @@ -15,6 +15,7 @@ services: - /var/run/docker.sock:/var/run/docker.sock networks: - configurator + - queues-development deploy: mode: replicated restart_policy: @@ -28,3 +29,5 @@ services: networks: configurator: external: true + queues-development: + external: true diff --git a/main.py b/main.py index ab1a9ad..49e44c4 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,8 @@ import io import os import subprocess import time + +from requests import post from configurator import configurator from mongo import mongo from blob import minio @@ -14,60 +16,92 @@ class Response: err: str +def send_notification(text: str): + post( + "http://queues:1239/api/v1/put", + headers={"queue": "botalka_mailbox"}, + json={ + "payload": { + "project": "notifications-bot", + "name": "telegram-bot", + "body": { + "text": text, + "chat_id": 84367486, + }, + }, + "seconds_to_execute": 1, + "delay": None, + }, + ) + + def call(command: str) -> Response: - p = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) + 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') + response.out, response.err = p.stdout.read().decode( + "utf-8" + ), p.stderr.read().decode("utf-8") return response def get_hosts() -> list[str]: if os.getenv("STAGE") == "development": - return list(set(list(configurator.get_config("hosts")) + ["platform.develop.sprinthub.ru"])) + return list(set(list(configurator.get_config("hosts")))) else: - return list(set(list(configurator.get_config("hosts")) + ["platform.sprinthub.ru"])) + return list(set(list(configurator.get_config("hosts")))) -def update_host(host: str) -> bool: +def update_host(host: str) -> str | None: if os.getenv("STAGE") == "development": - container_id_run = call(f"echo $(docker ps -q -f name=infra-development_nginx)") + container_id_run = call("echo $(docker ps -q -f name=infra-development_nginx)") else: - container_id_run = call(f"echo $(docker ps -q -f name=infra_nginx)") + container_id_run = call("echo $(docker ps -q -f name=infra_nginx)") if container_id_run.code != 0: - print(f"something wrong {container_id_run.err}") - return False + return container_id_run.err container_name = container_id_run.out.strip() if not container_name: - print("No nginx container") - return False + return "no nginx container" - gen_command = f"docker exec {container_name} certbot --nginx --email emmtvv@gmail.com --agree-tos --non-interactive -d \"{host}\"" - print(gen_command) + gen_command = f'docker exec {container_name} certbot --nginx --email emmtvv@gmail.com --agree-tos --non-interactive -d "{host}"' gen_cert = call(gen_command) if gen_cert.code != 0: - print(f"failed generating certificate: {gen_cert.err}") - print("Here is the log") - print(call(f"docker exec {container_name} cat /var/log/letsencrypt/letsencrypt.log").out) - return False + log = call( + f"docker exec {container_name} cat /var/log/letsencrypt/letsencrypt.log" + ).out + return f"failed generating certificate: {log}" - fullchain_command = call(f"docker exec {container_name} cat /etc/letsencrypt/live/{host}/fullchain.pem") + fullchain_command = call( + f"docker exec {container_name} cat /etc/letsencrypt/live/{host}/fullchain.pem" + ) if fullchain_command.code != 0: - print(f"failed getting fullchain: {fullchain_command.err}") - return True + return f"failed getting fullchain: {fullchain_command.err}" - privkey_command = call(f"docker exec {container_name} cat /etc/letsencrypt/live/{host}/privkey.pem") + privkey_command = call( + f"docker exec {container_name} cat /etc/letsencrypt/live/{host}/privkey.pem" + ) if privkey_command.code != 0: - print(f"failed getting fullchain: {privkey_command.err}") - return True + return f"failed getting fullchain: {privkey_command.err}" fullchain = fullchain_command.out.encode("utf-8") privkey = privkey_command.out.encode("utf-8") - minio.put_object("certupdater", f"certificates/{host}/fullchain.pem", io.BytesIO(fullchain), len(fullchain)) - minio.put_object("certupdater", f"certificates/{host}/privkey.pem", io.BytesIO(privkey), len(privkey)) - return True + minio.put_object( + "certupdater", + f"certificates/{host}/fullchain.pem", + io.BytesIO(fullchain), + len(fullchain), + ) + minio.put_object( + "certupdater", + f"certificates/{host}/privkey.pem", + io.BytesIO(privkey), + len(privkey), + ) + return None while True: @@ -75,15 +109,26 @@ while True: mongo_hosts = mongo.hosts updated = False for host in get_hosts(): - if now + datetime.timedelta(days=14) > mongo_hosts.get(host, {"expire_time": datetime.datetime.fromtimestamp(1)})["expire_time"]: + if ( + now + datetime.timedelta(days=14) + > mongo_hosts.get( + host, {"expire_time": datetime.datetime.fromtimestamp(1)} + )["expire_time"] + ): success = update_host(host) if success: - print(f"Host {host} updated") + send_notification( + f"host {host} was not updated with an error: {success}" + ) + else: mongo.update_date(host) updated = True + send_notification(f"host {host} updated") if updated: if os.getenv("STAGE") == "development": - container_id_run = call("echo $(docker ps -q -f name=infra-development_nginx)") + container_id_run = call( + "echo $(docker ps -q -f name=infra-development_nginx)" + ) else: container_id_run = call("echo $(docker ps -q -f name=infra_nginx)") @@ -94,5 +139,6 @@ while True: restart = call(command) print(restart.code, restart.out, restart.err) + send_notification(f"Balancer for {os.getenv("STAGE")} was restarted") time.sleep(30) -- 2.45.2 From fb7716e8fb88ce8e6c50dbcf3efcf10a1d0b633c Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Thu, 12 Jun 2025 01:10:55 +0300 Subject: [PATCH 11/11] fix --- .deploy/deploy-prod.yaml | 3 +++ storage.py | 42 ---------------------------------------- 2 files changed, 3 insertions(+), 42 deletions(-) delete mode 100644 storage.py diff --git a/.deploy/deploy-prod.yaml b/.deploy/deploy-prod.yaml index 245b25f..9119102 100644 --- a/.deploy/deploy-prod.yaml +++ b/.deploy/deploy-prod.yaml @@ -15,6 +15,7 @@ services: - /var/run/docker.sock:/var/run/docker.sock networks: - configurator + - queues deploy: mode: replicated restart_policy: @@ -28,3 +29,5 @@ services: networks: configurator: external: true + queues: + external: true diff --git a/storage.py b/storage.py deleted file mode 100644 index 2c5438d..0000000 --- a/storage.py +++ /dev/null @@ -1,42 +0,0 @@ -from cachetools import TTLCache -import os - -from utils.mongo import mongo - -CACHE_SIZE = int(os.getenv("CACHE_SIZE", 1000)) -CACHE_TTL = int(os.getenv("CACHE_TTL", 3600)) - -cache = TTLCache(CACHE_SIZE, CACHE_TTL) - - -def get_chat_info(chat_id: int) -> dict: - cached_info = cache.get(chat_id) - if cached_info is not None: - return cached_info - mongo_info = mongo.chats_collection.find_one({"chat_id": chat_id}) - if mongo_info is not None: - cache[chat_id] = mongo_info - return mongo_info - chat_info = {"chat_id": chat_id, "state": "default", "probability": 100} - mongo.chats_collection.insert_one(chat_info) - cache[chat_id] = chat_info - return chat_info - - -def set_values(chat_id: int, **values): - cached_info = cache.get(chat_id) - if cached_info is None: - mongo_info = mongo.chats_collection.find_one({"chat_id": chat_id}) - if mongo_info is None: - chat_info = {"chat_id": chat_id, "state": "default", "probability": 100} - chat_info.update(values) - mongo.chats_collection.insert_one(chat_info) - cache[chat_id] = chat_info - else: - mongo.chats_collection.update_one({"chat_id": chat_id}, {"$set": values}) - mongo_info = dict(mongo_info) - mongo_info.update(values) - cache[chat_id] = mongo_info - else: - cached_info.update(values) - mongo.chats_collection.update_one({"chat_id": chat_id}, {"$set": values}) -- 2.45.2