|
|
|
@ -4,51 +4,49 @@ import json
|
|
|
|
|
from typing import List |
|
|
|
|
import os |
|
|
|
|
import configparser |
|
|
|
|
import aiohttp |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def write_config_file( |
|
|
|
|
server_name: str, path_to_template_file: str, path_for_config: str |
|
|
|
|
async def write_config_files( |
|
|
|
|
servers: list, path_to_template_file: str, path_for_config: str |
|
|
|
|
) -> None: |
|
|
|
|
template: str = "" |
|
|
|
|
with open(path_to_template_file, "r") as file: |
|
|
|
|
template = file.read() |
|
|
|
|
|
|
|
|
|
config_body: str = template.replace( |
|
|
|
|
"server_name _;", f"server_name {server_name}.server.com;" |
|
|
|
|
) |
|
|
|
|
condifg_full_path: str = os.path.abspath(path_for_config) |
|
|
|
|
config_filename: str = f"{server_name}.conf" |
|
|
|
|
config_full_path: str = os.path.abspath(path_for_config) |
|
|
|
|
if not os.path.isdir(config_full_path): |
|
|
|
|
os.mkdir(config_full_path) |
|
|
|
|
|
|
|
|
|
if not os.path.isdir(condifg_full_path): |
|
|
|
|
os.mkdir(condifg_full_path) |
|
|
|
|
for server in servers: |
|
|
|
|
|
|
|
|
|
with open(os.path.join(condifg_full_path, config_filename), "w") as file: |
|
|
|
|
file.write(config_body) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def send_request(server: str, columns: list, limit: int = 1) -> dict: |
|
|
|
|
response = requests.get(server, json={"columns": columns, "limit": limit}) |
|
|
|
|
|
|
|
|
|
return response.json() |
|
|
|
|
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_hosts(server_response: dict) -> List[str]: |
|
|
|
|
"""Получить хосты из ответа сервера. |
|
|
|
|
|
|
|
|
|
Parameters |
|
|
|
|
---------- |
|
|
|
|
server_response : 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"] |
|
|
|
|
|
|
|
|
|
hosts: list = [] |
|
|
|
|
async with aiohttp.ClientSession() as session: |
|
|
|
|
tasks = [session.get(server, json=body) for i in range(limit)] |
|
|
|
|
responses = await asyncio.gather(*tasks) |
|
|
|
|
|
|
|
|
|
for host in server_response.get("result"): |
|
|
|
|
hosts.append(host.get("hostname")) |
|
|
|
|
for response in responses: |
|
|
|
|
r = await response.json() |
|
|
|
|
hosts = [i["hostname"] for i in r.get("result")] |
|
|
|
|
|
|
|
|
|
return hosts |
|
|
|
|
await write_config_files(hosts, path_to_template_file, path_for_config) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def read_config(path_to_conf_file: str, section: str = "Main") -> dict: |
|
|
|
|
def read_config(path_to_conf_file: str, section: str = "Main") -> dict: |
|
|
|
|
""" |
|
|
|
|
Считать конфиг с помощью `configparser`. |
|
|
|
|
|
|
|
|
@ -79,25 +77,14 @@ async def read_config(path_to_conf_file: str, section: str = "Main") -> dict:
|
|
|
|
|
return dict(config.items(section)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main(): |
|
|
|
|
async def main() -> None: |
|
|
|
|
|
|
|
|
|
cnf = await read_config("service.conf") |
|
|
|
|
cnf = read_config("service.conf") |
|
|
|
|
|
|
|
|
|
wait_sec: int = int(cnf["frequency_sec"]) |
|
|
|
|
|
|
|
|
|
while True: |
|
|
|
|
resp = await send_request( |
|
|
|
|
cnf["central_host_url"], columns=["hostname"], limit=9 |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
hosts = await get_hosts(resp) |
|
|
|
|
|
|
|
|
|
for host in hosts: |
|
|
|
|
await write_config_file( |
|
|
|
|
server_name=host, |
|
|
|
|
path_to_template_file=cnf["template"], |
|
|
|
|
path_for_config=cnf["path_for_config"], |
|
|
|
|
) |
|
|
|
|
await send_async_request(cnf, columns=["hostname"], limit=9999) |
|
|
|
|
await asyncio.sleep(wait_sec) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|