syncthing-monitor/syncthing_monitor/etcd_cluster_info.py

26 lines
746 B
Python
Raw Normal View History

2021-01-29 08:28:35 +00:00
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))