updated syncthing state

This commit is contained in:
Paul 2023-02-19 17:59:18 +01:00
parent 3f8fd7bccc
commit aed47f284f
4 changed files with 58 additions and 9 deletions

3
states/_modules/net.py Normal file
View File

@ -0,0 +1,3 @@
#!/usr/bin/python3
ip_cidr_regex="\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}\/(\d+)"

View File

@ -29,7 +29,7 @@ def get_apikey(configfile="/root/.config/syncthing/config.xml"):
return None return None
def get_config(url, verify, apikey): def get_config(url="http://localhost:8384", verify=False, apikey=get_apikey()):
fullurl = f"{url}/rest/config" fullurl = f"{url}/rest/config"
req = Request(method="GET", req = Request(method="GET",
url=fullurl) url=fullurl)
@ -43,7 +43,48 @@ def get_config(url, verify, apikey):
return None return None
def set_config(url, verify, apikey, config): def set_config_options(url="http://localhost:8384", verify=False, apikey=get_apikey(), options={}):
fullurl = f"{url}/rest/config/options"
req = Request(method="PUT",
url=fullurl,
data=json.dumps(options).encode())
req.add_header("Content-Type", "application/json")
req.add_header("X-API-Key", apikey)
try:
res = urlopen(req, context=get_context(verify))
except HTTPError as err:
if err.status != 307:
return False
req.full_url = urljoin(url, err.headers['Location'])
res = urlopen(req, context=get_context(verify))
if res.status == 200:
return True
return None
def set_config_gui(url="http://localhost:8384", verify=False, apikey=get_apikey(), gui={}):
fullurl = f"{url}/rest/config/gui"
req = Request(method="PUT",
url=fullurl,
data=json.dumps(gui).encode())
req.add_header("Content-Type", "application/json")
req.add_header("X-API-Key", apikey)
try:
res = urlopen(req, context=get_context(verify))
except HTTPError as err:
if err.status != 307:
return False
req.full_url = urljoin(url, err.headers['Location'])
res = urlopen(req, context=get_context(verify))
except http.client.RemoteDisconnected as err:
return True
if res.status == 200:
return True
return None
## deprecated
def _set_config(url="http://localhost:8384", verify=False, apikey=get_apikey(), config={}):
fullurl = f"{url}/rest/config" fullurl = f"{url}/rest/config"
req = Request(method="POST", req = Request(method="POST",
url=fullurl, url=fullurl,
@ -63,7 +104,7 @@ def set_config(url, verify, apikey, config):
return None return None
def insync(url, verify, apikey): def insync(url="http://localhost:8384", verify=False, apikey=get_apikey()):
fullurl = f"{url}/rest/config/restart-required" fullurl = f"{url}/rest/config/restart-required"
req = Request(method="GET", req = Request(method="GET",
url=fullurl) url=fullurl)
@ -76,7 +117,7 @@ def insync(url, verify, apikey):
return None return None
def restart(url, verify, apikey): def restart(url="http://localhost:8384", verify=False, apikey=get_apikey()):
fullurl = f"{url}/rest/system/restart" fullurl = f"{url}/rest/system/restart"
req = Request(method="POST", req = Request(method="POST",
url=fullurl) url=fullurl)

View File

@ -13,14 +13,18 @@ def config(name, verify, url, cfg):
cfg = dict(cfg) cfg = dict(cfg)
apikey = __salt__['syncthing.get_apikey']() apikey = __salt__['syncthing.get_apikey']()
st_cfg = __salt__['syncthing.get_config'](url, verify, apikey) current_cfg = __salt__['syncthing.get_config'](url=url, verify=verify, apikey=apikey)
cfg['gui']['apiKey'] = apikey cfg['gui']['apiKey'] = apikey
res_cfg = salt.utils.dictupdate.update(st_cfg, cfg, recursive_update=True, merge_lists=False) res_cfg = salt.utils.dictupdate.update(current_cfg, cfg, recursive_update=True, merge_lists=False)
ret['changes'] = salt.utils.dictdiffer.deep_diff(current_cfg, res_cfg)
## Return to managed to set result ## Return to managed to set result
ret['result'] = __salt__['syncthing.set_config'](url, verify, apikey, res_cfg) res = []
ret['changes'] = salt.utils.dictdiffer.deep_diff(st_cfg, res_cfg) res.append(__salt__['syncthing.set_config_options'](url, verify, apikey, res_cfg['options']))
res.append(__salt__['syncthing.set_config_gui'](url, verify, apikey, res_cfg['gui']))
ret['result'] = all(res)
return ret return ret

View File

@ -16,6 +16,7 @@ syncthing:
user: "username" user: "username"
password: "$2a$10$NeZ3cfyOgZcdMGy9ixB7LOAP6z8tCOVjico6ZGLoK2QIQy734qPw." password: "$2a$10$NeZ3cfyOgZcdMGy9ixB7LOAP6z8tCOVjico6ZGLoK2QIQy734qPw."
options: options:
crashReportingEnabled: false
minHomeDiskFree: minHomeDiskFree:
value: "1" value: 1
unit: "GB" unit: "GB"