Compare commits

..

7 commits

6 changed files with 51 additions and 97 deletions

View file

@ -7,10 +7,3 @@
```sh
echo "{\"nixos-anywhere\": $(nix-instantiate --eval --json -E '(import ../npins).nixos-anywhere.outPath')}" > .auto.tfvars.json
```
### local development
```sh
$ nix-shell
$ tofu init
```

View file

@ -3,24 +3,3 @@ body
margin: 0
font-family: sans-serif
box-sizing: border-box
.loader
width: 48px
height: 48px
border: 5px solid #000
border-bottom-color: #F34508
border-radius: 50%
box-sizing: border-box
animation: rotation 1s linear infinite
display: inline-block
@keyframes rotation
0% { transform: rotate(0deg) }
100% { transform: rotate(360deg) }
#spinner-container
position: absolute
top: 50%
left: 50%
transform: translate(-50%, -50%)
display: block

View file

@ -1,23 +1,11 @@
{% extends "base.html" %}
{% block content %}
<form method="post" enctype="multipart/form-data" action="{% url 'save' %}">
<form method="post" enctype="multipart/form-data" action="{% url 'configuration_form' %}">
{% csrf_token %}
{{ form.as_p }}
<button id="deploy-button" class="button"
hx-post="{% url 'deployment_status' %}"
hx-trigger="click"
hx-indicator="#spinner-container"
hx-disabled-elt="this"
hx-swap="none"
name="deploy">
Deploy
</button>
<button class="button" type="submit" name="deploy">Deploy</button>
<button class="button" type="submit" name="save">Save</button>
<div id="spinner-container" class="htmx-indicator">
<span class="loader"></span>
</div>
</form>
{% endblock %}

View file

@ -13,7 +13,7 @@ class ConfigurationForm(TestCase):
password=self.password
)
self.config_url = reverse('save')
self.config_url = reverse('configuration_form')
def test_configuration_form_submission(self):
config = Configuration.objects.create(
@ -36,13 +36,12 @@ class ConfigurationForm(TestCase):
enable=True,
mastodon_enable=True,
)
print(form_data)
response = self.client.post(self.config_url, data=form_data)
self.assertEqual(response.status_code, 302)
config.refresh_from_db()
print(config.parsed_value)
self.assertTrue(config.parsed_value.enable)
self.assertTrue(config.parsed_value.mastodon.enable)
# this should not have changed

View file

@ -26,6 +26,4 @@ urlpatterns = [
path("account/", views.AccountDetail.as_view(), name='account_detail'),
path("services/", views.ServiceList.as_view(), name='service_list'),
path("configuration/", views.ConfigurationForm.as_view(), name='configuration_form'),
path("deployment/status/", views.DeploymentStatus.as_view(), name='deployment_status'),
path("save/", views.Save.as_view(), name='save'),
]

View file

@ -2,7 +2,6 @@ from enum import Enum
import json
from os.path import expanduser
import subprocess
import os
from django.urls import reverse_lazy
from django.contrib.auth.mixins import LoginRequiredMixin
@ -11,9 +10,9 @@ from django.views.generic import TemplateView, DetailView
from django.views.generic.edit import FormView
from panel import models, settings
from panel import models
from panel.configuration import forms
class Index(TemplateView):
template_name = 'index.html'
@ -35,12 +34,58 @@ 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 = dummy_user | json.loads(submission)
env = {
"PATH": settings.bin_path,
# used in nixos-anywhere for ssh-copy-id
"HOME": expanduser("~"),
} | {
# pass in form info to our deployment
# FIXME: ensure sensitive info is protected
f"TF_VAR_{k}": v if isinstance(v, str) else json.dumps(v) for k, v in deployment.items()
}
cwd = f"{settings.repo_dir}/launch"
# FIXME: move init to packaging phase
cmd = [
"tofu",
# f"-chdir={cwd}",
"init",
"-get=false",
"-input=false",
# "-plugin-dir=...",
]
subprocess.run(cmd, cwd=cwd, env=env)
cmd = [
"tofu",
# f"-chdir={cwd}",
"apply",
f"-state={cwd}/terraform.tfstate", # FIXME: separate users' state
"--auto-approve",
]
subprocess.run(cmd, cwd=cwd, env=env)
return obj
# TODO(@fricklerhandwerk):
@ -83,57 +128,9 @@ 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()
return super().form_valid(form)
class DeploymentStatus(ConfigurationForm):
def form_valid(self, form):
obj = self.get_object()
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 = dummy_user | json.loads(submission)
env = {
"PATH": settings.bin_path,
# used in nixos-anywhere for ssh-copy-id
"HOME": expanduser("~"),
} | {
# pass in form info to our deployment
# FIXME: ensure sensitive info is protected
f"TF_VAR_{k}": v if isinstance(v, str) else json.dumps(v) for k, v in deployment.items()
}
cwd = f"{settings.repo_dir}/launch"
# FIXME: move init to packaging phase
cmd = [
"tofu",
# f"-chdir={cwd}",
"apply",
f"-state={cwd}/terraform.tfstate", # FIXME: separate users' state
"--auto-approve",
"-lock=false",
]
deployment_result = subprocess.run(cmd, cwd=cwd, env=env)
print(deployment_result)
return deployment_result