2021-01-29 12:56:07 +00:00
|
|
|
import os
|
2021-01-29 13:55:51 +00:00
|
|
|
import sys
|
2021-01-29 12:56:07 +00:00
|
|
|
import time
|
|
|
|
|
2021-01-31 09:45:07 +00:00
|
|
|
from retrying import retry
|
|
|
|
|
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-31 09:45:07 +00:00
|
|
|
SHARED_FOLDER_ID = "data"
|
|
|
|
SHARED_FOLDER_LABEL = "syncthing_monitor_data"
|
2021-01-29 13:55:51 +00:00
|
|
|
SYNCTHING_CONFIG_XML_PATH = '/config/config.xml'
|
|
|
|
SYNCTHING_GUI_PORT = 8384
|
|
|
|
ETCD_PORT = 2379
|
2021-01-28 10:56:11 +00:00
|
|
|
|
|
|
|
|
2021-01-29 13:55:51 +00:00
|
|
|
def main():
|
2021-01-31 09:45:07 +00:00
|
|
|
syncthing_mon = SyncthingMonitor(os.getenv('SYNCTHING_HOSTNAME'),
|
|
|
|
os.getenv('SYNCTHING_ADDRESS'),
|
|
|
|
os.getenv('SYNCTHING_DATA_PATH'),
|
|
|
|
SYNCTHING_CONFIG_XML_PATH, "sync", SYNCTHING_GUI_PORT,
|
|
|
|
"etcd", ETCD_PORT)
|
|
|
|
syncthing_mon.start()
|
2021-01-28 10:56:11 +00:00
|
|
|
|
2021-01-28 13:31:25 +00:00
|
|
|
|
2021-01-31 09:45:07 +00:00
|
|
|
class SyncthingMonitor:
|
|
|
|
def __init__(self, syncthing_public_host, syncthing_address, syncthing_data_path,
|
2021-01-29 13:55:51 +00:00
|
|
|
syncthing_config_xml_path, syncthing_hostname, syncthing_gui_port,
|
|
|
|
etcd_hostname, etcd_port):
|
2021-01-31 09:45:07 +00:00
|
|
|
self.syncthing_hostname = syncthing_public_host
|
|
|
|
self.syncthing_address = syncthing_address
|
|
|
|
self.syncthing_data_path = syncthing_data_path
|
2021-01-29 13:55:51 +00:00
|
|
|
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
|
2021-01-28 13:31:25 +00:00
|
|
|
|
2021-01-31 09:45:07 +00:00
|
|
|
@retry
|
2021-01-29 13:55:51 +00:00
|
|
|
def initialize_syncthing(self):
|
|
|
|
api_key = xml.parse_api_key(self.syncthing_config_xml_path)
|
|
|
|
print("Found API Key: {0}".format(api_key))
|
2021-01-29 12:56:07 +00:00
|
|
|
|
2021-01-29 13:55:51 +00:00
|
|
|
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)
|
2021-01-29 12:56:07 +00:00
|
|
|
|
2021-01-29 13:55:51 +00:00
|
|
|
self.syncthing = SyncthingClient(api_key, self.syncthing_hostname, self.syncthing_gui_port)
|
2021-01-29 12:56:07 +00:00
|
|
|
|
2021-01-31 09:45:07 +00:00
|
|
|
@retry
|
2021-01-29 13:55:51 +00:00
|
|
|
def start(self):
|
|
|
|
self.initialize_syncthing()
|
2021-01-29 12:56:07 +00:00
|
|
|
|
2021-01-29 13:55:51 +00:00
|
|
|
self.my_device_id = self.syncthing.get_my_device_id()
|
|
|
|
print("Found My Device ID: {0}".format(self.my_device_id))
|
|
|
|
|
2021-01-31 09:45:07 +00:00
|
|
|
self.syncthing.disable_announce_discovery_and_relay()
|
|
|
|
self.syncthing.create_shared_folder(SHARED_FOLDER_ID, SHARED_FOLDER_LABEL, self.syncthing_data_path,
|
|
|
|
[{'id': self.my_device_id}])
|
|
|
|
self.syncthing.sync_config()
|
2021-01-31 08:44:14 +00:00
|
|
|
|
2021-01-29 13:55:51 +00:00
|
|
|
self.etcd.register_device_update_handler(self.update_devices)
|
2021-01-31 09:45:07 +00:00
|
|
|
self.etcd.add_device_to_cluster(self.my_device_id, self.syncthing_hostname, self.syncthing_address)
|
2021-01-29 13:55:51 +00:00
|
|
|
|
|
|
|
self.loop()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def loop():
|
|
|
|
while True:
|
|
|
|
sys.stdout.flush()
|
|
|
|
time.sleep(1)
|
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)
|
2021-01-31 09:45:07 +00:00
|
|
|
self.syncthing.create_shared_folder(SHARED_FOLDER_ID, SHARED_FOLDER_LABEL, self.syncthing_data_path,
|
|
|
|
device_list)
|
|
|
|
self.syncthing.sync_config()
|
2021-01-29 12:56:07 +00:00
|
|
|
self.syncthing.print_config()
|
2021-01-29 08:28:35 +00:00
|
|
|
|
2021-01-28 10:56:11 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-01-29 13:55:51 +00:00
|
|
|
main()
|