Merge pull request 'master' (#57) from master into prod

Reviewed-on: #57
This commit is contained in:
emmatveev 2025-06-12 13:30:56 +03:00
commit d2f8022910
2 changed files with 44 additions and 126 deletions

View File

@ -1,88 +0,0 @@
import json
import os
import urllib.parse
from threading import Thread
from time import sleep
from requests import get
class ConfiguratorClient:
def __init__(self, app_name: str, stage: str, need_poll: bool = True):
self.app_name = app_name
self.stage = stage
self.endpoint = 'http://configurator/'
self.fetch_url = urllib.parse.urljoin(self.endpoint, '/api/v1/fetch')
self.config_storage = {}
self.experiment_storage = {}
self.staff_storage = {}
self.poll_data()
if need_poll:
self.poll_data_in_thread()
def poll_data_in_thread(self):
def inner():
while True:
sleep(30)
self.fetch()
Thread(target=inner, daemon=True).start()
def poll_data(self):
self.fetch(with_exception=True)
def request_with_retries(self, url, params, with_exception=False, retries_count=3):
exception_to_throw = None
for _ in range(retries_count):
try:
response = get(
url,
params=params
)
if response.status_code == 200:
return response.json()
print(f'Failed to request {url}, status_code={response.status_code}')
exception_to_throw = Exception('Not 200 status')
except Exception as exc:
print(exc)
exception_to_throw = exc
sleep(1)
print(f'Failed fetching with retries: {url}, {params}')
if with_exception:
raise exception_to_throw
def fetch(self, with_exception=False):
if self.stage == 'local':
local_platform = json.loads(open('local_platform.json', 'r').read())
self.config_storage = local_platform['configs']
self.experiment_storage = local_platform['experiments']
self.staff_storage = {
key: set(value)
for key, value in local_platform['platform_staff'].items()
}
return
response_data = self.request_with_retries(self.fetch_url, {
'project': self.app_name,
'stage': self.stage,
}, with_exception)
self.config_storage = response_data['configs']
self.experiment_storage = response_data['experiments']
self.staff_storage = {
key: set(value)
for key, value in response_data['platform_staff'].items()
}
def is_staff(self, **kwargs):
for key, value in kwargs.items():
if value in self.staff_storage[key]:
return True
return False
def get_config(self, name):
return self.config_storage[name]
def get_experiment(self, name):
return self.experiment_storage[name]
configurator = ConfiguratorClient("certupdater", os.getenv("STAGE"))

82
main.py
View File

@ -4,8 +4,7 @@ import os
import subprocess import subprocess
import time import time
from requests import post from requests import get, post
from configurator import configurator
from mongo import mongo from mongo import mongo
from blob import minio from blob import minio
@ -49,10 +48,11 @@ def call(command: str) -> Response:
def get_hosts() -> list[str]: def get_hosts() -> list[str]:
if os.getenv("STAGE") == "development": response = get(
return list(set(list(configurator.get_config("hosts")))) f"http://configurator/api/v1/fetch?project=certupdater&stage={os.getenv("STAGE")}"
else: ).json()
return list(set(list(configurator.get_config("hosts")))) hosts = response["configs"]["hosts"]
return list(hosts)
def update_host(host: str) -> str | None: def update_host(host: str) -> str | None:
@ -104,41 +104,47 @@ def update_host(host: str) -> str | None:
return None return None
while True: if __name__ == "__main__":
now = datetime.datetime.now() while True:
mongo_hosts = mongo.hosts now = datetime.datetime.now()
updated = False mongo_hosts = mongo.hosts
for host in get_hosts(): hosts = get_hosts()
if ( print(f"got hosts {hosts}")
now + datetime.timedelta(days=14) updated = False
> mongo_hosts.get( for host in hosts:
host, {"expire_time": datetime.datetime.fromtimestamp(1)} if (
)["expire_time"] now + datetime.timedelta(days=14)
): > mongo_hosts.get(
success = update_host(host) host, {"expire_time": datetime.datetime.fromtimestamp(1)}
if success: )["expire_time"]
send_notification( ):
f"host {host} was not updated with an error: {success}" success = update_host(host)
if success:
print(success)
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")
else:
print(f"Host {host} does not need to be updated")
if updated:
if os.getenv("STAGE") == "development":
container_id_run = call(
"echo $(docker ps -q -f name=infra-development_nginx)"
) )
else: else:
mongo.update_date(host) container_id_run = call("echo $(docker ps -q -f name=infra_nginx)")
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)"
)
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) print(container_id_run.code, container_id_run.out, container_id_run.err)
command = f"docker exec {container_id_run.out.strip()} ./refre.sh" command = f"docker exec {container_id_run.out.strip()} ./refre.sh"
print(command) print(command)
restart = call(command) restart = call(command)
print(restart.code, restart.out, restart.err) print(restart.code, restart.out, restart.err)
send_notification(f"Balancer for {os.getenv("STAGE")} was restarted") send_notification(f"Balancer for {os.getenv("STAGE")} was restarted")
time.sleep(30) time.sleep(30)