Skip to content

Commit

Permalink
noc.config section
Browse files Browse the repository at this point in the history
  • Loading branch information
dvolodin7 committed Oct 29, 2024
1 parent 28e9c88 commit 71ffba3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ To see unreleased changes, please see the [CHANGELOG on the master branch](https
### Added

* `restart` command.
* `noc.config` section.

### Changed

Expand Down
2 changes: 2 additions & 0 deletions src/gufo/thor/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class NocConfig(object):
in the interface.
theme: Web interface theme. One of: `noc`, `gray`.
migrate: Run migrations on start
config: User-defined config.
"""

tag: str = "master"
Expand All @@ -37,6 +38,7 @@ class NocConfig(object):
installation_name: str = "Unconfigured Installation"
theme: Literal["noc", "gray"] = "noc"
migrate: bool = True
config: Optional[Dict[str, Any]] = None

@staticmethod
def from_dict(data: Dict[str, Any]) -> "NocConfig":
Expand Down
2 changes: 1 addition & 1 deletion src/gufo/thor/services/kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class KafkaService(BaseService):
name = "kafka"
compose_image = "bitnami/kafka:3.6.2"
compose_volumes = [
"kafka_data:/data/",
"kafka_data:/bitnami/data/",
]
compose_volumes_config = {"kafka_data": {}}
service_discovery = {"kafka": 9093}
Expand Down
33 changes: 26 additions & 7 deletions src/gufo/thor/services/noc.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# ---------------------------------------------------------------------
# Gufo Thor: noc service
# ---------------------------------------------------------------------
# Copyright (C) 2023, Gufo Labs
# Copyright (C) 2023-24, Gufo Labs
# ---------------------------------------------------------------------
"""NocService base class."""

# Python Modules
from pathlib import Path
from typing import Any, Dict, List, Optional

# Third-party modules
import yaml

# Gufo Thor modules
from ..config import Config, ServiceConfig
from ..utils import merge_dict, write_file
from .base import BaseService, ComposeDependsCondition


Expand Down Expand Up @@ -90,12 +94,27 @@ def prepare_compose_config(
"""
if self._config_prepared:
return # Already configured from other subclass
NocService.render_file(
Path("etc", "noc", "settings.yml"),
"settings.yml",
installation_name=config.noc.installation_name,
theme=config.noc.theme,
)
# Build default config
cfg = {
"installation_name": config.noc.installation_name,
"pg": {
"db": "noc",
"user": "noc",
"password": "noc",
},
"web": {
"theme": config.noc.theme,
},
"msgstream": {
"client_class": "noc.core.msgstream.redpanda.RedPandaClient",
},
"redpanda": {"addresses": "kafka:9092"},
}
# Apply user config
if config.noc.config:
cfg = merge_dict(cfg, config.noc.config)
# Write
write_file(Path("etc", "noc", "settings.yml"), yaml.dump(cfg))
self._config_prepared = True

def get_compose_volumes_config(
Expand Down
13 changes: 0 additions & 13 deletions src/gufo/thor/templates/noc/settings.yml

This file was deleted.

36 changes: 34 additions & 2 deletions src/gufo/thor/utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# ---------------------------------------------------------------------
# Gufo Thor: Various utilities
# ---------------------------------------------------------------------
# Copyright (C) 2023, Gufo Labs
# Copyright (C) 2023--24, Gufo Labs
# ---------------------------------------------------------------------
"""Various utilities."""

# Python modules
import os
import shutil
from pathlib import Path
from typing import Optional, Union
from typing import Any, Dict, Optional, Union

# Gufo Thor modules
from .log import logger
Expand Down Expand Up @@ -59,3 +59,35 @@ def ensure_directory(path: Path) -> None:
return
logger.warning("Creating directory %s", path)
os.makedirs(path)


def merge_dict(x: Dict[str, Any], y: Dict[str, Any]) -> Dict[str, Any]:
"""
Deep merge dictionaries.
Do not affect source dictionaries.
Second dictionary overrides first.
Args:
x: First dictionary.
y: Second dictionary.
Returns:
Merged dictionary.
"""
r: Dict[str, Any] = {}
xk = set(x)
yk = set(y)
# Append keys which are only in first dictionary
for k in xk - yk:
r[k] = x[k]
# Append keys which are only in second dictionary
for k in yk - xk:
r[k] = y[k]
# Merge overlapped keys
for k in xk.intersection(yk):
if isinstance(x[k], dict) and isinstance(y[k], dict):
r[k] = merge_dict(x[k], y[k])
else:
r[k] = y[k]
return r

0 comments on commit 71ffba3

Please sign in to comment.