can update etcd with device id

master
B.J. Dweck 2021-01-29 10:28:35 +02:00
parent 3d90ed0708
commit 055eb5932a
5 changed files with 33 additions and 3 deletions

View File

@ -1,4 +1,4 @@
FROM python:3.9
FROM python:3.7
WORKDIR /app

View File

@ -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:

View File

@ -1,2 +1,3 @@
etcd3
retrying
requests

View File

@ -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()

View File

@ -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))