Skip to content

Commit

Permalink
Merge pull request #5 from skalenetwork/enhancement/SKALE-4622-websoc…
Browse files Browse the repository at this point in the history
…ket-support

Enhancement/skale 4622 websocket support
  • Loading branch information
dmytrotkk authored Sep 27, 2021
2 parents a97c473 + 636d93a commit 85a5f75
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions periodic_config_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def parse_chains(_network: str, _path: str) -> list:

list_of_http_endpoints = list()
list_of_https_endpoints = list()
list_of_ws_endpoints = list()
list_of_wss_endpoints = list()
list_of_domains = list()

for node in nodes:
Expand All @@ -63,20 +65,28 @@ def parse_chains(_network: str, _path: str) -> list:
endpoint_https = node["https_endpoint_domain"]
print(endpoint_https)
list_of_https_endpoints.append(endpoint_https)
endpoint_ws = node["ws_endpoint_domain"]
endpoint_wss = node["wss_endpoint_domain"]
list_of_ws_endpoints.append(endpoint_ws)
list_of_wss_endpoints.append(endpoint_wss)
list_of_domains.append(node['domain'])

chain_infos.append(ChainInfo(_network, name, list_of_http_endpoints, list_of_https_endpoints, list_of_domains))

chain_infos.append(ChainInfo(_network, name, list_of_http_endpoints,
list_of_https_endpoints, list_of_ws_endpoints,
list_of_wss_endpoints, list_of_domains))
return chain_infos


class ChainInfo:
def __init__(self, _network: str, _chain_name: str, _list_of_http_endpoints: list,
_list_of_https_endpoints: list, _list_of_domains: list):
_list_of_https_endpoints: list, _list_of_ws_endpoints: list,
_list_of_wss_endpoints: list, _list_of_domains: list):
self.network = _network
self.chain_name = _chain_name
self.list_of_http_endpoints = _list_of_http_endpoints
self.list_of_https_endpoints = _list_of_https_endpoints
self.list_of_ws_endpoints = _list_of_ws_endpoints
self.list_of_wss_endpoints = _list_of_wss_endpoints
self.list_of_domains = _list_of_domains


Expand All @@ -90,7 +100,7 @@ def print_global_server_config(_f, _use_ssl: bool) -> None:
if _use_ssl:
_f.write(" listen 443 ssl;\n")
_f.write(" ssl_certificate " + CERT_FILE + ";\n")
_f.write(" ssl_certificate_key " + KEY_FILE + ";\n")
_f.write(" ssl_certificate_key " + KEY_FILE + ";\n")
_f.write(" ssl_verify_client off;\n")
else:
_f.write(" listen 80;\n")
Expand All @@ -107,6 +117,14 @@ def print_group_definition(_chain_info: ChainInfo, _f) -> None:
_f.write("}\n")


def print_ws_group_definition(_chain_info: ChainInfo, _f) -> None:
_f.write("upstream ws-" + _chain_info.chain_name + " {\n")
_f.write(" ip_hash;\n")
for endpoint in _chain_info.list_of_ws_endpoints:
_f.write(" server " + endpoint[5:] + " max_fails=1 fail_timeout=600s;\n")
_f.write("}\n")


def print_storage_group_definition(_chain_info: ChainInfo, _f) -> None:
_f.write("upstream storage-" + _chain_info.chain_name + " {\n")
_f.write(" ip_hash;\n")
Expand All @@ -115,18 +133,27 @@ def print_storage_group_definition(_chain_info: ChainInfo, _f) -> None:
_f.write("}\n")


def print_storage_proxy_for_chain(_chain_info: ChainInfo, _f) -> None:
_f.write(" location /fs/" + _chain_info.chain_name + " {\n")
_f.write(" rewrite /fs/" + _chain_info.chain_name + "/(.*) /" + _chain_info.chain_name + "/$1 break;\n")
def print_loadbalacing_config_for_chain(_chain_info: ChainInfo, _f) -> None:
_f.write(" location /"+_chain_info.network + "/" + _chain_info.chain_name + " {\n")
_f.write(" proxy_http_version 1.1;\n")
_f.write(" proxy_pass http://storage-" + _chain_info.chain_name + "/;\n")
_f.write(" proxy_pass http://" + _chain_info.chain_name + "/;\n")
_f.write(" }\n")


def print_loadbalacing_config_for_chain(_chain_info: ChainInfo, _f) -> None:
_f.write(" location /"+_chain_info.network + "/" + _chain_info.chain_name + " {\n")
def print_ws_config_for_chain(_chain_info: ChainInfo, _f) -> None:
_f.write(" location /"+_chain_info.network + "/ws/" + _chain_info.chain_name + " {\n")
_f.write(" proxy_http_version 1.1;\n")
_f.write(" proxy_pass http://" + _chain_info.chain_name + "/;\n")
_f.write(" proxy_set_header Upgrade $http_upgrade;\n")
_f.write(" proxy_set_header Connection \"upgrade\";\n")
_f.write(" proxy_pass http://ws-" + _chain_info.chain_name + "/;\n")
_f.write(" }\n")


def print_storage_proxy_for_chain(_chain_info: ChainInfo, _f) -> None:
_f.write(" location /fs/" + _chain_info.chain_name + " {\n")
_f.write(" rewrite /fs/" + _chain_info.chain_name + "/(.*) /" + _chain_info.chain_name + "/$1 break;\n")
_f.write(" proxy_http_version 1.1;\n")
_f.write(" proxy_pass http://storage-" + _chain_info.chain_name + "/;\n")
_f.write(" }\n")


Expand All @@ -136,15 +163,18 @@ def print_config_file(_chain_infos: list) -> None:
with open(TMP_CONFIG_FILE, 'w') as f:
for chain_info in _chain_infos:
print_group_definition(chain_info, f)
print_ws_group_definition(chain_info, f)
print_storage_group_definition(chain_info, f)
print_global_server_config(f, False)
for chain_info in _chain_infos:
print_loadbalacing_config_for_chain(chain_info, f)
print_ws_config_for_chain(chain_info, f)
print_storage_proxy_for_chain(chain_info, f)
f.write("}\n")
print_global_server_config(f, True)
for chain_info in _chain_infos:
print_loadbalacing_config_for_chain(chain_info, f)
print_ws_config_for_chain(chain_info, f)
print_storage_proxy_for_chain(chain_info, f)
f.write("}\n")
f.close()
Expand Down

0 comments on commit 85a5f75

Please sign in to comment.