|
|
|
@ -5,36 +5,53 @@ from typing import List
|
|
|
|
|
import os |
|
|
|
|
import configparser |
|
|
|
|
import aiohttp |
|
|
|
|
import aiofiles |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def write_config_files( |
|
|
|
|
servers: list, path_to_template_file: str, path_for_config: str |
|
|
|
|
hosts: list, path_to_template_file: str, path_to_config_dir: str, template: str |
|
|
|
|
) -> None: |
|
|
|
|
"""Записываем конфиги для списка hosts.""" |
|
|
|
|
full_path_to_config_dir: str = os.path.abspath(path_to_config_dir) |
|
|
|
|
if not os.path.isdir(full_path_to_config_dir): |
|
|
|
|
os.mkdir(full_path_to_config_dir) |
|
|
|
|
|
|
|
|
|
for host in hosts: |
|
|
|
|
config_filename: str = f"{host}.conf" |
|
|
|
|
config_file = os.path.join(full_path_to_config_dir, config_filename) |
|
|
|
|
|
|
|
|
|
if not os.path.isfile(config_file): |
|
|
|
|
config_body: str = template.replace( |
|
|
|
|
"server_name _;", f"server_name {host}.server.com;" |
|
|
|
|
) |
|
|
|
|
async with aiofiles.open(config_file, mode="w") as file: |
|
|
|
|
await file.write(config_body) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_template(templ_file: str) -> str: |
|
|
|
|
"""Возвращает шаблон конфига для хоста из файла `templ_file`.""" |
|
|
|
|
|
|
|
|
|
template: str = "" |
|
|
|
|
with open(path_to_template_file, "r") as file: |
|
|
|
|
with open(templ_file, "r") as file: |
|
|
|
|
template = file.read() |
|
|
|
|
|
|
|
|
|
config_full_path: str = os.path.abspath(path_for_config) |
|
|
|
|
if not os.path.isdir(config_full_path): |
|
|
|
|
os.mkdir(config_full_path) |
|
|
|
|
return template |
|
|
|
|
|
|
|
|
|
for server in servers: |
|
|
|
|
|
|
|
|
|
config_body: str = template.replace( |
|
|
|
|
"server_name _;", f"server_name {server}.server.com;" |
|
|
|
|
) |
|
|
|
|
config_filename: str = f"{server}.conf" |
|
|
|
|
|
|
|
|
|
with open(os.path.join(config_full_path, config_filename), "w") as file: |
|
|
|
|
file.write(config_body) |
|
|
|
|
async def get_records_count() -> int: |
|
|
|
|
async with aiohttp.ClientSession() as session: |
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# изменить сигнатуру на (cfg: Conifg, json_body: dict) |
|
|
|
|
async def send_async_request(cfg: dict, columns: list, limit: int = 1) -> None: |
|
|
|
|
body = {"columns": columns, "limit": 1} |
|
|
|
|
server = cfg["central_host_url"] |
|
|
|
|
path_to_template_file = cfg["template"] |
|
|
|
|
path_for_config = cfg["path_for_config"] |
|
|
|
|
|
|
|
|
|
template: str = _get_template(path_to_template_file) |
|
|
|
|
|
|
|
|
|
async with aiohttp.ClientSession() as session: |
|
|
|
|
tasks = [session.get(server, json=body) for i in range(limit)] |
|
|
|
|
responses = await asyncio.gather(*tasks) |
|
|
|
@ -42,8 +59,9 @@ async def send_async_request(cfg: dict, columns: list, limit: int = 1) -> None:
|
|
|
|
|
for response in responses: |
|
|
|
|
r = await response.json() |
|
|
|
|
hosts = [i["hostname"] for i in r.get("result")] |
|
|
|
|
|
|
|
|
|
await write_config_files(hosts, path_to_template_file, path_for_config) |
|
|
|
|
await write_config_files( |
|
|
|
|
hosts, path_to_template_file, path_for_config, template |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read_config(path_to_conf_file: str, section: str = "Main") -> dict: |
|
|
|
@ -84,7 +102,7 @@ async def main() -> None:
|
|
|
|
|
wait_sec: int = int(cnf["frequency_sec"]) |
|
|
|
|
|
|
|
|
|
while True: |
|
|
|
|
await send_async_request(cnf, columns=["hostname"], limit=9999) |
|
|
|
|
await send_async_request(cnf, columns=["hostname"], limit=10) |
|
|
|
|
await asyncio.sleep(wait_sec) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|