factor reading env vars out to settings.py

This commit is contained in:
Kiara Grouwstra 2025-03-19 08:57:56 +01:00
parent 53d3791eaa
commit c5fe0157b0
Signed by: kiara
SSH key fingerprint: SHA256:COspvLoLJ5WC5rFb9ZDe5urVCkK4LJZOsjfF4duRJFU
2 changed files with 16 additions and 9 deletions

View file

@ -184,3 +184,11 @@ if user_settings_file is not None:
spec.loader.exec_module(module) spec.loader.exec_module(module)
sys.modules["user_settings"] = module sys.modules["user_settings"] = module
from user_settings import * # noqa: F403 # pyright: ignore [reportMissingImports] from user_settings import * # noqa: F403 # pyright: ignore [reportMissingImports]
# non-Django application settings
# a dir of nix supporting experimental feature `configurable-impure-env`.
nix_bin_dir=f"{env.get("NIX_DIR")}/bin/"
# path of the root flake to trigger nixops from, see #94.
# to deploy this should be specified, for dev just use a relative path.
repo_dir = env.get("REPO_DIR") or f"{os.getcwd()}/.."

View file

@ -1,5 +1,4 @@
from enum import Enum from enum import Enum
import os
import json import json
import subprocess import subprocess
@ -9,7 +8,7 @@ from django.contrib.auth.models import User
from django.views.generic import TemplateView, DetailView from django.views.generic import TemplateView, DetailView
from django.views.generic.edit import FormView from django.views.generic.edit import FormView
from panel import models from panel import models, settings
from panel.configuration import forms from panel.configuration import forms
@ -47,9 +46,6 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
# Check for deploy button # Check for deploy button
if "deploy" in self.request.POST.keys(): if "deploy" in self.request.POST.keys():
submission = obj.parsed_value.model_dump_json() submission = obj.parsed_value.model_dump_json()
# in dev we can use a relative path, for deployed versions we must explicitly
# specify our root flake as the directory to trigger nixops from, see #94.
cwd = os.getenv("REPO_DIR") or f"{os.getcwd()}/.."
# FIXME: let the user specify these from the form (#190) # FIXME: let the user specify these from the form (#190)
dummy_user = { dummy_user = {
"initialUser": { "initialUser": {
@ -62,9 +58,8 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
# serialize back and forth now we still need to manually inject the dummy user # serialize back and forth now we still need to manually inject the dummy user
deployment = json.dumps(dummy_user | json.loads(submission)) deployment = json.dumps(dummy_user | json.loads(submission))
env = { env = {
# use nix as implicit lix from a dev shell lacks configurable-impure-env "PATH": settings.nix_bin_dir,
"PATH": f"{os.getenv("NIX_DIR")}/bin/", # pass in form info to our deployment
# environment variable we use to pass in form info to our deployment
"DEPLOYMENT": deployment, "DEPLOYMENT": deployment,
} }
cmd = [ cmd = [
@ -78,7 +73,11 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
"apply", "apply",
"test", "test",
] ]
subprocess.run(cmd, cwd=cwd, env=env) subprocess.run(
cmd,
cwd=settings.repo_dir,
env=env,
)
return obj return obj
# TODO(@fricklerhandwerk): # TODO(@fricklerhandwerk):