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,46 +4,53 @@ import requests
|
||||||
from retrying import retry
|
from retrying import retry
|
||||||
|
|
||||||
|
|
||||||
@retry
|
class SyncthingClient:
|
||||||
def get_device_id(host, gui_port, api_key):
|
|
||||||
syncthing_headers = {'X-API-Key': api_key}
|
|
||||||
response = requests.get("http://" + host + ":" + gui_port + "/rest/system/status", headers=syncthing_headers)
|
|
||||||
return json.loads(response.content)["myID"]
|
|
||||||
|
|
||||||
|
def __init__(self, host, port, key):
|
||||||
|
self.host = host
|
||||||
|
self.port = port
|
||||||
|
self.key = key
|
||||||
|
|
||||||
@retry
|
@retry
|
||||||
def print_debug_info(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/config/devices", headers=syncthing_headers)
|
response = requests.get("http://" + self.host + ":" + self.port + "/rest/system/status",
|
||||||
print("/rest/config/devices: {0}".format(response.content))
|
headers=syncthing_headers)
|
||||||
|
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 post_devices(device_ids, host, gui_port, api_key):
|
def post_devices(self, device_ids):
|
||||||
syncthing_headers = {'X-API-Key': api_key}
|
syncthing_headers = {'X-API-Key': self.key}
|
||||||
|
|
||||||
for device_id in device_ids:
|
for device_id in device_ids:
|
||||||
post_data = {
|
post_data = {
|
||||||
"deviceID": device_id,
|
"deviceID": device_id,
|
||||||
"name": "Laptop",
|
"name": "Laptop",
|
||||||
"addresses": [
|
"addresses": [
|
||||||
"dynamic",
|
"dynamic",
|
||||||
"tcp://192.168.1.2:22000"
|
"tcp://192.168.1.2:22000"
|
||||||
],
|
],
|
||||||
"compression": "metadata",
|
"compression": "metadata",
|
||||||
"certName": "",
|
"certName": "",
|
||||||
"introducer": False,
|
"introducer": False,
|
||||||
"skipIntroductionRemovals": False,
|
"skipIntroductionRemovals": False,
|
||||||
"introducedBy": "",
|
"introducedBy": "",
|
||||||
"paused": False,
|
"paused": False,
|
||||||
"allowedNetworks": [],
|
"allowedNetworks": [],
|
||||||
"autoAcceptFolders": False,
|
"autoAcceptFolders": False,
|
||||||
"maxSendKbps": 0,
|
"maxSendKbps": 0,
|
||||||
"maxRecvKbps": 0,
|
"maxRecvKbps": 0,
|
||||||
"ignoredFolders": [],
|
"ignoredFolders": [],
|
||||||
"pendingFolders": [],
|
"pendingFolders": [],
|
||||||
"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