refactored main code into Program class and consolidated config vars
This commit is contained in:
parent
de749526da
commit
0af5811c96
|
@ -21,7 +21,7 @@ services:
|
||||||
- sync1config:/config
|
- sync1config:/config
|
||||||
- etcd
|
- etcd
|
||||||
environment:
|
environment:
|
||||||
- SYNCTHING_HOSTNAME=sync1
|
- SYNCTHING_PUBLIC_HOST=sync1
|
||||||
networks:
|
networks:
|
||||||
- syncnet
|
- syncnet
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ services:
|
||||||
volumes:
|
volumes:
|
||||||
- sync2config:/config
|
- sync2config:/config
|
||||||
environment:
|
environment:
|
||||||
- SYNCTHING_HOSTNAME=sync2
|
- SYNCTHING_PUBLIC_HOST=sync2
|
||||||
networks:
|
networks:
|
||||||
- syncnet
|
- syncnet
|
||||||
|
|
||||||
|
|
|
@ -1,40 +1,63 @@
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import syncthing_monitor.config_xml as xml
|
import syncthing_monitor.config_xml as xml
|
||||||
from .etcd_client import EtcdClient
|
from .etcd_client import EtcdClient
|
||||||
from .syncthing_client import SyncthingClient
|
from .syncthing_client import SyncthingClient
|
||||||
|
|
||||||
DEFAULT_ST_CONFIG_XML_PATH = '/config/config.xml'
|
SYNCTHING_CONFIG_XML_PATH = '/config/config.xml'
|
||||||
DEFAULT_ST_GUI_PORT = 8384
|
SYNCTHING_GUI_PORT = 8384
|
||||||
|
ETCD_PORT = 2379
|
||||||
|
|
||||||
|
|
||||||
def loop(gui_port="8384", host="sync"):
|
def main():
|
||||||
api_key = xml.parse_api_key(DEFAULT_ST_CONFIG_XML_PATH)
|
program = Program(os.getenv('SYNCTHING_PUBLIC_HOST'),
|
||||||
|
SYNCTHING_CONFIG_XML_PATH, "sync", SYNCTHING_GUI_PORT,
|
||||||
|
"etcd", ETCD_PORT)
|
||||||
|
program.start()
|
||||||
|
|
||||||
|
|
||||||
|
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("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)
|
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)
|
||||||
|
|
||||||
syncthing = SyncthingClient(api_key, host, gui_port)
|
self.syncthing = SyncthingClient(api_key, self.syncthing_hostname, self.syncthing_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')
|
def start(self):
|
||||||
|
self.initialize_syncthing()
|
||||||
|
|
||||||
etcd = EtcdClient('etcd')
|
self.my_device_id = self.syncthing.get_my_device_id()
|
||||||
updater = Updater(etcd, syncthing)
|
print("Found My Device ID: {0}".format(self.my_device_id))
|
||||||
etcd.register_device_update_handler(updater.update_devices)
|
|
||||||
etcd.append_device_id(my_device_id, my_syncthing_hostname)
|
|
||||||
|
|
||||||
|
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:
|
while True:
|
||||||
|
sys.stdout.flush()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
class Updater:
|
|
||||||
def __init__(self, etcd, syncthing):
|
|
||||||
self.etcd = etcd
|
|
||||||
self.syncthing = syncthing
|
|
||||||
|
|
||||||
# noinspection PyUnusedLocal
|
# noinspection PyUnusedLocal
|
||||||
def update_devices(self, event):
|
def update_devices(self, event):
|
||||||
device_list = self.etcd.get_device_list()
|
device_list = self.etcd.get_device_list()
|
||||||
|
@ -43,4 +66,4 @@ class Updater:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
loop()
|
main()
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
from xml.etree import ElementTree as elementTree
|
from xml.etree import ElementTree as elementTree
|
||||||
|
|
||||||
DEFAULT_CONFIG_XML_PATH = '/config/config.xml'
|
|
||||||
|
|
||||||
|
def parse_api_key(xml_path):
|
||||||
def parse_api_key(xml_path=DEFAULT_CONFIG_XML_PATH):
|
|
||||||
root = elementTree.parse(xml_path).getroot()
|
root = elementTree.parse(xml_path).getroot()
|
||||||
api_key = ''
|
api_key = ''
|
||||||
for gui in root.findall('gui'):
|
for gui in root.findall('gui'):
|
||||||
|
@ -12,7 +10,7 @@ def parse_api_key(xml_path=DEFAULT_CONFIG_XML_PATH):
|
||||||
return api_key
|
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:
|
with open(in_xml_path, 'r') as file:
|
||||||
xml = file.read()
|
xml = file.read()
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,10 @@ import etcd3 as etcd3
|
||||||
from retrying import retry
|
from retrying import retry
|
||||||
|
|
||||||
CLUSTER_INFO_KEY = '/syncthing_monitor/cluster_info'
|
CLUSTER_INFO_KEY = '/syncthing_monitor/cluster_info'
|
||||||
DEFAULT_PORT = 2379
|
|
||||||
|
|
||||||
|
|
||||||
class EtcdClient:
|
class EtcdClient:
|
||||||
def __init__(self, host, port=DEFAULT_PORT):
|
def __init__(self, host, port):
|
||||||
self.etcd = etcd3.client(host=host, port=port)
|
self.etcd = etcd3.client(host=host, port=port)
|
||||||
|
|
||||||
def load_cluster_info(self):
|
def load_cluster_info(self):
|
||||||
|
@ -18,7 +17,7 @@ class EtcdClient:
|
||||||
return json.loads(raw_value)
|
return json.loads(raw_value)
|
||||||
|
|
||||||
@retry
|
@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'):
|
with self.etcd.lock('syncthing_monitor'):
|
||||||
cluster_info = self.load_cluster_info()
|
cluster_info = self.load_cluster_info()
|
||||||
|
|
||||||
|
@ -27,7 +26,7 @@ class EtcdClient:
|
||||||
|
|
||||||
new_device = {
|
new_device = {
|
||||||
'id': device_id,
|
'id': device_id,
|
||||||
'hostname': hostname
|
'hostname': public_host
|
||||||
}
|
}
|
||||||
|
|
||||||
cluster_info['devices'].append(new_device)
|
cluster_info['devices'].append(new_device)
|
||||||
|
|
|
@ -3,12 +3,10 @@ import json
|
||||||
import requests
|
import requests
|
||||||
from retrying import retry
|
from retrying import retry
|
||||||
|
|
||||||
DEFAULT_GUI_PORT = 8384
|
|
||||||
|
|
||||||
|
|
||||||
class SyncthingClient:
|
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.api_key = api_key
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
|
|
Loading…
Reference in New Issue
Block a user