Skip to content

Commit

Permalink
Add Syncthing file sync
Browse files Browse the repository at this point in the history
  • Loading branch information
robballantyne committed Mar 18, 2024
1 parent 53627c4 commit 06c86e3
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
20 changes: 20 additions & 0 deletions build/COPY_ROOT/etc/supervisor/supervisord/conf.d/syncthing.conf
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions build/COPY_ROOT/opt/ai-dock/bin/build/layer0/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions build/COPY_ROOT/opt/ai-dock/bin/supervisor-syncthing.sh
Original file line number Diff line number Diff line change
@@ -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*<listenAddress>/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
5 changes: 5 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -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}
Expand Down

0 comments on commit 06c86e3

Please sign in to comment.