You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

80 lines
2.4 KiB

import argparse
import asyncio
import os
from asyncio import Task
from dataclasses import dataclass
from typing import List
import aiohttp
from aiohttp.client_exceptions import ClientConnectorError
from loguru import logger
from app_config import AppConfig
from config_object import ConfigFactory, ConfigObject
from request_builder import Json, RequestBulder
async def get_records_count(rb: RequestBulder) -> int:
resp: Json | bool = await rb.send_request("count", json_body={})
records_count = 0
if resp:
records_count = resp.get("result")
return records_count
async def custom_wrapper(
config_factory: ConfigFactory,
rb: RequestBulder,
usr: str,
json: Json,
) -> None:
resp: Json | bool = await rb.send_request("get", json)
# Если мы получили валидный ответ, то разбираем пачку хостов из
# resp['result'] с созданием для каждой строки кофнига через
# фабрику
if resp:
conf_list = [config_factory.create(host["hostname"]) for host in resp["result"]]
await asyncio.gather(*[c.write() for c in conf_list])
else:
logger.error(f"Сервер вернул ошибку")
async def main(config_path: str) -> None:
"""Точка входа."""
cfg: AppConfig = AppConfig(config_path)
rb = RequestBulder(cfg)
while True:
records_count = await get_records_count(rb)
json_boby_list = []
for offset in range(0, records_count, cfg.request_portion):
body_json = {
"columns": "['hostname']",
"limit": cfg.request_portion,
"offset": offset,
}
json_boby_list.append(body_json)
conf_factory = ConfigFactory(cfg.template, cfg.path_for_config)
await asyncio.gather(
*[custom_wrapper(conf_factory, rb, "get", jb) for jb in json_boby_list]
)
await rb.wait()
# закрываем сессию
await rb.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Service for nginx config creation.")
parser.add_argument(
"--config_path", required=True, type=str, help="path for conifg file"
)
args = parser.parse_args()
config_path = os.path.abspath(args.config_path)
asyncio.run(main(config_path))