151 lines
4.8 KiB
Python
151 lines
4.8 KiB
Python
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}
|
|
self.myID = None
|
|
|
|
@retry
|
|
def get_my_device_id(self):
|
|
if self.myID is None:
|
|
response = self.get("/rest/system/status")
|
|
status = json.loads(response.content)
|
|
self.myID = status["myID"]
|
|
|
|
return self.myID
|
|
|
|
def add_devices(self, device_list):
|
|
for device in device_list:
|
|
self.add_device(device)
|
|
|
|
def add_device(self, device):
|
|
device_address = device['address']
|
|
if device['id'] == self.myID:
|
|
device_address = 'dynamic'
|
|
|
|
post_data = {
|
|
"deviceID": device['id'],
|
|
"name": device['node_name'],
|
|
"addresses": [device_address],
|
|
"compression": "metadata",
|
|
"certName": "",
|
|
"introducer": False,
|
|
"skipIntroductionRemovals": False,
|
|
"introducedBy": "",
|
|
"paused": False,
|
|
"allowedNetworks": [],
|
|
"autoAcceptFolders": False,
|
|
"maxSendKbps": 0,
|
|
"maxRecvKbps": 0,
|
|
"ignoredFolders": [],
|
|
"pendingFolders": [],
|
|
"maxRequestKiB": 0
|
|
}
|
|
response = self.post("/rest/config/devices", json.dumps(post_data))
|
|
print("Attempt to add device {0} to syncthing: {1}".format(device, response.content), flush=True)
|
|
|
|
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 = self.post("/rest/config/folders", json.dumps(post_data))
|
|
print("Attempt to add shared folder to syncthing: {0}".format(response.content), flush=True)
|
|
|
|
@retry
|
|
def print_config(self):
|
|
response = self.get("/rest/config/devices")
|
|
print("/rest/config/devices: {0}".format(response.content), flush=True)
|
|
|
|
response = self.get("/rest/config/folders")
|
|
print("/rest/config/folders: {0}".format(response.content), flush=True)
|
|
|
|
response = self.get("/rest/config/options")
|
|
print("/rest/config/options: {0}".format(response.content), flush=True)
|
|
|
|
@retry
|
|
def config_is_in_sync(self):
|
|
response = self.get("/rest/config/insync")
|
|
return bool(json.loads(response.content)['configInSync'])
|
|
|
|
@retry
|
|
def restart(self):
|
|
response = self.post("/rest/system/restart", '')
|
|
print("System reset: {0}".format(response.content), flush=True)
|
|
|
|
def sync_config(self):
|
|
if not self.config_is_in_sync():
|
|
self.restart()
|
|
|
|
def disable_announce_discovery_and_relay(self):
|
|
config_patch = {
|
|
"globalAnnounceEnabled": False,
|
|
"localAnnounceEnabled": False,
|
|
"relaysEnabled": False,
|
|
"announceLANAddresses": False,
|
|
}
|
|
response = self.patch("/rest/config/options", json.dumps(config_patch))
|
|
print("Patched syncthing configuration: {0}".format(response.content), flush=True)
|
|
|
|
def make_url(self, endpoint):
|
|
return "http://{0}:{1}{2}".format(self.host, self.port, endpoint)
|
|
|
|
def get(self, endpoint):
|
|
return requests.get(self.make_url(endpoint), headers=self.headers)
|
|
|
|
def post(self, endpoint, data):
|
|
return requests.post(self.make_url(endpoint), headers=self.headers, data=data)
|
|
|
|
def patch(self, endpoint, data):
|
|
return requests.patch(self.make_url(endpoint), headers=self.headers, data=data)
|