updated scripts

This commit is contained in:
Paul 2021-10-02 12:39:16 +02:00
parent 5966e77b98
commit 360fe18d57
5 changed files with 83 additions and 93 deletions

View File

@ -1,31 +1,28 @@
#!/usr/bin/python3
import requests
import json
from urllib.request import urlopen, Request
def get_jobs(url="http://localhost:8898", verify=False):
def get_jobs(url="http://localhost:8898"):
"""get_jobs fetch jobs from dkron"""
fullurl = f"{url}/v1/jobs"
ret = dict()
try:
req = requests.request("GET", fullurl, verify=verify)
except (requests.exceptions.RequestException) as exc:
raise f"Exception {exc} occured"
ret = req.json()
if req.status_code == 200:
return ret
req = Request(method="GET", url=fullurl)
res = urlopen(req)
dkron_ret = json.loads(res.read())
if res.status == 200:
return dkron_ret
return None
def set_jobs(url="http://localhost:8898", verify=False, job=None):
def set_jobs(url="http://localhost:8898", job=None):
"""set_jobs set jobs on dkron"""
fullurl = f"{url}/v1/jobs"
ret = dict()
try:
req = requests.request("POST", fullurl, verify=verify, json=job)
except (requests.exceptions.RequestException) as exc:
raise f"Exception {exc} occured"
ret = req.json()
if req.status_code == 201:
return ret
data = json.dumps(job).encode('utf-8')
req = Request(method="POST", url=fullurl, data=data)
req.add_header('Content-Type', 'application/json')
res = urlopen(req)
dkron_ret = json.loads(res.read())
if res.status == 201:
return dkron_ret
return None

View File

@ -1,31 +1,29 @@
#!/usr/bin/python3
# vim:syntax=python
import os
import requests
import json
import base64
from urllib.request import urlopen
from urllib.request import Request
def get_file_content(checkfile=None):
try:
with open(checkfile, "r") as f:
ret = f.read()
except OSError:
return None
return ret
def write_file_content(content=None,
filename=None):
ret = None
try:
with open(filename, "w") as f:
ret = f.write(content)
except Exception as err:
pass
return
def get_file_content(checkfile=None):
ret = None
try:
with open(checkfile, 'r') as f:
ret = f.read()
except FileNotFoundError as err:
pass
except OSError:
return None
return ret
@ -34,16 +32,11 @@ def get_pki_cert(url="http://pki",
username=None,
password=None,
domains=None):
ret = None
req = Request(method="GET",
url=f"{url}/domain/{domains}")
authstring = base64.b64encode(f"{username}:{password}".encode()).decode()
req.add_header("Authorization", f"Basic {authstring}")
res = urlopen(req)
resj = json.loads(res.read())
try:
res = requests.request(method="GET",
url=f"{url}/domain/{domains}",
auth=(username, password))
resj = res.json()
return resj["certificate"], resj["privatekey"]
except Exception as err:
pass
return None, None
return resj["certificate"], resj["privatekey"]

View File

@ -1,7 +1,19 @@
#!/usr/bin/python3
import ssl
import json
import xml.etree.ElementTree as ET
import requests
from urllib.request import urlopen
from urllib.request import Request
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
def get_apikey(configfile="/root/.config/syncthing/config.xml"):
@ -18,16 +30,13 @@ def get_apikey(configfile="/root/.config/syncthing/config.xml"):
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:
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:
return ret
return None
@ -35,15 +44,12 @@ def get_config(url, verify, apikey):
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:
req = Request(method="POST",
url=fullurl,
data=json.dumps(config).encode())
req.add_header("X-API-Key", apikey)
res = urlopen(req, context=get_context(verify))
if res.status == 200:
return True
return None
@ -51,15 +57,12 @@ def set_config(url, verify, apikey, config):
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:
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:
return ret
return None
@ -67,14 +70,11 @@ def insync(url, verify, apikey):
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:
req = Request(method="POST",
url=fullurl)
req.add_header("X-API-Key", apikey)
res = urlopen(req, context=get_context(verify))
if res.status == 200:
return {}
return None

View File

@ -11,7 +11,7 @@ def jobs(name, url="http://localhost:8080", verify=False, jobs_list=[]):
'comment': 'Config is up to date'}
for job in jobs_list:
res = __salt__['dkron.set_jobs'](url, verify, job)
res = __salt__['dkron.set_jobs'](url, job)
if res is not None:
ret['changes'][job['name']] = res
else:

View File

@ -30,8 +30,8 @@ def fetched(name=None,
password=password,
domains=domain_concat)
if currentcert != newcert or currentkey != newkey:
if len(newcert) > 10 or len(newkey) > 10:
if all([newcert,newkey]):
if currentcert != newcert or currentkey != newkey:
wcert = __salt__['pki.write_file_content'](newcert, fullcertfile)
wkey = __salt__['pki.write_file_content'](newkey, keyfile)
@ -42,10 +42,10 @@ def fetched(name=None,
ret["comment"] = "Updated certificates and keys"
ret["result"] = all([wcert, wkey])
else:
ret["comment"] = "Error fetching in certificate / key length"
return ret
ret["comment"] = "Config is good and not changed"
ret["result"] = True
else:
ret["comment"] = "Config is good and not changed"
ret["result"] = True
ret["comment"] = "Error fetching in certificate / key length"
return ret
return ret