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 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 from syncthing_monitor.syncthing_rest import SyncthingClient
DEFAULT_CONFIG_XML_PATH = '/config/config.xml' 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() my_device_id = syncthing.get_my_device_id()
print("Found Device ID: {0}".format(my_device_id)) print("Found Device ID: {0}".format(my_device_id))
append_device_id(my_device_id, 'etcd') etcd = EtcdClient('etcd')
device_ids = get_device_list('etcd') etcd.append_device_id(my_device_id)
syncthing.post_devices(device_ids) device_ids = etcd.get_device_list()
syncthing.add_devices(device_ids)
syncthing.print_debug_info() syncthing.print_debug_info()

View File

@ -6,11 +6,15 @@ from retrying import retry
CLUSTER_INFO_KEY = '/syncthing_monitor/cluster_info' CLUSTER_INFO_KEY = '/syncthing_monitor/cluster_info'
@retry class EtcdClient:
def append_device_id(device_id, host, port=2379): def __init__(self, host):
self.host = host
@retry
def append_device_id(self, device_id, port=2379):
cluster_info = {'devices': []} cluster_info = {'devices': []}
etcd = etcd3.client(host=host, port=port) etcd = etcd3.client(host=self.host, port=port)
with etcd.lock('syncthing_monitor'): with etcd.lock('syncthing_monitor'):
raw_value = etcd.get(CLUSTER_INFO_KEY)[0] raw_value = etcd.get(CLUSTER_INFO_KEY)[0]
@ -24,12 +28,11 @@ def append_device_id(device_id, host, port=2379):
print("Updating etcd value for '{0}': {1}".format(CLUSTER_INFO_KEY, json.dumps(cluster_info)), flush=True) 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)) etcd.put(CLUSTER_INFO_KEY, json.dumps(cluster_info))
@retry
@retry def get_device_list(self, port=2379):
def get_device_list(host, port=2379):
cluster_info = {'devices': []} cluster_info = {'devices': []}
etcd = etcd3.client(host=host, port=port) etcd = etcd3.client(host=self.host, port=port)
raw_value = etcd.get(CLUSTER_INFO_KEY)[0] raw_value = etcd.get(CLUSTER_INFO_KEY)[0]

View File

@ -25,7 +25,7 @@ class SyncthingClient:
headers=syncthing_headers) headers=syncthing_headers)
print("/rest/config/devices: {0}".format(response.content)) 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} syncthing_headers = {'X-API-Key': self.key}
for device_id in device_ids: for device_id in device_ids: