Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions apps/dokploy/server/api/routers/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
import {
IS_CLOUD,
createRegistry,
execAsync,
execAsyncRemote,
execFileAsync,
findRegistryById,
removeRegistry,
updateRegistry,
Expand Down Expand Up @@ -83,7 +83,13 @@ export const registryRouter = createTRPCRouter({
.input(apiTestRegistry)
.mutation(async ({ input }) => {
try {
const loginCommand = `echo ${input.password} | docker login ${input.registryUrl} --username ${input.username} --password-stdin`;
const args = [
"login",
input.registryUrl,
"--username",
input.username,
"--password-stdin",
];

if (IS_CLOUD && !input.serverId) {
throw new TRPCError({
Expand All @@ -93,9 +99,14 @@ export const registryRouter = createTRPCRouter({
}

if (input.serverId && input.serverId !== "none") {
await execAsyncRemote(input.serverId, loginCommand);
await execAsyncRemote(
input.serverId,
`echo ${input.password} | docker ${args.join(" ")}`,
);
} else {
await execAsync(loginCommand);
await execFileAsync("docker", args, {
input: Buffer.from(input.password).toString(),
});
}

return true;
Expand Down
41 changes: 40 additions & 1 deletion packages/server/src/utils/process/execAsync.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
import { exec } from "node:child_process";
import { exec, execFile } from "node:child_process";
import util from "node:util";
import { findServerById } from "@dokploy/server/services/server";
import { Client } from "ssh2";

export const execAsync = util.promisify(exec);

export const execFileAsync = async (
command: string,
args: string[],
options: { input?: string } = {},
): Promise<{ stdout: string; stderr: string }> => {
const child = execFile(command, args);

if (options.input && child.stdin) {
child.stdin.write(options.input);
child.stdin.end();
}

return new Promise((resolve, reject) => {
let stdout = "";
let stderr = "";

child.stdout?.on("data", (data) => {
stdout += data.toString();
});

child.stderr?.on("data", (data) => {
stderr += data.toString();
});

child.on("close", (code) => {
if (code === 0) {
resolve({ stdout, stderr });
} else {
reject(
new Error(`Command failed with code ${code}. Stderr: ${stderr}`),
);
}
});

child.on("error", reject);
});
};

export const execAsyncRemote = async (
serverId: string | null,
command: string,
Expand Down