refactored fields/methods within SyncthingClient
This commit is contained in:
parent
a760965c12
commit
241642b5bc
|
@ -2,24 +2,26 @@ import syncthing_monitor.config_xml as xml
|
||||||
from syncthing_monitor.etcd_cluster_info import EtcdClient
|
from syncthing_monitor.etcd_cluster_info import EtcdClient
|
||||||
from syncthing_monitor.syncthing_rest import SyncthingClient
|
from syncthing_monitor.syncthing_rest import SyncthingClient
|
||||||
|
|
||||||
DEFAULT_CONFIG_XML_PATH = '/config/config.xml'
|
DEFAULT_ST_CONFIG_XML_PATH = '/config/config.xml'
|
||||||
|
DEFAULT_ST_GUI_PORT = 8384
|
||||||
|
|
||||||
|
|
||||||
def loop(gui_port="8384", host="sync"):
|
def loop(gui_port="8384", host="sync"):
|
||||||
api_key = xml.parse_api_key(DEFAULT_CONFIG_XML_PATH)
|
api_key = xml.parse_api_key(DEFAULT_ST_CONFIG_XML_PATH)
|
||||||
print("Found API Key: {0}".format(api_key))
|
print("Found API Key: {0}".format(api_key))
|
||||||
|
|
||||||
xml.set_listen_ip_to_any(DEFAULT_CONFIG_XML_PATH, DEFAULT_CONFIG_XML_PATH, 8384)
|
xml.set_listen_ip_to_any(DEFAULT_ST_CONFIG_XML_PATH, DEFAULT_ST_CONFIG_XML_PATH, DEFAULT_ST_GUI_PORT)
|
||||||
|
|
||||||
syncthing = SyncthingClient(host, gui_port, api_key)
|
syncthing = SyncthingClient(api_key, host, gui_port)
|
||||||
my_device_id = syncthing.get_my_device_id()
|
my_device_id = syncthing.get_my_device_id()
|
||||||
print("Found Device ID: {0}".format(my_device_id))
|
print("Found Device ID: {0}".format(my_device_id))
|
||||||
|
|
||||||
etcd = EtcdClient('etcd')
|
etcd = EtcdClient('etcd')
|
||||||
etcd.append_device_id(my_device_id)
|
etcd.append_device_id(my_device_id)
|
||||||
device_ids = etcd.get_device_list()
|
device_ids = etcd.get_device_list()
|
||||||
|
|
||||||
syncthing.add_devices(device_ids)
|
syncthing.add_devices(device_ids)
|
||||||
syncthing.print_debug_info()
|
syncthing.print_config()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -3,54 +3,57 @@ import json
|
||||||
import requests
|
import requests
|
||||||
from retrying import retry
|
from retrying import retry
|
||||||
|
|
||||||
|
DEFAULT_GUI_PORT = 8384
|
||||||
|
|
||||||
|
|
||||||
class SyncthingClient:
|
class SyncthingClient:
|
||||||
|
|
||||||
def __init__(self, host, port, key):
|
def __init__(self, api_key, host, port=DEFAULT_GUI_PORT):
|
||||||
|
self.api_key = api_key
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
self.key = key
|
self.headers = {'X-API-Key': self.api_key}
|
||||||
|
|
||||||
@retry
|
@retry
|
||||||
def get_my_device_id(self):
|
def get_my_device_id(self):
|
||||||
syncthing_headers = {'X-API-Key': self.key}
|
response = requests.get(self.make_url("/rest/system/status"), headers=self.headers)
|
||||||
response = requests.get("http://" + self.host + ":" + self.port + "/rest/system/status",
|
|
||||||
headers=syncthing_headers)
|
|
||||||
return json.loads(response.content)["myID"]
|
return json.loads(response.content)["myID"]
|
||||||
|
|
||||||
|
def add_devices(self, device_ids):
|
||||||
|
for device_id in device_ids:
|
||||||
|
self.add_device(device_id)
|
||||||
|
|
||||||
|
def add_device(self, device_id):
|
||||||
|
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(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_id, response.content))
|
||||||
|
|
||||||
@retry
|
@retry
|
||||||
def print_debug_info(self):
|
def print_config(self):
|
||||||
syncthing_headers = {'X-API-Key': self.key}
|
response = requests.get(self.make_url("/rest/config/devices"), headers=self.headers)
|
||||||
response = requests.get("http://" + self.host + ":" + self.port + "/rest/config/devices",
|
|
||||||
headers=syncthing_headers)
|
|
||||||
print("/rest/config/devices: {0}".format(response.content))
|
print("/rest/config/devices: {0}".format(response.content))
|
||||||
|
|
||||||
def add_devices(self, device_ids):
|
def make_url(self, endpoint):
|
||||||
syncthing_headers = {'X-API-Key': self.key}
|
return "http://{0}:{1}{2}".format(self.host, self.port, endpoint)
|
||||||
|
|
||||||
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://" + self.host + ":" + self.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))
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user