Skip to content

Instantly share code, notes, and snippets.

@AndrewSav
Last active June 2, 2020 08:25
Show Gist options
  • Save AndrewSav/765c57197839c570c1606e36faf95720 to your computer and use it in GitHub Desktop.
Save AndrewSav/765c57197839c570c1606e36faf95720 to your computer and use it in GitHub Desktop.
# Testing Syncthing interaction via REST api from scratch via command line
# Make sure you have xml2 and jq:
# apt intall xml2 jq
# Create docker network for the container instances
docker network create syncthing
# Create individual directories for each instance
mkdir -p syncthing1
mkdir -p syncthing2
# Create two containers
docker run --network syncthing --name syncthing1 -d -p 61001:8384 -v "$(pwd)/syncthing1:/var/syncthing" syncthing/syncthing
docker run --network syncthing --name syncthing2 -d -p 61002:8384 -v "$(pwd)/syncthing2:/var/syncthing" syncthing/syncthing
# Wait a bit here for the containers to initialize
# Get Device IDs and API keys
DEVICE_ID1=$(xml2 < syncthing1/config/config.xml | grep "/configuration/device/@id" | cut -d '=' -f 2)
DEVICE_ID2=$(xml2 < syncthing2/config/config.xml | grep "/configuration/device/@id" | cut -d '=' -f 2)
API_KEY1=$(xml2 < syncthing1/config/config.xml | grep "/configuration/gui/apikey" | cut -d '=' -f 2)
API_KEY2=$(xml2 < syncthing2/config/config.xml | grep "/configuration/gui/apikey" | cut -d '=' -f 2)
# Add each instance as a peer to the other
CONFIG2=$(curl -s -X GET -H "X-API-Key: $API_KEY2" http://localhost:61002/rest/system/config | jq ".devices += [{\"deviceID\":\"$DEVICE_ID1\",\"addresses\": [\"tcp://syncthing1:22000\"]}]")
curl -X POST -H "X-API-Key: $API_KEY2" http://localhost:61002/rest/system/config -d "$CONFIG2"
CONFIG1=$(curl -s -X GET -H "X-API-Key: $API_KEY1" http://localhost:61001/rest/system/config | jq ".devices += [{\"deviceID\":\"$DEVICE_ID2\",\"addresses\": [\"tcp://syncthing2:22000\"]}]")
curl -X POST -H "X-API-Key: $API_KEY1" http://localhost:61001/rest/system/config -d "$CONFIG1"
# Share the default directory from both sides:
CONFIG2=$(curl -s -X GET -H "X-API-Key: $API_KEY2" http://localhost:61002/rest/system/config | jq ".folders[0].devices += [{\"deviceID\":\"$DEVICE_ID1\"}]")
curl -X POST -H "X-API-Key: $API_KEY2" http://localhost:61002/rest/system/config -d "$CONFIG2"
CONFIG1=$(curl -s -X GET -H "X-API-Key: $API_KEY1" http://localhost:61001/rest/system/config | jq ".folders[0].devices += [{\"deviceID\":\"$DEVICE_ID2\"}]")
curl -X POST -H "X-API-Key: $API_KEY1" http://localhost:61001/rest/system/config -d "$CONFIG1"
# Create a file and force sync
touch syncthing1/Sync/.env
curl -X POST -H "X-API-Key: $API_KEY1" http://localhost:61001/rest/db/scan?folder=default
# Wait a moment to stabilize
# Check that the file is synced
ls -al syncthing2/Sync/
# Clean up
docker rm syncthing1 -f
docker rm syncthing2 -f
rm -rf syncthing1
rm -rf syncthing2
docker network rm syncthing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment