import asyncio import requests import json from typing import List import os def write_config_file( server_name: str, 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};") 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): os.mkdir(condifg_full_path) with open(os.path.join(condifg_full_path, config_filename), "w") as file: file.write(config_body) def send_request(server: str, columns: list, limit: int) -> dict: response = requests.get(server, json={"columns": columns, "limit": limit}) return response.json() def get_hosts(server_response: dict) -> List[str]: hosts: list = [] for host in server_response.get("result"): hosts.append(host.get("hostname")) return hosts def main(): TEMPLATE = "template_conf.conf" PATH_FOR_CONFIG = "ng" FREQUENCY_SEC = 10 resp = send_request( "http://127.0.0.1:5000/api/portal/get", columns=["hostname"], limit=9 ) hosts = get_hosts(resp) for host in hosts: write_config_file( server_name=host, path_to_template_file=TEMPLATE, path_for_config=PATH_FOR_CONFIG, ) if __name__ == "__main__": main()