1
0
Fork 0

fix double ran nixops deployment

This commit is contained in:
Kevin Muller 2025-03-19 13:44:50 +01:00
parent 67ecaf3e27
commit e07576b65e
2 changed files with 46 additions and 37 deletions
panel/src/panel

View file

@ -4,7 +4,6 @@
{% csrf_token %}
{{ form.as_p }}
<button id="deploy-button" class="button"
hx-post="{% url 'configuration_form' %}"
hx-trigger="click"

View file

@ -8,7 +8,9 @@ from django.contrib.auth.models import User
from django.views.generic import TemplateView, DetailView
from django.views.generic.edit import FormView
from panel import models, settings
from time import sleep
from panel import models
from panel.configuration import forms
@ -43,43 +45,44 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
operator=self.request.user,
)
# Check for deploy button
if "deploy" in self.request.POST.keys():
submission = obj.parsed_value.model_dump_json()
# FIXME: let the user specify these from the form (#190)
dummy_user = {
"initialUser": {
"displayName": "Testy McTestface",
"username": "test",
"password": "testtest",
"email": "test@test.com",
},
}
# serialize back and forth now we still need to manually inject the dummy user
deployment = json.dumps(dummy_user | json.loads(submission))
env = {
"PATH": settings.nix_bin_dir,
# pass in form info to our deployment
"DEPLOYMENT": deployment,
}
cmd = [
"nix",
"develop",
# workaround to pass in info to nixops4 thru env vars, tho impure :(
"--extra-experimental-features",
"configurable-impure-env",
"--command",
"nixops4",
"apply",
"test",
]
subprocess.run(
cmd,
cwd=settings.repo_dir,
env=env,
)
return obj
def run_deployment(self, obj):
print("DEPLOYING:")
print(os.getenv("REPO_DIR"))
print(os.getenv("NIX_DIR"))
submission = obj.parsed_value.model_dump_json()
deployment = json.dumps(json.loads(submission) | {
"initialUser": {
"displayName": "Testy McTestface",
"username": "test",
"password": "testtest",
"email": "test@test.com",
},
})
env = {
"DEPLOYMENT": deployment,
"PATH": f"{os.getenv("NIX_DIR")}/bin/",
}
print(f"env: {env}")
print(f"Path: {os.getcwd()}/..")
cmd = [
"nix",
"develop",
"--extra-experimental-features",
"configurable-impure-env",
"--command",
"nixops4",
"--show-trace",
"--verbose",
"apply",
"test",
]
print('start deployment')
sleep(5)
print('done with deployment')
#subprocess.run(cmd, cwd=os.getenv("REPO_DIR") or f"{os.getcwd()}/..", env=env)
# TODO(@fricklerhandwerk):
# this should probably live somewhere else
def convert_enums_to_names(self, data_dict):
@ -125,4 +128,11 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
obj.value = form.to_python().model_dump_json()
obj.save()
print('-------------debug-------------')
print(self.request.POST)
print('-------------debug-------------')
if "deploy" in self.request.POST.keys():
threading.Thread(target=self.run_deployment, args=(obj,)).start()
return super().form_valid(form)