can add devices to syncthing

This commit is contained in:
B.J. Dweck 2021-01-29 11:59:59 +02:00
parent b837926c11
commit 3589064dd0
3 changed files with 58 additions and 2 deletions

View File

@ -1,6 +1,6 @@
from syncthing_monitor.config_xml import parse_api_key, set_listen_ip_to_any 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.etcd_cluster_info import append_device_id, get_device_list
from syncthing_monitor.syncthing_rest import get_device_id from syncthing_monitor.syncthing_rest import get_device_id, print_debug_info, post_devices
def loop(gui_port="8384", host="sync"): 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)) print("Found Device ID: {0}".format(device_id))
append_device_id(device_id, 'etcd') 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__": if __name__ == "__main__":

View File

@ -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) 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)) 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']

View File

@ -9,3 +9,41 @@ def get_device_id(host, gui_port, api_key):
syncthing_headers = {'X-API-Key': api_key} syncthing_headers = {'X-API-Key': api_key}
response = requests.get("http://" + host + ":" + gui_port + "/rest/system/status", headers=syncthing_headers) response = requests.get("http://" + host + ":" + gui_port + "/rest/system/status", headers=syncthing_headers)
return json.loads(response.content)["myID"] 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))