Skip to content

Instantly share code, notes, and snippets.

@nett00n
Created April 6, 2024 08:49
Show Gist options
  • Save nett00n/0cd014be7d6c35ee0a053e05b7f9b82c to your computer and use it in GitHub Desktop.
Save nett00n/0cd014be7d6c35ee0a053e05b7f9b82c to your computer and use it in GitHub Desktop.
syncthing entrypoint
#!/bin/sh
set -eu
[ -n "${UMASK:-}" ] && umask "$UMASK"
if [ "$(id -u)" = '0' ]; then
binary="$1"
if [ -z "${PCAP:-}" ]; then
# If Syncthing should have no extra capabilities, make sure to remove them
# from the binary. This will fail with an error if there are no
# capabilities to remove, hence the || true etc.
setcap -r "$binary" 2>/dev/null || true
else
# Set capabilities on the Syncthing binary before launching it.
setcap "$PCAP" "$binary"
fi
# Chown may fail, which may cause us to be unable to start; but maybe
# it'll work anyway, so we let the error slide.
chown "${PUID}:${PGID}" "${HOME}" || true
exec su-exec "${PUID}:${PGID}" \
env HOME="$HOME" "$@"
if [ -z ${SYNCTHING_RANDOM_PASSWORD} ]
then
test -f /var/syncthing/config/config.xml && echo "Config exists. Starting the app." && exit 0
RANDOM_PASSWORD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-16} | sed '$d;$d' | head -n 1)
echo "Config does not exist. Generating new password."
/bin/syncthing generate --gui-password="${RANDOM_PASSWORD}" --gui-user=admin
test -z "${DOMAIN_NAME}" && DOMAIN_NAME=local
echo "Starting Syncthing on syncthing.${DOMAIN_NAME}"
echo "USER: admin"
echo "PASSWORD: ${RANDOM_PASSWORD}"
fi
else
exec "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment