Skip to content

Commit

Permalink
feat: Convert config template to pydantic model
Browse files Browse the repository at this point in the history
  • Loading branch information
romeonicholas committed Feb 28, 2024
1 parent 1dd9f84 commit 8f34a29
Show file tree
Hide file tree
Showing 31 changed files with 292 additions and 195 deletions.
1 change: 0 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

.idea
config/*
!config/config_template.yaml
.history
**/__pycache__/
.vscode
Expand Down
5 changes: 0 additions & 5 deletions backend/capellacollab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@

from importlib import metadata

from capellacollab import config as config_module

try:
__version__ = metadata.version("capellacollab-backend")
except metadata.PackageNotFoundError:
__version__ = "0.0.0+unknown"
del metadata


config_module.validate_schema()
6 changes: 3 additions & 3 deletions backend/capellacollab/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@
handlers: list[logging.Handler] = [
logging.StreamHandler(),
core_logging.CustomTimedRotatingFileHandler(
str(config["logging"]["logPath"]) + "backend.log"
str(config.logging.logPath) + "backend.log"
),
]

for handler in handlers:
handler.setFormatter(core_logging.CustomFormatter())

logging.basicConfig(level=config["logging"]["level"], handlers=handlers)
logging.basicConfig(level=config.logging.level, handlers=handlers)


async def startup():
migration.migrate_db(engine, config["database"]["url"])
migration.migrate_db(engine, config.database.url)
logging.info("Migrations done - Server is running")

# This is needed to load the Kubernetes configuration at startup
Expand Down
4 changes: 2 additions & 2 deletions backend/capellacollab/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# access to the values within the .ini file in use.
config = context.config

logging.basicConfig(level=cfg["logging"]["level"])
logging.basicConfig(level=cfg.logging.level)
if os.getenv("ALEMBIC_CONFIGURE_LOGGER", "true") != "false":
logging.getLogger("capellacollab").setLevel("WARNING")

Expand All @@ -25,7 +25,7 @@
# this will overwrite the ini-file sqlalchemy.url path
# with the path given in the config of the main code
if not config.get_main_option("sqlalchemy.url"):
config.set_main_option("sqlalchemy.url", cfg["database"]["url"])
config.set_main_option("sqlalchemy.url", cfg.database.url)

# Import models

Expand Down
18 changes: 3 additions & 15 deletions backend/capellacollab/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0


import logging

import jsonschema
import jsonschema.exceptions

from . import exceptions, loader
from . import loader, models

log = logging.getLogger(__name__)
config = loader.load_yaml()


def validate_schema():
config_schema = loader.load_config_schema()
try:
jsonschema.validate(config, config_schema)
except jsonschema.exceptions.ValidationError as error:
raise exceptions.InvalidConfigurationError(
f"{error.__class__.__name__}: {error.message}",
) from None
config_data = loader.load_yaml()
config = models.AppConfig(**config_data)
Original file line number Diff line number Diff line change
Expand Up @@ -4,99 +4,64 @@
docker:
registry: k3d-myregistry.localhost:12345
externalRegistry: docker.io

k8s:
# Only required when using operator k8s
context: k3d-collab-cluster # Only required, if you'd like to use a local k3d environment
context: k3d-collab-cluster
namespace: collab-sessions

# apiURL: dummy # Only required when no kubectl context is available
# token: dummy # Only required when no kubectl context is available

storageClassName: local-path
storageAccessMode: ReadWriteOnce

cluster:
imagePullPolicy: Always
podSecurityContext:
runAsUser: 1004370000
runAsGroup: 1004370000
fsGroup: 1004370000
runAsNonRoot: true

promtail:
lokiEnabled: True
lokiEnabled: true
lokiUrl: http://localhost:30001/loki/api/v1/push
lokiUsername: localLokiUser
lokiPassword: localLokiPassword
serverPort: 3101

ingressClassName: traefik
general:
host: localhost
port: 8000
scheme: http
wildcardHost: False

wildcardHost: false
extensions:
guacamole:
baseURI: http://localhost:8080/guacamole
publicURI: http://localhost:8080/guacamole

username: guacadmin
password: guacadmin

jupyter:
publicURI: http://localhost:8080/jupyter

authentication:
provider: oauth # oauth | azure
provider: oauth
jwt:
usernameClaim: sub # preferred_username

usernameClaim: sub
oauth:
# Only required when using provider oauth
endpoints:
wellKnown: http://localhost:8083/default/.well-known/openid-configuration
tokenIssuance:
authorization:

tokenIssuance: null
authorization: null
audience: default

scopes:
- openid

- openid
client:
id: default
secret:

secret: null
redirectURI: http://localhost:4200/oauth2/callback

# azure:
# # Only required when using provider azure
# authorizationEndpoint: http://tbd

# client:
# id: tbd
# secret: tbd

# audience: tbd
# redirectURI: http://localhost:4200/oauth2/callback

pipelines:
timeout: 60

database:
url: postgresql://dev:dev@localhost:5432/dev

initial:
admin: admin

logging:
level: DEBUG
logPath: logs/

requests:
timeout: 2

prometheus:
url: http://localhost:8080/prometheus/
15 changes: 6 additions & 9 deletions backend/capellacollab/config/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

# pylint: disable=bad-builtin

import pathlib

import deepdiff
import yaml

from . import loader
from . import models as config_models


class bcolors:
Expand All @@ -22,16 +20,15 @@ class bcolors:

print("Start comparison of configuration files")

config_template = yaml.safe_load(
(
pathlib.Path(__file__).parents[2] / "config" / "config_template.yaml"
).open()
)
pydantic_config = config_models.AppConfig()

config = loader.load_yaml()

diff = deepdiff.DeepDiff(
config, config_template, ignore_order=True, report_repetition=True
config,
pydantic_config.model_dump(),
ignore_order=True,
report_repetition=True,
)

for key, value in (
Expand Down
15 changes: 15 additions & 0 deletions backend/capellacollab/config/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import yaml

from . import models

app_config = models.AppConfig()
config_dict = app_config.model_dump()
yaml_str = yaml.dump(config_dict, sort_keys=False)

with open(
"backend/capellacollab/config/config.yaml", "w", encoding="utf-8"
) as yaml_file:
yaml_file.write(yaml_str)
4 changes: 4 additions & 0 deletions backend/capellacollab/config/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
log = logging.getLogger(__name__)

config_locations: list[pathlib.Path] = [
pathlib.Path(__file__).parents[0] / "config.yaml",
pathlib.Path(__file__).parents[2] / "config" / "config.yaml",
pathlib.Path(appdirs.user_config_dir("capellacollab", "db"))
/ "config.yaml",
Expand Down Expand Up @@ -56,6 +57,9 @@ def load_yaml() -> dict:
)
return yaml.safe_load(loc.open())

log.error(
"No configuration file found. Generate a default configuration with 'python -m capellacollab.config.generate'"
)
raise FileNotFoundError("config.yaml")


Expand Down
Loading

0 comments on commit 8f34a29

Please sign in to comment.