updated molotov state

This commit is contained in:
Paul 2021-07-11 17:37:42 +02:00
parent 68572bce08
commit f68c24fff7
15 changed files with 183 additions and 71 deletions

View File

@ -2,12 +2,13 @@
import requests import requests
def get_jobs(url="http://localhost:8898", verify=False): def get_jobs(url="http://localhost:8898", verify=False):
"""get_jobs fetch jobs from dkron""" """get_jobs fetch jobs from dkron"""
fullurl = f"{url}/v1/jobs" fullurl = f"{url}/v1/jobs"
ret = dict() ret = dict()
try: try:
req = requests.request("get", fullurl, verify=verify) req = requests.request("GET", fullurl, verify=verify)
except (requests.exceptions.RequestException) as exc: except (requests.exceptions.RequestException) as exc:
raise f"Exception {exc} occured" raise f"Exception {exc} occured"
ret = req.json() ret = req.json()
@ -15,12 +16,13 @@ def get_jobs(url="http://localhost:8898", verify=False):
return ret return ret
return None return None
def set_jobs(url="http://localhost:8898", verify=False, job=None): def set_jobs(url="http://localhost:8898", verify=False, 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() ret = dict()
try: try:
req = requests.request("post", fullurl, verify=verify, json=job) req = requests.request("POST", fullurl, verify=verify, json=job)
except (requests.exceptions.RequestException) as exc: except (requests.exceptions.RequestException) as exc:
raise f"Exception {exc} occured" raise f"Exception {exc} occured"
ret = req.json() ret = req.json()

View File

@ -6,9 +6,11 @@ import ovh
from salt.exceptions import CommandExecutionError, ArgumentValueError from salt.exceptions import CommandExecutionError, ArgumentValueError
from ovh.exceptions import ResourceNotFoundError, APIError from ovh.exceptions import ResourceNotFoundError, APIError
def __virtual__(): def __virtual__():
return True return True
def _config(): def _config():
config = __salt__['config.get']('ovh') config = __salt__['config.get']('ovh')
if not config: if not config:
@ -18,6 +20,7 @@ def _config():
return config return config
def _auth(): def _auth():
cfg = _config() cfg = _config()
client = ovh.Client( client = ovh.Client(
@ -29,6 +32,7 @@ def _auth():
return client return client
def domain_get_zone(zone=""): def domain_get_zone(zone=""):
''' '''
Get DNS zone extraction Get DNS zone extraction
@ -78,6 +82,7 @@ def domain_get_record(zone="", fieldType="", subDomain="", target=""):
return "Query failed in OVH API" return "Query failed in OVH API"
return None return None
def domain_post_record(zone="", def domain_post_record(zone="",
fieldType="", fieldType="",
subDomain="", subDomain="",
@ -112,6 +117,7 @@ def domain_post_record(zone="",
return req return req
def domain_put_record(zone="", def domain_put_record(zone="",
fieldType="", fieldType="",
subDomain="", subDomain="",
@ -154,6 +160,7 @@ def domain_put_record(zone="",
return None return None
def domain_delete_record(zone="", fieldType="", subDomain=""): def domain_delete_record(zone="", fieldType="", subDomain=""):
''' '''
Delete a DNS record (Don't forget to refresh the zone) Delete a DNS record (Don't forget to refresh the zone)
@ -185,6 +192,7 @@ def domain_delete_record(zone="", fieldType="", subDomain=""):
return results return results
def domain_refresh_zone(zone=""): def domain_refresh_zone(zone=""):
''' '''
Apply zone modification on DNS servers Apply zone modification on DNS servers

49
states/_modules/pki.py Normal file
View File

@ -0,0 +1,49 @@
#!/usr/bin/python3
# vim:syntax=python
import os
import requests
def write_file_content(content=None,
file=None):
ret = None
try:
with open(file, "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
return ret
def get_pki_cert(url="http://pki",
username=None,
password=None,
domains=None):
ret = None
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

View File

@ -3,6 +3,7 @@
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import requests import requests
def get_apikey(configfile="/root/.config/syncthing/config.xml"): def get_apikey(configfile="/root/.config/syncthing/config.xml"):
try: try:
tree = ET.parse(configfile) tree = ET.parse(configfile)
@ -12,7 +13,8 @@ def get_apikey(configfile="/root/.config/syncthing/config.xml"):
except (FileNotFoundError, ET.ParseError, AttributeError) as exc: except (FileNotFoundError, ET.ParseError, AttributeError) as exc:
raise f"Exception {exc} occured" raise f"Exception {exc} occured"
return "" return None
def get_config(url, verify, apikey): def get_config(url, verify, apikey):
fullurl = f"{url}/rest/system/config" fullurl = f"{url}/rest/system/config"
@ -30,6 +32,7 @@ def get_config(url, verify, apikey):
return None return None
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: try:
@ -45,6 +48,7 @@ def set_config(url, verify, apikey, config):
return None return None
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: try:
@ -60,6 +64,7 @@ def insync(url, verify, apikey):
return None return None
def restart(url, verify, apikey): def restart(url, verify, apikey):
fullurl = f"{url}/rest/system/restart" fullurl = f"{url}/rest/system/restart"
try: try:

41
states/_states/pki.py Normal file
View File

@ -0,0 +1,41 @@
#!/usr/bin/python3
def fetched(name=None,
url="http://pki",
username=None,
password=None,
domains=None,
certfile=None,
keyfile=None):
ret = {
'name': name,
'changes': {},
'result': False,
'comment': 'Config is up to date'
}
currentcert = None
currentkey = None
domain_concat = domains.join(',')
currentcert = __salt__['pki.get_file_content'](checkfile=certfile)
currentkey = __salt__['pki.get_file_content'](checkfile=keyfile)
newcert, newkey = __salt__['pki.get_pki_cert'](url=url,
username=username,
password=password,
domains=domain_concat)
if currentcert != newcert or currentkey != newkey:
wcert = __salt__['pki.write_file_content'](newcert, certfile)
wkey = __salt__['pki.write_file_content'](newkey, keyfile)
ret["changes"]["old"] = [currentcert,currentkey].join("\n")
ret["changes"]["new"] = [newcert,newkey].join("\n")
ret["changes"]["diff"] = salt.utils.stringutils.get_diff([currentcert,currentkey].join("\n"),
[newcert,newkey].join("\n"))
ret["comment"] = "Updated certificates and keys"
ret["result"] = all([wcert, wkey])
return ret

View File

@ -8,7 +8,7 @@ def config(name, verify, url, cfg):
ret = {'name': name, ret = {'name': name,
'changes': {}, 'changes': {},
'result': True, 'result': True,
'comment': 'config is up to date'} 'comment': 'Config is up to date'}
cfg = dict(cfg) cfg = dict(cfg)

View File

@ -1,6 +1,6 @@
# vim:syntax=yaml # vim:syntax=yaml
---
{%- from "acme/map.jinja" import acme with context %} {%- from "acme/map.jinja" import acme with context %}
---
acmesh-install: acmesh-install:
cmd.run: cmd.run:
- name: "curl https://get.acme.sh | sh" - name: "curl https://get.acme.sh | sh"

View File

@ -1,7 +1,6 @@
# vim:syntax=yaml # vim:syntax=yaml
---
{%- from "acme/map.jinja" import acme with context %} {%- from "acme/map.jinja" import acme with context %}
---
{%- for dir in acme.directories %} {%- for dir in acme.directories %}
acme-directories-{{ dir }}: acme-directories-{{ dir }}:
file.directory: file.directory:

View File

@ -11,7 +11,7 @@ acme:
keysize: 4096 keysize: 4096
domains: [] domains: []
dns: "dns_provider" dns: "dns_provider"
fullcertfile: "/etc/acme/certs/certificate.crt" certfile: "/etc/acme/certs/certificate.crt"
keyfile: "/etc/acme/keys/private.key" keyfile: "/etc/acme/keys/private.key"
provider: provider:
api: api:

View File

@ -1,22 +1,12 @@
# vim:syntax=yaml # vim:syntax=yaml
---
{%- from "acme/map.jinja" import acme with context %} {%- from "acme/map.jinja" import acme with context %}
pkic-install: ---
file.managed: pki-fetched:
pki.fetched:
- name: /etc/acme/pkic.py - name: /etc/acme/pkic.py
- template: jinja - url: '{{ acme.provider.pki.url }}'
- source: salt://acme/pkic.py.j2 - username: '{{ acme.provider.pki.username }}'
- mode: 755 - password: '{{ acme.provider.pki.password }}'
- domains: '{{ acme.domains }}'
pkic-run: - certfile: '{{ acme.certfile }}'
cmd.run: - keyfile: '{{ acme.keyfile }}'
- name: /etc/acme/pkic.py
- env:
- URL: '{{ acme.provider.pki.url }}'
- DOMAINS: '{{ acme.domains|join(',') }}'
- FULLCERTFILE: '{{ acme.fullcertfile }}'
- KEYFILE: '{{ acme.keyfile }}'
- USERNAME: '{{ acme.provider.pki.username }}'
- PASSWORD: '{{ acme.provider.pki.password }}'
- require:
- file: pkic-install

View File

@ -0,0 +1,8 @@
---
molotov:
url: http://desktop-auto-upgrade.molotov.tv/linux
file: molotov.AppImage
version: 4.4.4
dest_path: /usr/local/bin
icon_path: /usr/share/icons/molotov.png
desktop_entry_path: /usr/share/applications/molotov.desktop

View File

@ -1,41 +1,3 @@
--- ---
molotov-install: include:
file.managed: - .install
- name: {{ salt['pillar.get']('molotov:dest_path') }}/molotov.{{ salt['pillar.get']('molotov:version') }}
- source: {{ salt['pillar.get']('molotov:url') }}/{{ salt['pillar.get']('molotov:version') }}/{{ salt['pillar.get']('molotov:file') }}
- skip_verify: true
- user: root
- group: root
- mode: 755
- if_missing: {{ salt['pillar.get']('molotov:dest_path') }}/molotov.{{ salt['pillar.get']('molotov:version') }}
molotov-symlink:
file.symlink:
- name: {{ salt['pillar.get']('molotov:dest_path') }}/molotov
- target: {{ salt['pillar.get']('molotov:dest_path') }}/molotov.{{ salt['pillar.get']('molotov:version') }}
- user: root
- group: root
- mode: 755
- require:
- file: molotov-install
molotov-icon:
file.managed:
- name: /usr/share/icons/molotov.png
- source: salt://molotov/molotov.png
- user: root
- group: root
- mode: 644
- require:
- file: molotov-install
molotov-desktop-entry:
file.managed:
- name: /usr/share/applications/molotov.desktop
- source: salt://molotov/molotov.desktop.j2
- template: jinja
- user: root
- group: root
- mode: 644
- require:
- file: molotov-install

View File

@ -0,0 +1,42 @@
---
{%- from "molotov/map.jinja" import molotov with context %}
molotov-install:
file.managed:
- name: {{ molotov.dest_path }}/molotov.{{ molotov.version }}
- source: {{ molotov.url }}/{{ molotov.version }}/{{ molotov.file }}
- skip_verify: true
- user: root
- group: root
- mode: 755
- if_missing: {{ molotov.dest_path }}/molotov.{{ molotov.version }}
molotov-symlink:
file.symlink:
- name: {{ molotov.dest_path }}/molotov
- target: {{ molotov.dest_path }}/molotov.{{ molotov.version }}
- user: root
- group: root
- mode: 755
- require:
- file: molotov-install
molotov-icon:
file.managed:
- name: {{ molotov.icon_path }}
- source: salt://molotov/molotov.png
- user: root
- group: root
- mode: 644
- require:
- file: molotov-install
molotov-desktop-entry:
file.managed:
- name: {{ molotov.desktop_entry_path }}
- source: salt://molotov/molotov.desktop.j2
- template: jinja
- user: root
- group: root
- mode: 644
- require:
- file: molotov-install

5
states/molotov/map.jinja Normal file
View File

@ -0,0 +1,5 @@
{%- import_yaml "molotov/defaults.yaml" as default_settings -%}
{%- set defaults = salt['grains.filter_by'](default_settings, default='molotov') -%}
{%- set molotov = salt['pillar.get']('molotov', default=defaults, merge=True) -%}

View File

@ -1,10 +1,11 @@
{%- from "molotov/map.jinja" import molotov with context %}
[Desktop Entry] [Desktop Entry]
Name=Molotov Name=Molotov
Encoding=UTF-8 Encoding=UTF-8
Version=1.0 Version={{ molotov.version }}
Comment=The app to watch TV, for free Comment=The app to watch TV, for free
Exec={{ salt['pillar.get']('molotov:dest_path') }}/molotov Exec={{ molotov.dest_path }}/molotov
Icon=/usr/share/icons/molotov.png Icon={{ molotov.icon_path }}
Terminal=false Terminal=false
StartupWMClass=Molotov StartupWMClass=Molotov
Type=Application Type=Application