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.
76 lines
2.4 KiB
76 lines
2.4 KiB
"""Создание конфиг. файлов для хостов.""" |
|
import argparse |
|
import asyncio |
|
import os |
|
|
|
from loguru import logger |
|
|
|
from app_config import AppConfig |
|
from config_object import ConfigFactory |
|
from request_builder import RequestBulder |
|
|
|
|
|
async def get_records_count(rb: RequestBulder) -> int: |
|
"""Обертка для получения количества записей с сервера.""" |
|
resp: dict = await rb.send_request("count", json_body={}) |
|
|
|
records_count: int = int(resp["result"]) if resp else 0 |
|
|
|
return int(records_count) |
|
|
|
|
|
async def custom_wrapper( |
|
config_factory: ConfigFactory, |
|
rb: RequestBulder, |
|
json: dict, |
|
) -> None: |
|
"""Обертка для создания конфигов и их записи.""" |
|
resp: dict = 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("Сервер вернул ошибку") |
|
|
|
|
|
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, 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))
|
|
|