From afb1a2f7bdcd50cc219eb00ca6139cf695943fa3 Mon Sep 17 00:00:00 2001 From: Benjamin Dweck Date: Sun, 31 Jan 2021 10:44:14 +0200 Subject: [PATCH] can sync file! integration test works --- .env | 4 +- docker-compose.test.yml | 2 +- integration-test.bat | 3 ++ integration-test.sh | 5 ++ integrationtest/Dockerfile | 6 ++- integrationtest/test.sh | 12 ----- integrationtest/test_file_sync.sh | 9 ++++ syncthing_monitor/__main__.py | 10 ++++ syncthing_monitor/syncthing_client.py | 77 +++++++++++++++++++++++++++ 9 files changed, 111 insertions(+), 17 deletions(-) create mode 100644 integration-test.bat create mode 100644 integration-test.sh delete mode 100644 integrationtest/test.sh create mode 100644 integrationtest/test_file_sync.sh diff --git a/.env b/.env index a1d104b..03cfe4e 100644 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ SYNCTHING_VERSION=latest -PUID=1001 -PGID=1001 +PUID=0 +PGID=0 TZ=Europe/London diff --git a/docker-compose.test.yml b/docker-compose.test.yml index 19088c7..86e606c 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -17,9 +17,9 @@ services: build: . links: - sync1:sync + - etcd volumes: - sync1config:/config - - etcd environment: - SYNCTHING_PUBLIC_HOST=sync1 networks: diff --git a/integration-test.bat b/integration-test.bat new file mode 100644 index 0000000..bea7eb1 --- /dev/null +++ b/integration-test.bat @@ -0,0 +1,3 @@ +docker-compose -f docker-compose.test.yml -p ci down -v +docker-compose -f docker-compose.test.yml -p ci up --build --force-recreate -d +docker wait ci_test_1 diff --git a/integration-test.sh b/integration-test.sh new file mode 100644 index 0000000..6807c01 --- /dev/null +++ b/integration-test.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +docker-compose -f docker-compose.test.yml -p ci down -v +docker-compose -f docker-compose.test.yml -p ci up --build --force-recreate -d +docker wait ci_test_1 diff --git a/integrationtest/Dockerfile b/integrationtest/Dockerfile index 5aabcc9..7bded62 100644 --- a/integrationtest/Dockerfile +++ b/integrationtest/Dockerfile @@ -4,6 +4,8 @@ RUN apt-get update && apt-get install -yq curl && apt-get clean WORKDIR /app -ADD test.sh /app/test.sh +ADD test_file_sync.sh /app/test_file_sync.sh -CMD ["bash", "test.sh"] +VOLUME [ "/data1", "/data2" ] + +CMD ["bash", "test_file_sync.sh"] diff --git a/integrationtest/test.sh b/integrationtest/test.sh deleted file mode 100644 index 14fba83..0000000 --- a/integrationtest/test.sh +++ /dev/null @@ -1,12 +0,0 @@ -sleep 5 -echo "test string" > /sync1/testfile -echo /sync1/testfile -echo /sync2/testfile - -#if curl web | grep -q 'Visits: '; then -# echo "Tests passed!" -# exit 0 -#else -# echo "Tests failed!" -# exit 1 -#fi diff --git a/integrationtest/test_file_sync.sh b/integrationtest/test_file_sync.sh new file mode 100644 index 0000000..3681800 --- /dev/null +++ b/integrationtest/test_file_sync.sh @@ -0,0 +1,9 @@ +TEST_TEXT="test string" + +echo "${TEST_TEXT}" > /data1/testfile + +# shellcheck disable=SC2034 +for i in $(seq 1 30); do + [ "${TEST_TEXT}" == "$(cat /data2/testfile)" ] && s=0 && break || s=$? && sleep 2 +done +(exit $s) diff --git a/syncthing_monitor/__main__.py b/syncthing_monitor/__main__.py index 84098fd..0f940cf 100644 --- a/syncthing_monitor/__main__.py +++ b/syncthing_monitor/__main__.py @@ -47,6 +47,12 @@ class Program: self.my_device_id = self.syncthing.get_my_device_id() print("Found My Device ID: {0}".format(self.my_device_id)) + self.syncthing.patch_config() + self.syncthing.create_shared_folder("GXWxf-3zgnU", "SharedFolder", "/data", [{'id': self.my_device_id}]) + + if not self.syncthing.config_is_in_sync(): + self.syncthing.restart() + self.etcd.register_device_update_handler(self.update_devices) self.etcd.add_device_to_cluster(self.my_device_id, self.syncthing_public_host) @@ -62,8 +68,12 @@ class Program: def update_devices(self, event): device_list = self.etcd.get_device_list() self.syncthing.add_devices(device_list) + self.syncthing.create_shared_folder("GXWxf-3zgnU", "SharedFolder", "/data", device_list) self.syncthing.print_config() + if not self.syncthing.config_is_in_sync(): + self.syncthing.restart() + if __name__ == "__main__": main() diff --git a/syncthing_monitor/syncthing_client.py b/syncthing_monitor/syncthing_client.py index 556d31b..ec17827 100644 --- a/syncthing_monitor/syncthing_client.py +++ b/syncthing_monitor/syncthing_client.py @@ -48,10 +48,87 @@ class SyncthingClient: print("Attempt to add device {0} to syncthing: {1}".format(device, response.content)) + def create_shared_folder(self, folder_id, label, path, devices): + post_data = { + "id": folder_id, + "label": label, + "filesystemType": "basic", + "path": path, + "type": "sendreceive", + "devices": [], + "rescanIntervalS": 60, + "fsWatcherEnabled": False, + "fsWatcherDelayS": 10, + "ignorePerms": False, + "autoNormalize": False, + "minDiskFree": { + "value": 1, + "unit": "%" + }, + "versioning": { + "type": "simple", + "params": { + "keep": "5" + } + }, + "copiers": 0, + "pullerMaxPendingKiB": 0, + "hashers": 0, + "order": "random", + "ignoreDelete": False, + "scanProgressIntervalS": 0, + "pullerPauseS": 0, + "maxConflicts": 10, + "disableSparseFiles": False, + "disableTempIndexes": False, + "paused": False, + "weakHashThresholdPct": 25, + "markerName": ".stfolder", + "copyOwnershipFromParent": False, + "modTimeWindowS": 0 + } + + for device in devices: + folder_device = { + 'deviceID': device['id'], + 'introducedBy': '' + } + post_data['devices'].append(folder_device) + + response = requests.post(self.make_url("/rest/config/folders"), + headers=self.headers, data=json.dumps(post_data)) + + print("Attempt to add shared folder to syncthing: {0}".format(response.content)) + @retry def print_config(self): response = requests.get(self.make_url("/rest/config/devices"), headers=self.headers) print("/rest/config/devices: {0}".format(response.content)) + response = requests.get(self.make_url("/rest/config/folders"), headers=self.headers) + print("/rest/config/folders: {0}".format(response.content)) + response = requests.get(self.make_url("/rest/config/options"), headers=self.headers) + print("/rest/config/options: {0}".format(response.content)) + + def config_is_in_sync(self): + response = requests.get(self.make_url("/rest/config/insync"), headers=self.headers) + return bool(json.loads(response.content)['configInSync']) + + def restart(self): + response = requests.post(self.make_url("/rest/system/restart"), headers=self.headers, data='') + print("System reset: {0}".format(response.content)) + + + @retry + def patch_config(self): + config_patch = { + "globalAnnounceEnabled": False, + "localAnnounceEnabled": False, + "relaysEnabled": False, + "announceLANAddresses": False, + } + response = requests.patch(self.make_url("/rest/config/options"), + headers=self.headers, data=json.dumps(config_patch)) + print("Patched syncthing configuration: {0}".format(response.content)) def make_url(self, endpoint): return "http://{0}:{1}{2}".format(self.host, self.port, endpoint)