2021-01-28 13:31:25 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from retrying import retry
|
|
|
|
|
|
|
|
|
2021-01-29 10:13:59 +00:00
|
|
|
class SyncthingClient:
|
|
|
|
|
2021-01-29 13:55:51 +00:00
|
|
|
def __init__(self, api_key, host, port):
|
2021-01-29 10:35:18 +00:00
|
|
|
self.api_key = api_key
|
2021-01-29 10:13:59 +00:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
2021-01-29 10:35:18 +00:00
|
|
|
self.headers = {'X-API-Key': self.api_key}
|
2021-01-29 10:13:59 +00:00
|
|
|
|
|
|
|
@retry
|
|
|
|
def get_my_device_id(self):
|
2021-01-29 10:35:18 +00:00
|
|
|
response = requests.get(self.make_url("/rest/system/status"), headers=self.headers)
|
2021-01-29 10:13:59 +00:00
|
|
|
return json.loads(response.content)["myID"]
|
|
|
|
|
2021-01-29 12:56:07 +00:00
|
|
|
def add_devices(self, device_list):
|
|
|
|
for device in device_list:
|
|
|
|
self.add_device(device)
|
2021-01-29 10:35:18 +00:00
|
|
|
|
2021-01-29 12:56:07 +00:00
|
|
|
def add_device(self, device):
|
2021-01-29 10:35:18 +00:00
|
|
|
post_data = {
|
2021-01-29 12:56:07 +00:00
|
|
|
"deviceID": device['id'],
|
|
|
|
"name": device['hostname'],
|
2021-01-29 10:35:18 +00:00
|
|
|
"addresses": [
|
|
|
|
"dynamic",
|
2021-01-29 12:56:07 +00:00
|
|
|
"tcp://{0}:22000".format(device['hostname'])
|
2021-01-29 10:35:18 +00:00
|
|
|
],
|
|
|
|
"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))
|
|
|
|
|
2021-01-29 12:56:07 +00:00
|
|
|
print("Attempt to add device {0} to syncthing: {1}".format(device, response.content))
|
2021-01-29 10:35:18 +00:00
|
|
|
|
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))
|
|
|
|
|
2021-01-29 10:13:59 +00:00
|
|
|
@retry
|
2021-01-29 10:35:18 +00:00
|
|
|
def print_config(self):
|
|
|
|
response = requests.get(self.make_url("/rest/config/devices"), headers=self.headers)
|
2021-01-29 10:13:59 +00:00
|
|
|
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))
|
2021-01-29 10:13:59 +00:00
|
|
|
|
2021-01-29 10:35:18 +00:00
|
|
|
def make_url(self, endpoint):
|
|
|
|
return "http://{0}:{1}{2}".format(self.host, self.port, endpoint)
|