Skip to content

Commit 9dcdb53

Browse files
committed
feat(transfer): move services between servers with their volumes, mounts and config
Adds a Transfer action on every service page that moves an application, compose or database to another server without S3: volumes, bind mounts and deployment logs are streamed through the panel (ssh2/spawn pipe), file mounts and Traefik config are recreated on the target, the source is cleaned up and the service is deployed on the target. Failures before cleanup roll back to the source server.
1 parent bda8124 commit 9dcdb53

17 files changed

Lines changed: 1198 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { promises as fs } from "node:fs";
2+
import os from "node:os";
3+
import path from "node:path";
4+
import { describe, expect, it, vi } from "vitest";
5+
6+
vi.mock("@dokploy/server/services/server", () => ({
7+
findServerById: vi.fn(),
8+
}));
9+
10+
import { pipeBetweenServers } from "@dokploy/server/utils/process/remoteStream";
11+
12+
describe("pipeBetweenServers", () => {
13+
it("delivers a short source stream that ends before the target is ready", async () => {
14+
const bytes = await pipeBetweenServers({
15+
source: { serverId: null, command: "printf 'hello world'" },
16+
target: { serverId: null, command: "sleep 0.3; cat > /dev/null" },
17+
});
18+
expect(bytes).toBe(11);
19+
});
20+
21+
it("pipes multi-megabyte data unchanged", async () => {
22+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "pipe-"));
23+
const source = path.join(dir, "src.bin");
24+
const target = path.join(dir, "dst.bin");
25+
const size = 3 * 1024 * 1024;
26+
const progress: number[] = [];
27+
28+
const bytes = await pipeBetweenServers({
29+
source: {
30+
serverId: null,
31+
command: `head -c ${size} /dev/urandom | tee '${source}'`,
32+
},
33+
target: { serverId: null, command: `cat > '${target}'` },
34+
onProgress: (transferred) => progress.push(transferred),
35+
});
36+
37+
expect(bytes).toBe(size);
38+
expect(progress.at(-1)).toBe(size);
39+
expect(await fs.readFile(target)).toEqual(await fs.readFile(source));
40+
await fs.rm(dir, { recursive: true, force: true });
41+
});
42+
43+
it("reports a target failure with its stderr", async () => {
44+
await expect(
45+
pipeBetweenServers({
46+
source: { serverId: null, command: "printf x" },
47+
target: { serverId: null, command: "echo boom >&2; exit 3" },
48+
}),
49+
).rejects.toThrow("target exited with code 3: boom");
50+
});
51+
52+
it("reports a source failure", async () => {
53+
await expect(
54+
pipeBetweenServers({
55+
source: { serverId: null, command: "exit 2" },
56+
target: { serverId: null, command: "cat > /dev/null" },
57+
}),
58+
).rejects.toThrow("source exited with code 2");
59+
});
60+
});
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
import type { ServiceType } from "@dokploy/server/db/schema";
2+
import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema";
3+
import { ArrowRightLeft } from "lucide-react";
4+
import { useState } from "react";
5+
import { useForm } from "react-hook-form";
6+
import { toast } from "sonner";
7+
import { z } from "zod";
8+
import { AlertBlock } from "@/components/shared/alert-block";
9+
import { DrawerLogs } from "@/components/shared/drawer-logs";
10+
import { Button } from "@/components/ui/button";
11+
import { Checkbox } from "@/components/ui/checkbox";
12+
import {
13+
Dialog,
14+
DialogContent,
15+
DialogDescription,
16+
DialogFooter,
17+
DialogHeader,
18+
DialogTitle,
19+
DialogTrigger,
20+
} from "@/components/ui/dialog";
21+
import {
22+
Form,
23+
FormControl,
24+
FormField,
25+
FormItem,
26+
FormLabel,
27+
FormMessage,
28+
} from "@/components/ui/form";
29+
import {
30+
Select,
31+
SelectContent,
32+
SelectItem,
33+
SelectTrigger,
34+
SelectValue,
35+
} from "@/components/ui/select";
36+
import { api } from "@/utils/api";
37+
import { type LogLine, parseLogs } from "../docker/logs/utils";
38+
39+
const LOCAL_SERVER = "dokploy";
40+
41+
const transferSchema = z.object({
42+
targetServerId: z.string().min(1, { message: "Select a target server" }),
43+
removeSourceData: z.boolean(),
44+
});
45+
46+
type TransferForm = z.infer<typeof transferSchema>;
47+
48+
interface Props {
49+
id: string;
50+
type: ServiceType;
51+
serverId?: string | null;
52+
}
53+
54+
export const TransferService = ({ id, type, serverId }: Props) => {
55+
const utils = api.useUtils();
56+
const { data: isCloud } = api.settings.isCloud.useQuery();
57+
const { data: servers } = api.server.withSSHKey.useQuery();
58+
const [isOpen, setIsOpen] = useState(false);
59+
const [isDrawerOpen, setIsDrawerOpen] = useState(false);
60+
const [isTransferring, setIsTransferring] = useState(false);
61+
const [logs, setLogs] = useState<LogLine[]>([]);
62+
const [request, setRequest] = useState<{
63+
targetServerId: string | null;
64+
removeSourceData: boolean;
65+
} | null>(null);
66+
67+
const targets = [
68+
...(!isCloud && serverId
69+
? [{ serverId: LOCAL_SERVER, name: "Dokploy Server" }]
70+
: []),
71+
...(servers ?? []).filter((server) => server.serverId !== serverId),
72+
];
73+
74+
const form = useForm<TransferForm>({
75+
defaultValues: { targetServerId: "", removeSourceData: false },
76+
resolver: zodResolver(transferSchema),
77+
});
78+
79+
api.transfer.start.useSubscription(
80+
{
81+
serviceType: type,
82+
serviceId: id,
83+
targetServerId: request?.targetServerId ?? null,
84+
removeSourceData: request?.removeSourceData ?? false,
85+
},
86+
{
87+
enabled: isTransferring && request !== null,
88+
onData(line) {
89+
setLogs((prev) => [...prev, ...parseLogs(line)]);
90+
if (line.startsWith("Transfer completed")) {
91+
setIsTransferring(false);
92+
toast.success("Service transferred successfully");
93+
utils.invalidate();
94+
} else if (line.startsWith("Transfer failed")) {
95+
setIsTransferring(false);
96+
toast.error("Transfer failed, check the logs");
97+
utils.invalidate();
98+
}
99+
},
100+
onError(error) {
101+
setIsTransferring(false);
102+
toast.error(error.message);
103+
},
104+
},
105+
);
106+
107+
const onSubmit = (values: TransferForm) => {
108+
setRequest({
109+
targetServerId:
110+
values.targetServerId === LOCAL_SERVER ? null : values.targetServerId,
111+
removeSourceData: values.removeSourceData,
112+
});
113+
setLogs([]);
114+
setIsDrawerOpen(true);
115+
setIsTransferring(true);
116+
setIsOpen(false);
117+
};
118+
119+
return (
120+
<>
121+
<Dialog open={isOpen} onOpenChange={setIsOpen}>
122+
<DialogTrigger asChild>
123+
<Button
124+
variant="ghost"
125+
size="icon"
126+
className="group hover:bg-blue-500/10"
127+
isLoading={isTransferring}
128+
>
129+
<ArrowRightLeft className="size-4 text-primary group-hover:text-blue-500" />
130+
</Button>
131+
</DialogTrigger>
132+
<DialogContent className="sm:max-w-lg">
133+
<DialogHeader>
134+
<DialogTitle>Transfer to another server</DialogTitle>
135+
<DialogDescription>
136+
Moves this service with its volumes, bind mounts, file mounts and
137+
configuration to the selected server, then deploys it there.
138+
</DialogDescription>
139+
</DialogHeader>
140+
{targets.length === 0 ? (
141+
<AlertBlock type="info">
142+
There are no other servers available to transfer this service to.
143+
</AlertBlock>
144+
) : (
145+
<Form {...form}>
146+
<form
147+
onSubmit={form.handleSubmit(onSubmit)}
148+
id="hook-form-transfer-service"
149+
className="grid w-full gap-4"
150+
>
151+
<FormField
152+
control={form.control}
153+
name="targetServerId"
154+
render={({ field }) => (
155+
<FormItem>
156+
<FormLabel>Target server</FormLabel>
157+
<Select
158+
onValueChange={field.onChange}
159+
defaultValue={field.value}
160+
>
161+
<FormControl>
162+
<SelectTrigger>
163+
<SelectValue placeholder="Select a server" />
164+
</SelectTrigger>
165+
</FormControl>
166+
<SelectContent>
167+
{targets.map((server) => (
168+
<SelectItem
169+
key={server.serverId}
170+
value={server.serverId}
171+
>
172+
{server.name}
173+
</SelectItem>
174+
))}
175+
</SelectContent>
176+
</Select>
177+
<FormMessage />
178+
</FormItem>
179+
)}
180+
/>
181+
<FormField
182+
control={form.control}
183+
name="removeSourceData"
184+
render={({ field }) => (
185+
<FormItem>
186+
<div className="flex items-center">
187+
<FormControl>
188+
<Checkbox
189+
checked={field.value}
190+
onCheckedChange={field.onChange}
191+
/>
192+
</FormControl>
193+
<FormLabel className="ml-2">
194+
Remove volumes from the source server after the
195+
transfer
196+
</FormLabel>
197+
</div>
198+
<FormMessage />
199+
</FormItem>
200+
)}
201+
/>
202+
<AlertBlock type="warning">
203+
<ul className="list-disc pl-4 space-y-1">
204+
<li>The service is stopped while its data is copied.</li>
205+
<li>
206+
Point your DNS records to the new server, certificates are
207+
issued again there.
208+
</li>
209+
<li>
210+
Networks that only exist on the current server are
211+
detached.
212+
</li>
213+
<li>
214+
Bind mount host paths are copied but never deleted from
215+
the source server.
216+
</li>
217+
</ul>
218+
</AlertBlock>
219+
</form>
220+
</Form>
221+
)}
222+
<DialogFooter>
223+
<Button variant="secondary" onClick={() => setIsOpen(false)}>
224+
Cancel
225+
</Button>
226+
{targets.length > 0 && (
227+
<Button
228+
isLoading={isTransferring}
229+
form="hook-form-transfer-service"
230+
type="submit"
231+
>
232+
Transfer
233+
</Button>
234+
)}
235+
</DialogFooter>
236+
</DialogContent>
237+
</Dialog>
238+
<DrawerLogs
239+
isOpen={isDrawerOpen}
240+
onClose={() => setIsDrawerOpen(false)}
241+
filteredLogs={logs}
242+
/>
243+
</>
244+
);
245+
};

