refactored main code into Program class and consolidated config vars

This commit is contained in:
B.J. Dweck 2021-01-29 15:55:51 +02:00
parent de749526da
commit 0af5811c96
5 changed files with 57 additions and 39 deletions

View File

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

View File

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

View File

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

View File

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

View File

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