From 1379b4d11aa28db7fd8abba92a112a5c70a3b955 Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Fri, 10 Nov 2023 16:03:37 +0100 Subject: [PATCH] Move instance > purge to a separate page --- internal/dis/component/server/admin/admin.go | 6 ++ .../component/server/admin/html/instance.html | 19 ------ .../server/admin/html/instance_purge.html | 13 ++++ .../dis/component/server/admin/instance.go | 2 +- .../component/server/admin/instance_purge.go | 59 +++++++++++++++++++ 5 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 internal/dis/component/server/admin/html/instance_purge.html create mode 100644 internal/dis/component/server/admin/instance_purge.go diff --git a/internal/dis/component/server/admin/admin.go b/internal/dis/component/server/admin/admin.go index a57e02e..ad00a47 100644 --- a/internal/dis/component/server/admin/admin.go +++ b/internal/dis/component/server/admin/admin.go @@ -73,6 +73,7 @@ var ( menuInstance = component.DummyMenuItem() menuRebuild = component.DummyMenuItem() menuGrants = component.DummyMenuItem() + menuPurge = component.DummyMenuItem() ) func (admin *Admin) HandleRoute(ctx context.Context, route string) (handler http.Handler, err error) { @@ -133,6 +134,11 @@ func (admin *Admin) HandleRoute(ctx context.Context, route string) (handler http router.Handler(http.MethodPost, route+"grants/", iUsers) // NOTE(twiesing): This path is intentionally different! } + { + purge := admin.instancePurge(ctx) + router.Handler(http.MethodGet, route+"instance/:slug/purge", purge) + } + // add a router for the login page router.Handler(http.MethodPost, route+"login", admin.loginHandler(ctx)) diff --git a/internal/dis/component/server/admin/html/instance.html b/internal/dis/component/server/admin/html/instance.html index 5161325..6b06c62 100644 --- a/internal/dis/component/server/admin/html/instance.html +++ b/internal/dis/component/server/admin/html/instance.html @@ -10,7 +10,6 @@ Statistics SSH Keys Snapshots - Dangerous Actions @@ -548,22 +547,4 @@ {{ end}} - - -
-

Dangerous Actions

-
- -
-

- Purging this instance completely removes it from the distillery. - Backups containing the instance will remain, but it will not be possible to restore it directly. - You must enter the slug {{ .Instance.Slug }} to confirm purging. -

-
-
- - -
-
\ No newline at end of file diff --git a/internal/dis/component/server/admin/html/instance_purge.html b/internal/dis/component/server/admin/html/instance_purge.html new file mode 100644 index 0000000..821c14d --- /dev/null +++ b/internal/dis/component/server/admin/html/instance_purge.html @@ -0,0 +1,13 @@ +
+

+ Purging this instance completely removes it from the distillery. + Backups containing the instance will remain, but it will not be possible to restore it directly. + You must enter the slug {{ .Instance.Slug }} to confirm purging. +

+
+
+ + +
+
+
\ No newline at end of file diff --git a/internal/dis/component/server/admin/instance.go b/internal/dis/component/server/admin/instance.go index 13b7ca9..0b961e0 100644 --- a/internal/dis/component/server/admin/instance.go +++ b/internal/dis/component/server/admin/instance.go @@ -79,6 +79,7 @@ func (admin *Admin) instanceTabs(slug string, active string) templating.FlagFunc {Title: "Overview", Path: template.URL("/admin/instance/" + slug), Active: active == "overview"}, {Title: "Rebuild", Path: template.URL("/admin/instance/" + slug + "/rebuild"), Active: active == "rebuild"}, {Title: "Users & Grants", Path: template.URL("/admin/instance/" + slug + "/users"), Active: active == "users"}, + {Title: "Purge", Path: template.URL("/admin/instance/" + slug + "/purge"), Active: active == "purge"}, // TODO: These still need to be migrated to their own tabs // Then we also need to redo the main page @@ -90,7 +91,6 @@ func (admin *Admin) instanceTabs(slug string, active string) templating.FlagFunc {Title: "Stats", Path: template.URL("/instance/" + slug + "/stats"), Active: active == "stats"}, {Title: "SSH", Path: template.URL("/instance/" + slug + "/ssh"), Active: active == "ssh"}, {Title: "Snapshots", Path: template.URL("/instance/" + slug + "/snapshots"), Active: active == "snapshots"}, - {Title: "Danger Zone", Path: template.URL("/instance/" + slug + "/danger"), Active: active == "danger"}, */ } return flags diff --git a/internal/dis/component/server/admin/instance_purge.go b/internal/dis/component/server/admin/instance_purge.go new file mode 100644 index 0000000..93da21b --- /dev/null +++ b/internal/dis/component/server/admin/instance_purge.go @@ -0,0 +1,59 @@ +package admin + +import ( + "context" + _ "embed" + "html/template" + "net/http" + + "github.com/FAU-CDI/wisski-distillery/internal/dis/component" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating" + "github.com/FAU-CDI/wisski-distillery/internal/wisski" + "github.com/tkw1536/pkglib/httpx" + + "github.com/julienschmidt/httprouter" +) + +//go:embed "html/instance_purge.html" +var instancePurgeHTML []byte +var instancePurgeTemplate = templating.Parse[instancePurgeContext]( + "instance_purge.html", instancePurgeHTML, nil, + + templating.Assets(assets.AssetsAdmin), +) + +type instancePurgeContext struct { + templating.RuntimeFlags + + Instance *wisski.WissKI +} + +func (admin *Admin) instancePurge(ctx context.Context) http.Handler { + tpl := instancePurgeTemplate.Prepare( + admin.dependencies.Templating, + templating.Crumbs( + menuAdmin, + menuInstances, + menuInstance, + menuPurge, + ), + ) + + return tpl.HTMLHandlerWithFlags(func(r *http.Request) (ctx instancePurgeContext, funcs []templating.FlagFunc, err error) { + slug := httprouter.ParamsFromContext(r.Context()).ByName("slug") + + // setup the context with just the instance + ctx.Instance, err = admin.dependencies.Instances.WissKI(r.Context(), slug) + if err != nil { + return ctx, nil, httpx.ErrNotFound + } + + return ctx, []templating.FlagFunc{ + templating.ReplaceCrumb(menuInstance, component.MenuItem{Title: "Instance", Path: template.URL("/admin/instance/" + ctx.Instance.Slug)}), + templating.ReplaceCrumb(menuPurge, component.MenuItem{Title: "Purge", Path: template.URL("/admin/instance/" + ctx.Instance.Slug + "/purge")}), + templating.Title(ctx.Instance.Slug + " - Purge"), + admin.instanceTabs(slug, "purge"), + }, nil + }) +}