diff --git a/syncthing_monitor/__main__.py b/syncthing_monitor/__main__.py index f4c1e95..557ca23 100644 --- a/syncthing_monitor/__main__.py +++ b/syncthing_monitor/__main__.py @@ -1,6 +1,6 @@ from syncthing_monitor.config_xml import parse_api_key, set_listen_ip_to_any -from syncthing_monitor.etcd_cluster_info import append_device_id -from syncthing_monitor.syncthing_rest import get_device_id +from syncthing_monitor.etcd_cluster_info import append_device_id, get_device_list +from syncthing_monitor.syncthing_rest import get_device_id, print_debug_info, post_devices def loop(gui_port="8384", host="sync"): @@ -13,6 +13,9 @@ def loop(gui_port="8384", host="sync"): print("Found Device ID: {0}".format(device_id)) append_device_id(device_id, 'etcd') + device_ids = get_device_list('etcd') + post_devices(device_ids, host, gui_port, api_key) + print_debug_info(host, gui_port, api_key) if __name__ == "__main__": diff --git a/syncthing_monitor/etcd_cluster_info.py b/syncthing_monitor/etcd_cluster_info.py index a481b7d..fce600e 100644 --- a/syncthing_monitor/etcd_cluster_info.py +++ b/syncthing_monitor/etcd_cluster_info.py @@ -23,3 +23,18 @@ def append_device_id(device_id, host, port=2379): print("Updating etcd value for '{0}': {1}".format(CLUSTER_INFO_KEY, json.dumps(cluster_info)), flush=True) etcd.put(CLUSTER_INFO_KEY, json.dumps(cluster_info)) + + +@retry +def get_device_list(host, port=2379): + cluster_info = {'devices': []} + + etcd = etcd3.client(host=host, port=port) + + raw_value = etcd.get(CLUSTER_INFO_KEY)[0] + + if raw_value is not None: + cluster_info = json.loads(raw_value) + + print("Obtained cluster_info devices from etcd: {0}".format(json.dumps(cluster_info['devices']))) + return cluster_info['devices'] diff --git a/syncthing_monitor/syncthing_rest.py b/syncthing_monitor/syncthing_rest.py index 561726b..515a923 100644 --- a/syncthing_monitor/syncthing_rest.py +++ b/syncthing_monitor/syncthing_rest.py @@ -9,3 +9,41 @@ def get_device_id(host, gui_port, api_key): syncthing_headers = {'X-API-Key': api_key} response = requests.get("http://" + host + ":" + gui_port + "/rest/system/status", headers=syncthing_headers) return json.loads(response.content)["myID"] + + +@retry +def print_debug_info(host, gui_port, api_key): + syncthing_headers = {'X-API-Key': api_key} + response = requests.get("http://" + host + ":" + gui_port + "/rest/config/devices", headers=syncthing_headers) + print("/rest/config/devices: {0}".format(response.content)) + + +def post_devices(device_ids, host, gui_port, api_key): + syncthing_headers = {'X-API-Key': api_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://" + host + ":" + gui_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))