Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/support loki 3 #2

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# E221 multiple spaces before operator
# E251 unexpected spaces around keyword / parameter equals

ignore = E221,E251
# ignore = E221,E251

exclude =
# No need to traverse our git directory
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/loki.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ jobs:
- with-updates
- "2.5"
- "2.6"
- "2.7"
- "3.2"
collection_role:
- loki

Expand Down
1 change: 0 additions & 1 deletion plugins/filter/grafana.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def keyfiles(self, data):

return result


def content_security_policy(self, data):
"""
"""
Expand Down
8 changes: 4 additions & 4 deletions plugins/filter/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def validate_datasource_type(self, data):
)

return dict(
valid = True,
msg = "All datasources are valid."
valid=True,
msg="All datasources are valid."
)

def absent_datasources(self, data):
Expand All @@ -121,8 +121,8 @@ def absent_datasources(self, data):
# res = {}
if values.get("state", "present") == "absent":
res = dict(
name = datasource,
orgId = values.get("org_id", 1)
name=datasource,
orgId=values.get("org_id", 1)
)
result.append(res)

Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/grafana_api_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def run(self):
self.module.log(msg=f" all : {self.grafana_api_keys} / {type(self.grafana_api_keys)}")

present_keys = [v for v in self.grafana_api_keys if v.get('state', "present") == "present"]
absent_keys = [v for v in self.grafana_api_keys if v.get('state', "present") == "absent"]
absent_keys = [v for v in self.grafana_api_keys if v.get('state', "present") == "absent"]
# disabled_keys = [v for v in self.grafana_api_keys if v.get('state', "present") == "disabled"]

# self.module.log(msg=f" present : {present_keys}")
Expand Down
26 changes: 13 additions & 13 deletions plugins/modules/grafana_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ def _list(self):
_failed = False

return dict(
failed = _failed,
changed = _changed,
installed = plugin_list
failed=_failed,
changed=_changed,
installed=plugin_list
)

def _list_remote(self):
Expand Down Expand Up @@ -162,9 +162,9 @@ def _install(self):

if len(self.plugins) == 0:
return dict(
failed = True,
changed =False,
msg = "Missing a list of Plugins to install."
failed=True,
changed=False,
msg="Missing a list of Plugins to install."
)

plugin_list = self._list()
Expand Down Expand Up @@ -196,8 +196,8 @@ def _install(self):

res = {}
res[p] = dict(
failed = False,
changed = False,
failed=False,
changed=False,
state=f"already in version {plugin_version} installed."
)
result_state.append(res)
Expand All @@ -217,8 +217,8 @@ def _install(self):

res = {}
res[p] = dict(
failed = False,
changed = True,
failed=False,
changed=True,
state=f"version {version_string} successfuly installed."
)
result_state.append(res)
Expand Down Expand Up @@ -249,9 +249,9 @@ def _install(self):
# self.module.log(msg=f" result_msg : '{result_msg}'")

return dict(
failed = failed,
changed = changed,
msg = result_msg
failed=failed,
changed=changed,
msg=result_msg
)

def _uninstall(self):
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/grafana_service_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def run(self):
self.module.log(msg=f" account : {self.grafana_service_accounts}")

present_keys = [v for v in self.grafana_service_accounts if v.get('state', "present") == "present"]
absent_keys = [v for v in self.grafana_service_accounts if v.get('state', "present") == "absent"]
absent_keys = [v for v in self.grafana_service_accounts if v.get('state', "present") == "absent"]
# disabled_keys = [v for v in self.grafana_service_accounts if v.get('state', "present") == "disabled"]

# self.module.log(msg=f" must present : {present_keys}")
Expand Down
150 changes: 150 additions & 0 deletions plugins/modules/loki_verify_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-

# (c) 2022, Bodo Schulz <[email protected]>

from __future__ import absolute_import, division, print_function
from ansible.module_utils.basic import AnsibleModule

# import os
# from requests.exceptions import ConnectionError
import json
# import requests
# from pathlib import Path

# ---------------------------------------------------------------------------------------

DOCUMENTATION = """
module: loki_verify_config
version_added: 1.1.0
author: "Bodo Schulz (@bodsch) <[email protected]>"

short_description: TBD

description:
- TBD
"""

EXAMPLES = """
"""

RETURN = """
"""

# ---------------------------------------------------------------------------------------


class LokiVerifyConfig(object):

def __init__(self, module):
"""
"""
self.module = module

self.log_level = module.params.get("log_level")
self.config_file = module.params.get("config_file")

self._loki_cli = module.get_bin_path("loki", True)

