paulbsd-salt/states/_modules/syncthing.py

80 lines
2.2 KiB
Python
Raw Normal View History

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-01-17 17:43:56 +01: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
2020-07-10 00:58:55 +02:00
return ""
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
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
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
return None
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
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
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
return None
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
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
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
return None
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
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 {}
2021-01-17 17:43:56 +01:00
2020-09-15 13:14:34 +02:00
return None