Browse Source

Initial commit

master
Дмитрий 3 years ago
commit
775c7561b9
  1. 22
      server.py
  2. 60
      solution.py
  3. 47
      template_conf.conf

22
server.py

@ -0,0 +1,22 @@
from flask import Flask, jsonify, request
import json
app = Flask(__name__)
# http get 127.0.0.1:5000/api/portal/get columns:='["hostname"]' limit=9
@app.route("/api/portal/get", methods=["GET"])
def index():
print(request.json)
columns = request.json.get("columns")
limit = int(request.json.get("limit"))
hosts: List[dict] = []
for host in range(1, limit + 1):
hosts.append({"hostname": f"content-creator-{host}"})
# определить количество хостов в запросе ключ `limit`
return jsonify({"result": hosts, "done": True})
app.run()

60
solution.py

@ -0,0 +1,60 @@
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()

47
template_conf.conf

@ -0,0 +1,47 @@
server {
listen 80;
listen [::]:80;
server_name _;
location /.well-known/acme-challenge/ {
content_by_lua_block {
auto_ssl:challenge_server()
}
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name _;
ssl_certificate_by_lua_block {
auto_ssl:ssl_certificate()
}
ssl_certificate /etc/ssl/resty-auto-ssl-fallback.crt;
ssl_certificate_key /etc/ssl/resty-auto-ssl-fallback.key;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_intercept_errors off;
client_max_body_size 0;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
location / {
proxy_pass http://88.99.161.26:31653;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Loading…
Cancel
Save