def run(self):
"""
/usr/bin/loki -verify-config -log.format json -log.level debug -config.file /etc/loki/loki.yml
"""
result = dict(
rc=0,
failed=False,
changed=False,
msg="Loki verify config ..."
)

_failed = True

args = []

args.append(self._loki_cli)
args.append("-verify-config")

args.append("-log.format")
args.append("json")

if self.log_level:
args.append("-log.level")
args.append(self.log_level)

if self.config_file:
args.append("-config.file")
args.append(self.config_file)

rc, out, err = self.__exec(args, check_rc=False)

err = err.strip()
# self.module.log(msg=f" -> '{err}' ({type(err)})")

if rc == 0:
_failed = False

if rc != 0 and len(err) > 0:
if isinstance(err, str):
err = json.loads(err)

if isinstance(err, dict):
# log_level = err.get("level", "info")
error_msg = err.get("err", None)
msg = err.get("msg", None)
# if log_level == "warn":
# msg = error_msg = err.get("msg", None)
if rc != 0 and error_msg:
msg = error_msg.split("\n")
else:
msg = err
else:
msg = "unknow config error."

result = dict(
failed=_failed,
changed=False,
cmd=" ".join(args),
msg=msg
)

return result

def __exec(self, commands, check_rc=True):
"""
"""
rc, out, err = self.module.run_command(commands, check_rc=check_rc)
# self.module.log(msg=f" rc : '{rc}'")
# self.module.log(msg=f" out: '{out}'")
# self.module.log(msg=f" err: '{err}'")
return rc, out, err


def main():
"""
"""
specs = dict(
log_level=dict(
default="info",
choices=["debug", "info", "warn", "error"]
),
config_file=dict(
default="/etc/loki/loki.yml",
type="str",
),
)

module = AnsibleModule(
argument_spec=specs,
supports_check_mode=False,
)

o = LokiVerifyConfig(module)
result = o.run()

module.log(msg=f"= result: {result}")

