80 lines
2.6 KiB
Python
80 lines
2.6 KiB
Python
import os
|
|
import sys
|
|
import time
|
|
|
|
import syncthing_monitor.config_xml as xml
|
|
from .etcd_client import EtcdClient
|
|
from .syncthing_client import SyncthingClient
|
|
|
|
SYNCTHING_CONFIG_XML_PATH = '/config/config.xml'
|
|
SYNCTHING_GUI_PORT = 8384
|
|
ETCD_PORT = 2379
|
|
|
|
|
|
def main():
|
|
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("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.syncthing.patch_config()
|
|
self.syncthing.create_shared_folder("GXWxf-3zgnU", "SharedFolder", "/data", [{'id': self.my_device_id}])
|
|
|
|
if not self.syncthing.config_is_in_sync():
|
|
self.syncthing.restart()
|
|
|
|
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):
|
|
device_list = self.etcd.get_device_list()
|
|
self.syncthing.add_devices(device_list)
|
|
self.syncthing.create_shared_folder("GXWxf-3zgnU", "SharedFolder", "/data", device_list)
|
|
self.syncthing.print_config()
|
|
|
|
if not self.syncthing.config_is_in_sync():
|
|
self.syncthing.restart()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|