mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
feat(registry): refactor Docker login command execution to use execFileAsync for improved input handling
This commit is contained in:
parent
e83efa3379
commit
cb20950dd9
@ -10,8 +10,8 @@ import {
|
||||
import {
|
||||
IS_CLOUD,
|
||||
createRegistry,
|
||||
execAsync,
|
||||
execAsyncRemote,
|
||||
execFileAsync,
|
||||
findRegistryById,
|
||||
removeRegistry,
|
||||
updateRegistry,
|
||||
@ -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({
|
||||
@ -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;
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user