Skip to content

Commit

Permalink
Merge pull request #41 from anyproto/fixQuicListen
Browse files Browse the repository at this point in the history
Fix QUIC listening
  • Loading branch information
fb929 committed Apr 26, 2024
2 parents 98d528f + ed0520b commit 79ff5d8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
11 changes: 5 additions & 6 deletions docker-generateconfig/processing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ source .env

# Set file paths
DEST_PATH="./etc"
NETWORK_FILE="${DEST_PATH}/network.yml"
NETWORK_FILE="./storage/docker-generateconfig/network.yml"

echo "INFO: Create directories for all node types"
for NODE_TYPE in node-1 node-2 node-3 filenode coordinator consensusnode admin; do
Expand All @@ -17,14 +17,13 @@ echo "INFO: Create directory for aws credentials"
mkdir -p "${DEST_PATH}/.aws"

echo "INFO: Configure external listen host"
./docker-generateconfig/setListenIp.py "./storage/docker-generateconfig/nodes.yml" ${EXTERNAL_LISTEN_HOST} ${EXTERNAL_LISTEN_HOSTS}
./docker-generateconfig/setListenIp.py "./storage/docker-generateconfig/nodes.yml" "./storage/docker-generateconfig/nodesProcessed.yml"

echo "INFO: Create config for clients"
cp "./storage/docker-generateconfig/nodes.yml" "${DEST_PATH}/client.yml"
cp "./storage/docker-generateconfig/nodesProcessed.yml" "${DEST_PATH}/client.yml"

echo "INFO: Generate network file"
#sed 's|^| |; 1s|^|network:\n|' "generateconfig/nodes.yml" > "${NETWORK_FILE}"
yq eval '. as $item | {"network": $item}' --indent 2 ./storage/docker-generateconfig/nodes.yml > "${NETWORK_FILE}"
yq eval '. as $item | {"network": $item}' --indent 2 ./storage/docker-generateconfig/nodesProcessed.yml > "${NETWORK_FILE}"

echo "INFO: Generate config files for 3 nodes"
for i in {0..2}; do
Expand All @@ -47,7 +46,7 @@ cat "${NETWORK_FILE}" docker-generateconfig/etc/common.yml storage/docker-genera
> ${DEST_PATH}/any-sync-consensusnode/config.yml

echo "INFO: Copy network file to coordinator directory"
cp "storage/docker-generateconfig/nodes.yml" "${DEST_PATH}/any-sync-coordinator/network.yml"
cp "storage/docker-generateconfig/nodesProcessed.yml" "${DEST_PATH}/any-sync-coordinator/network.yml"

echo "INFO: Copy any-sync-admin config"
cp "docker-generateconfig/etc/admin.yml" "${DEST_PATH}/any-sync-admin/config.yml"
Expand Down
61 changes: 49 additions & 12 deletions docker-generateconfig/setListenIp.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import sys
import re
import yaml
import json

arguments = sys.argv[1:]
yamlFile = arguments[0]
listenHosts = arguments[1:]
# load .env vars
envVars = dict()
if os.path.exists('.env') and os.path.getsize('.env') > 0:
with open('.env') as file:
for line in file:
if line.startswith('#') or not line.strip():
continue
key, value = line.strip().split('=', 1)
value = value.replace('"', '')
if key in envVars:
print(f"WARNING: dublicate key={key} in env file='.env'")
envVars[key] = value
else:
print(f"ERROR: file='.env' not found or size=0")
exit(1)

with open(yamlFile, 'r') as file:
#print(f"DEBUG: envVars={json.dumps(envVars,indent=4)}")

inputYamlFile = sys.argv[1]
outputYamlFile = sys.argv[2]
listenHosts = envVars['EXTERNAL_LISTEN_HOSTS'].split()
if 'EXTERNAL_LISTEN_HOST' in envVars:
listenHosts.append(envVars['EXTERNAL_LISTEN_HOST'])

print(f"DEBUG: listenHosts={listenHosts}")

# read input yaml file
with open(inputYamlFile, 'r') as file:
config = yaml.load(file,Loader=yaml.Loader)

# processing addresses for nodes
for index, nodes in enumerate(config['nodes']):
addresses = nodes['addresses']
port = addresses[0].split(':')[1]
for listenHost in listenHosts:
listenAddress = listenHost +':'+ port
if listenAddress not in addresses:
addresses.append(listenAddress)

with open(yamlFile, 'w') as file:
listenHost = nodes['addresses'][0].split(':')[0]
listenPort = nodes['addresses'][0].split(':')[1]
nodeListenHosts = [listenHost] + listenHosts
for nodeListenHost in nodeListenHosts:
listenAddress = nodeListenHost +':'+ str(listenPort)
if listenAddress not in nodes['addresses']:
nodes['addresses'].append(listenAddress)
# add "quic" listen address
for name,value in envVars.items():
if re.match(r"^(ANY_SYNC_.*_PORT)$", name) and value == listenPort:
quicPortKey = name.replace('_PORT', '_QUIC_PORT')
quicPortValue = envVars[quicPortKey]
quicListenAddress = 'quic://'+ nodeListenHost +':'+ str(quicPortValue)
if ( quicPortValue ) and ( quicListenAddress not in nodes['addresses']):
nodes['addresses'].append(quicListenAddress)

# write output yaml file
with open(outputYamlFile, 'w') as file:
yaml.dump(config, file)

0 comments on commit 79ff5d8

Please sign in to comment.