From 0af5811c96284a885b6d6fa4b2a7517bd74a8665 Mon Sep 17 00:00:00 2001 From: Benjamin Dweck Date: Fri, 29 Jan 2021 15:55:51 +0200 Subject: [PATCH] refactored main code into Program class and consolidated config vars --- docker-compose.test.yml | 4 +- syncthing_monitor/__main__.py | 75 +++++++++++++++++---------- syncthing_monitor/config_xml.py | 6 +-- syncthing_monitor/etcd_client.py | 7 ++- syncthing_monitor/syncthing_client.py | 4 +- 5 files changed, 57 insertions(+), 39 deletions(-) diff --git a/docker-compose.test.yml b/docker-compose.test.yml index d3c4c37..19088c7 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -21,7 +21,7 @@ services: - sync1config:/config - etcd environment: - - SYNCTHING_HOSTNAME=sync1 + - SYNCTHING_PUBLIC_HOST=sync1 networks: - syncnet @@ -45,7 +45,7 @@ services: volumes: - sync2config:/config environment: - - SYNCTHING_HOSTNAME=sync2 + - SYNCTHING_PUBLIC_HOST=sync2 networks: - syncnet diff --git a/syncthing_monitor/__main__.py b/syncthing_monitor/__main__.py index 6c39d01..84098fd 100644 --- a/syncthing_monitor/__main__.py +++ b/syncthing_monitor/__main__.py @@ -1,39 +1,62 @@ import os +import sys import time import syncthing_monitor.config_xml as xml from .etcd_client import EtcdClient from .syncthing_client import SyncthingClient -DEFAULT_ST_CONFIG_XML_PATH = '/config/config.xml' -DEFAULT_ST_GUI_PORT = 8384 +SYNCTHING_CONFIG_XML_PATH = '/config/config.xml' +SYNCTHING_GUI_PORT = 8384 +ETCD_PORT = 2379 -def loop(gui_port="8384", host="sync"): - api_key = xml.parse_api_key(DEFAULT_ST_CONFIG_XML_PATH) - print("Found API Key: {0}".format(api_key)) - - xml.set_listen_ip_to_any(DEFAULT_ST_CONFIG_XML_PATH, DEFAULT_ST_CONFIG_XML_PATH, DEFAULT_ST_GUI_PORT) - - syncthing = SyncthingClient(api_key, host, gui_port) - my_device_id = syncthing.get_my_device_id() - print("Found Device ID: {0}".format(my_device_id)) - - my_syncthing_hostname = os.getenv('SYNCTHING_HOSTNAME') - - etcd = EtcdClient('etcd') - updater = Updater(etcd, syncthing) - etcd.register_device_update_handler(updater.update_devices) - etcd.append_device_id(my_device_id, my_syncthing_hostname) - - while True: - time.sleep(1) +def main(): + program = Program(os.getenv('SYNCTHING_PUBLIC_HOST'), + SYNCTHING_CONFIG_XML_PATH, "sync", SYNCTHING_GUI_PORT, + "etcd", ETCD_PORT) + program.start() -class Updater: - def __init__(self, etcd, syncthing): - self.etcd = etcd - self.syncthing = syncthing +class Program: + def __init__(self, syncthing_public_host, + syncthing_config_xml_path, syncthing_hostname, syncthing_gui_port, + etcd_hostname, etcd_port): + self.syncthing_public_host = syncthing_public_host + self.my_device_id = None + self.syncthing = None + self.etcd = EtcdClient(etcd_hostname, etcd_port) + self.syncthing_gui_port = syncthing_gui_port + self.syncthing_hostname = syncthing_hostname + self.syncthing_config_xml_path = syncthing_config_xml_path + + def initialize_syncthing(self): + api_key = xml.parse_api_key(self.syncthing_config_xml_path) + print("Found API Key: {0}".format(api_key)) + + print("Configuring Syncthing to listen on 0.0.0.0...") + xml.set_listen_ip_to_any(self.syncthing_config_xml_path, + self.syncthing_config_xml_path, + self.syncthing_gui_port) + + self.syncthing = SyncthingClient(api_key, self.syncthing_hostname, self.syncthing_gui_port) + + def start(self): + self.initialize_syncthing() + + self.my_device_id = self.syncthing.get_my_device_id() + print("Found My Device ID: {0}".format(self.my_device_id)) + + self.etcd.register_device_update_handler(self.update_devices) + self.etcd.add_device_to_cluster(self.my_device_id, self.syncthing_public_host) + + self.loop() + + @staticmethod + def loop(): + while True: + sys.stdout.flush() + time.sleep(1) # noinspection PyUnusedLocal def update_devices(self, event): @@ -43,4 +66,4 @@ class Updater: if __name__ == "__main__": - loop() + main() diff --git a/syncthing_monitor/config_xml.py b/syncthing_monitor/config_xml.py index 5987226..32e5278 100644 --- a/syncthing_monitor/config_xml.py +++ b/syncthing_monitor/config_xml.py @@ -1,9 +1,7 @@ from xml.etree import ElementTree as elementTree -DEFAULT_CONFIG_XML_PATH = '/config/config.xml' - -def parse_api_key(xml_path=DEFAULT_CONFIG_XML_PATH): +def parse_api_key(xml_path): root = elementTree.parse(xml_path).getroot() api_key = '' for gui in root.findall('gui'): @@ -12,7 +10,7 @@ def parse_api_key(xml_path=DEFAULT_CONFIG_XML_PATH): return api_key -def set_listen_ip_to_any(in_xml_path=DEFAULT_CONFIG_XML_PATH, out_xml_path=DEFAULT_CONFIG_XML_PATH, gui_port='8384'): +def set_listen_ip_to_any(in_xml_path, out_xml_path, gui_port): with open(in_xml_path, 'r') as file: xml = file.read() diff --git a/syncthing_monitor/etcd_client.py b/syncthing_monitor/etcd_client.py index 6035cb4..f455251 100644 --- a/syncthing_monitor/etcd_client.py +++ b/syncthing_monitor/etcd_client.py @@ -4,11 +4,10 @@ import etcd3 as etcd3 from retrying import retry CLUSTER_INFO_KEY = '/syncthing_monitor/cluster_info' -DEFAULT_PORT = 2379 class EtcdClient: - def __init__(self, host, port=DEFAULT_PORT): + def __init__(self, host, port): self.etcd = etcd3.client(host=host, port=port) def load_cluster_info(self): @@ -18,7 +17,7 @@ class EtcdClient: return json.loads(raw_value) @retry - def append_device_id(self, device_id, hostname): + def add_device_to_cluster(self, device_id, public_host): with self.etcd.lock('syncthing_monitor'): cluster_info = self.load_cluster_info() @@ -27,7 +26,7 @@ class EtcdClient: new_device = { 'id': device_id, - 'hostname': hostname + 'hostname': public_host } cluster_info['devices'].append(new_device) diff --git a/syncthing_monitor/syncthing_client.py b/syncthing_monitor/syncthing_client.py index b29721c..556d31b 100644 --- a/syncthing_monitor/syncthing_client.py +++ b/syncthing_monitor/syncthing_client.py @@ -3,12 +3,10 @@ import json import requests from retrying import retry -DEFAULT_GUI_PORT = 8384 - class SyncthingClient: - def __init__(self, api_key, host, port=DEFAULT_GUI_PORT): + def __init__(self, api_key, host, port): self.api_key = api_key self.host = host self.port = port