paulbsd-salt/states/_modules/syncthing.py

89 lines
2.3 KiB
Python
Raw Normal View History

2020-07-10 00:58:55 +02:00
#!/usr/bin/python3
2021-10-02 12:39:16 +02:00
import ssl
import json
2020-07-10 00:58:55 +02:00
import xml.etree.ElementTree as ET
2021-10-30 18:39:27 +02:00
from urllib.request import urlopen, Request
from urllib.parse import urljoin
from urllib.error import HTTPError
2021-10-02 12:39:16 +02:00
def get_context(verify):
ctx = None
if not verify:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
return ctx
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"
2021-10-02 12:39:16 +02:00
req = Request(method="GET",
url=fullurl)
req.add_header("X-API-Key", apikey)
res = urlopen(req, context=get_context(verify))
ret = json.loads(res.read())
if res.status == 200:
2020-07-10 00:58:55 +02:00
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"
2021-10-02 12:39:16 +02:00
req = Request(method="POST",
url=fullurl,
data=json.dumps(config).encode())
2021-11-14 13:35:25 +01:00
req.add_header("Content-Type", "application/json")
2021-10-02 12:39:16 +02:00
req.add_header("X-API-Key", apikey)
2021-10-30 18:39:27 +02:00
try:
res = urlopen(req, context=get_context(verify))
except HTTPError as err:
if err.status != 307:
return None
req.full_url = urljoin(url, err.headers['Location'])
res = urlopen(req, context=get_context(verify))
2021-10-02 12:39:16 +02:00
if res.status == 200:
2020-07-10 00:58:55 +02:00
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"
2021-10-02 12:39:16 +02:00
req = Request(method="GET",
url=fullurl)
req.add_header("X-API-Key", apikey)
res = urlopen(req, context=get_context(verify))
ret = json.loads(res.read())
if res.status == 200:
2020-07-10 00:58:55 +02:00
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"
2021-10-02 12:39:16 +02:00
req = Request(method="POST",
url=fullurl)
req.add_header("X-API-Key", apikey)
res = urlopen(req, context=get_context(verify))
if res.status == 200:
2020-07-10 00:58:55 +02:00
return {}
2021-01-17 17:43:56 +01:00
2020-09-15 13:14:34 +02:00
return None