From a760965c12eafcbb71020844ef441edb78695876 Mon Sep 17 00:00:00 2001 From: Benjamin Dweck Date: Fri, 29 Jan 2021 12:19:53 +0200 Subject: [PATCH] refactored etcd client methods into EtcdClient class --- syncthing_monitor/__main__.py | 9 +++-- syncthing_monitor/etcd_cluster_info.py | 53 ++++++++++++++------------ syncthing_monitor/syncthing_rest.py | 2 +- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/syncthing_monitor/__main__.py b/syncthing_monitor/__main__.py index cadfcd2..bedd0f0 100644 --- a/syncthing_monitor/__main__.py +++ b/syncthing_monitor/__main__.py @@ -1,5 +1,5 @@ import syncthing_monitor.config_xml as xml -from syncthing_monitor.etcd_cluster_info import append_device_id, get_device_list +from syncthing_monitor.etcd_cluster_info import EtcdClient from syncthing_monitor.syncthing_rest import SyncthingClient DEFAULT_CONFIG_XML_PATH = '/config/config.xml' @@ -15,9 +15,10 @@ def loop(gui_port="8384", host="sync"): my_device_id = syncthing.get_my_device_id() print("Found Device ID: {0}".format(my_device_id)) - append_device_id(my_device_id, 'etcd') - device_ids = get_device_list('etcd') - syncthing.post_devices(device_ids) + etcd = EtcdClient('etcd') + etcd.append_device_id(my_device_id) + device_ids = etcd.get_device_list() + syncthing.add_devices(device_ids) syncthing.print_debug_info() diff --git a/syncthing_monitor/etcd_cluster_info.py b/syncthing_monitor/etcd_cluster_info.py index fce600e..f340fad 100644 --- a/syncthing_monitor/etcd_cluster_info.py +++ b/syncthing_monitor/etcd_cluster_info.py @@ -6,35 +6,38 @@ from retrying import retry CLUSTER_INFO_KEY = '/syncthing_monitor/cluster_info' -@retry -def append_device_id(device_id, host, port=2379): - cluster_info = {'devices': []} +class EtcdClient: + def __init__(self, host): + self.host = host - etcd = etcd3.client(host=host, port=port) + @retry + def append_device_id(self, device_id, port=2379): + cluster_info = {'devices': []} + + etcd = etcd3.client(host=self.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)) + + @retry + def get_device_list(self, port=2379): + cluster_info = {'devices': []} + + etcd = etcd3.client(host=self.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)) - - -@retry -def get_device_list(host, port=2379): - cluster_info = {'devices': []} - - etcd = etcd3.client(host=host, port=port) - - raw_value = etcd.get(CLUSTER_INFO_KEY)[0] - - if raw_value is not None: - cluster_info = json.loads(raw_value) - - print("Obtained cluster_info devices from etcd: {0}".format(json.dumps(cluster_info['devices']))) - return cluster_info['devices'] + print("Obtained cluster_info devices from etcd: {0}".format(json.dumps(cluster_info['devices']))) + return cluster_info['devices'] diff --git a/syncthing_monitor/syncthing_rest.py b/syncthing_monitor/syncthing_rest.py index d943927..922907e 100644 --- a/syncthing_monitor/syncthing_rest.py +++ b/syncthing_monitor/syncthing_rest.py @@ -25,7 +25,7 @@ class SyncthingClient: headers=syncthing_headers) print("/rest/config/devices: {0}".format(response.content)) - def post_devices(self, device_ids): + def add_devices(self, device_ids): syncthing_headers = {'X-API-Key': self.key} for device_id in device_ids: