diff --git a/states/_modules/net.py b/states/_modules/net.py new file mode 100644 index 0000000..7d5f660 --- /dev/null +++ b/states/_modules/net.py @@ -0,0 +1,3 @@ +#!/usr/bin/python3 + +ip_cidr_regex="\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}\/(\d+)" diff --git a/states/_modules/syncthing.py b/states/_modules/syncthing.py index dd78109..a36f306 100644 --- a/states/_modules/syncthing.py +++ b/states/_modules/syncthing.py @@ -29,7 +29,7 @@ def get_apikey(configfile="/root/.config/syncthing/config.xml"): 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" req = Request(method="GET", url=fullurl) @@ -43,7 +43,48 @@ def get_config(url, verify, apikey): 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" req = Request(method="POST", url=fullurl, @@ -63,7 +104,7 @@ def set_config(url, verify, apikey, config): 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" req = Request(method="GET", url=fullurl) @@ -76,7 +117,7 @@ def insync(url, verify, apikey): return None -def restart(url, verify, apikey): +def restart(url="http://localhost:8384", verify=False, apikey=get_apikey()): fullurl = f"{url}/rest/system/restart" req = Request(method="POST", url=fullurl) diff --git a/states/_states/syncthing.py b/states/_states/syncthing.py index 05c58a8..3aac8fc 100644 --- a/states/_states/syncthing.py +++ b/states/_states/syncthing.py @@ -13,14 +13,18 @@ def config(name, verify, url, cfg): cfg = dict(cfg) 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 - 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 - ret['result'] = __salt__['syncthing.set_config'](url, verify, apikey, res_cfg) - ret['changes'] = salt.utils.dictdiffer.deep_diff(st_cfg, res_cfg) + res = [] + 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 diff --git a/states/syncthing/defaults.yaml b/states/syncthing/defaults.yaml index abff183..a243fdb 100644 --- a/states/syncthing/defaults.yaml +++ b/states/syncthing/defaults.yaml @@ -16,6 +16,7 @@ syncthing: user: "username" password: "$2a$10$NeZ3cfyOgZcdMGy9ixB7LOAP6z8tCOVjico6ZGLoK2QIQy734qPw." options: + crashReportingEnabled: false minHomeDiskFree: - value: "1" + value: 1 unit: "GB"