1
0
Fork 0

fix double ran nixops deployment

This commit is contained in:
Kevin Muller 2025-03-19 13:44:50 +01:00 committed by lois
parent 6fb828e872
commit 7b843e6977
2 changed files with 96 additions and 42 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

@ -1,6 +1,7 @@
from enum import Enum
import json
import subprocess
import os
from django.urls import reverse_lazy
from django.contrib.auth.mixins import LoginRequiredMixin
@ -8,9 +9,10 @@ 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 panel.configuration import forms
from time import sleep
from panel import models
from panel.configuration import forms
class Index(TemplateView):
template_name = 'index.html'
@ -33,53 +35,50 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
success_url = reverse_lazy('configuration_form')
form_class = forms.Form
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
def get_object(self):
"""Get or create the configuration object for the current user"""
obj, created = models.Configuration.objects.get_or_create(
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.bin_path,
# 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):
@ -120,9 +119,65 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
initial.update(self.convert_enums_to_names(config_dict))
return initial
class Save(ConfigurationForm):
def form_valid(self, form):
obj = self.get_object()
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)
class DeploymentStatus(ConfigurationForm):
def form_valid(self, form):
obj = self.get_object()
obj.value = form.to_python().model_dump_json()
obj.save()
# Check for deploy button
if "deploy" in self.request.POST.keys():
self.deployment(obj)
return super().form_valid(form)
def deployment(self, obj):
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",
"--extra-experimental-features",
"configurable-impure-env",
"--command",
"nixops4",
"apply",
"test",
]
deployment_result = subprocess.run(
cmd,
cwd=settings.repo_dir,
env=env,
)
print(deployment_result)
return deployment_result