Compare commits
21 Commits
master
...
96a9bd8fc8
| Author | SHA1 | Date | |
|---|---|---|---|
| 96a9bd8fc8 | |||
| 75a8a5e307 | |||
| 5dd11e1fc8 | |||
| d0c26f15e2 | |||
| 560d21c9fe | |||
| 4f0571ec02 | |||
| ad5a22e985 | |||
| d288a8a883 | |||
| 78eeea9e08 | |||
| 1f7cef1f4b | |||
| 6cedf34b8e | |||
| df0af634d6 | |||
| 41072ac03f | |||
| 43191f99a9 | |||
| 41dea63243 | |||
| ee9c363b56 | |||
| 1ad823a301 | |||
| ded08985b5 | |||
| cb847f8b8f | |||
| a7b082dc7a | |||
| 741eed066c |
@@ -6,7 +6,9 @@ services:
|
|||||||
image: mathwave/sprint-repo:certupdater
|
image: mathwave/sprint-repo:certupdater
|
||||||
command: worker
|
command: worker
|
||||||
environment:
|
environment:
|
||||||
|
MINIO_HOST: "minio.develop.sprinthub.ru"
|
||||||
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_DEV
|
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_DEV
|
||||||
|
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
volumes:
|
volumes:
|
||||||
@@ -14,8 +16,6 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- configurator
|
- configurator
|
||||||
- queues-development
|
- queues-development
|
||||||
- minio-development
|
|
||||||
- mongo-development
|
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
@@ -31,7 +31,3 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
queues-development:
|
queues-development:
|
||||||
external: true
|
external: true
|
||||||
minio-development:
|
|
||||||
external: true
|
|
||||||
mongo-development:
|
|
||||||
external: true
|
|
||||||
|
|||||||
@@ -6,16 +6,15 @@ services:
|
|||||||
image: mathwave/sprint-repo:certupdater
|
image: mathwave/sprint-repo:certupdater
|
||||||
command: worker
|
command: worker
|
||||||
environment:
|
environment:
|
||||||
|
MINIO_HOST: "minio.sprinthub.ru"
|
||||||
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_PROD
|
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_PROD
|
||||||
|
MONGO_HOST: "mongo.sprinthub.ru"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
volumes:
|
volumes:
|
||||||
- /var/run/docker.sock:/var/run/docker.sock
|
- /var/run/docker.sock:/var/run/docker.sock
|
||||||
networks:
|
networks:
|
||||||
- configurator
|
- configurator
|
||||||
- queues
|
|
||||||
- minio
|
|
||||||
- mongo
|
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
@@ -29,9 +28,3 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
configurator:
|
configurator:
|
||||||
external: true
|
external: true
|
||||||
queues:
|
|
||||||
external: true
|
|
||||||
minio:
|
|
||||||
external: true
|
|
||||||
mongo:
|
|
||||||
external: true
|
|
||||||
|
|||||||
2
blob.py
2
blob.py
@@ -1,7 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
from minio import Minio
|
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_ACCESS_KEY = os.getenv("MINIO_ACCESS_KEY", "serviceminioadmin")
|
||||||
MINIO_SECRET_KEY = os.getenv("MINIO_SECRET_KEY", "minioadmin")
|
MINIO_SECRET_KEY = os.getenv("MINIO_SECRET_KEY", "minioadmin")
|
||||||
|
|
||||||
|
|||||||
88
configurator.py
Normal file
88
configurator.py
Normal 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"))
|
||||||
82
main.py
82
main.py
@@ -4,7 +4,8 @@ import os
|
|||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from requests import get, post
|
from requests import post
|
||||||
|
from configurator import configurator
|
||||||
from mongo import mongo
|
from mongo import mongo
|
||||||
from blob import minio
|
from blob import minio
|
||||||
|
|
||||||
@@ -48,11 +49,10 @@ def call(command: str) -> Response:
|
|||||||
|
|
||||||
|
|
||||||
def get_hosts() -> list[str]:
|
def get_hosts() -> list[str]:
|
||||||
response = get(
|
if os.getenv("STAGE") == "development":
|
||||||
f"http://configurator/api/v1/fetch?project=certupdater&stage={os.getenv("STAGE")}"
|
return list(set(list(configurator.get_config("hosts"))))
|
||||||
).json()
|
else:
|
||||||
hosts = response["configs"]["hosts"]
|
return list(set(list(configurator.get_config("hosts"))))
|
||||||
return list(hosts)
|
|
||||||
|
|
||||||
|
|
||||||
def update_host(host: str) -> str | None:
|
def update_host(host: str) -> str | None:
|
||||||
@@ -104,47 +104,41 @@ def update_host(host: str) -> str | None:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
while True:
|
||||||
while True:
|
now = datetime.datetime.now()
|
||||||
now = datetime.datetime.now()
|
mongo_hosts = mongo.hosts
|
||||||
mongo_hosts = mongo.hosts
|
updated = False
|
||||||
hosts = get_hosts()
|
for host in get_hosts():
|
||||||
print(f"got hosts {hosts}")
|
if (
|
||||||
updated = False
|
now + datetime.timedelta(days=14)
|
||||||
for host in hosts:
|
> mongo_hosts.get(
|
||||||
if (
|
host, {"expire_time": datetime.datetime.fromtimestamp(1)}
|
||||||
now + datetime.timedelta(days=14)
|
)["expire_time"]
|
||||||
> mongo_hosts.get(
|
):
|
||||||
host, {"expire_time": datetime.datetime.fromtimestamp(1)}
|
success = update_host(host)
|
||||||
)["expire_time"]
|
if success:
|
||||||
):
|
send_notification(
|
||||||
success = update_host(host)
|
f"host {host} was not updated with an error: {success}"
|
||||||
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:
|
||||||
container_id_run = call("echo $(docker ps -q -f name=infra_nginx)")
|
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)"
|
||||||
|
)
|
||||||
|
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)
|
||||||
|
|||||||
3
mongo.py
3
mongo.py
@@ -4,11 +4,12 @@ import os
|
|||||||
|
|
||||||
MONGO_USER = os.getenv("MONGO_USER", "mongo")
|
MONGO_USER = os.getenv("MONGO_USER", "mongo")
|
||||||
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
|
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
|
||||||
|
MONGO_HOST = os.getenv("MONGO_HOST", "localhost")
|
||||||
|
|
||||||
|
|
||||||
class Mongo:
|
class Mongo:
|
||||||
def __init__(self):
|
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.client: pymongo.MongoClient = pymongo.MongoClient(url)
|
||||||
self.database = self.client.get_database("certupdater")
|
self.database = self.client.get_database("certupdater")
|
||||||
self.hosts_collection.create_index([
|
self.hosts_collection.create_index([
|
||||||
|
|||||||
42
storage.py
Normal file
42
storage.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
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})
|
||||||
Reference in New Issue
Block a user