From 3c0eece3116a1482edb7a9f0b0f399e4e8bc9ba4 Mon Sep 17 00:00:00 2001 From: Benjamin Dweck Date: Thu, 28 Jan 2021 12:56:11 +0200 Subject: [PATCH] can parse api key --- .env | 7 ++ Dockerfile | 8 +- app.py | 16 --- docker-compose.test.yml | 78 ++++++++++-- docker-compose.yml | 38 ++++-- Dockerfile.test => integrationtest/Dockerfile | 0 integrationtest/test.sh | 12 ++ requirements.txt | 2 - syncthing_monitor/__init__.py | 0 syncthing_monitor/__main__.py | 10 ++ syncthing_monitor/config_xml.py | 10 ++ test.sh | 8 -- test/__init__.py | 0 test/test-syncthing-config.xml | 114 ++++++++++++++++++ test/test_config.py | 10 ++ 15 files changed, 261 insertions(+), 52 deletions(-) create mode 100644 .env delete mode 100644 app.py rename Dockerfile.test => integrationtest/Dockerfile (100%) create mode 100644 integrationtest/test.sh create mode 100644 syncthing_monitor/__init__.py create mode 100644 syncthing_monitor/__main__.py create mode 100644 syncthing_monitor/config_xml.py delete mode 100644 test.sh create mode 100644 test/__init__.py create mode 100644 test/test-syncthing-config.xml create mode 100644 test/test_config.py diff --git a/.env b/.env new file mode 100644 index 0000000..c094305 --- /dev/null +++ b/.env @@ -0,0 +1,7 @@ +SYNCTHING_VERSION=latest +PUID=1001 +PGID=1001 +TZ=Europe/London +SYNC_LISTEN_PORT=22000 +SYNC1_LISTEN_PORT=22100 +SYNC2_LISTEN_PORT=22101 diff --git a/Dockerfile b/Dockerfile index c79dd2e..50ea700 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,10 @@ -FROM python:2.7 +FROM python:3.9 WORKDIR /app ADD requirements.txt /app/requirements.txt RUN pip install -r requirements.txt -ADD app.py /app/app.py +ADD syncthing_monitor /app/syncthing_monitor -EXPOSE 80 - -CMD ["python", "app.py"] +CMD ["python", "-m", "syncthing_monitor"] diff --git a/app.py b/app.py deleted file mode 100644 index dcff02f..0000000 --- a/app.py +++ /dev/null @@ -1,16 +0,0 @@ -from flask import Flask -from redis import Redis - -app = Flask(__name__) -redis = Redis(host="redis") - -@app.route("/") -def hello(): - visits = redis.incr('counter') - html = "

Hello World!

