updated scripts
This commit is contained in:
parent
5966e77b98
commit
360fe18d57
@ -1,31 +1,28 @@
|
|||||||
#!/usr/bin/python3
|
#!/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"""
|
"""get_jobs fetch jobs from dkron"""
|
||||||
fullurl = f"{url}/v1/jobs"
|
fullurl = f"{url}/v1/jobs"
|
||||||
ret = dict()
|
req = Request(method="GET", url=fullurl)
|
||||||
try:
|
res = urlopen(req)
|
||||||
req = requests.request("GET", fullurl, verify=verify)
|
dkron_ret = json.loads(res.read())
|
||||||
except (requests.exceptions.RequestException) as exc:
|
if res.status == 200:
|
||||||
raise f"Exception {exc} occured"
|
return dkron_ret
|
||||||
ret = req.json()
|
|
||||||
if req.status_code == 200:
|
|
||||||
return ret
|
|
||||||
return None
|
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"""
|
"""set_jobs set jobs on dkron"""
|
||||||
fullurl = f"{url}/v1/jobs"
|
fullurl = f"{url}/v1/jobs"
|
||||||
ret = dict()
|
data = json.dumps(job).encode('utf-8')
|
||||||
try:
|
req = Request(method="POST", url=fullurl, data=data)
|
||||||
req = requests.request("POST", fullurl, verify=verify, json=job)
|
req.add_header('Content-Type', 'application/json')
|
||||||
except (requests.exceptions.RequestException) as exc:
|
res = urlopen(req)
|
||||||
raise f"Exception {exc} occured"
|
dkron_ret = json.loads(res.read())
|
||||||
ret = req.json()
|
if res.status == 201:
|
||||||
if req.status_code == 201:
|
return dkron_ret
|
||||||
return ret
|
|
||||||
return None
|
return None
|
||||||
|
@ -1,31 +1,29 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
# vim:syntax=python
|
# vim:syntax=python
|
||||||
|
|
||||||
import os
|
import json
|
||||||
import requests
|
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,
|
def write_file_content(content=None,
|
||||||
filename=None):
|
filename=None):
|
||||||
ret = None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(filename, "w") as f:
|
with open(filename, "w") as f:
|
||||||
ret = f.write(content)
|
ret = f.write(content)
|
||||||
except Exception as err:
|
except OSError:
|
||||||
pass
|
return None
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def get_file_content(checkfile=None):
|
|
||||||
ret = None
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(checkfile, 'r') as f:
|
|
||||||
ret = f.read()
|
|
||||||
except FileNotFoundError as err:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@ -34,16 +32,11 @@ def get_pki_cert(url="http://pki",
|
|||||||
username=None,
|
username=None,
|
||||||
password=None,
|
password=None,
|
||||||
domains=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:
|
return resj["certificate"], resj["privatekey"]
|
||||||
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
|
|
||||||
|
@ -1,7 +1,19 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import ssl
|
||||||
|
import json
|
||||||
import xml.etree.ElementTree as ET
|
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"):
|
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):
|
def get_config(url, verify, apikey):
|
||||||
fullurl = f"{url}/rest/system/config"
|
fullurl = f"{url}/rest/system/config"
|
||||||
ret = dict()
|
req = Request(method="GET",
|
||||||
try:
|
url=fullurl)
|
||||||
req = requests.request("get",
|
req.add_header("X-API-Key", apikey)
|
||||||
fullurl,
|
res = urlopen(req, context=get_context(verify))
|
||||||
verify=verify,
|
|
||||||
headers={"X-API-Key": apikey})
|
ret = json.loads(res.read())
|
||||||
except (requests.exceptions.RequestException) as exc:
|
if res.status == 200:
|
||||||
raise f"Exception {exc} occured"
|
|
||||||
ret = req.json()
|
|
||||||
if req.status_code == 200:
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
return None
|
return None
|
||||||
@ -35,15 +44,12 @@ def get_config(url, verify, apikey):
|
|||||||
|
|
||||||
def set_config(url, verify, apikey, config):
|
def set_config(url, verify, apikey, config):
|
||||||
fullurl = f"{url}/rest/system/config"
|
fullurl = f"{url}/rest/system/config"
|
||||||
try:
|
req = Request(method="POST",
|
||||||
req = requests.request("post",
|
url=fullurl,
|
||||||
fullurl,
|
data=json.dumps(config).encode())
|
||||||
verify=verify,
|
req.add_header("X-API-Key", apikey)
|
||||||
headers={"X-API-Key": apikey},
|
res = urlopen(req, context=get_context(verify))
|
||||||
json=config)
|
if res.status == 200:
|
||||||
except (requests.exceptions.RequestException) as exc:
|
|
||||||
raise f"Exception {exc} occured"
|
|
||||||
if req.status_code == 200:
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return None
|
return None
|
||||||
@ -51,15 +57,12 @@ def set_config(url, verify, apikey, config):
|
|||||||
|
|
||||||
def insync(url, verify, apikey):
|
def insync(url, verify, apikey):
|
||||||
fullurl = f"{url}/rest/system/config/insync"
|
fullurl = f"{url}/rest/system/config/insync"
|
||||||
try:
|
req = Request(method="GET",
|
||||||
req = requests.request("get",
|
url=fullurl)
|
||||||
fullurl,
|
req.add_header("X-API-Key", apikey)
|
||||||
verify=verify,
|
res = urlopen(req, context=get_context(verify))
|
||||||
headers={"X-API-Key": apikey})
|
ret = json.loads(res.read())
|
||||||
except (requests.exceptions.RequestException) as exc:
|
if res.status == 200:
|
||||||
raise f"Exception {exc} occured"
|
|
||||||
ret = req.json()
|
|
||||||
if req.status_code == 200:
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
return None
|
return None
|
||||||
@ -67,14 +70,11 @@ def insync(url, verify, apikey):
|
|||||||
|
|
||||||
def restart(url, verify, apikey):
|
def restart(url, verify, apikey):
|
||||||
fullurl = f"{url}/rest/system/restart"
|
fullurl = f"{url}/rest/system/restart"
|
||||||
try:
|
req = Request(method="POST",
|
||||||
req = requests.request("post",
|
url=fullurl)
|
||||||
fullurl,
|
req.add_header("X-API-Key", apikey)
|
||||||
verify=verify,
|
res = urlopen(req, context=get_context(verify))
|
||||||
headers={"X-API-Key": apikey})
|
if res.status == 200:
|
||||||
except (requests.exceptions.RequestException) as exc:
|
|
||||||
raise f"Exception {exc} occured"
|
|
||||||
if req.status_code == 200:
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
@ -11,7 +11,7 @@ def jobs(name, url="http://localhost:8080", verify=False, jobs_list=[]):
|
|||||||
'comment': 'Config is up to date'}
|
'comment': 'Config is up to date'}
|
||||||
|
|
||||||
for job in jobs_list:
|
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:
|
if res is not None:
|
||||||
ret['changes'][job['name']] = res
|
ret['changes'][job['name']] = res
|
||||||
else:
|
else:
|
||||||
|
@ -30,8 +30,8 @@ def fetched(name=None,
|
|||||||
password=password,
|
password=password,
|
||||||
domains=domain_concat)
|
domains=domain_concat)
|
||||||
|
|
||||||
if currentcert != newcert or currentkey != newkey:
|
if all([newcert,newkey]):
|
||||||
if len(newcert) > 10 or len(newkey) > 10:
|
if currentcert != newcert or currentkey != newkey:
|
||||||
wcert = __salt__['pki.write_file_content'](newcert, fullcertfile)
|
wcert = __salt__['pki.write_file_content'](newcert, fullcertfile)
|
||||||
wkey = __salt__['pki.write_file_content'](newkey, keyfile)
|
wkey = __salt__['pki.write_file_content'](newkey, keyfile)
|
||||||
|
|
||||||
@ -42,10 +42,10 @@ def fetched(name=None,
|
|||||||
ret["comment"] = "Updated certificates and keys"
|
ret["comment"] = "Updated certificates and keys"
|
||||||
ret["result"] = all([wcert, wkey])
|
ret["result"] = all([wcert, wkey])
|
||||||
else:
|
else:
|
||||||
ret["comment"] = "Error fetching in certificate / key length"
|
ret["comment"] = "Config is good and not changed"
|
||||||
return ret
|
ret["result"] = True
|
||||||
else:
|
else:
|
||||||
ret["comment"] = "Config is good and not changed"
|
ret["comment"] = "Error fetching in certificate / key length"
|
||||||
ret["result"] = True
|
return ret
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
Loading…
Reference in New Issue
Block a user