externalized config params (incl etcd key) as env vars

This commit is contained in:
bj
2021-01-31 13:37:25 +02:00
parent 9f4084fb53
commit 738d8bb8f9
6 changed files with 57 additions and 31 deletions
+19 -14
View File
@@ -13,29 +13,34 @@ SHARED_FOLDER_LABEL = "syncthing_monitor_data"
SYNCTHING_CONFIG_XML_PATH = '/config/config.xml'
SYNCTHING_GUI_PORT = 8384
ETCD_PORT = 2379
ETCD_KEY = '/syncthing_monitor/cluster_info'
def main():
syncthing_mon = SyncthingMonitor(os.getenv('SYNCTHING_HOSTNAME'),
os.getenv('SYNCTHING_ADDRESS'),
os.getenv('SYNCTHING_DATA_PATH'),
SYNCTHING_CONFIG_XML_PATH, "sync", SYNCTHING_GUI_PORT,
"etcd", ETCD_PORT)
syncthing_mon = SyncthingMonitor(os.getenv('SYNCTHING_NODE_NAME', 'sync'),
os.getenv('SYNCTHING_PUBLISH_ADDRESS', 'tcp4://sync'),
os.getenv('SYNCTHING_DATA_PATH', '/data'),
os.getenv('SYNCTHING_CONFIG_XML_PATH', SYNCTHING_CONFIG_XML_PATH),
os.getenv('SYNCTHING_LOCAL_HOSTNAME', 'sync'),
os.getenv('SYNCTHING_GUI_PORT', SYNCTHING_GUI_PORT),
os.getenv('ETCD_HOSTNAME', 'etcd'),
os.getenv('ETCD_PORT', ETCD_PORT),
os.getenv('ETCD_KEY', ETCD_KEY))
syncthing_mon.start()
class SyncthingMonitor:
def __init__(self, syncthing_public_host, syncthing_address, syncthing_data_path,
syncthing_config_xml_path, syncthing_hostname, syncthing_gui_port,
etcd_hostname, etcd_port):
self.syncthing_hostname = syncthing_public_host
self.syncthing_address = syncthing_address
def __init__(self, syncthing_node_name, syncthing_publish_address, syncthing_data_path,
syncthing_config_xml_path, syncthing_local_hostname, syncthing_gui_port,
etcd_hostname, etcd_port, etcd_key):
self.syncthing_node_name = syncthing_node_name
self.syncthing_publish_address = syncthing_publish_address
self.syncthing_data_path = syncthing_data_path
self.my_device_id = None
self.syncthing = None
self.etcd = EtcdClient(etcd_hostname, etcd_port)
self.etcd = EtcdClient(etcd_hostname, etcd_port, etcd_key)
self.syncthing_gui_port = syncthing_gui_port
self.syncthing_hostname = syncthing_hostname
self.syncthing_local_hostname = syncthing_local_hostname
self.syncthing_config_xml_path = syncthing_config_xml_path
@retry
@@ -48,7 +53,7 @@ class SyncthingMonitor:
self.syncthing_config_xml_path,
self.syncthing_gui_port)
self.syncthing = SyncthingClient(api_key, self.syncthing_hostname, self.syncthing_gui_port)
self.syncthing = SyncthingClient(api_key, self.syncthing_local_hostname, self.syncthing_gui_port)
@retry
def start(self):
@@ -63,7 +68,7 @@ class SyncthingMonitor:
self.syncthing.sync_config()
self.etcd.register_device_update_handler(self.update_devices)
self.etcd.add_device_to_cluster(self.my_device_id, self.syncthing_hostname, self.syncthing_address)
self.etcd.add_device_to_cluster(self.my_device_id, self.syncthing_node_name, self.syncthing_publish_address)
self.loop()
+8 -7
View File
@@ -7,17 +7,18 @@ CLUSTER_INFO_KEY = '/syncthing_monitor/cluster_info'
class EtcdClient:
def __init__(self, host, port):
def __init__(self, host, port, key=CLUSTER_INFO_KEY):
self.etcd = etcd3.client(host=host, port=port)
self.key = key
def load_cluster_info(self):
raw_value = self.etcd.get(CLUSTER_INFO_KEY)[0]
raw_value = self.etcd.get(self.key)[0]
if raw_value is None:
return {'devices': []}
return json.loads(raw_value)
@retry
def add_device_to_cluster(self, device_id, hostname, address):
def add_device_to_cluster(self, device_id, node_name, address):
with self.etcd.lock('syncthing_monitor'):
cluster_info = self.load_cluster_info()
@@ -26,14 +27,14 @@ class EtcdClient:
new_device = {
'id': device_id,
'hostname': hostname,
'node_name': node_name,
'address': address
}
cluster_info['devices'].append(new_device)
print("Updating etcd value for '{0}': {1}".format(CLUSTER_INFO_KEY, json.dumps(cluster_info)))
self.etcd.put(CLUSTER_INFO_KEY, json.dumps(cluster_info))
print("Updating etcd value for '{0}': {1}".format(self.key, json.dumps(cluster_info)))
self.etcd.put(self.key, json.dumps(cluster_info))
@retry
def get_device_list(self):
@@ -42,4 +43,4 @@ class EtcdClient:
return device_list
def register_device_update_handler(self, device_update_handler):
self.etcd.add_watch_callback(CLUSTER_INFO_KEY, device_update_handler)
self.etcd.add_watch_callback(self.key, device_update_handler)
+1 -1
View File
@@ -33,7 +33,7 @@ class SyncthingClient:
post_data = {
"deviceID": device['id'],
"name": device['hostname'],
"name": device['node_name'],
"addresses": [device_address],
"compression": "metadata",
"certName": "",