From bf58ae0f0ff1d354d8ae2ac931b31d476498bf83 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Thu, 12 Sep 2024 01:33:33 -0600 Subject: [PATCH] feat(remote): add docker compose remote --- apps/dokploy/server/api/services/compose.ts | 26 +++++++------------ apps/dokploy/server/utils/builders/compose.ts | 11 +++++--- apps/dokploy/server/utils/docker/domain.ts | 4 +-- apps/dokploy/server/utils/providers/git.ts | 11 ++++++-- .../server/utils/servers/connection.ts | 2 +- 5 files changed, 30 insertions(+), 24 deletions(-) diff --git a/apps/dokploy/server/api/services/compose.ts b/apps/dokploy/server/api/services/compose.ts index 9db65fbe..73f36e10 100644 --- a/apps/dokploy/server/api/services/compose.ts +++ b/apps/dokploy/server/api/services/compose.ts @@ -241,22 +241,16 @@ export const deployCompose = async ({ command += getCreateComposeFileCommand(compose); } - // await executeCommand(compose.serverId, command); - command += await getBuildComposeCommand(compose, deployment.logPath); - - console.log(command); - - // console.log(buildCommand); - try { - const { stderr, stdout } = await execAsyncRemote( - compose.serverId, - command, - ); - console.log(stderr); - console.log(stdout); - } catch (error) { - console.log(error); - } + Promise.resolve() + .then(() => { + return execAsyncRemote(compose.serverId, command); + }) + .then(() => { + return getBuildComposeCommand(compose, deployment.logPath); + }) + .then(() => { + console.log(" ---- done ----"); + }); } else { if (compose.sourceType === "github") { await cloneGithubRepository(compose, deployment.logPath, true); diff --git a/apps/dokploy/server/utils/builders/compose.ts b/apps/dokploy/server/utils/builders/compose.ts index 2dd98683..ad70b880 100644 --- a/apps/dokploy/server/utils/builders/compose.ts +++ b/apps/dokploy/server/utils/builders/compose.ts @@ -14,6 +14,7 @@ import { } from "../docker/domain"; import { prepareEnvironmentVariables } from "../docker/utils"; import { spawnAsync } from "../process/spawnAsync"; +import { execAsyncRemote } from "../process/execAsync"; export type ComposeNested = InferResultType< "compose", @@ -97,12 +98,16 @@ Compose Type: ${composeType} ✅`; }); const bashCommand = ` -${newCompose} echo "${logBox}" >> ${logPath}; +${newCompose} +${envCommand} cd ${projectPath} || exit 1; docker ${command.split(" ").join(" ")} >> ${logPath} 2>&1; echo "Docker Compose Deployed: ✅" >> ${logPath}; `; + + await execAsyncRemote(compose.serverId, bashCommand); + return bashCommand; }; @@ -180,7 +185,7 @@ export const getCreateEnvFileCommand = (compose: ComposeNested) => { const envFileContent = prepareEnvironmentVariables(envContent).join("\n"); return ` - mkdir -p ${envFilePath}; - echo "${envFileContent}" > ${envFilePath} 2>/dev/null; +mkdir -p ${envFilePath}; +echo "${envFileContent}" > ${envFilePath}; `; }; diff --git a/apps/dokploy/server/utils/docker/domain.ts b/apps/dokploy/server/utils/docker/domain.ts index d7b5da01..7d10b255 100644 --- a/apps/dokploy/server/utils/docker/domain.ts +++ b/apps/dokploy/server/utils/docker/domain.ts @@ -144,7 +144,7 @@ export const writeDomainsToComposeRemote = async ( try { if (compose.serverId) { const composeString = dump(composeConverted, { lineWidth: 1000 }); - return `echo "${composeString}" >> ${path};`; + return `printf '%s' '${composeString.replace(/'/g, "'\\''")}' > ${path}`; } } catch (error) { throw error; @@ -160,7 +160,7 @@ export const addDomainToCompose = async ( let result: ComposeSpecification | null; if (compose.serverId) { - result = await loadDockerComposeRemote(compose); + result = await loadDockerComposeRemote(compose); // aca hay que ir al servidor e ir a traer el compose file al servidor } else { result = await loadDockerCompose(compose); } diff --git a/apps/dokploy/server/utils/providers/git.ts b/apps/dokploy/server/utils/providers/git.ts index 10c7d75c..9a6b661c 100644 --- a/apps/dokploy/server/utils/providers/git.ts +++ b/apps/dokploy/server/utils/providers/git.ts @@ -94,11 +94,18 @@ export const getCustomGitCloneCommand = async ( customGitUrl?: string | null; customGitBranch?: string | null; customGitSSHKeyId?: string | null; + serverId: string | null; }, logPath: string, isCompose = false, ) => { - const { appName, customGitUrl, customGitBranch, customGitSSHKeyId } = entity; + const { + appName, + customGitUrl, + customGitBranch, + customGitSSHKeyId, + serverId, + } = entity; if (!customGitUrl || !customGitBranch) { throw new TRPCError({ @@ -141,7 +148,7 @@ export const getCustomGitCloneCommand = async ( fi `, ); - command.push(`echo "Cloned Custom Git ${customGitUrl}: ✅" >> ${logPath}`); + command.push(`echo "Cloned Custom Git ${customGitUrl}: ✅" >> ${logPath};`); return command.join("\n"); } catch (error) { console.log(error); diff --git a/apps/dokploy/server/utils/servers/connection.ts b/apps/dokploy/server/utils/servers/connection.ts index 1829ad64..0402006f 100644 --- a/apps/dokploy/server/utils/servers/connection.ts +++ b/apps/dokploy/server/utils/servers/connection.ts @@ -11,7 +11,7 @@ export const connectSSH = async (serverId: string) => { return new Promise((resolve, reject) => { client - .on("ready", () => resolve(client)) + .once("ready", () => resolve(client)) .on("error", reject) .connect({ host: server.ipAddress,