Compare commits

..

5 Commits

Author SHA1 Message Date
911c6225dc Merge pull request 'master' (#48) from master into prod
Reviewed-on: #48
2025-06-12 01:58:33 +03:00
a85e7d4ee2 Merge pull request 'fix' (#36) from master into prod
Reviewed-on: #36
2025-06-04 21:14:44 +03:00
88fa100c07 Merge pull request 'fix' (#34) from master into prod
Reviewed-on: #34
2025-06-04 03:07:23 +03:00
dba11ffb3d Merge pull request 'fix' (#33) from master into prod
Reviewed-on: #33
2025-06-04 02:56:27 +03:00
55323f1be1 Merge pull request 'master' (#32) from master into prod
Reviewed-on: #32
2025-06-04 02:53:40 +03:00
7 changed files with 133 additions and 58 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@@ -6,7 +6,9 @@ services:
image: mathwave/sprint-repo:certupdater
command: worker
environment:
MINIO_HOST: "minio.develop.sprinthub.ru"
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_DEV
MONGO_HOST: "mongo.develop.sprinthub.ru"
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
STAGE: "development"
volumes:
@@ -14,8 +16,6 @@ services:
networks:
- configurator
- queues-development
- minio-development
- mongo-development
deploy:
mode: replicated
restart_policy:
@@ -31,7 +31,3 @@ networks:
external: true
queues-development:
external: true
minio-development:
external: true
mongo-development:
external: true

View File

@@ -6,7 +6,9 @@ services:
image: mathwave/sprint-repo:certupdater
command: worker
environment:
MINIO_HOST: "minio.sprinthub.ru"
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_PROD
MONGO_HOST: "mongo.sprinthub.ru"
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
STAGE: "production"
volumes:
@@ -14,8 +16,6 @@ services:
networks:
- configurator
- queues
- minio
- mongo
deploy:
mode: replicated
restart_policy:
@@ -31,7 +31,3 @@ networks:
external: true
queues:
external: true
minio:
external: true
mongo:
external: true

View File

@@ -1,7 +1,7 @@
import os
from minio import Minio
MINIO_HOST = "minio:9000"
MINIO_HOST = os.getenv("MINIO_HOST", "localhost") + ":9000"
MINIO_ACCESS_KEY = os.getenv("MINIO_ACCESS_KEY", "serviceminioadmin")
MINIO_SECRET_KEY = os.getenv("MINIO_SECRET_KEY", "minioadmin")

88
configurator.py Normal file
View File

@@ -0,0 +1,88 @@
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"))

22
main.py
View File

@@ -4,7 +4,8 @@ import os
import subprocess
import time
from requests import get, post
from requests import post
from configurator import configurator
from mongo import mongo
from blob import minio
@@ -48,11 +49,10 @@ def call(command: str) -> Response:
def get_hosts() -> list[str]:
response = get(
f"http://configurator/api/v1/fetch?project=certupdater&stage={os.getenv("STAGE")}"
).json()
hosts = response["configs"]["hosts"]
return list(hosts)
if os.getenv("STAGE") == "development":
return list(set(list(configurator.get_config("hosts"))))
else:
return list(set(list(configurator.get_config("hosts"))))
def update_host(host: str) -> str | None:
@@ -104,14 +104,11 @@ def update_host(host: str) -> str | None:
return None
if __name__ == "__main__":
while True:
while True:
now = datetime.datetime.now()
mongo_hosts = mongo.hosts
hosts = get_hosts()
print(f"got hosts {hosts}")
updated = False
for host in hosts:
for host in get_hosts():
if (
now + datetime.timedelta(days=14)
> mongo_hosts.get(
@@ -120,7 +117,6 @@ if __name__ == "__main__":
):
success = update_host(host)
if success:
print(success)
send_notification(
f"host {host} was not updated with an error: {success}"
)
@@ -128,8 +124,6 @@ if __name__ == "__main__":
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(

View File

@@ -4,11 +4,12 @@ import os
MONGO_USER = os.getenv("MONGO_USER", "mongo")
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
MONGO_HOST = os.getenv("MONGO_HOST", "localhost")
class Mongo:
def __init__(self):
url = f"mongodb://{MONGO_USER}:{MONGO_PASSWORD}@mongo:27017/"
url = f"mongodb://{MONGO_USER}:{MONGO_PASSWORD}@{MONGO_HOST}:27017/"
self.client: pymongo.MongoClient = pymongo.MongoClient(url)
self.database = self.client.get_database("certupdater")
self.hosts_collection.create_index([