mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-07-03 13:05:23 +02:00
feat(environment): implement environment management with create, duplicate, and delete functionalities; add environment schema and database migrations
This commit is contained in:
@@ -0,0 +1,387 @@
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Copy, Plus, Settings, Trash2 } from "lucide-react";
|
||||
import { useState } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { toast } from "sonner";
|
||||
import { z } from "zod";
|
||||
import { DateTooltip } from "@/components/shared/date-tooltip";
|
||||
import { DialogAction } from "@/components/shared/dialog-action";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog";
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { api } from "@/utils/api";
|
||||
|
||||
const createEnvironmentSchema = z.object({
|
||||
name: z.string().min(1, "Environment name is required"),
|
||||
description: z.string().optional(),
|
||||
});
|
||||
|
||||
const duplicateEnvironmentSchema = z.object({
|
||||
name: z.string().min(1, "Environment name is required"),
|
||||
description: z.string().optional(),
|
||||
});
|
||||
|
||||
type CreateEnvironment = z.infer<typeof createEnvironmentSchema>;
|
||||
type DuplicateEnvironment = z.infer<typeof duplicateEnvironmentSchema>;
|
||||
|
||||
interface Props {
|
||||
projectId: string;
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
|
||||
export const EnvironmentManagement = ({ projectId, children }: Props) => {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false);
|
||||
const [isDuplicateDialogOpen, setIsDuplicateDialogOpen] = useState(false);
|
||||
const [selectedEnvironmentId, setSelectedEnvironmentId] =
|
||||
useState<string>("");
|
||||
|
||||
const utils = api.useUtils();
|
||||
|
||||
// Queries
|
||||
const { data: environments, isLoading: environmentsLoading } =
|
||||
api.environment.byProjectId.useQuery(
|
||||
{ projectId },
|
||||
{ enabled: !!projectId },
|
||||
);
|
||||
|
||||
// Mutations
|
||||
const createEnvironmentMutation = api.environment.create.useMutation();
|
||||
const duplicateEnvironmentMutation = api.environment.duplicate.useMutation();
|
||||
const deleteEnvironmentMutation = api.environment.remove.useMutation();
|
||||
|
||||
// Forms
|
||||
const createForm = useForm<CreateEnvironment>({
|
||||
defaultValues: {
|
||||
name: "",
|
||||
description: "",
|
||||
},
|
||||
resolver: zodResolver(createEnvironmentSchema),
|
||||
});
|
||||
|
||||
const duplicateForm = useForm<DuplicateEnvironment>({
|
||||
defaultValues: {
|
||||
name: "",
|
||||
description: "",
|
||||
},
|
||||
resolver: zodResolver(duplicateEnvironmentSchema),
|
||||
});
|
||||
|
||||
const onCreateSubmit = async (formData: CreateEnvironment) => {
|
||||
try {
|
||||
await createEnvironmentMutation.mutateAsync({
|
||||
...formData,
|
||||
projectId,
|
||||
});
|
||||
toast.success("Environment created successfully");
|
||||
utils.environment.byProjectId.invalidate({ projectId });
|
||||
setIsCreateDialogOpen(false);
|
||||
createForm.reset();
|
||||
} catch (error) {
|
||||
toast.error("Error creating environment");
|
||||
}
|
||||
};
|
||||
|
||||
const onDuplicateSubmit = async (formData: DuplicateEnvironment) => {
|
||||
if (!selectedEnvironmentId) return;
|
||||
|
||||
try {
|
||||
await duplicateEnvironmentMutation.mutateAsync({
|
||||
environmentId: selectedEnvironmentId,
|
||||
name: formData.name,
|
||||
description: formData.description,
|
||||
});
|
||||
toast.success("Environment duplicated successfully");
|
||||
utils.environment.byProjectId.invalidate({ projectId });
|
||||
setIsDuplicateDialogOpen(false);
|
||||
duplicateForm.reset();
|
||||
setSelectedEnvironmentId("");
|
||||
} catch (error) {
|
||||
toast.error("Error duplicating environment");
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteEnvironment = async (environmentId: string) => {
|
||||
try {
|
||||
await deleteEnvironmentMutation.mutateAsync({ environmentId });
|
||||
toast.success("Environment deleted successfully");
|
||||
utils.environment.byProjectId.invalidate({ projectId });
|
||||
} catch (error) {
|
||||
toast.error("Error deleting environment");
|
||||
}
|
||||
};
|
||||
|
||||
const handleDuplicateClick = (
|
||||
environmentId: string,
|
||||
environmentName: string,
|
||||
) => {
|
||||
setSelectedEnvironmentId(environmentId);
|
||||
duplicateForm.setValue("name", `${environmentName} (copy)`);
|
||||
setIsDuplicateDialogOpen(true);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Dialog open={isOpen} onOpenChange={setIsOpen}>
|
||||
<DialogTrigger asChild>
|
||||
{children ?? (
|
||||
<Button variant="outline">
|
||||
<Settings className="h-4 w-4 mr-2" />
|
||||
Environments
|
||||
</Button>
|
||||
)}
|
||||
</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-4xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Environment Management</DialogTitle>
|
||||
<DialogDescription>
|
||||
Manage project environments. Each environment can have its own
|
||||
configuration and settings.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4">
|
||||
<div className="flex justify-between items-center">
|
||||
<h3 className="text-lg font-medium">Project Environments</h3>
|
||||
<Button
|
||||
onClick={() => setIsCreateDialogOpen(true)}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
<Plus className="h-4 w-4" />
|
||||
Create Environment
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{environmentsLoading ? (
|
||||
<div className="flex justify-center py-8">
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Loading environments...
|
||||
</div>
|
||||
</div>
|
||||
) : environments && environments.length > 0 ? (
|
||||
<div className="space-y-3">
|
||||
{environments.map((env) => (
|
||||
<div
|
||||
key={env.environmentId}
|
||||
className="flex items-center justify-between p-4 border rounded-lg"
|
||||
>
|
||||
<div className="space-y-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<h4 className="font-medium">{env.name}</h4>
|
||||
{env.name === "production" && (
|
||||
<Badge variant="default">Default</Badge>
|
||||
)}
|
||||
</div>
|
||||
{env.description && (
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{env.description}
|
||||
</p>
|
||||
)}
|
||||
<DateTooltip date={env.createdAt}>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
Created
|
||||
</span>
|
||||
</DateTooltip>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() =>
|
||||
handleDuplicateClick(env.environmentId, env.name)
|
||||
}
|
||||
>
|
||||
<Copy className="h-4 w-4" />
|
||||
</Button>
|
||||
{env.name !== "production" && (
|
||||
<DialogAction
|
||||
title="Delete Environment"
|
||||
description={`Are you sure you want to delete the "${env.name}" environment? This action cannot be undone.`}
|
||||
type="destructive"
|
||||
onClick={() =>
|
||||
handleDeleteEnvironment(env.environmentId)
|
||||
}
|
||||
>
|
||||
<Button variant="outline" size="sm">
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</DialogAction>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-center py-8">
|
||||
<div className="text-sm text-muted-foreground">
|
||||
No environments found. The production environment should be
|
||||
created automatically.
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
||||
{/* Create Environment Dialog */}
|
||||
<Dialog open={isCreateDialogOpen} onOpenChange={setIsCreateDialogOpen}>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>Create New Environment</DialogTitle>
|
||||
<DialogDescription>
|
||||
Create a new environment for this project.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<Form {...createForm}>
|
||||
<form
|
||||
onSubmit={createForm.handleSubmit(onCreateSubmit)}
|
||||
className="space-y-4"
|
||||
>
|
||||
<FormField
|
||||
control={createForm.control}
|
||||
name="name"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Environment Name</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
placeholder="e.g., staging, development"
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={createForm.control}
|
||||
name="description"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Description (Optional)</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
placeholder="Describe this environment..."
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<DialogFooter>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => setIsCreateDialogOpen(false)}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
type="submit"
|
||||
isLoading={createEnvironmentMutation.isLoading}
|
||||
>
|
||||
Create Environment
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</Form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
||||
{/* Duplicate Environment Dialog */}
|
||||
<Dialog
|
||||
open={isDuplicateDialogOpen}
|
||||
onOpenChange={setIsDuplicateDialogOpen}
|
||||
>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>Duplicate Environment</DialogTitle>
|
||||
<DialogDescription>
|
||||
Create a copy of the selected environment.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<Form {...duplicateForm}>
|
||||
<form
|
||||
onSubmit={duplicateForm.handleSubmit(onDuplicateSubmit)}
|
||||
className="space-y-4"
|
||||
>
|
||||
<FormField
|
||||
control={duplicateForm.control}
|
||||
name="name"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>New Environment Name</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="e.g., staging-copy" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={duplicateForm.control}
|
||||
name="description"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Description (Optional)</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
placeholder="Describe this environment..."
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<DialogFooter>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
setIsDuplicateDialogOpen(false);
|
||||
setSelectedEnvironmentId("");
|
||||
duplicateForm.reset();
|
||||
}}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
type="submit"
|
||||
isLoading={duplicateEnvironmentMutation.isLoading}
|
||||
>
|
||||
Duplicate Environment
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</Form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</>
|
||||
);
|
||||
};
|
||||
26
apps/dokploy/drizzle/0107_charming_chimera.sql
Normal file
26
apps/dokploy/drizzle/0107_charming_chimera.sql
Normal file
@@ -0,0 +1,26 @@
|
||||
CREATE TABLE "environment" (
|
||||
"environmentId" text PRIMARY KEY NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"description" text,
|
||||
"createdAt" text NOT NULL,
|
||||
"projectId" text NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "environment" ADD CONSTRAINT "environment_projectId_project_projectId_fk" FOREIGN KEY ("projectId") REFERENCES "public"."project"("projectId") ON DELETE cascade ON UPDATE no action;
|
||||
|
||||
|
||||
-- Insertar un ambiente "production" para cada proyecto existente
|
||||
INSERT INTO "environment" ("environmentId", "name", "description", "createdAt", "projectId")
|
||||
SELECT
|
||||
-- Generar un ID único para cada ambiente usando el projectId como base
|
||||
'env_prod_' || "projectId" || '_' || EXTRACT(EPOCH FROM NOW())::text,
|
||||
'production',
|
||||
'Production environment',
|
||||
NOW()::text,
|
||||
"projectId"
|
||||
FROM "project"
|
||||
WHERE "projectId" NOT IN (
|
||||
SELECT DISTINCT "projectId"
|
||||
FROM "environment"
|
||||
WHERE "name" = 'production'
|
||||
);
|
||||
111
apps/dokploy/drizzle/0108_keen_doctor_faustus.sql
Normal file
111
apps/dokploy/drizzle/0108_keen_doctor_faustus.sql
Normal file
@@ -0,0 +1,111 @@
|
||||
-- Step 1: Add environmentId columns as nullable first
|
||||
ALTER TABLE "application" ADD COLUMN "environmentId" text;--> statement-breakpoint
|
||||
ALTER TABLE "compose" ADD COLUMN "environmentId" text;--> statement-breakpoint
|
||||
ALTER TABLE "mariadb" ADD COLUMN "environmentId" text;--> statement-breakpoint
|
||||
ALTER TABLE "mongo" ADD COLUMN "environmentId" text;--> statement-breakpoint
|
||||
ALTER TABLE "mysql" ADD COLUMN "environmentId" text;--> statement-breakpoint
|
||||
ALTER TABLE "postgres" ADD COLUMN "environmentId" text;--> statement-breakpoint
|
||||
ALTER TABLE "redis" ADD COLUMN "environmentId" text;--> statement-breakpoint
|
||||
|
||||
-- Step 2: Create production environment for each project that doesn't have one
|
||||
-- INSERT INTO "environment" ("environmentId", "name", "description", "createdAt", "projectId")
|
||||
-- SELECT
|
||||
-- 'env_prod_' || p."projectId" || '_' || EXTRACT(EPOCH FROM NOW())::text,
|
||||
-- 'production',
|
||||
-- 'Production environment',
|
||||
-- NOW()::text,
|
||||
-- p."projectId"
|
||||
-- FROM "project" p
|
||||
-- WHERE NOT EXISTS (
|
||||
-- SELECT 1 FROM "environment" e
|
||||
-- WHERE e."projectId" = p."projectId" AND e."name" = 'production'
|
||||
-- );--> statement-breakpoint
|
||||
|
||||
-- Step 3: Update all services to point to their project's production environment
|
||||
-- Update applications
|
||||
UPDATE "application"
|
||||
SET "environmentId" = (
|
||||
SELECT e."environmentId"
|
||||
FROM "environment" e
|
||||
WHERE e."projectId" = "application"."projectId"
|
||||
AND e."name" = 'production'
|
||||
LIMIT 1
|
||||
);--> statement-breakpoint
|
||||
|
||||
-- Update compose
|
||||
UPDATE "compose"
|
||||
SET "environmentId" = (
|
||||
SELECT e."environmentId"
|
||||
FROM "environment" e
|
||||
WHERE e."projectId" = "compose"."projectId"
|
||||
AND e."name" = 'production'
|
||||
LIMIT 1
|
||||
);--> statement-breakpoint
|
||||
|
||||
-- Update mariadb
|
||||
UPDATE "mariadb"
|
||||
SET "environmentId" = (
|
||||
SELECT e."environmentId"
|
||||
FROM "environment" e
|
||||
WHERE e."projectId" = "mariadb"."projectId"
|
||||
AND e."name" = 'production'
|
||||
LIMIT 1
|
||||
);--> statement-breakpoint
|
||||
|
||||
-- Update mongo
|
||||
UPDATE "mongo"
|
||||
SET "environmentId" = (
|
||||
SELECT e."environmentId"
|
||||
FROM "environment" e
|
||||
WHERE e."projectId" = "mongo"."projectId"
|
||||
AND e."name" = 'production'
|
||||
LIMIT 1
|
||||
);--> statement-breakpoint
|
||||
|
||||
-- Update mysql
|
||||
UPDATE "mysql"
|
||||
SET "environmentId" = (
|
||||
SELECT e."environmentId"
|
||||
FROM "environment" e
|
||||
WHERE e."projectId" = "mysql"."projectId"
|
||||
AND e."name" = 'production'
|
||||
LIMIT 1
|
||||
);--> statement-breakpoint
|
||||
|
||||
-- Update postgres
|
||||
UPDATE "postgres"
|
||||
SET "environmentId" = (
|
||||
SELECT e."environmentId"
|
||||
FROM "environment" e
|
||||
WHERE e."projectId" = "postgres"."projectId"
|
||||
AND e."name" = 'production'
|
||||
LIMIT 1
|
||||
);--> statement-breakpoint
|
||||
|
||||
-- Update redis
|
||||
UPDATE "redis"
|
||||
SET "environmentId" = (
|
||||
SELECT e."environmentId"
|
||||
FROM "environment" e
|
||||
WHERE e."projectId" = "redis"."projectId"
|
||||
AND e."name" = 'production'
|
||||
LIMIT 1
|
||||
);--> statement-breakpoint
|
||||
|
||||
-- Step 4: Make environmentId columns NOT NULL
|
||||
ALTER TABLE "application" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "compose" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "mariadb" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "mongo" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "mysql" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "postgres" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "redis" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
|
||||
|
||||
-- Step 5: Add foreign key constraints
|
||||
ALTER TABLE "application" ADD CONSTRAINT "application_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "compose" ADD CONSTRAINT "compose_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "mariadb" ADD CONSTRAINT "mariadb_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "mongo" ADD CONSTRAINT "mongo_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "mysql" ADD CONSTRAINT "mysql_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "postgres" ADD CONSTRAINT "postgres_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "redis" ADD CONSTRAINT "redis_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;
|
||||
6481
apps/dokploy/drizzle/meta/0107_snapshot.json
Normal file
6481
apps/dokploy/drizzle/meta/0107_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
6595
apps/dokploy/drizzle/meta/0108_snapshot.json
Normal file
6595
apps/dokploy/drizzle/meta/0108_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -750,6 +750,20 @@
|
||||
"when": 1754912062243,
|
||||
"tag": "0106_purple_maggott",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 107,
|
||||
"version": "7",
|
||||
"when": 1756766249772,
|
||||
"tag": "0107_charming_chimera",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 108,
|
||||
"version": "7",
|
||||
"when": 1756767917601,
|
||||
"tag": "0108_keen_doctor_faustus",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -32,6 +32,7 @@ import { AddCompose } from "@/components/dashboard/project/add-compose";
|
||||
import { AddDatabase } from "@/components/dashboard/project/add-database";
|
||||
import { AddTemplate } from "@/components/dashboard/project/add-template";
|
||||
import { DuplicateProject } from "@/components/dashboard/project/duplicate-project";
|
||||
import { EnvironmentManagement } from "@/components/dashboard/project/environment-management";
|
||||
import { ProjectEnvironment } from "@/components/dashboard/projects/project-environment";
|
||||
import {
|
||||
MariadbIcon,
|
||||
@@ -46,7 +47,6 @@ import { BreadcrumbSidebar } from "@/components/shared/breadcrumb-sidebar";
|
||||
import { DateTooltip } from "@/components/shared/date-tooltip";
|
||||
import { DialogAction } from "@/components/shared/dialog-action";
|
||||
import { StatusTooltip } from "@/components/shared/status-tooltip";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Card,
|
||||
@@ -721,6 +721,9 @@ const Project = (
|
||||
<ProjectEnvironment projectId={projectId}>
|
||||
<Button variant="outline">Project Environment</Button>
|
||||
</ProjectEnvironment>
|
||||
<EnvironmentManagement projectId={projectId}>
|
||||
<Button variant="outline">Environments</Button>
|
||||
</EnvironmentManagement>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button>
|
||||
|
||||
@@ -11,6 +11,7 @@ import { deploymentRouter } from "./routers/deployment";
|
||||
import { destinationRouter } from "./routers/destination";
|
||||
import { dockerRouter } from "./routers/docker";
|
||||
import { domainRouter } from "./routers/domain";
|
||||
import { environmentRouter } from "./routers/environment";
|
||||
import { gitProviderRouter } from "./routers/git-provider";
|
||||
import { giteaRouter } from "./routers/gitea";
|
||||
import { githubRouter } from "./routers/github";
|
||||
@@ -84,6 +85,7 @@ export const appRouter = createTRPCRouter({
|
||||
schedule: scheduleRouter,
|
||||
rollback: rollbackRouter,
|
||||
volumeBackups: volumeBackupsRouter,
|
||||
environment: environmentRouter,
|
||||
});
|
||||
|
||||
// export type definition of API
|
||||
|
||||
113
apps/dokploy/server/api/routers/environment.ts
Normal file
113
apps/dokploy/server/api/routers/environment.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import {
|
||||
createEnvironment,
|
||||
deleteEnvironment,
|
||||
duplicateEnvironment,
|
||||
findEnvironmentById,
|
||||
findEnvironmentsByProjectId,
|
||||
updateEnvironmentById,
|
||||
} from "@dokploy/server";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
import { createTRPCRouter, protectedProcedure } from "@/server/api/trpc";
|
||||
import {
|
||||
apiCreateEnvironment,
|
||||
apiDuplicateEnvironment,
|
||||
apiFindOneEnvironment,
|
||||
apiRemoveEnvironment,
|
||||
apiUpdateEnvironment,
|
||||
} from "@/server/db/schema";
|
||||
|
||||
export const environmentRouter = createTRPCRouter({
|
||||
create: protectedProcedure
|
||||
.input(apiCreateEnvironment)
|
||||
.mutation(async ({ input }) => {
|
||||
try {
|
||||
// Check if user has access to the project
|
||||
// This would typically involve checking project ownership/membership
|
||||
// For now, we'll use a basic organization check
|
||||
|
||||
const environment = await createEnvironment(input);
|
||||
return environment;
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Error creating the environment: ${error instanceof Error ? error.message : error}`,
|
||||
cause: error,
|
||||
});
|
||||
}
|
||||
}),
|
||||
|
||||
one: protectedProcedure
|
||||
.input(apiFindOneEnvironment)
|
||||
.query(async ({ input }) => {
|
||||
try {
|
||||
const environment = await findEnvironmentById(input.environmentId);
|
||||
return environment;
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Environment not found",
|
||||
});
|
||||
}
|
||||
}),
|
||||
|
||||
byProjectId: protectedProcedure
|
||||
.input(z.object({ projectId: z.string() }))
|
||||
.query(async ({ input }) => {
|
||||
try {
|
||||
const environments = await findEnvironmentsByProjectId(input.projectId);
|
||||
return environments;
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Error fetching environments: ${error instanceof Error ? error.message : error}`,
|
||||
});
|
||||
}
|
||||
}),
|
||||
|
||||
remove: protectedProcedure
|
||||
.input(apiRemoveEnvironment)
|
||||
.mutation(async ({ input }) => {
|
||||
try {
|
||||
const deletedEnvironment = await deleteEnvironment(input.environmentId);
|
||||
return deletedEnvironment;
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Error deleting the environment: ${error instanceof Error ? error.message : error}`,
|
||||
});
|
||||
}
|
||||
}),
|
||||
|
||||
update: protectedProcedure
|
||||
.input(apiUpdateEnvironment)
|
||||
.mutation(async ({ input }) => {
|
||||
try {
|
||||
const { environmentId, ...updateData } = input;
|
||||
const environment = await updateEnvironmentById(
|
||||
environmentId,
|
||||
updateData,
|
||||
);
|
||||
return environment;
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Error updating the environment: ${error instanceof Error ? error.message : error}`,
|
||||
});
|
||||
}
|
||||
}),
|
||||
|
||||
duplicate: protectedProcedure
|
||||
.input(apiDuplicateEnvironment)
|
||||
.mutation(async ({ input }) => {
|
||||
try {
|
||||
const duplicatedEnvironment = await duplicateEnvironment(input);
|
||||
return duplicatedEnvironment;
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Error duplicating the environment: ${error instanceof Error ? error.message : error}`,
|
||||
});
|
||||
}
|
||||
}),
|
||||
});
|
||||
@@ -117,29 +117,42 @@ export const projectRouter = createTRPCRouter({
|
||||
eq(projects.organizationId, ctx.session.activeOrganizationId),
|
||||
),
|
||||
with: {
|
||||
applications: {
|
||||
where: buildServiceFilter(
|
||||
applications.applicationId,
|
||||
accessedServices,
|
||||
),
|
||||
},
|
||||
compose: {
|
||||
where: buildServiceFilter(compose.composeId, accessedServices),
|
||||
},
|
||||
mariadb: {
|
||||
where: buildServiceFilter(mariadb.mariadbId, accessedServices),
|
||||
},
|
||||
mongo: {
|
||||
where: buildServiceFilter(mongo.mongoId, accessedServices),
|
||||
},
|
||||
mysql: {
|
||||
where: buildServiceFilter(mysql.mysqlId, accessedServices),
|
||||
},
|
||||
postgres: {
|
||||
where: buildServiceFilter(postgres.postgresId, accessedServices),
|
||||
},
|
||||
redis: {
|
||||
where: buildServiceFilter(redis.redisId, accessedServices),
|
||||
environments: {
|
||||
with: {
|
||||
applications: {
|
||||
where: buildServiceFilter(
|
||||
applications.applicationId,
|
||||
accessedServices,
|
||||
),
|
||||
},
|
||||
compose: {
|
||||
where: buildServiceFilter(
|
||||
compose.composeId,
|
||||
accessedServices,
|
||||
),
|
||||
},
|
||||
mariadb: {
|
||||
where: buildServiceFilter(
|
||||
mariadb.mariadbId,
|
||||
accessedServices,
|
||||
),
|
||||
},
|
||||
mongo: {
|
||||
where: buildServiceFilter(mongo.mongoId, accessedServices),
|
||||
},
|
||||
mysql: {
|
||||
where: buildServiceFilter(mysql.mysqlId, accessedServices),
|
||||
},
|
||||
postgres: {
|
||||
where: buildServiceFilter(
|
||||
postgres.postgresId,
|
||||
accessedServices,
|
||||
),
|
||||
},
|
||||
redis: {
|
||||
where: buildServiceFilter(redis.redisId, accessedServices),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -182,31 +195,38 @@ export const projectRouter = createTRPCRouter({
|
||||
eq(projects.organizationId, ctx.session.activeOrganizationId),
|
||||
),
|
||||
with: {
|
||||
applications: {
|
||||
where: buildServiceFilter(
|
||||
applications.applicationId,
|
||||
accessedServices,
|
||||
),
|
||||
with: { domains: true },
|
||||
},
|
||||
mariadb: {
|
||||
where: buildServiceFilter(mariadb.mariadbId, accessedServices),
|
||||
},
|
||||
mongo: {
|
||||
where: buildServiceFilter(mongo.mongoId, accessedServices),
|
||||
},
|
||||
mysql: {
|
||||
where: buildServiceFilter(mysql.mysqlId, accessedServices),
|
||||
},
|
||||
postgres: {
|
||||
where: buildServiceFilter(postgres.postgresId, accessedServices),
|
||||
},
|
||||
redis: {
|
||||
where: buildServiceFilter(redis.redisId, accessedServices),
|
||||
},
|
||||
compose: {
|
||||
where: buildServiceFilter(compose.composeId, accessedServices),
|
||||
with: { domains: true },
|
||||
environments: {
|
||||
with: {
|
||||
applications: {
|
||||
where: buildServiceFilter(
|
||||
applications.applicationId,
|
||||
accessedServices,
|
||||
),
|
||||
with: { domains: true },
|
||||
},
|
||||
mariadb: {
|
||||
where: buildServiceFilter(mariadb.mariadbId, accessedServices),
|
||||
},
|
||||
mongo: {
|
||||
where: buildServiceFilter(mongo.mongoId, accessedServices),
|
||||
},
|
||||
mysql: {
|
||||
where: buildServiceFilter(mysql.mysqlId, accessedServices),
|
||||
},
|
||||
postgres: {
|
||||
where: buildServiceFilter(
|
||||
postgres.postgresId,
|
||||
accessedServices,
|
||||
),
|
||||
},
|
||||
redis: {
|
||||
where: buildServiceFilter(redis.redisId, accessedServices),
|
||||
},
|
||||
compose: {
|
||||
where: buildServiceFilter(compose.composeId, accessedServices),
|
||||
with: { domains: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: desc(projects.createdAt),
|
||||
@@ -215,19 +235,23 @@ export const projectRouter = createTRPCRouter({
|
||||
|
||||
return await db.query.projects.findMany({
|
||||
with: {
|
||||
applications: {
|
||||
environments: {
|
||||
with: {
|
||||
domains: true,
|
||||
},
|
||||
},
|
||||
mariadb: true,
|
||||
mongo: true,
|
||||
mysql: true,
|
||||
postgres: true,
|
||||
redis: true,
|
||||
compose: {
|
||||
with: {
|
||||
domains: true,
|
||||
applications: {
|
||||
with: {
|
||||
domains: true,
|
||||
},
|
||||
},
|
||||
mariadb: true,
|
||||
mongo: true,
|
||||
mysql: true,
|
||||
postgres: true,
|
||||
redis: true,
|
||||
compose: {
|
||||
with: {
|
||||
domains: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user