How to run a Typesense HA Cluster on Fly?

start.py

from subprocess import check_call
from os import execvp, environ


def call_exec(args):
    execvp(args[0], args)

class Config:
    def __init__(self, host, peer_port, api_port):
        self.host = host
        self.peer_port = peer_port
        self.api_port = api_port

if environ['FLY_REGION'] == 'lax':
    API_PORT = "8062"
elif environ['FLY_REGION'] == 'sjc':
    API_PORT = "8063"
else:
    raise AssertionError('Unsupported region {0}'.format(environ['FLY_REGION']))

configs = [Config(*h.split(':')) for h in environ['REAL_TYPESENSE_NODES'].split(',')]
config_lookup = dict([(config.api_port, config) for config in configs])

our_config = config_lookup.get(API_PORT)
other_configs = [config for config in config_lookup.values() if config != our_config]

check_call(["6tunnel", "-6", "-l", "::", "8108", "127.0.0.1", our_config.api_port])
# Force tunnel here as well.
#our_config.host
check_call(["6tunnel", "-f", "-6", "-l", "fly-local-6pn", our_config.api_port, "127.0.0.1", our_config.api_port])
#our_config.host
check_call(["6tunnel", "-f", "-6", "-l", "fly-local-6pn", our_config.peer_port, "127.0.0.1", our_config.peer_port])

# Force tunnel, -f, because other servers might not be up yet.
for config in other_configs:
    check_call(["6tunnel", "-f", "-4", "-l", "127.0.0.1", config.api_port, config.host])
    check_call(["6tunnel", "-f", "-4", "-l", "127.0.0.1", config.peer_port, config.host])


call_exec(["/opt/typesense-server", "--peering-address=127.0.0.1", "--api-address=127.0.0.1", "--data-dir=/data", "--api-key=xxx", "--api-port="+our_config.api_port, "--peering-port="+our_config.peer_port, "--nodes=/etc/typesense-nodes", "--reset-peers-on-error"])

fly.toml

# fly.toml app configuration file generated for laspilitas-ts on 2023-05-20T09:06:34-07:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "laspilitas-ts"
kill_signal = "SIGINT"
kill_timeout = "5s"

[build]
  dockerfile = "Dockerfile.remote"

[env]
  REAL_TYPESENSE_NODES = "lax.laspilitas-ts.internal:8107:8062,sjc.laspilitas-ts.internal:8109:8063"

Dockerfile.remote

FROM typesense/typesense:0.24.0.rcn21
COPY ./typesense-nodes /etc/typesense-nodes
COPY ./start.py /start.py
RUN apt update
RUN mkdir /data
RUN apt upgrade -y
RUN apt install -y python3 6tunnel curl
ENTRYPOINT ["python3", "/start.py"]
CMD ["python3", "/start.py"]