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
|
|
|
|
|
|
|
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"
|
2020-07-10 00:58:55 +02:00
|
|
|
return ""
|
|
|
|
|
|
|
|
def get_config(url, verify, apikey):
|
|
|
|
fullurl = "{0}/rest/system/config".format(url)
|
|
|
|
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
|
|
|
|
return None
|
|
|
|
|
|
|
|
def set_config(url, verify, apikey, config):
|
|
|
|
fullurl = "{0}/rest/system/config".format(url)
|
|
|
|
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
|
|
|
|
return None
|
|
|
|
|
|
|
|
def insync(url, verify, apikey):
|
|
|
|
fullurl = "{0}/rest/system/config/insync".format(url)
|
|
|
|
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
|
|
|
|
return None
|
|
|
|
|
|
|
|
def restart(url, verify, apikey):
|
|
|
|
fullurl = "{0}/rest/system/restart".format(url)
|
|
|
|
try:
|
2020-09-15 13:14:34 +02:00
|
|
|
req = requests.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 {}
|
2020-09-15 13:14:34 +02:00
|
|
|
return None
|