syncthing-monitor/syncthing_monitor/syncthing_client.py

135 lines
4.6 KiB
Python
Raw Normal View History

2021-01-28 13:31:25 +00:00
import json
import requests
from retrying import retry
class SyncthingClient:
def __init__(self, api_key, host, port):
self.api_key = api_key
self.host = host
self.port = port
self.headers = {'X-API-Key': self.api_key}
@retry
def get_my_device_id(self):
response = requests.get(self.make_url("/rest/system/status"), headers=self.headers)
return json.loads(response.content)["myID"]
def add_devices(self, device_list):
for device in device_list:
self.add_device(device)
def add_device(self, device):
post_data = {
"deviceID": device['id'],
"name": device['hostname'],
"addresses": [
"dynamic",
"tcp://{0}:22000".format(device['hostname'])
],
"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, response.content))
2021-01-31 08:44:14 +00:00
def create_shared_folder(self, folder_id, label, path, devices):
post_data = {
"id": folder_id,
"label": label,
"filesystemType": "basic",
"path": path,
"type": "sendreceive",
"devices": [],
"rescanIntervalS": 60,
"fsWatcherEnabled": False,
"fsWatcherDelayS": 10,
"ignorePerms": False,
"autoNormalize": False,
"minDiskFree": {
"value": 1,
"unit": "%"
},
"versioning": {
"type": "simple",
"params": {
"keep": "5"
}
},
"copiers": 0,
"pullerMaxPendingKiB": 0,
"hashers": 0,
"order": "random",
"ignoreDelete": False,
"scanProgressIntervalS": 0,
"pullerPauseS": 0,
"maxConflicts": 10,
"disableSparseFiles": False,
"disableTempIndexes": False,
"paused": False,
"weakHashThresholdPct": 25,
"markerName": ".stfolder",
"copyOwnershipFromParent": False,
"modTimeWindowS": 0
}
for device in devices:
folder_device = {
'deviceID': device['id'],
'introducedBy': ''
}
post_data['devices'].append(folder_device)
response = requests.post(self.make_url("/rest/config/folders"),
headers=self.headers, data=json.dumps(post_data))
print("Attempt to add shared folder to syncthing: {0}".format(response.content))
@retry
def print_config(self):
response = requests.get(self.make_url("/rest/config/devices"), headers=self.headers)
print("/rest/config/devices: {0}".format(response.content))
2021-01-31 08:44:14 +00:00
response = requests.get(self.make_url("/rest/config/folders"), headers=self.headers)
print("/rest/config/folders: {0}".format(response.content))
response = requests.get(self.make_url("/rest/config/options"), headers=self.headers)
print("/rest/config/options: {0}".format(response.content))
def config_is_in_sync(self):
response = requests.get(self.make_url("/rest/config/insync"), headers=self.headers)
return bool(json.loads(response.content)['configInSync'])
def restart(self):
response = requests.post(self.make_url("/rest/system/restart"), headers=self.headers, data='')
print("System reset: {0}".format(response.content))
@retry
def patch_config(self):
config_patch = {
"globalAnnounceEnabled": False,
"localAnnounceEnabled": False,
"relaysEnabled": False,
"announceLANAddresses": False,
}
response = requests.patch(self.make_url("/rest/config/options"),
headers=self.headers, data=json.dumps(config_patch))
print("Patched syncthing configuration: {0}".format(response.content))
def make_url(self, endpoint):
return "http://{0}:{1}{2}".format(self.host, self.port, endpoint)