diff --git a/Dockerfile b/Dockerfile index 50ea700..0bf72ce 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.9 +FROM python:3.7 WORKDIR /app diff --git a/docker-compose.test.yml b/docker-compose.test.yml index c39472c..abdd40d 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -19,6 +19,7 @@ services: - sync1:sync volumes: - sync1config:/config + - etcd networks: - syncnet @@ -38,6 +39,7 @@ services: build: . links: - sync2:sync + - etcd volumes: - sync2config:/config networks: @@ -57,7 +59,7 @@ services: etcd: image: quay.io/coreos/etcd - command: /usr/local/bin/etcd --data-dir=/etcd-data + command: /usr/local/bin/etcd --data-dir=/etcd-data --advertise-client-urls http://etcd:2379 --listen-client-urls=http://0.0.0.0:2379 volumes: - etcd-data:/etcd-data networks: diff --git a/requirements.txt b/requirements.txt index d5f6187..b219f5b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ +etcd3 retrying requests diff --git a/syncthing_monitor/__main__.py b/syncthing_monitor/__main__.py index 70c5e3d..f4c1e95 100644 --- a/syncthing_monitor/__main__.py +++ b/syncthing_monitor/__main__.py @@ -1,9 +1,9 @@ from syncthing_monitor.config_xml import parse_api_key, set_listen_ip_to_any +from syncthing_monitor.etcd_cluster_info import append_device_id from syncthing_monitor.syncthing_rest import get_device_id def loop(gui_port="8384", host="sync"): - api_key = parse_api_key() print("Found API Key: {0}".format(api_key)) @@ -12,6 +12,8 @@ def loop(gui_port="8384", host="sync"): device_id = get_device_id(host, gui_port, api_key) print("Found Device ID: {0}".format(device_id)) + append_device_id(device_id, 'etcd') + if __name__ == "__main__": loop() diff --git a/syncthing_monitor/etcd_cluster_info.py b/syncthing_monitor/etcd_cluster_info.py new file mode 100644 index 0000000..a481b7d --- /dev/null +++ b/syncthing_monitor/etcd_cluster_info.py @@ -0,0 +1,25 @@ +import json + +import etcd3 as etcd3 +from retrying import retry + +CLUSTER_INFO_KEY = '/syncthing_monitor/cluster_info' + + +@retry +def append_device_id(device_id, host, port=2379): + cluster_info = {'devices': []} + + etcd = etcd3.client(host=host, port=port) + + with etcd.lock('syncthing_monitor'): + raw_value = etcd.get(CLUSTER_INFO_KEY)[0] + + if raw_value is not None: + cluster_info = json.loads(raw_value) + + if not any(device_id in element for element in cluster_info['devices']): + cluster_info['devices'].append(device_id) + + print("Updating etcd value for '{0}': {1}".format(CLUSTER_INFO_KEY, json.dumps(cluster_info)), flush=True) + etcd.put(CLUSTER_INFO_KEY, json.dumps(cluster_info))