Browse Source

Рабочая но бесполезная версия на aiohttp

master
Дмитрий 3 years ago
parent
commit
07a1af133b
  1. 71
      solution.py

71
solution.py

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

Loading…
Cancel
Save