Compare commits

..

10 commits

Author SHA1 Message Date
85edbf616b
skip tf lock in views.py over read-only nix env 2025-03-26 10:22:54 +01:00
974f6a902a
move tf init out of python over read-only nix env 2025-03-26 10:22:11 +01:00
8473330317
properly pass repo dir for prod, be it with hard-coded TF init 2025-03-26 10:21:07 +01:00
1d0c131dc5
use flake-sourced nixos-anywhere in tf, to reproduce modules for nix 2025-03-26 10:21:07 +01:00
0bbc676838
switch launch shell to root flake's nixpkgs, see #279 2025-03-26 10:21:07 +01:00
601e4ee62d
Revert "deduplicate flake inputs"
This reverts commit 95769084ce.
2025-03-26 10:21:07 +01:00
5e0e0e52fe
make re-exports explicit again 2025-03-26 10:21:07 +01:00
02380f9e21
deduplicate flake inputs 2025-03-26 10:21:07 +01:00
3e016ff059
tf 2025-03-26 10:21:07 +01:00
b4fbc457a6 Progress Indicator (#259)
closes #74
Show progress indicator to track deployment

- Disable deploy button when deployment is in progress.

Co-authored-by: kevin <kevin@procolix.com>
Reviewed-on: Fediversity/Fediversity#259
Reviewed-by: kiara Grouwstra <kiara@procolix.eu>
2025-03-26 10:14:06 +01:00
6 changed files with 97 additions and 51 deletions

View file

@ -7,3 +7,10 @@
```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,3 +3,24 @@ 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,11 +1,23 @@
{% extends "base.html" %}
{% block content %}
<form method="post" enctype="multipart/form-data" action="{% url 'configuration_form' %}">
<form method="post" enctype="multipart/form-data" action="{% url 'save' %}">
{% 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('configuration_form')
self.config_url = reverse('save')
def test_configuration_form_submission(self):
config = Configuration.objects.create(
@ -36,12 +36,13 @@ 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,4 +26,6 @@ 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,6 +2,7 @@ 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
@ -10,9 +11,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'
@ -34,58 +35,12 @@ 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):
@ -128,9 +83,57 @@ 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