paulbsd-salt/states/_modules/syncthing.py

76 lines
2.2 KiB
Python

#!/usr/bin/python3
import xml.etree.ElementTree as ET
import requests
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
except (FileNotFoundError, ET.ParseError, AttributeError) as exc:
raise f"Exception {exc} occured"
return ""
def get_config(url, verify, apikey):
fullurl = f"{url}/rest/system/config"
ret = dict()
try:
req = requests.request("get",
fullurl,
verify=verify,
headers={"X-API-Key": apikey})
except (requests.exceptions.RequestException) as exc:
raise f"Exception {exc} occured"
ret = req.json()
if req.status_code == 200:
return ret
return None
def set_config(url, verify, apikey, config):
fullurl = f"{url}/rest/system/config"
try:
req = requests.request("post",
fullurl,
verify=verify,
headers={"X-API-Key": apikey},
json=config)
except (requests.exceptions.RequestException) as exc:
raise f"Exception {exc} occured"
if req.status_code == 200:
return True
return None
def insync(url, verify, apikey):
fullurl = f"{url}/rest/system/config/insync"
try:
req = requests.request("get",
fullurl,
verify=verify,
headers={"X-API-Key": apikey})
except (requests.exceptions.RequestException) as exc:
raise f"Exception {exc} occured"
ret = req.json()
if req.status_code == 200:
return ret
return None
def restart(url, verify, apikey):
fullurl = f"{url}/rest/system/restart"
try:
req = requests.request("post",
fullurl,
verify=verify,
headers={"X-API-Key": apikey})
except (requests.exceptions.RequestException) as exc:
raise f"Exception {exc} occured"
if req.status_code == 200:
return {}
return None