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,32 +3,27 @@ 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"]
|
||||||
|
|
||||||
@retry
|
|
||||||
def print_debug_info(self):
|
|
||||||
syncthing_headers = {'X-API-Key': self.key}
|
|
||||||
response = requests.get("http://" + self.host + ":" + self.port + "/rest/config/devices",
|
|
||||||
headers=syncthing_headers)
|
|
||||||
print("/rest/config/devices: {0}".format(response.content))
|
|
||||||
|
|
||||||
def add_devices(self, device_ids):
|
def add_devices(self, device_ids):
|
||||||
syncthing_headers = {'X-API-Key': self.key}
|
|
||||||
|
|
||||||
for device_id in device_ids:
|
for device_id in device_ids:
|
||||||
|
self.add_device(device_id)
|
||||||
|
|
||||||
|
def add_device(self, device_id):
|
||||||
post_data = {
|
post_data = {
|
||||||
"deviceID": device_id,
|
"deviceID": device_id,
|
||||||
"name": "Laptop",
|
"name": "Laptop",
|
||||||
|
@ -50,7 +45,15 @@ class SyncthingClient:
|
||||||
"pendingFolders": [],
|
"pendingFolders": [],
|
||||||
"maxRequestKiB": 0
|
"maxRequestKiB": 0
|
||||||
}
|
}
|
||||||
|
response = requests.post(self.make_url("/rest/config/devices"),
|
||||||
|
headers=self.headers, data=json.dumps(post_data))
|
||||||
|
|
||||||
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))
|
print("Attempt to add device {0} to syncthing: {1}".format(device_id, 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))
|
||||||
|
|
||||||
|
def make_url(self, endpoint):
|
||||||
|
return "http://{0}:{1}{2}".format(self.host, self.port, endpoint)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user