refactored etcd client methods into EtcdClient class
This commit is contained in:
parent
4c45a7d72b
commit
a760965c12
|
@ -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()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,35 +6,38 @@ 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):
|
||||||
cluster_info = {'devices': []}
|
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]
|
raw_value = etcd.get(CLUSTER_INFO_KEY)[0]
|
||||||
|
|
||||||
if raw_value is not None:
|
if raw_value is not None:
|
||||||
cluster_info = json.loads(raw_value)
|
cluster_info = json.loads(raw_value)
|
||||||
|
|
||||||
if not any(device_id in element for element in cluster_info['devices']):
|
print("Obtained cluster_info devices from etcd: {0}".format(json.dumps(cluster_info['devices'])))
|
||||||
cluster_info['devices'].append(device_id)
|
return cluster_info['devices']
|
||||||
|
|
||||||
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']
|
|
||||||
|
|
|
@ -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:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user