‎apps/dokploy/pages/dashboard/project/[projectId]/environment/[environmentId]/services/application/[applicationId].tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { DeleteService } from "@/components/dashboard/compose/delete-service";
3636
import { ContainerFreeMonitoring } from "@/components/dashboard/monitoring/free/container/show-free-container-monitoring";
3737
import { ContainerPaidMonitoring } from "@/components/dashboard/monitoring/paid/container/show-paid-container-monitoring";
3838
import { AssignNetworks } from "@/components/dashboard/networks/assign-networks";
39+
import { TransferService } from "@/components/dashboard/shared/transfer-service";
3940
import { DashboardLayout } from "@/components/layouts/dashboard-layout";
4041
import { AdvanceBreadcrumb } from "@/components/shared/advance-breadcrumb";
4142
import { StatusTooltip } from "@/components/shared/status-tooltip";
@@ -194,6 +195,13 @@ const Service = (
194195
{permissions?.service.create && (
195196
<UpdateApplication applicationId={applicationId} />
196197
)}
198+
{permissions?.service.create && (
199+
<TransferService
200+
id={applicationId}
201+
type="application"
202+
serverId={data?.serverId}
203+
/>
204+
)}
197205
{permissions?.service.delete && (
198206
<DeleteService id={applicationId} type="application" />
199207
)}

‎apps/dokploy/pages/dashboard/project/[projectId]/environment/[environmentId]/services/compose/[composeId].tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { ShowBackups } from "@/components/dashboard/database/backups/show-backup
3333
import { ComposeFreeMonitoring } from "@/components/dashboard/monitoring/free/container/show-free-compose-monitoring";
3434
import { ComposePaidMonitoring } from "@/components/dashboard/monitoring/paid/container/show-paid-compose-monitoring";
3535
import { AssignComposeNetworks } from "@/components/dashboard/networks/assign-compose-networks";
36+
import { TransferService } from "@/components/dashboard/shared/transfer-service";
3637
import { DashboardLayout } from "@/components/layouts/dashboard-layout";
3738
import { AdvanceBreadcrumb } from "@/components/shared/advance-breadcrumb";
3839
import { StatusTooltip } from "@/components/shared/status-tooltip";
@@ -184,6 +185,13 @@ const Service = (
184185
<UpdateCompose composeId={composeId} />
185186
)}
186187

