2020-07-10 00:58:55 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import xml.etree.ElementTree as ET
|
2020-09-15 13:14:34 +02:00
|
|
|
import requests
|
2020-07-10 00:58:55 +02:00
|
|
|
|
2021-07-11 17:37:42 +02:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
def get_apikey(configfile="/root/.config/syncthing/config.xml"):
|
|
|
|
try:
|
|
|
|
tree = ET.parse(configfile)
|
|
|
|
root = tree.getroot()
|
|
|
|
apikey = root.find("./gui/apikey").text
|
|
|
|
return apikey
|
2020-09-15 13:14:34 +02:00
|
|
|
except (FileNotFoundError, ET.ParseError, AttributeError) as exc:
|
|
|
|
raise f"Exception {exc} occured"
|
2021-01-17 17:43:56 +01:00
|
|
|
|
2021-07-11 17:37:42 +02:00
|
|
|
return None
|
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
|
|
|
|
def get_config(url, verify, apikey):
|
2021-06-14 23:05:24 +02:00
|
|
|
fullurl = f"{url}/rest/system/config"
|
2020-07-10 00:58:55 +02:00
|
|
|
ret = dict()
|
|
|
|
try:
|
2020-09-15 13:14:34 +02:00
|
|
|
req = requests.request("get",
|
|
|
|
fullurl,
|
|
|
|
verify=verify,
|
|
|
|
headers={"X-API-Key": apikey})
|
2020-07-10 00:58:55 +02:00
|
|
|
except (requests.exceptions.RequestException) as exc:
|
2020-09-15 13:14:34 +02:00
|
|
|
raise f"Exception {exc} occured"
|
2020-07-10 00:58:55 +02:00
|
|
|
ret = req.json()
|
|
|
|
if req.status_code == 200:
|
|
|
|
return ret
|
2021-01-17 17:43:56 +01:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
return None
|
|
|
|
|
2021-07-11 17:37:42 +02:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
def set_config(url, verify, apikey, config):
|
2021-06-14 23:05:24 +02:00
|
|
|
fullurl = f"{url}/rest/system/config"
|
2020-07-10 00:58:55 +02:00
|
|
|
try:
|
2020-09-15 13:14:34 +02:00
|
|
|
req = requests.request("post",
|
|
|
|
fullurl,
|
|
|
|
verify=verify,
|
|
|
|
headers={"X-API-Key": apikey},
|
|
|
|
json=config)
|
2020-07-10 00:58:55 +02:00
|
|
|
except (requests.exceptions.RequestException) as exc:
|
2020-09-15 13:14:34 +02:00
|
|
|
raise f"Exception {exc} occured"
|
2020-07-10 00:58:55 +02:00
|
|
|
if req.status_code == 200:
|
|
|
|
return True
|
2021-01-17 17:43:56 +01:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
return None
|
|
|
|
|
2021-07-11 17:37:42 +02:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
def insync(url, verify, apikey):
|
2021-06-14 23:05:24 +02:00
|
|
|
fullurl = f"{url}/rest/system/config/insync"
|
2020-07-10 00:58:55 +02:00
|
|
|
try:
|
2020-09-15 13:14:34 +02:00
|
|
|
req = requests.request("get",
|
|
|
|
fullurl,
|
|
|
|
verify=verify,
|
|
|
|
headers={"X-API-Key": apikey})
|
2020-07-10 00:58:55 +02:00
|
|
|
except (requests.exceptions.RequestException) as exc:
|
2020-09-15 13:14:34 +02:00
|
|
|
raise f"Exception {exc} occured"
|
2020-07-10 00:58:55 +02:00
|
|
|
ret = req.json()
|
|
|
|
if req.status_code == 200:
|
|
|
|
return ret
|
2021-01-17 17:43:56 +01:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
return None
|
|
|
|
|
2021-07-11 17:37:42 +02:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
def restart(url, verify, apikey):
|
2021-06-14 23:05:24 +02:00
|
|
|
fullurl = f"{url}/rest/system/restart"
|
2020-07-10 00:58:55 +02:00
|
|
|
try:
|
2021-06-17 10:06:44 +02:00
|
|
|
req = requests.request("post",
|
|
|
|
fullurl,
|
|
|
|
verify=verify,
|
|
|
|
headers={"X-API-Key": apikey})
|
2020-07-10 00:58:55 +02:00
|
|
|
except (requests.exceptions.RequestException) as exc:
|
2020-09-15 13:14:34 +02:00
|
|
|
raise f"Exception {exc} occured"
|
2020-07-10 00:58:55 +02:00
|
|
|
if req.status_code == 200:
|
|
|
|
return {}
|
2021-01-17 17:43:56 +01:00
|
|
|
|
2020-09-15 13:14:34 +02:00
|
|
|
return None
|