feat: initial commit

This commit is contained in:
Mauricio Siu
2024-04-28 23:57:52 -06:00
parent 8857a20344
commit be56ba046c
412 changed files with 60777 additions and 1 deletions

View File

@@ -0,0 +1,59 @@
import fs, { promises as fsPromises } from "node:fs";
import path from "node:path";
import type { Application } from "@/server/api/services/application";
import { APPLICATIONS_PATH, MONITORING_PATH } from "@/server/constants";
import { execAsync } from "../process/execAsync";
export const recreateDirectory = async (pathFolder: string): Promise<void> => {
try {
await removeDirectoryIfExistsContent(pathFolder);
await fsPromises.mkdir(pathFolder, { recursive: true });
} catch (error) {
console.error(`Error recreating directory '${pathFolder}':`, error);
}
};
export const removeDirectoryIfExistsContent = async (
path: string,
): Promise<void> => {
if (fs.existsSync(path) && fs.readdirSync(path).length !== 0) {
await execAsync(`rm -rf ${path}`);
}
};
export const removeDirectoryCode = async (appName: string) => {
const directoryPath = path.join(APPLICATIONS_PATH, appName);
try {
await execAsync(`rm -rf ${directoryPath}`);
} catch (error) {
console.error(`Error to remove ${directoryPath}: ${error}`);
throw error;
}
};
export const removeMonitoringDirectory = async (appName: string) => {
const directoryPath = path.join(MONITORING_PATH, appName);
try {
await execAsync(`rm -rf ${directoryPath}`);
} catch (error) {
console.error(`Error to remove ${directoryPath}: ${error}`);
throw error;
}
};
export const getBuildAppDirectory = (application: Application) => {
const { appName, buildType, sourceType, customGitBuildPath, dockerfile } =
application;
const buildPath =
sourceType === "github" ? application?.buildPath : customGitBuildPath;
if (buildType === "dockerfile") {
return path.join(
APPLICATIONS_PATH,
appName,
buildPath ?? "",
dockerfile || "",
);
}
return path.join(APPLICATIONS_PATH, appName, buildPath ?? "");
};

View File

@@ -0,0 +1,62 @@
import * as fs from "node:fs";
import * as path from "node:path";
import { SSH_PATH } from "@/server/constants";
import { spawnAsync } from "../process/spawnAsync";
export const generateSSHKey = async (appName: string) => {
const applicationDirectory = SSH_PATH;
if (!fs.existsSync(applicationDirectory)) {
fs.mkdirSync(applicationDirectory, { recursive: true });
}
const keyPath = path.join(applicationDirectory, `${appName}_rsa`);
if (fs.existsSync(`${keyPath}`)) {
fs.unlinkSync(`${keyPath}`);
}
if (fs.existsSync(`${keyPath}.pub`)) {
fs.unlinkSync(`${keyPath}.pub`);
}
const args = [
"-t",
"rsa",
"-b",
"4096",
"-C",
"dokploy",
"-f",
keyPath,
"-N",
"",
];
try {
await spawnAsync("ssh-keygen", args);
return keyPath;
} catch (error) {
throw error;
}
};
export const readRSAFile = async (appName: string) => {
try {
if (!fs.existsSync(SSH_PATH)) {
fs.mkdirSync(SSH_PATH, { recursive: true });
}
const keyPath = path.join(SSH_PATH, `${appName}_rsa.pub`);
const data = fs.readFileSync(keyPath, { encoding: "utf-8" });
return data;
} catch (error) {
throw error;
}
};
export const removeRSAFiles = async (appName: string) => {
try {
const publicKeyPath = path.join(SSH_PATH, `${appName}_rsa.pub`);
const privateKeyPath = path.join(SSH_PATH, `${appName}_rsa`);
await fs.promises.unlink(publicKeyPath);
await fs.promises.unlink(privateKeyPath);
} catch (error) {
throw error;
}
};