refactor(volumes): rework volumes and paths

This commit is contained in:
Mauricio Siu
2024-07-21 18:02:42 -06:00
parent 63a1039439
commit 2e79c7230f
17 changed files with 3151 additions and 127 deletions

View File

@@ -8,10 +8,7 @@ import { dirname, join } from "node:path";
import { COMPOSE_PATH } from "@/server/constants";
import type { InferResultType } from "@/server/types/with";
import boxen from "boxen";
import {
generateFileMountsCompose,
prepareEnvironmentVariables,
} from "../docker/utils";
import { prepareEnvironmentVariables } from "../docker/utils";
import { spawnAsync } from "../process/spawnAsync";
export type ComposeNested = InferResultType<
@@ -24,7 +21,6 @@ export const buildCompose = async (compose: ComposeNested, logPath: string) => {
compose;
try {
const command = createCommand(compose);
generateFileMountsCompose(appName, mounts);
createEnvFile(compose);
@@ -46,7 +42,7 @@ Compose Type: ${composeType} ✅`;
});
writeStream.write(`\n${logBox}\n`);
const projectPath = join(COMPOSE_PATH, compose.appName);
const projectPath = join(COMPOSE_PATH, compose.appName, "code");
await spawnAsync(
"docker",
[...command.split(" ")],
@@ -104,8 +100,8 @@ export const createCommand = (compose: ComposeNested) => {
const createEnvFile = (compose: ComposeNested) => {
const { env, composePath, appName } = compose;
const composeFilePath =
join(COMPOSE_PATH, appName, composePath) ||
join(COMPOSE_PATH, appName, "docker-compose.yml");
join(COMPOSE_PATH, appName, "code", composePath) ||
join(COMPOSE_PATH, appName, "code", "docker-compose.yml");
const envFilePath = join(dirname(composeFilePath), ".env");
let envContent = env || "";

View File

@@ -6,8 +6,7 @@ import { recreateDirectory } from "../filesystem/directory";
export const unzipDrop = async (zipFile: File, appName: string) => {
try {
const basePath = APPLICATIONS_PATH;
const outputPath = join(basePath, appName);
const outputPath = join(APPLICATIONS_PATH, appName, "code");
await recreateDirectory(outputPath);
const arrayBuffer = await zipFile.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);

View File

@@ -11,6 +11,7 @@ export const buildNixpacks = async (
) => {
const { env, appName } = application;
const buildAppDirectory = getBuildAppDirectory(application);
const envVariables = prepareEnvironmentVariables(env);
try {
const args = ["build", buildAppDirectory, "--name", appName];

View File

@@ -1,5 +1,5 @@
import fs from "node:fs";
import path from "node:path";
import path, { join } from "node:path";
import type { Readable } from "node:stream";
import { APPLICATIONS_PATH, COMPOSE_PATH, docker } from "@/server/constants";
import type { ContainerInfo, ResourceRequirements } from "dockerode";
@@ -306,20 +306,9 @@ export const generateFileMounts = (
.filter((mount) => mount.type === "file")
.map((mount) => {
const fileName = mount.mountPath.split("/").pop();
if (!fileName) {
throw new Error("File name not found");
}
const absoluteBasePath = path.resolve(APPLICATIONS_PATH);
const directory = path.join(absoluteBasePath, appName, "files");
const filePath = path.join(directory, fileName);
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
}
fs.writeFileSync(filePath, mount.content || "");
const filePath = path.join(directory, fileName || "");
return {
Type: "bind" as const,
Source: filePath,
@@ -328,31 +317,34 @@ export const generateFileMounts = (
});
};
export const generateFileMountsCompose = (
appName: string,
mounts: ApplicationNested["mounts"],
export const createFile = async (
outputPath: string,
filePath: string,
content: string,
) => {
if (!mounts || mounts.length === 0) {
return [];
try {
// Unir outputPath con filePath
const fullPath = path.join(outputPath, filePath);
// Verificar si la ruta termina en separador (indica que es un directorio)
if (fullPath.endsWith(path.sep) || filePath.endsWith("/")) {
fs.mkdirSync(fullPath, { recursive: true });
console.log(`Directorio creado: ${fullPath}`);
return;
}
// Para archivos, obtener el directorio del archivo
const directory = path.dirname(fullPath);
// Crear el directorio si no existe
fs.mkdirSync(directory, { recursive: true });
// Escribir el archivo
fs.writeFileSync(fullPath, content || "");
console.log(`Archivo creado: ${fullPath}`);
} catch (error) {
console.log(error);
}
return mounts
.filter((mount) => mount.type === "file")
.map((mount) => {
const fileName = path.basename(mount.mountPath);
const directory = path.join(
COMPOSE_PATH,
appName,
path.dirname(mount.mountPath),
);
fs.mkdirSync(directory, { recursive: true });
const filePath = path.join(directory, fileName);
fs.writeFileSync(filePath, mount.content || "");
return {};
});
};
export const getServiceContainer = async (appName: string) => {

View File

@@ -25,6 +25,15 @@ export const removeDirectoryIfExistsContent = async (
}
};
export const removeFileOrDirectory = async (path: string) => {
try {
await execAsync(`rm -rf ${path}`);
} catch (error) {
console.error(`Error to remove ${path}: ${error}`);
throw error;
}
};
export const removeDirectoryCode = async (appName: string) => {
const directoryPath = path.join(APPLICATIONS_PATH, appName);
@@ -72,9 +81,11 @@ export const getBuildAppDirectory = (application: Application) => {
return path.join(
APPLICATIONS_PATH,
appName,
"code",
buildPath ?? "",
dockerfile || "",
);
}
return path.join(APPLICATIONS_PATH, appName, buildPath ?? "");
return path.join(APPLICATIONS_PATH, appName, "code", buildPath ?? "");
};

View File

@@ -28,7 +28,7 @@ export const cloneGitRepository = async (
const writeStream = createWriteStream(logPath, { flags: "a" });
const keyPath = path.join(SSH_PATH, `${appName}_rsa`);
const basePath = isCompose ? COMPOSE_PATH : APPLICATIONS_PATH;
const outputPath = join(basePath, appName);
const outputPath = join(basePath, appName, "code");
const knownHostsPath = path.join(SSH_PATH, "known_hosts");
try {

View File

@@ -93,7 +93,7 @@ export const cloneGithubRepository = async (
});
}
const basePath = isCompose ? COMPOSE_PATH : APPLICATIONS_PATH;
const outputPath = join(basePath, appName);
const outputPath = join(basePath, appName, "code");
const octokit = authGithub(admin);
const token = await getGithubToken(octokit);
const repoclone = `github.com/${owner}/${repository}.git`;

View File

@@ -8,7 +8,7 @@ import { recreateDirectory } from "../filesystem/directory";
export const createComposeFile = async (compose: Compose, logPath: string) => {
const { appName, composeFile } = compose;
const writeStream = createWriteStream(logPath, { flags: "a" });
const outputPath = join(COMPOSE_PATH, appName);
const outputPath = join(COMPOSE_PATH, appName, "code");
try {
await recreateDirectory(outputPath);