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-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-31 11:37:25 +00:00
|
|
|
ETCD_KEY = '/syncthing_monitor/cluster_info'
|
2021-01-28 10:56:11 +00:00
|
|
|
|
|
|
|
|
2021-01-29 13:55:51 +00:00
|
|
|
def main():
|
2021-01-31 11:37:25 +00:00
|
|
|
syncthing_mon = SyncthingMonitor(os.getenv('SYNCTHING_NODE_NAME', 'sync'),
|
|
|
|
os.getenv('SYNCTHING_PUBLISH_ADDRESS', 'tcp4://sync'),
|
|
|
|
os.getenv('SYNCTHING_DATA_PATH', '/data'),
|
|
|
|
os.getenv('SYNCTHING_CONFIG_XML_PATH', SYNCTHING_CONFIG_XML_PATH),
|
|
|
|
os.getenv('SYNCTHING_LOCAL_HOSTNAME', 'sync'),
|
|
|
|
os.getenv('SYNCTHING_GUI_PORT', SYNCTHING_GUI_PORT),
|
|
|
|
os.getenv('ETCD_HOSTNAME', 'etcd'),
|
|
|
|
os.getenv('ETCD_PORT', ETCD_PORT),
|
|
|
|
os.getenv('ETCD_KEY', ETCD_KEY))
|
2021-01-31 09:45:07 +00:00
|
|
|
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:
|
2021-01-31 11:37:25 +00:00
|
|
|
def __init__(self, syncthing_node_name, syncthing_publish_address, syncthing_data_path,
|
|
|
|
syncthing_config_xml_path, syncthing_local_hostname, syncthing_gui_port,
|
|
|
|
etcd_hostname, etcd_port, etcd_key):
|
|
|
|
self.syncthing_node_name = syncthing_node_name
|
|
|
|
self.syncthing_publish_address = syncthing_publish_address
|
2021-01-31 09:45:07 +00:00
|
|
|
self.syncthing_data_path = syncthing_data_path
|
2021-01-29 13:55:51 +00:00
|
|
|
self.my_device_id = None
|
|
|
|
self.syncthing = None
|
2021-01-31 11:37:25 +00:00
|
|
|
self.etcd = EtcdClient(etcd_hostname, etcd_port, etcd_key)
|
2021-01-29 13:55:51 +00:00
|
|
|
self.syncthing_gui_port = syncthing_gui_port
|
2021-01-31 11:37:25 +00:00
|
|
|
self.syncthing_local_hostname = syncthing_local_hostname
|
2021-01-29 13:55:51 +00:00
|
|
|
self.syncthing_config_xml_path = syncthing_config_xml_path
|
2021-01-28 13:31:25 +00:00
|
|
|
|
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-02-01 14:50:03 +00:00
|
|
|
print("Configuring Syncthing to listen on 0.0.0.0...", flush=True)
|
2021-01-29 13:55:51 +00:00
|
|
|
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-31 11:37:25 +00:00
|
|
|
self.syncthing = SyncthingClient(api_key, self.syncthing_local_hostname, self.syncthing_gui_port)
|
2021-01-29 12:56:07 +00:00
|
|
|
|
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()
|
2021-02-01 14:50:03 +00:00
|
|
|
print("Found My Device ID: {0}".format(self.my_device_id), flush=True)
|
2021-01-29 13:55:51 +00:00
|
|
|
|
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-02-02 08:48:19 +00:00
|
|
|
print("Registering etcd update callback...", flush=True)
|
2021-01-29 13:55:51 +00:00
|
|
|
self.etcd.register_device_update_handler(self.update_devices)
|
2021-02-02 08:48:19 +00:00
|
|
|
print("Attempting to add this device to etcd...", flush=True)
|
2021-01-31 11:37:25 +00:00
|
|
|
self.etcd.add_device_to_cluster(self.my_device_id, self.syncthing_node_name, self.syncthing_publish_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()
|