" \ - "Visits: {visits}" \ - "
" - return html.format(visits=visits) - -if __name__ == "__main__": - app.run(host="0.0.0.0", port=80) \ No newline at end of file diff --git a/docker-compose.test.yml b/docker-compose.test.yml index 04d6185..bf363ad 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -1,12 +1,66 @@ -sut: - build: . - dockerfile: Dockerfile.test - links: - - web -web: - build: . - dockerfile: Dockerfile - links: - - redis -redis: - image: redis +--- +version: "2.1" + +services: + test: + build: integrationtest + links: + - sync1 + - sync2 + volumes: + - sync1data:/data1 + - sync2data:/data2 + + sut1: + build: . + links: + - sync1 + volumes: + - sync1config:/config:ro + + sync1: + image: ghcr.io/linuxserver/syncthing:${SYNCTHING_VERSION} + environment: + - PUID=${PUID} + - PGID=${PGID} + - TZ=${TZ} + volumes: + - sync1config:/config + - sync1data:/data + ports: + - ${SYNC1_LISTEN_PORT}:22000 + + sut2: + build: . + links: + - sync2 + volumes: + - sync2config:/config:ro + + sync2: + image: ghcr.io/linuxserver/syncthing:${SYNCTHING_VERSION} + environment: + - PUID=${PUID} + - PGID=${PGID} + - TZ=${TZ} + volumes: + - sync2config:/config + - sync2data:/data + ports: + - ${SYNC2_LISTEN_PORT}:22000 + + etcd: + image: quay.io/coreos/etcd + command: /usr/local/bin/etcd --data-dir=/etcd-data + ports: + - 2379:2379 + - 2380:2380 + volumes: + - etcd-data:/etcd-data + +volumes: + sync1data: + sync1config: + sync2data: + sync2config: + etcd-data: \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index c6014fb..1468bb0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,9 +1,29 @@ -web: - build: . - dockerfile: Dockerfile - links: - - redis - ports: - - "80:80" -redis: - image: redis \ No newline at end of file +--- +version: "2.1" + +services: + syncmon: + build: . + links: + - sync + volumes: + - syncconfig:/config + - syncdata:/data + + sync: + image: ghcr.io/linuxserver/syncthing:${SYNCTHING_VERSION} + container_name: syncthing + hostname: syncthing + environment: + - PUID=${PUID} + - PGID=${PGID} + - TZ=${TZ} + volumes: + - syncconfig:/config + - syncdata:/data + ports: + - ${SYNC_LISTEN_PORT}:22000 + +volumes: + syncconfig: + syncdata: diff --git a/Dockerfile.test b/integrationtest/Dockerfile similarity index 100% rename from Dockerfile.test rename to integrationtest/Dockerfile diff --git a/integrationtest/test.sh b/integrationtest/test.sh new file mode 100644 index 0000000..14fba83 --- /dev/null +++ b/integrationtest/test.sh @@ -0,0 +1,12 @@ +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/requirements.txt b/requirements.txt index 8862084..e69de29 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +0,0 @@ -Flask -Redis \ No newline at end of file diff --git a/syncthing_monitor/__init__.py b/syncthing_monitor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/syncthing_monitor/__main__.py b/syncthing_monitor/__main__.py new file mode 100644 index 0000000..43b3950 --- /dev/null +++ b/syncthing_monitor/__main__.py @@ -0,0 +1,10 @@ +from syncthing_monitor.config_xml import parse_api_key + + +def loop(): + api_key = parse_api_key() + print("Found API Key: {0}".format(api_key)) + + +if __name__ == "__main__": + loop() diff --git a/syncthing_monitor/config_xml.py b/syncthing_monitor/config_xml.py new file mode 100644 index 0000000..3a48dd3 --- /dev/null +++ b/syncthing_monitor/config_xml.py @@ -0,0 +1,10 @@ +from xml.etree import ElementTree as elementTree + + +def parse_api_key(xml_path='/config/config.xml'): + root = elementTree.parse(xml_path).getroot() + api_key = '' + for gui in root.findall('gui'): + # noinspection SpellCheckingInspection + api_key = gui.find('apikey').text + return api_key diff --git a/test.sh b/test.sh deleted file mode 100644 index f14ae47..0000000 --- a/test.sh +++ /dev/null @@ -1,8 +0,0 @@ -sleep 5 -if curl web | grep -q 'Visits: '; then - echo "Tests passed!" - exit 0 -else - echo "Tests failed!" - exit 1 -fi diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test-syncthing-config.xml b/test/test-syncthing-config.xml new file mode 100644 index 0000000..d44bc55 --- /dev/null +++ b/test/test-syncthing-config.xml @@ -0,0 +1,114 @@ + + + basic + + + + + + + 1 + + 0 + + 0 + 0 + 0 + random + false + 0 + 0 + 10 + false + false + false + 25 + .stfolder + false + 0 + 2 + false + standard + standard + false + false + + +
dynamic
+ false + 10.122.122.0/24 + false + 0 + 0 + 0 + false + 0 +
+ +
tcp://blackberry
+ false + 10.122.122.0/24 + false + 0 + 0 + 0 + false + 0 +
+ +
127.0.0.1:8384
+ syncadmin + $2a$10$.nzDwmF2w9gWfPFWto1wmOOek4RzEF/MlolVyzOLsOCXL733IleVC + dFoLNEaqEZFSP62EFVGC2Ds5juuTmvH2 + default +
+ + + default + default + false + false + 21027 + [ff12::8384]:21027 + 0 + 0 + 60 + false + 10 + true + false + 60 + 30 + 10 + -1 + 3 + + https://data.syncthing.net/newdata + false + 1800 + true + 12 + false + 24 + false + 5 + false + 1 + https://upgrades.syncthing.net/meta.json + false + 10 + 0 + ~ + true + 0 + https://crash.syncthing.net/newcrash + false + 180 + 20 + default + auto + 0 + false + false + +
diff --git a/test/test_config.py b/test/test_config.py new file mode 100644 index 0000000..602c175 --- /dev/null +++ b/test/test_config.py @@ -0,0 +1,10 @@ +import unittest + +from syncthing_monitor.config_xml import parse_api_key + + +class ConfigTests(unittest.TestCase): + + def test_can_parse_api_key(self): + # noinspection SpellCheckingInspection + self.assertEqual(parse_api_key('test/test-syncthing-config.xml'), "dFoLNEaqEZFSP62EFVGC2Ds5juuTmvH2")