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

[health-check] Refactor the config usage #9662

Open
wants to merge 4 commits into
base: health-check-skeleton
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions health-check/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies = [
"requests",
"Jinja2",
"PyYAML",
"tomli",
]
maintainers = [
{name = "Pablo Suárez Hernández", email = "[email protected]"},
Expand Down
11 changes: 0 additions & 11 deletions health-check/src/uyuni_health_check/config.ini

This file was deleted.

71 changes: 71 additions & 0 deletions health-check/src/uyuni_health_check/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import functools
import os
from typing import Any, Dict
import tomli
from pathlib import Path
import json
import jinja2

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_DIR = os.path.join(BASE_DIR, "config")
TEMPLATES_DIR = os.path.join(CONFIG_DIR, "templates")
CONTAINERS_DIR = os.path.join(BASE_DIR, "containers")
CONFIG_TOML_PATH = os.environ.get("HEALTH_CHECK_TOML", os.path.join(BASE_DIR, "config.toml"))

@functools.lru_cache
def _init_jinja_env() -> jinja2.Environment:
return jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATES_DIR))

@functools.lru_cache
def parse_config() -> Dict:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if I get it right, the idea is not to load the config once and then pass around the config object (which is in fact, nicer) but to allow the parsing function to be called every time needed and speed it up by caching the returned values, correct?

if not os.path.exists(CONFIG_TOML_PATH):
raise ValueError(f"Config file does not exist: {CONFIG_TOML_PATH}")

with open(CONFIG_TOML_PATH, "rb") as f:
conf = tomli.load(f)
return conf

def get_json_template_filepath(json_relative_path: str) -> str:
return os.path.join(TEMPLATES_DIR, json_relative_path)

def load_jinja_template(template: str) -> jinja2.Template:
return _init_jinja_env().get_template(template)

def load_dockerfile_dir(dockerfile_dir: str) -> str:
agraul marked this conversation as resolved.
Show resolved Hide resolved
return os.path.join(CONTAINERS_DIR, dockerfile_dir)

def get_config_dir_path(component: str) -> str:
return os.path.join(CONFIG_DIR, component)

def load_prop(property: str) -> Any:
res = parse_config().copy()
for prop_part in property.split('.'):
try:
res = res.get(prop_part, {})
except (AttributeError, ValueError):
res = None
Comment on lines +45 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to turn access errors into None? This might silence errors caused by bad callers (or bad configs)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you'd prefer we let the caller deal with the error in case it asks for non-existent value? In my mind, when you ask a config for a non-existent prop, the correct behavior is to return a non-existent value (i.e. None).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The caller always needs to deal with the case that it asks for something from the config that can't be found (load_prop(x) returns None).

The question is: do we force the caller to deal with it by raising an exception or are we okay with the caller ignoring that? Both ways are okay, but since we don't have any config validation, it might make sense to not silence lookup errors (e.g. because the config is incomplete and mandatory values are missing) at runtime

break
return res

def write_config(component: str, config_file_path: str, content: str, is_json=False):
basedir = Path(get_config_dir_path(component))
if not basedir.exists():
basedir.mkdir(parents=True)
file_path = os.path.join(basedir, config_file_path)
with open(file_path, "w") as file:
if is_json:
json.dump(content, file, indent=4)
else:
file.write(content)

def get_config_file_path(component):
return os.path.join(get_config_dir_path(component), "config.yaml")

def get_sources_dir(component):
return os.path.join(BASE_DIR, component)

def get_grafana_config_dir():
return os.path.join(CONFIG_DIR, "grafana")

def get_prometheus_config_dir():
return os.path.join(CONFIG_DIR, "prometheus")
11 changes: 11 additions & 0 deletions health-check/src/uyuni_health_check/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[podman]
network_name = "health-check-network"

[loki]
loki_container_name = "uyuni_health_check_loki"
loki_port = 3100
jobs = ["cobbler", "postgresql", "rhn", "apache"]

[logcli]
logcli_container_name = "uyuni_health_check_logcli"
logcli_image_name = "logcli"
66 changes: 0 additions & 66 deletions health-check/src/uyuni_health_check/config_loader.py

This file was deleted.

15 changes: 5 additions & 10 deletions health-check/src/uyuni_health_check/exporters/exporter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
from jinja2 import Environment, FileSystemLoader
from uyuni_health_check.utils import HealthException, console
from uyuni_health_check import config
from uyuni_health_check.utils import console
from uyuni_health_check.containers.manager import (
image_exists,
build_image,
Expand All @@ -9,7 +8,7 @@
)


