refactored etcd client methods into EtcdClient class

This commit is contained in:
B.J. Dweck 2021-01-29 12:19:53 +02:00
parent 4c45a7d72b
commit a760965c12
3 changed files with 34 additions and 30 deletions

View File

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

View File

@ -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']

View File

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