188+
{permissions?.service.create && (
189+
<TransferService
190+
id={composeId}
191+
type="compose"
192+
serverId={data?.serverId}
193+
/>
194+
)}
187195
{permissions?.service.delete && (
188196
<DeleteService id={composeId} type="compose" />
189197
)}

‎apps/dokploy/pages/dashboard/project/[projectId]/environment/[environmentId]/services/libsql/[libsqlId].tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { UpdateLibsql } from "@/components/dashboard/libsql/update-libsql";
2323
import { ContainerFreeMonitoring } from "@/components/dashboard/monitoring/free/container/show-free-container-monitoring";
2424
import { ContainerPaidMonitoring } from "@/components/dashboard/monitoring/paid/container/show-paid-container-monitoring";
2525
import { ShowDatabaseAdvancedSettings } from "@/components/dashboard/shared/show-database-advanced-settings";
26+
import { TransferService } from "@/components/dashboard/shared/transfer-service";
2627
import { LibsqlIcon } from "@/components/icons/data-tools-icons";
2728
import { DashboardLayout } from "@/components/layouts/dashboard-layout";
2829
import { AdvanceBreadcrumb } from "@/components/shared/advance-breadcrumb";
@@ -145,6 +146,13 @@ const Libsql = (
145146
</div>
146147
<div className="flex flex-row gap-2 justify-end">
147148
<UpdateLibsql libsqlId={libsqlId} />
149+
{(auth?.role === "owner" || auth?.canCreateServices) && (
150+
<TransferService
151+
id={libsqlId}
152+
type="libsql"
153+
serverId={data?.serverId}
154+
/>
155+
)}
148156
{(auth?.role === "owner" || auth?.canDeleteServices) && (
149157
<DeleteService id={libsqlId} type="libsql" />
150158
)}

‎apps/dokploy/pages/dashboard/project/[projectId]/environment/[environmentId]/services/mariadb/[mariadbId].tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { UpdateMariadb } from "@/components/dashboard/mariadb/update-mariadb";
2323
import { ContainerFreeMonitoring } from "@/components/dashboard/monitoring/free/container/show-free-container-monitoring";
2424
import { ContainerPaidMonitoring } from "@/components/dashboard/monitoring/paid/container/show-paid-container-monitoring";
2525
import { ShowDatabaseAdvancedSettings } from "@/components/dashboard/shared/show-database-advanced-settings";
26+
import { TransferService } from "@/components/dashboard/shared/transfer-service";
2627
import { MariadbIcon } from "@/components/icons/data-tools-icons";
2728
import { DashboardLayout } from "@/components/layouts/dashboard-layout";
2829
import { AdvanceBreadcrumb } from "@/components/shared/advance-breadcrumb";
@@ -159,6 +160,13 @@ const Mariadb = (
159160
{permissions?.service.create && (
160161
<UpdateMariadb mariadbId={mariadbId} />
161162
)}
163+
{permissions?.service.create && (
164+
<TransferService
165+
id={mariadbId}
166+
type="mariadb"
167+
serverId={data?.serverId}
168+
/>
169+
)}
162170
{permissions?.service.delete && (
163171
<DeleteService id={mariadbId} type="mariadb" />
164172
)}

0 commit comments

Comments
 (0)