2021-01-29 12:56:07 +00:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
2021-01-29 10:13:59 +00:00
|
|
|
import syncthing_monitor.config_xml as xml
|
2021-01-29 10:37:42 +00:00
|
|
|
from .etcd_client import EtcdClient
|
|
|
|
from .syncthing_client import SyncthingClient
|
2021-01-29 10:13:59 +00:00
|
|
|
|
2021-01-29 10:35:18 +00:00
|
|
|
DEFAULT_ST_CONFIG_XML_PATH = '/config/config.xml'
|
|
|
|
DEFAULT_ST_GUI_PORT = 8384
|
2021-01-28 10:56:11 +00:00
|
|
|
|
|
|
|
|
2021-01-28 13:31:25 +00:00
|
|
|
def loop(gui_port="8384", host="sync"):
|
2021-01-29 10:35:18 +00:00
|
|
|
api_key = xml.parse_api_key(DEFAULT_ST_CONFIG_XML_PATH)
|
2021-01-28 10:56:11 +00:00
|
|
|
print("Found API Key: {0}".format(api_key))
|
|
|
|
|
2021-01-29 10:35:18 +00:00
|
|
|
xml.set_listen_ip_to_any(DEFAULT_ST_CONFIG_XML_PATH, DEFAULT_ST_CONFIG_XML_PATH, DEFAULT_ST_GUI_PORT)
|
2021-01-28 13:31:25 +00:00
|
|
|
|
2021-01-29 10:35:18 +00:00
|
|
|
syncthing = SyncthingClient(api_key, host, gui_port)
|
2021-01-29 10:13:59 +00:00
|
|
|
my_device_id = syncthing.get_my_device_id()
|
|
|
|
print("Found Device ID: {0}".format(my_device_id))
|
2021-01-28 13:31:25 +00:00
|
|
|
|
2021-01-29 12:56:07 +00:00
|
|
|
my_syncthing_hostname = os.getenv('SYNCTHING_HOSTNAME')
|
|
|
|
|
2021-01-29 10:19:53 +00:00
|
|
|
etcd = EtcdClient('etcd')
|
2021-01-29 12:56:07 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
class Updater:
|
|
|
|
def __init__(self, etcd, syncthing):
|
|
|
|
self.etcd = etcd
|
|
|
|
self.syncthing = syncthing
|
2021-01-29 10:35:18 +00:00
|
|
|
|
2021-01-29 12:56:07 +00:00
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
def update_devices(self, event):
|
|
|
|
device_list = self.etcd.get_device_list()
|
|
|
|
self.syncthing.add_devices(device_list)
|
|
|
|
self.syncthing.print_config()
|
2021-01-29 08:28:35 +00:00
|
|
|
|
2021-01-28 10:56:11 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
loop()
|