diff --git a/syncthing_monitor/__main__.py b/syncthing_monitor/__main__.py index bedd0f0..800111c 100644 --- a/syncthing_monitor/__main__.py +++ b/syncthing_monitor/__main__.py @@ -2,24 +2,26 @@ import syncthing_monitor.config_xml as xml from syncthing_monitor.etcd_cluster_info import EtcdClient from syncthing_monitor.syncthing_rest import SyncthingClient -DEFAULT_CONFIG_XML_PATH = '/config/config.xml' +DEFAULT_ST_CONFIG_XML_PATH = '/config/config.xml' +DEFAULT_ST_GUI_PORT = 8384 def loop(gui_port="8384", host="sync"): - api_key = xml.parse_api_key(DEFAULT_CONFIG_XML_PATH) + api_key = xml.parse_api_key(DEFAULT_ST_CONFIG_XML_PATH) print("Found API Key: {0}".format(api_key)) - xml.set_listen_ip_to_any(DEFAULT_CONFIG_XML_PATH, DEFAULT_CONFIG_XML_PATH, 8384) + xml.set_listen_ip_to_any(DEFAULT_ST_CONFIG_XML_PATH, DEFAULT_ST_CONFIG_XML_PATH, DEFAULT_ST_GUI_PORT) - syncthing = SyncthingClient(host, gui_port, api_key) + syncthing = SyncthingClient(api_key, host, gui_port) my_device_id = syncthing.get_my_device_id() print("Found Device ID: {0}".format(my_device_id)) etcd = EtcdClient('etcd') etcd.append_device_id(my_device_id) device_ids = etcd.get_device_list() + syncthing.add_devices(device_ids) - syncthing.print_debug_info() + syncthing.print_config() if __name__ == "__main__": diff --git a/syncthing_monitor/syncthing_rest.py b/syncthing_monitor/syncthing_rest.py index 922907e..0874c1a 100644 --- a/syncthing_monitor/syncthing_rest.py +++ b/syncthing_monitor/syncthing_rest.py @@ -3,54 +3,57 @@ import json import requests from retrying import retry +DEFAULT_GUI_PORT = 8384 + class SyncthingClient: - def __init__(self, host, port, key): + def __init__(self, api_key, host, port=DEFAULT_GUI_PORT): + self.api_key = api_key self.host = host self.port = port - self.key = key + self.headers = {'X-API-Key': self.api_key} @retry def get_my_device_id(self): - syncthing_headers = {'X-API-Key': self.key} - response = requests.get("http://" + self.host + ":" + self.port + "/rest/system/status", - headers=syncthing_headers) + response = requests.get(self.make_url("/rest/system/status"), headers=self.headers) return json.loads(response.content)["myID"] + def add_devices(self, device_ids): + for device_id in device_ids: + self.add_device(device_id) + + def add_device(self, device_id): + post_data = { + "deviceID": device_id, + "name": "Laptop", + "addresses": [ + "dynamic", + "tcp://192.168.1.2:22000" + ], + "compression": "metadata", + "certName": "", + "introducer": False, + "skipIntroductionRemovals": False, + "introducedBy": "", + "paused": False, + "allowedNetworks": [], + "autoAcceptFolders": False, + "maxSendKbps": 0, + "maxRecvKbps": 0, + "ignoredFolders": [], + "pendingFolders": [], + "maxRequestKiB": 0 + } + response = requests.post(self.make_url("/rest/config/devices"), + headers=self.headers, data=json.dumps(post_data)) + + print("Attempt to add device {0} to syncthing: {1}".format(device_id, response.content)) + @retry - def print_debug_info(self): - syncthing_headers = {'X-API-Key': self.key} - response = requests.get("http://" + self.host + ":" + self.port + "/rest/config/devices", - headers=syncthing_headers) + def print_config(self): + response = requests.get(self.make_url("/rest/config/devices"), headers=self.headers) print("/rest/config/devices: {0}".format(response.content)) - def add_devices(self, device_ids): - syncthing_headers = {'X-API-Key': self.key} - - for device_id in device_ids: - post_data = { - "deviceID": device_id, - "name": "Laptop", - "addresses": [ - "dynamic", - "tcp://192.168.1.2:22000" - ], - "compression": "metadata", - "certName": "", - "introducer": False, - "skipIntroductionRemovals": False, - "introducedBy": "", - "paused": False, - "allowedNetworks": [], - "autoAcceptFolders": False, - "maxSendKbps": 0, - "maxRecvKbps": 0, - "ignoredFolders": [], - "pendingFolders": [], - "maxRequestKiB": 0 - } - - response = requests.post("http://" + self.host + ":" + self.port + "/rest/config/devices", - headers=syncthing_headers, data=json.dumps(post_data)) - print("Attempt to add device {0} to syncthing: {1}".format(device_id, response.content)) + def make_url(self, endpoint): + return "http://{0}:{1}{2}".format(self.host, self.port, endpoint)