diff --git a/apps/dokploy/pages/dashboard/deploy-stack.tsx b/apps/dokploy/pages/dashboard/deploy-stack.tsx new file mode 100644 index 000000000..923f17b32 --- /dev/null +++ b/apps/dokploy/pages/dashboard/deploy-stack.tsx @@ -0,0 +1,381 @@ +import { validateRequest } from "@nomploy/server/lib/auth"; +import { createServerSideHelpers } from "@trpc/react-query/server"; +import { CheckCircle2, Layers, Loader2, XCircle } from "lucide-react"; +import type { GetServerSidePropsContext } from "next"; +import { useRouter } from "next/router"; +import { type ReactElement, useEffect, useMemo, useState } from "react"; +import superjson from "superjson"; +import { toast } from "sonner"; +import { DashboardLayout } from "@/components/layouts/dashboard-layout"; +import { Button } from "@/components/ui/button"; +import { + Card, + CardContent, + CardDescription, + CardHeader, + CardTitle, +} from "@/components/ui/card"; +import { Label } from "@/components/ui/label"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { slugify } from "@/lib/slug"; +import { appRouter } from "@/server/api/root"; +import { api } from "@/utils/api"; + +const DEFAULT_REGISTRY = "github.com/Nomploy/nomad-packs"; +// The community default registry is addressed by a blank nomadPackRegistry. +const COMMUNITY_REGISTRY = + "github.com/hashicorp/nomad-pack-community-registry"; + +// A strong random secret. When the pack's default looks like a fixed-length hex +// token (e.g. a 64-char encryption key), match that so format constraints hold; +// otherwise a 32-char alphanumeric (safe in HCL and in URLs/DSNs). +function genSecret(def: string): string { + const rand = (n: number) => { + const a = new Uint8Array(n); + crypto.getRandomValues(a); + return a; + }; + if (/^[0-9a-f]+$/i.test(def) && (def.length === 32 || def.length === 64)) { + return Array.from(rand(def.length / 2)) + .map((b) => b.toString(16).padStart(2, "0")) + .join(""); + } + const ALPH = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + return Array.from(rand(32)) + .map((b) => ALPH[b % ALPH.length]) + .join(""); +} + +const hclEscape = (s: string) => + s.replace(/\\/g, "\\\\").replace(/"/g, '\\"'); + +type Phase = "pending" | "creating" | "deploying" | "done" | "error"; + +/** + * Landing page for a "Deploy stack to Nomploy" deep-link from a pack registry. + * The link carries ?packs=a,b,c®istry=(&stack=); the logged-in user + * picks a project + environment, then every pack is created as a Nomad-Pack + * service (with auto-generated secrets) and deployed. On completion we jump to + * the environment where all the new services appear. + */ +const DeployStack = () => { + const router = useRouter(); + const packs = useMemo(() => { + const raw = router.query.packs; + const s = Array.isArray(raw) ? raw.join(",") : (raw ?? ""); + return Array.from( + new Set( + s + .split(",") + .map((x) => x.trim().toLowerCase()) + .filter((x) => /^[a-z0-9][a-z0-9-]*$/.test(x)), + ), + ); + }, [router.query.packs]); + const registry = + typeof router.query.registry === "string" && router.query.registry + ? router.query.registry + : DEFAULT_REGISTRY; + const stackName = + typeof router.query.stack === "string" ? router.query.stack : ""; + + const { data: projects, isLoading } = api.project.all.useQuery(); + const [projectId, setProjectId] = useState(""); + const [environmentId, setEnvironmentId] = useState(""); + const [deployNow, setDeployNow] = useState(true); + const [running, setRunning] = useState(false); + const [finished, setFinished] = useState(false); + const [phases, setPhases] = useState>({}); + + const utils = api.useUtils(); + const createMut = api.compose.create.useMutation(); + const deployMut = api.compose.deploy.useMutation(); + + const projectList = useMemo( + () => + (projects ?? []).map((p) => ({ + projectId: p.projectId, + name: p.name, + environments: (p.environments ?? []).map((e) => ({ + environmentId: e.environmentId, + name: e.name, + })), + })), + [projects], + ); + const selectedProject = projectList.find((p) => p.projectId === projectId); + const environments = selectedProject?.environments ?? []; + + useEffect(() => { + const only = projectList.length === 1 ? projectList[0] : undefined; + if (!projectId && only) setProjectId(only.projectId); + }, [projectList, projectId]); + useEffect(() => { + const only = environments.length === 1 ? environments[0] : undefined; + setEnvironmentId(only ? only.environmentId : ""); + }, [projectId]); + + const runDeploy = async () => { + if (!environmentId || running) return; + setRunning(true); + setFinished(false); + const base = slugify(selectedProject?.name); + let ok = 0; + for (const id of packs) { + setPhases((p) => ({ ...p, [id]: "creating" })); + try { + // Auto-generate a value for each sensitive variable so the stack can + // come up without manual secret entry. Non-secret vars keep their + // pack defaults; review a service afterwards to customize it. + let composeFile: string | undefined; + try { + const meta = await utils.nomad.getNomadPack.fetch({ + registryUrl: registry, + id, + }); + const lines = (meta?.variables ?? []) + .filter( + (v) => v.sensitive && (v.kind ?? "string") === "string", + ) + .map( + (v) => + `${v.name} = "${hclEscape( + genSecret(String(v.default ?? "")), + )}"`, + ); + if (lines.length) composeFile = `${lines.join("\n")}\n`; + } catch { + // Registry fetch failed — fall back to the pack's own defaults. + } + + const created = await createMut.mutateAsync({ + name: id, + environmentId, + composeType: "nomad-pack", + appName: `${base}-${slugify(id)}`, + nomadPack: id, + nomadPackRegistry: registry === COMMUNITY_REGISTRY ? "" : registry, + ...(composeFile ? { composeFile } : {}), + }); + + if (deployNow) { + setPhases((p) => ({ ...p, [id]: "deploying" })); + await deployMut.mutateAsync({ composeId: created.composeId }); + } + setPhases((p) => ({ ...p, [id]: "done" })); + ok++; + } catch (e) { + setPhases((p) => ({ ...p, [id]: "error" })); + toast.error( + `${id}: ${e instanceof Error ? e.message : "failed"}`, + ); + } + } + + await utils.environment.one.invalidate({ environmentId }); + await utils.project.all.invalidate(); + setRunning(false); + setFinished(true); + if (ok > 0) { + toast.success( + `${ok}/${packs.length} ${deployNow ? "deploying" : "created"} — opening the environment`, + ); + router.push( + `/dashboard/project/${projectId}/environment/${environmentId}`, + ); + } + }; + + if (packs.length === 0) { + return ( +
+ + + No packs specified + + This page expects a ?packs=<a,b,c> query + parameter, usually from a "Deploy stack to Nomploy" link on a pack + registry. + + + + + + +
+ ); + } + + const phaseIcon = (id: string) => { + const ph = phases[id]; + if (ph === "done") + return ; + if (ph === "error") return ; + if (ph === "creating" || ph === "deploying") + return ; + return ; + }; + + return ( +
+ + +
+
+ +
+
+ + Deploy {stackName ? `the ${stackName} stack` : "stack"} + + + {packs.length} packs will be created as services + {deployNow ? " and deployed" : ""}. Choose where. + +
+
+
+ +
+ {packs.map((id) => ( + + {phaseIcon(id)} + {id} + + ))} +
+ + {isLoading ? ( +
+ Loading projects… +
+ ) : projectList.length === 0 ? ( +

+ You have no projects yet. Create one first, then come back to this + link. +

+ ) : ( + <> +
+ + +
+ +
+ + +
+ + + +

+ Secrets are auto-generated for each pack. Review a service + afterwards to customize non-secret settings (ports, domains, + resources). +

+ + + + )} +
+
+
+ ); +}; + +export default DeployStack; + +DeployStack.getLayout = (page: ReactElement) => { + return {page}; +}; + +export async function getServerSideProps(ctx: GetServerSidePropsContext) { + const { req, res } = ctx; + const { user, session } = await validateRequest(req); + + const helpers = createServerSideHelpers({ + router: appRouter, + ctx: { + req: req as any, + res: res as any, + db: null as any, + session: session as any, + user: user as any, + }, + transformer: superjson, + }); + + if (!user) { + return { redirect: { permanent: false, destination: "/" } }; + } + await helpers.settings.isCloud.prefetch(); + await helpers.user.get.prefetch(); + await helpers.project.all.prefetch(); + + return { props: { trpcState: helpers.dehydrate() } }; +}