mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
feat: initial commit
This commit is contained in:
59
server/utils/filesystem/directory.ts
Normal file
59
server/utils/filesystem/directory.ts
Normal 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 ?? "");
|
||||
};
|
||||
62
server/utils/filesystem/ssh.ts
Normal file
62
server/utils/filesystem/ssh.ts
Normal 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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user