diff --git a/build/COPY_ROOT/etc/supervisor/supervisord/conf.d/syncthing.conf b/build/COPY_ROOT/etc/supervisor/supervisord/conf.d/syncthing.conf new file mode 100644 index 0000000..cc39aed --- /dev/null +++ b/build/COPY_ROOT/etc/supervisor/supervisord/conf.d/syncthing.conf @@ -0,0 +1,20 @@ +[program:syncthing] +user=user +environment=PROC_NAME="%(program_name)s",USER=$USER_NAME,HOME=/home/$USER_NAME +command=supervisor-syncthing.sh +process_name=%(program_name)s +numprocs=1 +directory=/home/$USER_NAME +priority=100 +autostart=true +startsecs=5 +startretries=3 +autorestart=unexpected +stopsignal=TERM +stopwaitsecs=10 +stopasgroup=true +killasgroup=true +stdout_logfile=/var/log/supervisor/syncthing.log +stdout_logfile_maxbytes=10MB +stdout_logfile_backups=1 +redirect_stderr=true diff --git a/build/COPY_ROOT/opt/ai-dock/bin/build/layer0/common.sh b/build/COPY_ROOT/opt/ai-dock/bin/build/layer0/common.sh index 3a0fe1a..adbd894 100755 --- a/build/COPY_ROOT/opt/ai-dock/bin/build/layer0/common.sh +++ b/build/COPY_ROOT/opt/ai-dock/bin/build/layer0/common.sh @@ -118,6 +118,16 @@ mkdir -p /var/empty mkdir -p /etc/rclone touch /etc/rclone/rclone.conf +# Install SyncThing to enable transport between local machine and cloud instance + +SYNCTHING_VERSION="$(curl -fsSL "https://api.github.com/repos/syncthing/syncthing/releases/latest" \ + | jq -r '.tag_name' | sed 's/[^0-9\.\-]*//g')" + +SYNCTHING_URL="https://github.com/syncthing/syncthing/releases/download/v${SYNCTHING_VERSION}/syncthing-linux-amd64-v${SYNCTHING_VERSION}.tar.gz" +mkdir /opt/syncthing/ +wget -O /opt/syncthing.tar.gz $SYNCTHING_URL && (cd /opt && tar -zxf syncthing.tar.gz -C /opt/syncthing/ --strip-components=1) && rm -f /opt/syncthing.tar.gz +ln -s /opt/syncthing/syncthing /opt/ai-dock/bin/syncthing + # Ensure correct environment for child builds printf "source /opt/ai-dock/etc/environment.sh\n" >> /etc/profile.d/02-ai-dock.sh diff --git a/build/COPY_ROOT/opt/ai-dock/bin/supervisor-syncthing.sh b/build/COPY_ROOT/opt/ai-dock/bin/supervisor-syncthing.sh new file mode 100755 index 0000000..6dbe7c7 --- /dev/null +++ b/build/COPY_ROOT/opt/ai-dock/bin/supervisor-syncthing.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +trap cleanup EXIT + +LISTEN_PORT=18384 +METRICS_PORT=${SYNCTHING_METRICS_PORT:-28384} +PROXY_PORT=${SYNCTHING_PORT_HOST:-8384} +QUICKTUNNELS=true + +SERVICE_NAME="Syncthing (File Sync)" + +function cleanup() { + rm /run/http_ports/$PROXY_PORT > /dev/null 2>&1 + fuser -k -SIGTERM ${LISTEN_PORT}/tcp > /dev/null 2>&1 & + wait -n +} + +function start() { + source /opt/ai-dock/etc/environment.sh + + if [[ ${SERVERLESS,,} = "true" ]]; then + printf "Refusing to start $SERVICE_NAME in serverless mode\n" + exec sleep 6 + fi + + file_content="$( + jq --null-input \ + --arg listen_port "${LISTEN_PORT}" \ + --arg metrics_port "${METRICS_PORT}" \ + --arg proxy_port "${PROXY_PORT}" \ + --arg proxy_secure "${PROXY_SECURE,,}" \ + --arg service_name "${SERVICE_NAME}" \ + '$ARGS.named' + )" + + printf "%s\n" "$file_content" > /run/http_ports/$PROXY_PORT + + printf "Starting ${SERVICE_NAME}...\n" + + fuser -k -SIGKILL ${LISTEN_PORT}/tcp > /dev/null 2>&1 & + wait -n + + syncthing generate + + sed -i '/^\s*/d' "/home/${USER_NAME}/.local/state/syncthing/config.xml" + + syncthing --gui-address="127.0.0.1:${LISTEN_PORT}" --gui-apikey="${WEB_TOKEN}" & + syncthing_pid=$! + + until curl -i 127.0.0.1:${LISTEN_PORT} > /dev/null 2>&1; do + sleep 1 + done + + # Already behind proxy with auth + syncthing cli --gui-address="127.0.0.1:${LISTEN_PORT}" --gui-apikey="${WEB_TOKEN}" config gui insecure-admin-access set true + syncthing cli --gui-address="127.0.0.1:${LISTEN_PORT}" --gui-apikey="${WEB_TOKEN}" config gui insecure-skip-host-check set true + syncthing cli --gui-address="127.0.0.1:${LISTEN_PORT}" --gui-apikey="${WEB_TOKEN}" config options raw-listen-addresses add "tcp://0.0.0.0:${SYNCTHING_TRANSPORT_PORT_HOST:-22999}" + syncthing cli --gui-address="127.0.0.1:${LISTEN_PORT}" --gui-apikey="${WEB_TOKEN}" config options raw-listen-addresses add default + + wait $syncthing_pid +} + +start 2>&1 \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index f6e87b1..b4007df 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -45,6 +45,9 @@ services: ports: # SSH available on host machine port 2222 to avoid conflict. Change to suit - ${SSH_PORT_HOST:-2222}:22 + # Syncthing + - ${SYNCTHING_UI_PORT_HOST:-8384}:${SYNCTHING_UI_PORT_HOST:-8384} + - ${SYNCTHING_TRANSPORT_PORT_HOST:-20000}:${SYNCTHING_TRANSPORT_PORT_HOST:-22999} # Caddy port for service portal - ${SERVICEPORTAL_PORT_HOST:-1111}:${SERVICEPORTAL_PORT_HOST:-1111} @@ -61,6 +64,8 @@ services: - WEB_USER=${WEB_USER:-user} - WEB_PASSWORD=${WEB_PASSWORD:-password} - SERVERLESS=${SERVERLESS:-false} + - SYNCTHING_UI_PORT_HOST=${SYNCTHING_UI_PORT_HOST:-8384} + - SYNCTHING_TRANSPORT_PORT_HOST=${SYNCTHING_TRANSPORT_PORT_HOST:-22999} - SSH_PORT_HOST=${SSH_PORT_HOST:-2222} - SERVICEPORTAL_PORT_HOST=${SERVICEPORTAL_PORT_HOST:-1111} - SERVICEPORTAL_METRICS_PORT=${SERVICEPORTAL_METRICS_PORT:-21111}