refactored syncthing client methods into SyncthingClient class
This commit is contained in:
parent
3589064dd0
commit
4c45a7d72b
|
@ -1,21 +1,24 @@
|
||||||
from syncthing_monitor.config_xml import parse_api_key, set_listen_ip_to_any
|
import syncthing_monitor.config_xml as xml
|
||||||
from syncthing_monitor.etcd_cluster_info import append_device_id, get_device_list
|
from syncthing_monitor.etcd_cluster_info import append_device_id, get_device_list
|
||||||
from syncthing_monitor.syncthing_rest import get_device_id, print_debug_info, post_devices
|
from syncthing_monitor.syncthing_rest import SyncthingClient
|
||||||
|
|
||||||
|
DEFAULT_CONFIG_XML_PATH = '/config/config.xml'
|
||||||
|
|
||||||
|
|
||||||
def loop(gui_port="8384", host="sync"):
|
def loop(gui_port="8384", host="sync"):
|
||||||
api_key = parse_api_key()
|
api_key = xml.parse_api_key(DEFAULT_CONFIG_XML_PATH)
|
||||||
print("Found API Key: {0}".format(api_key))
|
print("Found API Key: {0}".format(api_key))
|
||||||
|
|
||||||
set_listen_ip_to_any()
|
xml.set_listen_ip_to_any(DEFAULT_CONFIG_XML_PATH, DEFAULT_CONFIG_XML_PATH, 8384)
|
||||||
|
|
||||||
device_id = get_device_id(host, gui_port, api_key)
|
syncthing = SyncthingClient(host, gui_port, api_key)
|
||||||
print("Found Device ID: {0}".format(device_id))
|
my_device_id = syncthing.get_my_device_id()
|
||||||
|
print("Found Device ID: {0}".format(my_device_id))
|
||||||
|
|
||||||
append_device_id(device_id, 'etcd')
|
append_device_id(my_device_id, 'etcd')
|
||||||
device_ids = get_device_list('etcd')
|
device_ids = get_device_list('etcd')
|
||||||
post_devices(device_ids, host, gui_port, api_key)
|
syncthing.post_devices(device_ids)
|
||||||
print_debug_info(host, gui_port, api_key)
|
syncthing.print_debug_info()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -4,22 +4,29 @@ import requests
|
||||||
from retrying import retry
|
from retrying import retry
|
||||||
|
|
||||||
|
|
||||||
|
class SyncthingClient:
|
||||||
|
|
||||||
|
def __init__(self, host, port, key):
|
||||||
|
self.host = host
|
||||||
|
self.port = port
|
||||||
|
self.key = key
|
||||||
|
|
||||||
@retry
|
@retry
|
||||||
def get_device_id(host, gui_port, api_key):
|
def get_my_device_id(self):
|
||||||
syncthing_headers = {'X-API-Key': api_key}
|
syncthing_headers = {'X-API-Key': self.key}
|
||||||
response = requests.get("http://" + host + ":" + gui_port + "/rest/system/status", headers=syncthing_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
|
@retry
|
||||||
def print_debug_info(host, gui_port, api_key):
|
def print_debug_info(self):
|
||||||
syncthing_headers = {'X-API-Key': api_key}
|
syncthing_headers = {'X-API-Key': self.key}
|
||||||
response = requests.get("http://" + host + ":" + gui_port + "/rest/config/devices", headers=syncthing_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 post_devices(self, device_ids):
|
||||||
def post_devices(device_ids, host, gui_port, api_key):
|
syncthing_headers = {'X-API-Key': self.key}
|
||||||
syncthing_headers = {'X-API-Key': api_key}
|
|
||||||
|
|
||||||
for device_id in device_ids:
|
for device_id in device_ids:
|
||||||
post_data = {
|
post_data = {
|
||||||
|
@ -44,6 +51,6 @@ def post_devices(device_ids, host, gui_port, api_key):
|
||||||
"maxRequestKiB": 0
|
"maxRequestKiB": 0
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.post("http://" + host + ":" + gui_port + "/rest/config/devices",
|
response = requests.post("http://" + self.host + ":" + self.port + "/rest/config/devices",
|
||||||
headers=syncthing_headers, data=json.dumps(post_data))
|
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))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user