def prepare_exporter(config=None, verbose=False, supportconfig_path=None):
def prepare_exporter(verbose=False, supportconfig_path=None):
"""
Build the prometheus exporter image and deploy it on the server

Expand All @@ -18,19 +17,16 @@ def prepare_exporter(config=None, verbose=False, supportconfig_path=None):
exporter_name = "supportconfig-exporter"
exporter_dir = config.load_dockerfile_dir("exporter")
create_supportconfig_exporter_cfg(
config=config, supportconfig_path=supportconfig_path
supportconfig_path=supportconfig_path
)
exporter_config = config.get_config_file_path("exporter")
# config = get_supportconfig_exporter_cfg(supportconfig_path=supportconfig_path)

exporter_sources = config.get_sources_dir("exporters")

console.log(f"[bold]Building {exporter_name} image")
if image_exists(f"{exporter_name}"):
console.log(f"[yellow]Skipped as the {exporter_name} image is already present")
else:
build_image(f"{exporter_name}", exporter_dir, verbose=verbose)
# build_image(f"{exporter_name}", os.path.dirname(__file__), build_args=[f"config={config}"],verbose=verbose)
console.log(f"[green]The {exporter_name} image was built successfully")

# Run the container
Expand Down Expand Up @@ -72,8 +68,7 @@ def prepare_exporter(config=None, verbose=False, supportconfig_path=None):
)


def create_supportconfig_exporter_cfg(config=None, supportconfig_path=None):
def create_supportconfig_exporter_cfg(supportconfig_path=None):
exporter_template = config.load_jinja_template("exporter/exporter.yaml.j2")
opts = {"supportconfig_path": supportconfig_path}
#exporter_config_file_path = config.get_config_file_path("exporter")
config.write_config("exporter", "config.yaml", exporter_template.render(**opts))
21 changes: 9 additions & 12 deletions health-check/src/uyuni_health_check/grafana/grafana_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
from uyuni_health_check.config_loader import ConfigLoader
from uyuni_health_check.utils import run_command, HealthException, console
from uyuni_health_check import config
from uyuni_health_check.utils import console
from uyuni_health_check.containers.manager import (
console,
build_image,
Expand All @@ -9,20 +9,17 @@
podman,
)

conf = ConfigLoader()


def prepare_grafana(from_datetime=None, to_datetime=None, verbose=False, config=None):
def prepare_grafana(from_datetime=None, to_datetime=None, verbose=False):
if container_is_running("uyuni-health-check-grafana"):
console.log(
"Skipped as the uyuni-health-check-grafana container is already running"
)
else:
build_grafana_image("health-check-grafana", config)
grafana_cfg = conf.get_config_dir_path("grafana")
build_grafana_image("health-check-grafana")
grafana_cfg = config.get_config_dir_path("grafana")
console.log("GRAFANA CFG DIR: ",grafana_cfg)
grafana_dasthboard_template = config.get_json_template_filepath("grafana_dashboard/supportconfig_with_logs.template.json")
render_grafana_dashboard_cfg(grafana_dasthboard_template, from_datetime, to_datetime, config)
render_grafana_dashboard_cfg(grafana_dasthboard_template, from_datetime, to_datetime)

# Run the container
podman(
Expand All @@ -48,7 +45,7 @@ def prepare_grafana(from_datetime=None, to_datetime=None, verbose=False, config=
],
)

def build_grafana_image(image, config):
def build_grafana_image(image: str):
console.log(f"Building {image}")
if image_exists(image):
console.log(f"[yellow]Skipped as the {image} image is already present")
Expand All @@ -58,7 +55,7 @@ def build_grafana_image(image, config):
build_image(image, image_path=image_path)
console.log(f"[green]The {image} image was built successfully")

def render_grafana_dashboard_cfg(grafana_dashboard_template, from_datetime, to_datetime, config=None):
def render_grafana_dashboard_cfg(grafana_dashboard_template, from_datetime, to_datetime):
"""
Render grafana dashboard file
"""
Expand All @@ -67,4 +64,4 @@ def render_grafana_dashboard_cfg(grafana_dashboard_template, from_datetime, to_d
data = json.load(f)
data["time"]["from"] = from_datetime
data["time"]["to"] = to_datetime
config.write_config("grafana", "dashboards/supportconfig_with_logs.json", data, isjson=True)
config.write_config("grafana", "dashboards/supportconfig_with_logs.json", data, is_json=True)
14 changes: 6 additions & 8 deletions health-check/src/uyuni_health_check/loki/logs_gatherer.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from uyuni_health_check.containers.manager import podman
from uyuni_health_check.utils import run_command, HealthException, console
from uyuni_health_check.outputter import outputter
from uyuni_health_check.config_loader import ConfigLoader
from uyuni_health_check import config
from rich.markdown import Markdown
from datetime import datetime, timedelta
from rich import print
import json
from json.decoder import JSONDecodeError

conf = ConfigLoader()


def show_full_error_logs(from_datetime, to_datetime, since, console: "Console", loki=None):
"""
Expand Down Expand Up @@ -58,14 +56,14 @@ def show_error_logs_stats(from_datetime, to_datetime, since, console: "Console",

def query_loki(from_dt, to_dt, since, query):

loki_container_name = conf.global_config['loki']['loki_container_name']
loki_port = conf.global_config['loki']['loki_port']
loki_container_name = config.load_prop('loki.loki_container_name')
loki_port = config.load_prop('loki.loki_port')
loki_url = f"http://{loki_container_name}:{loki_port}"

network_name = conf.global_config['podman']['network_name']
logcli_container_name = conf.global_config['logcli']['logcli_container_name']
network_name = config.load_prop('podman.network_name')
logcli_container_name = config.load_prop('logcli.logcli_container_name')

logcli_image_name = conf.global_config['logcli']['logcli_image_name']
logcli_image_name = config.load_prop('logcli.logcli_image_name')

podman_args = [
"run",
Expand Down
Loading
Loading