module.exit_json(**result)


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion plugins/modules/sync_dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def main():
"""
"""
args = dict(
source_directory = dict(
source_directory=dict(
required=True,
type='str'
),
Expand Down
3 changes: 0 additions & 3 deletions roles/dashboards/vars/redhat.yml

This file was deleted.

2 changes: 1 addition & 1 deletion roles/grafana/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---

grafana_version: 9.4.7
grafana_version: 11.3.0

grafana_scm:
use_tags: true
Expand Down
18 changes: 9 additions & 9 deletions roles/grafana/molecule/configured/tests/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def read_ansible_yaml(file_name, role_name):
read_file = None

for e in ["yml", "yaml"]:
test_file = "{}.{}".format(file_name, e)
test_file = f"{file_name}.{e}"
if os.path.isfile(test_file):
read_file = test_file
break
Expand Down Expand Up @@ -76,16 +76,16 @@ def get_vars(host):
# print(" -> {} / {}".format(distribution, os))
# print(" -> {}".format(base_dir))

file_defaults = read_ansible_yaml(f"{base_dir}/defaults/main", "role_defaults")
file_vars = read_ansible_yaml(f"{base_dir}/vars/main", "role_vars")
file_distibution = read_ansible_yaml(f"{base_dir}/vars/{operation_system}", "role_distibution")
file_molecule = read_ansible_yaml(f"{molecule_dir}/group_vars/all/vars", "test_vars")
file_defaults = read_ansible_yaml(f"{base_dir}/defaults/main", "role_defaults")
file_vars = read_ansible_yaml(f"{base_dir}/vars/main", "role_vars")
file_distibution = read_ansible_yaml(f"{base_dir}/vars/{operation_system}", "role_distibution")
file_molecule = read_ansible_yaml(f"{molecule_dir}/group_vars/all/vars", "test_vars")
# file_host_molecule = read_ansible_yaml("{}/host_vars/{}/vars".format(base_dir, HOST), "host_vars")

defaults_vars = host.ansible("include_vars", file_defaults).get("ansible_facts").get("role_defaults")
vars_vars = host.ansible("include_vars", file_vars).get("ansible_facts").get("role_vars")
distibution_vars = host.ansible("include_vars", file_distibution).get("ansible_facts").get("role_distibution")
molecule_vars = host.ansible("include_vars", file_molecule).get("ansible_facts").get("test_vars")
defaults_vars = host.ansible("include_vars", file_defaults).get("ansible_facts").get("role_defaults")
vars_vars = host.ansible("include_vars", file_vars).get("ansible_facts").get("role_vars")
distibution_vars = host.ansible("include_vars", file_distibution).get("ansible_facts").get("role_distibution")
molecule_vars = host.ansible("include_vars", file_molecule).get("ansible_facts").get("test_vars")
# host_vars = host.ansible("include_vars", file_host_molecule).get("ansible_facts").get("host_vars")

ansible_vars = defaults_vars
Expand Down
16 changes: 8 additions & 8 deletions roles/grafana/molecule/default/tests/test_grafana.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ def get_vars(host):
# print(" -> {} / {}".format(distribution, os))
# print(" -> {}".format(base_dir))

file_defaults = read_ansible_yaml(f"{base_dir}/defaults/main", "role_defaults")
file_vars = read_ansible_yaml(f"{base_dir}/vars/main", "role_vars")
file_distibution = read_ansible_yaml(f"{base_dir}/vars/{operation_system}", "role_distibution")
file_molecule = read_ansible_yaml(f"{molecule_dir}/group_vars/all/vars", "test_vars")
file_defaults = read_ansible_yaml(f"{base_dir}/defaults/main", "role_defaults")
file_vars = read_ansible_yaml(f"{base_dir}/vars/main", "role_vars")
file_distibution = read_ansible_yaml(f"{base_dir}/vars/{operation_system}", "role_distibution")
file_molecule = read_ansible_yaml(f"{molecule_dir}/group_vars/all/vars", "test_vars")
# file_host_molecule = read_ansible_yaml("{}/host_vars/{}/vars".format(base_dir, HOST), "host_vars")

defaults_vars = host.ansible("include_vars", file_defaults).get("ansible_facts").get("role_defaults")
vars_vars = host.ansible("include_vars", file_vars).get("ansible_facts").get("role_vars")
distibution_vars = host.ansible("include_vars", file_distibution).get("ansible_facts").get("role_distibution")
molecule_vars = host.ansible("include_vars", file_molecule).get("ansible_facts").get("test_vars")
defaults_vars = host.ansible("include_vars", file_defaults).get("ansible_facts").get("role_defaults")
vars_vars = host.ansible("include_vars", file_vars).get("ansible_facts").get("role_vars")
distibution_vars = host.ansible("include_vars", file_distibution).get("ansible_facts").get("role_distibution")
molecule_vars = host.ansible("include_vars", file_molecule).get("ansible_facts").get("test_vars")
# host_vars = host.ansible("include_vars", file_host_molecule).get("ansible_facts").get("host_vars")

ansible_vars = defaults_vars
Expand Down
16 changes: 8 additions & 8 deletions roles/grafana/molecule/default/tests/test_grafana_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ def get_vars(host):
# print(" -> {} / {}".format(distribution, os))
# print(" -> {}".format(base_dir))

file_defaults = read_ansible_yaml(f"{base_dir}/defaults/main", "role_defaults")
file_vars = read_ansible_yaml(f"{base_dir}/vars/main", "role_vars")
file_distibution = read_ansible_yaml(f"{base_dir}/vars/{operation_system}", "role_distibution")
file_molecule = read_ansible_yaml(f"{molecule_dir}/group_vars/all/vars", "test_vars")
file_defaults = read_ansible_yaml(f"{base_dir}/defaults/main", "role_defaults")
file_vars = read_ansible_yaml(f"{base_dir}/vars/main", "role_vars")
file_distibution = read_ansible_yaml(f"{base_dir}/vars/{operation_system}", "role_distibution")
file_molecule = read_ansible_yaml(f"{molecule_dir}/group_vars/all/vars", "test_vars")
# file_host_molecule = read_ansible_yaml("{}/host_vars/{}/vars".format(base_dir, HOST), "host_vars")

defaults_vars = host.ansible("include_vars", file_defaults).get("ansible_facts").get("role_defaults")
vars_vars = host.ansible("include_vars", file_vars).get("ansible_facts").get("role_vars")
distibution_vars = host.ansible("include_vars", file_distibution).get("ansible_facts").get("role_distibution")
molecule_vars = host.ansible("include_vars", file_molecule).get("ansible_facts").get("test_vars")
defaults_vars = host.ansible("include_vars", file_defaults).get("ansible_facts").get("role_defaults")
vars_vars = host.ansible("include_vars", file_vars).get("ansible_facts").get("role_vars")
distibution_vars = host.ansible("include_vars", file_distibution).get("ansible_facts").get("role_distibution")
molecule_vars = host.ansible("include_vars", file_molecule).get("ansible_facts").get("test_vars")
# host_vars = host.ansible("include_vars", file_host_molecule).get("ansible_facts").get("host_vars")

ansible_vars = defaults_vars
Expand Down
Loading