From 8d41bafb93bba188a7d345f5478c2b9f2e999a02 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sun, 13 Oct 2024 02:27:33 -0600 Subject: [PATCH] fix(logs): improve logs in remote server when is error, and fix the env parsing to allow any values in enviroment variables --- packages/server/src/services/application.ts | 23 +++++++++++++++++++ packages/server/src/services/compose.ts | 22 +++++++++++++++++- .../server/src/utils/builders/docker-file.ts | 2 +- packages/server/src/utils/builders/heroku.ts | 2 +- .../server/src/utils/builders/nixpacks.ts | 2 +- packages/server/src/utils/builders/paketo.ts | 2 +- packages/server/src/utils/builders/utils.ts | 12 +++++----- 7 files changed, 54 insertions(+), 11 deletions(-) diff --git a/packages/server/src/services/application.ts b/packages/server/src/services/application.ts index afd9eb30f..fbb61df25 100644 --- a/packages/server/src/services/application.ts +++ b/packages/server/src/services/application.ts @@ -38,6 +38,7 @@ import { eq } from "drizzle-orm"; import { getDokployUrl } from "./admin"; import { createDeployment, updateDeploymentStatus } from "./deployment"; import { validUniqueServerAppName } from "./project"; +import { encodeBase64 } from "../utils/docker/utils"; export type Application = typeof applications.$inferSelect; export const createApplication = async ( @@ -312,6 +313,17 @@ export const deployRemoteApplication = async ({ buildLink, }); } catch (error) { + // @ts-ignore + const encodedContent = encodeBase64(error?.message); + + await execAsyncRemote( + application.serverId, + ` + echo "\n\n===================================EXTRA LOGS============================================" >> ${deployment.logPath}; + echo "Error occurred ❌, check the logs for details." >> ${deployment.logPath}; + echo "${encodedContent}" | base64 -d >> "${deployment.logPath}";`, + ); + await updateDeploymentStatus(deployment.deploymentId, "error"); await updateApplicationStatus(applicationId, "error"); await sendBuildErrorNotifications({ @@ -365,6 +377,17 @@ export const rebuildRemoteApplication = async ({ await updateDeploymentStatus(deployment.deploymentId, "done"); await updateApplicationStatus(applicationId, "done"); } catch (error) { + // @ts-ignore + const encodedContent = encodeBase64(error?.message); + + await execAsyncRemote( + application.serverId, + ` + echo "\n\n===================================EXTRA LOGS============================================" >> ${deployment.logPath}; + echo "Error occurred ❌, check the logs for details." >> ${deployment.logPath}; + echo "${encodedContent}" | base64 -d >> "${deployment.logPath}";`, + ); + await updateDeploymentStatus(deployment.deploymentId, "error"); await updateApplicationStatus(applicationId, "error"); throw error; diff --git a/packages/server/src/services/compose.ts b/packages/server/src/services/compose.ts index b1a76b6db..40c428d42 100644 --- a/packages/server/src/services/compose.ts +++ b/packages/server/src/services/compose.ts @@ -44,6 +44,7 @@ import { eq } from "drizzle-orm"; import { getDokployUrl } from "./admin"; import { createDeploymentCompose, updateDeploymentStatus } from "./deployment"; import { validUniqueServerAppName } from "./project"; +import { encodeBase64 } from "../utils/docker/utils"; export type Compose = typeof compose.$inferSelect; @@ -351,7 +352,16 @@ export const deployRemoteCompose = async ({ buildLink, }); } catch (error) { - console.log(error); + // @ts-ignore + const encodedContent = encodeBase64(error?.message); + + await execAsyncRemote( + compose.serverId, + ` + echo "\n\n===================================EXTRA LOGS============================================" >> ${deployment.logPath}; + echo "Error occurred ❌, check the logs for details." >> ${deployment.logPath}; + echo "${encodedContent}" | base64 -d >> "${deployment.logPath}";`, + ); await updateDeploymentStatus(deployment.deploymentId, "error"); await updateCompose(composeId, { composeStatus: "error", @@ -394,6 +404,16 @@ export const rebuildRemoteCompose = async ({ composeStatus: "done", }); } catch (error) { + // @ts-ignore + const encodedContent = encodeBase64(error?.message); + + await execAsyncRemote( + compose.serverId, + ` + echo "\n\n===================================EXTRA LOGS============================================" >> ${deployment.logPath}; + echo "Error occurred ❌, check the logs for details." >> ${deployment.logPath}; + echo "${encodedContent}" | base64 -d >> "${deployment.logPath}";`, + ); await updateDeploymentStatus(deployment.deploymentId, "error"); await updateCompose(composeId, { composeStatus: "error", diff --git a/packages/server/src/utils/builders/docker-file.ts b/packages/server/src/utils/builders/docker-file.ts index ab5cc0669..0ee2f4b11 100644 --- a/packages/server/src/utils/builders/docker-file.ts +++ b/packages/server/src/utils/builders/docker-file.ts @@ -95,7 +95,7 @@ export const getDockerCommand = ( command += createEnvFileCommand(dockerFilePath, env); } - command = ` + command += ` echo "Building ${appName}" >> ${logPath}; cd ${dockerContextPath} >> ${logPath} 2>> ${logPath} || { echo "❌ The path ${dockerContextPath} does not exist" >> ${logPath}; diff --git a/packages/server/src/utils/builders/heroku.ts b/packages/server/src/utils/builders/heroku.ts index be7f7877b..e3039ebf4 100644 --- a/packages/server/src/utils/builders/heroku.ts +++ b/packages/server/src/utils/builders/heroku.ts @@ -56,7 +56,7 @@ export const getHerokuCommand = ( ]; for (const env of envVariables) { - args.push("--env", env); + args.push("--env", `'${env}'`); } const command = `pack ${args.join(" ")}`; diff --git a/packages/server/src/utils/builders/nixpacks.ts b/packages/server/src/utils/builders/nixpacks.ts index 2d81a7c01..bcc670d0e 100644 --- a/packages/server/src/utils/builders/nixpacks.ts +++ b/packages/server/src/utils/builders/nixpacks.ts @@ -94,7 +94,7 @@ export const getNixpacksCommand = ( const args = ["build", buildAppDirectory, "--name", appName]; for (const env of envVariables) { - args.push("--env", env); + args.push("--env", `'${env}'`); } if (publishDirectory) { diff --git a/packages/server/src/utils/builders/paketo.ts b/packages/server/src/utils/builders/paketo.ts index 1faf42aea..f7d170ead 100644 --- a/packages/server/src/utils/builders/paketo.ts +++ b/packages/server/src/utils/builders/paketo.ts @@ -55,7 +55,7 @@ export const getPaketoCommand = ( ]; for (const env of envVariables) { - args.push("--env", env); + args.push("--env", `'${env}'`); } const command = `pack ${args.join(" ")}`; diff --git a/packages/server/src/utils/builders/utils.ts b/packages/server/src/utils/builders/utils.ts index 3eeb45225..a72707f2f 100644 --- a/packages/server/src/utils/builders/utils.ts +++ b/packages/server/src/utils/builders/utils.ts @@ -1,6 +1,6 @@ import { existsSync, mkdirSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; -import { prepareEnvironmentVariables } from "../docker/utils"; +import { encodeBase64, prepareEnvironmentVariables } from "../docker/utils"; export const createEnvFile = (directory: string, env: string | null) => { const envFilePath = join(dirname(directory), ".env"); @@ -12,10 +12,10 @@ export const createEnvFile = (directory: string, env: string | null) => { }; export const createEnvFileCommand = (directory: string, env: string | null) => { - const envFilePath = join(dirname(directory), ".env"); - if (!existsSync(dirname(envFilePath))) { - mkdirSync(dirname(envFilePath), { recursive: true }); - } const envFileContent = prepareEnvironmentVariables(env).join("\n"); - return `echo "${envFileContent}" > ${envFilePath}`; + + const encodedContent = encodeBase64(envFileContent || ""); + const envFilePath = join(dirname(directory), ".env"); + + return `echo "${encodedContent}" | base64 -d > "${envFilePath}";`; };