chore: format whole repository with new configs

This commit is contained in:
Krzysztof Durek
2024-07-15 01:08:18 +02:00
parent 7a5c71cda3
commit 906e8de13b
349 changed files with 3565 additions and 3549 deletions

View File

@@ -1,15 +1,15 @@
import { findAdmin } from "@/server/api/services/admin";
import { scheduleJob } from "node-schedule";
import { db } from "../../db/index";
import { runMariadbBackup } from "./mariadb";
import { runMongoBackup } from "./mongo";
import { runMySqlBackup } from "./mysql";
import { runPostgresBackup } from "./postgres";
import {
cleanUpDockerBuilder,
cleanUpSystemPrune,
cleanUpUnusedImages,
} from "../docker/utils";
import { findAdmin } from "@/server/api/services/admin";
import { runMariadbBackup } from "./mariadb";
import { runMongoBackup } from "./mongo";
import { runMySqlBackup } from "./mysql";
import { runPostgresBackup } from "./postgres";
export const initCronJobs = async () => {
console.log("Setting up cron jobs....");

View File

@@ -1,10 +1,10 @@
import { unlink } from "node:fs/promises";
import path from "node:path";
import { execAsync } from "../process/execAsync";
import { uploadToS3 } from "./utils";
import type { BackupSchedule } from "@/server/api/services/backup";
import type { Mariadb } from "@/server/api/services/mariadb";
import { getServiceContainer } from "../docker/utils";
import { execAsync } from "../process/execAsync";
import { uploadToS3 } from "./utils";
export const runMariadbBackup = async (
mariadb: Mariadb,

View File

@@ -1,10 +1,10 @@
import { unlink } from "node:fs/promises";
import path from "node:path";
import { execAsync } from "../process/execAsync";
import { uploadToS3 } from "./utils";
import type { BackupSchedule } from "@/server/api/services/backup";
import type { Mongo } from "@/server/api/services/mongo";
import { getServiceContainer } from "../docker/utils";
import { execAsync } from "../process/execAsync";
import { uploadToS3 } from "./utils";
// mongodb://mongo:Bqh7AQl-PRbnBu@localhost:27017/?tls=false&directConnection=true
export const runMongoBackup = async (mongo: Mongo, backup: BackupSchedule) => {

View File

@@ -1,10 +1,10 @@
import path from "node:path";
import { execAsync } from "../process/execAsync";
import { unlink } from "node:fs/promises";
import { uploadToS3 } from "./utils";
import path from "node:path";
import type { BackupSchedule } from "@/server/api/services/backup";
import type { MySql } from "@/server/api/services/mysql";
import { getServiceContainer } from "../docker/utils";
import { execAsync } from "../process/execAsync";
import { uploadToS3 } from "./utils";
export const runMySqlBackup = async (mysql: MySql, backup: BackupSchedule) => {
const { appName, databaseRootPassword } = mysql;

View File

@@ -1,10 +1,10 @@
import path from "node:path";
import { execAsync } from "../process/execAsync";
import { unlink } from "node:fs/promises";
import { uploadToS3 } from "./utils";
import path from "node:path";
import type { BackupSchedule } from "@/server/api/services/backup";
import type { Postgres } from "@/server/api/services/postgres";
import { getServiceContainer } from "../docker/utils";
import { execAsync } from "../process/execAsync";
import { uploadToS3 } from "./utils";
export const runPostgresBackup = async (
postgres: Postgres,

View File

@@ -1,12 +1,12 @@
import { readFile } from "node:fs/promises";
import type { BackupSchedule } from "@/server/api/services/backup";
import type { Destination } from "@/server/api/services/destination";
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { scheduleJob, scheduledJobs } from "node-schedule";
import { readFile } from "node:fs/promises";
import { runPostgresBackup } from "./postgres";
import { runMySqlBackup } from "./mysql";
import { runMongoBackup } from "./mongo";
import { runMariadbBackup } from "./mariadb";
import { runMongoBackup } from "./mongo";
import { runMySqlBackup } from "./mysql";
import { runPostgresBackup } from "./postgres";
export const uploadToS3 = async (
destination: Destination,

View File

@@ -4,15 +4,15 @@ import {
mkdirSync,
writeFileSync,
} from "node:fs";
import type { InferResultType } from "@/server/types/with";
import { spawnAsync } from "../process/spawnAsync";
import { COMPOSE_PATH } from "@/server/constants";
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 boxen from "boxen";
import { spawnAsync } from "../process/spawnAsync";
export type ComposeNested = InferResultType<
"compose",

View File

@@ -1,40 +1,40 @@
import { docker } from "@/server/constants";
import type { WriteStream } from "node:fs";
import { docker } from "@/server/constants";
import * as tar from "tar-fs";
import type { ApplicationNested } from ".";
import { getBuildAppDirectory } from "../filesystem/directory";
export const buildCustomDocker = async (
application: ApplicationNested,
writeStream: WriteStream,
application: ApplicationNested,
writeStream: WriteStream,
) => {
const { appName } = application;
const dockerFilePath = getBuildAppDirectory(application);
try {
const image = `${appName}`;
const contextPath =
dockerFilePath.substring(0, dockerFilePath.lastIndexOf("/") + 1) || ".";
const tarStream = tar.pack(contextPath);
const { appName } = application;
const dockerFilePath = getBuildAppDirectory(application);
try {
const image = `${appName}`;
const contextPath =
dockerFilePath.substring(0, dockerFilePath.lastIndexOf("/") + 1) || ".";
const tarStream = tar.pack(contextPath);
const stream = await docker.buildImage(tarStream, {
t: image,
dockerfile: dockerFilePath.substring(dockerFilePath.lastIndexOf("/") + 1),
// TODO: maybe use or not forcerm
// forcerm: true,
});
const stream = await docker.buildImage(tarStream, {
t: image,
dockerfile: dockerFilePath.substring(dockerFilePath.lastIndexOf("/") + 1),
// TODO: maybe use or not forcerm
// forcerm: true,
});
await new Promise((resolve, reject) => {
docker.modem.followProgress(
stream,
(err, res) => (err ? reject(err) : resolve(res)),
(event) => {
if (event.stream) {
writeStream.write(event.stream);
}
},
);
});
} catch (error) {
throw error;
}
await new Promise((resolve, reject) => {
docker.modem.followProgress(
stream,
(err, res) => (err ? reject(err) : resolve(res)),
(event) => {
if (event.stream) {
writeStream.write(event.stream);
}
},
);
});
} catch (error) {
throw error;
}
};

View File

@@ -1,8 +1,8 @@
import type { WriteStream } from "node:fs";
import type { ApplicationNested } from ".";
import { prepareEnvironmentVariables } from "../docker/utils";
import { getBuildAppDirectory } from "../filesystem/directory";
import { spawnAsync } from "../process/spawnAsync";
import type { WriteStream } from "node:fs";
// TODO: integrate in the vps sudo chown -R $(whoami) ~/.docker
export const buildHeroku = async (

View File

@@ -2,6 +2,7 @@ import { createWriteStream } from "node:fs";
import { docker } from "@/server/constants";
import type { InferResultType } from "@/server/types/with";
import type { CreateServiceOptions } from "dockerode";
import { uploadImage } from "../cluster/upload";
import {
calculateResources,
generateBindMounts,
@@ -14,7 +15,6 @@ import { buildCustomDocker } from "./docker-file";
import { buildHeroku } from "./heroku";
import { buildNixpacks } from "./nixpacks";
import { buildPaketo } from "./paketo";
import { uploadImage } from "../cluster/upload";
// NIXPACKS codeDirectory = where is the path of the code directory
// HEROKU codeDirectory = where is the path of the code directory

View File

@@ -1,8 +1,8 @@
import type { WriteStream } from "node:fs";
import type { ApplicationNested } from ".";
import { prepareEnvironmentVariables } from "../docker/utils";
import { getBuildAppDirectory } from "../filesystem/directory";
import { spawnAsync } from "../process/spawnAsync";
import type { WriteStream } from "node:fs";
// TODO: integrate in the vps sudo chown -R $(whoami) ~/.docker
export const buildNixpacks = async (

View File

@@ -1,8 +1,8 @@
import type { WriteStream } from "node:fs";
import { spawnAsync } from "../process/spawnAsync";
import type { ApplicationNested } from ".";
import { getBuildAppDirectory } from "../filesystem/directory";
import { prepareEnvironmentVariables } from "../docker/utils";
import { getBuildAppDirectory } from "../filesystem/directory";
import { spawnAsync } from "../process/spawnAsync";
// TODO: integrate in the vps sudo chown -R $(whoami) ~/.docker
export const buildPaketo = async (

View File

@@ -1,6 +1,6 @@
import type { WriteStream } from "node:fs";
import type { ApplicationNested } from "../builders";
import { spawnAsync } from "../process/spawnAsync";
import type { WriteStream } from "node:fs";
export const uploadImage = async (
application: ApplicationNested,

View File

@@ -1,4 +1,5 @@
import type { Mariadb } from "@/server/api/services/mariadb";
import type { Mount } from "@/server/api/services/mount";
import { docker } from "@/server/constants";
import type { CreateServiceOptions } from "dockerode";
import {
@@ -8,7 +9,6 @@ import {
generateVolumeMounts,
prepareEnvironmentVariables,
} from "../docker/utils";
import type { Mount } from "@/server/api/services/mount";
type MariadbWithMounts = Mariadb & {
mounts: Mount[];

View File

@@ -1,4 +1,6 @@
import type { Mongo } from "@/server/api/services/mongo";
import type { Mount } from "@/server/api/services/mount";
import type { Postgres } from "@/server/api/services/postgres";
import { docker } from "@/server/constants";
import type { CreateServiceOptions } from "dockerode";
import {
@@ -8,8 +10,6 @@ import {
generateVolumeMounts,
prepareEnvironmentVariables,
} from "../docker/utils";
import type { Postgres } from "@/server/api/services/postgres";
import type { Mount } from "@/server/api/services/mount";
type MongoWithMounts = Mongo & {
mounts: Mount[];

View File

@@ -1,4 +1,7 @@
import type { Mount } from "@/server/api/services/mount";
import type { MySql } from "@/server/api/services/mysql";
import { docker } from "@/server/constants";
import type { CreateServiceOptions } from "dockerode";
import {
calculateResources,
generateBindMounts,
@@ -6,9 +9,6 @@ import {
generateVolumeMounts,
prepareEnvironmentVariables,
} from "../docker/utils";
import { docker } from "@/server/constants";
import type { CreateServiceOptions } from "dockerode";
import type { Mount } from "@/server/api/services/mount";
type MysqlWithMounts = MySql & {
mounts: Mount[];

View File

@@ -1,4 +1,7 @@
import type { Mount } from "@/server/api/services/mount";
import type { Postgres } from "@/server/api/services/postgres";
import { docker } from "@/server/constants";
import type { CreateServiceOptions } from "dockerode";
import {
calculateResources,
generateBindMounts,
@@ -6,9 +9,6 @@ import {
generateVolumeMounts,
prepareEnvironmentVariables,
} from "../docker/utils";
import { docker } from "@/server/constants";
import type { CreateServiceOptions } from "dockerode";
import type { Mount } from "@/server/api/services/mount";
type PostgresWithMounts = Postgres & {
mounts: Mount[];

View File

@@ -1,4 +1,7 @@
import type { Mount } from "@/server/api/services/mount";
import type { Redis } from "@/server/api/services/redis";
import { docker } from "@/server/constants";
import type { CreateServiceOptions } from "dockerode";
import {
calculateResources,
generateBindMounts,
@@ -6,9 +9,6 @@ import {
generateVolumeMounts,
prepareEnvironmentVariables,
} from "../docker/utils";
import { docker } from "@/server/constants";
import type { CreateServiceOptions } from "dockerode";
import type { Mount } from "@/server/api/services/mount";
type RedisWithMounts = Redis & {
mounts: Mount[];

View File

@@ -1,12 +1,12 @@
import crypto from "node:crypto";
import type { ComposeSpecification } from "./types";
import { findComposeById } from "@/server/api/services/compose";
import { dump, load } from "js-yaml";
import { addPrefixToAllVolumes } from "./compose/volume";
import { addPrefixToAllConfigs } from "./compose/configs";
import { addPrefixToAllNetworks } from "./compose/network";
import { addPrefixToAllSecrets } from "./compose/secrets";
import { addPrefixToAllServiceNames } from "./compose/service";
import { addPrefixToAllVolumes } from "./compose/volume";
import type { ComposeSpecification } from "./types";
export const generateRandomHash = (): string => {
return crypto.randomBytes(4).toString("hex");

View File

@@ -3,9 +3,9 @@ import path from "node:path";
import type { Readable } from "node:stream";
import { APPLICATIONS_PATH, COMPOSE_PATH, docker } from "@/server/constants";
import type { ContainerInfo, ResourceRequirements } from "dockerode";
import { parse } from "dotenv";
import type { ApplicationNested } from "../builders";
import { execAsync } from "../process/execAsync";
import { parse } from "dotenv";
interface RegistryAuth {
username: string;

View File

@@ -4,59 +4,59 @@ import { SSH_PATH } from "@/server/constants";
import { spawnAsync } from "../process/spawnAsync";
export const generateSSHKey = async (appName: string) => {
const applicationDirectory = SSH_PATH;
const applicationDirectory = SSH_PATH;
if (!fs.existsSync(applicationDirectory)) {
fs.mkdirSync(applicationDirectory, { recursive: true });
}
if (!fs.existsSync(applicationDirectory)) {
fs.mkdirSync(applicationDirectory, { recursive: true });
}
const keyPath = path.join(applicationDirectory, `${appName}_rsa`);
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;
}
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;
}
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;
}
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;
}
};

View File

@@ -1,12 +1,12 @@
import { createWriteStream } from "node:fs";
import { join } from "node:path";
import type { Admin } from "@/server/api/services/admin";
import { APPLICATIONS_PATH, COMPOSE_PATH } from "@/server/constants";
import { createAppAuth } from "@octokit/auth-app";
import { TRPCError } from "@trpc/server";
import { Octokit } from "octokit";
import { recreateDirectory } from "../filesystem/directory";
import { spawnAsync } from "../process/spawnAsync";
import type { Admin } from "@/server/api/services/admin";
export const authGithub = (admin: Admin) => {
if (!haveGithubRequirements(admin)) {

View File

@@ -1,9 +1,9 @@
import { createWriteStream } from "node:fs";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import type { Compose } from "@/server/api/services/compose";
import { COMPOSE_PATH } from "@/server/constants";
import { createWriteStream } from "node:fs";
import { join } from "node:path";
import { recreateDirectory } from "../filesystem/directory";
import { writeFile } from "node:fs/promises";
export const createComposeFile = async (compose: Compose, logPath: string) => {
const { appName, composeFile } = compose;

View File

@@ -1,11 +1,11 @@
import type { Domain } from "@/server/api/services/domain";
import type { ApplicationNested } from "../builders";
import {
createServiceConfig,
loadOrCreateConfig,
removeTraefikConfig,
writeTraefikConfig,
} from "./application";
import type { ApplicationNested } from "../builders";
import type { Domain } from "@/server/api/services/domain";
import type { FileConfig, HttpRouter } from "./file-types";
export const manageDomain = async (app: ApplicationNested, domain: Domain) => {

View File

@@ -1,7 +1,7 @@
import { dump, load } from "js-yaml";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { DYNAMIC_TRAEFIK_PATH } from "@/server/constants";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { dump, load } from "js-yaml";
import type { ApplicationNested } from "../builders";
import type { FileConfig } from "./file-types";

View File

@@ -1,12 +1,12 @@
import type { Redirect } from "@/server/api/services/redirect";
import { loadOrCreateConfig, writeTraefikConfig } from "./application";
import type { FileConfig } from "./file-types";
import {
addMiddleware,
deleteMiddleware,
loadMiddlewares,
writeMiddleware,
} from "./middleware";
import type { FileConfig } from "./file-types";
export const updateRedirectMiddleware = (appName: string, data: Redirect) => {
const config = loadMiddlewares<FileConfig>();

View File

@@ -1,10 +1,10 @@
import type { FileConfig, HttpRouter } from "./file-types";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import type { Registry } from "@/server/api/services/registry";
import { removeDirectoryIfExistsContent } from "../filesystem/directory";
import { REGISTRY_PATH } from "@/server/constants";
import { dump, load } from "js-yaml";
import { join } from "node:path";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { removeDirectoryIfExistsContent } from "../filesystem/directory";
import type { FileConfig, HttpRouter } from "./file-types";
export const manageRegistry = async (registry: Registry) => {
if (!existsSync(REGISTRY_PATH)) {

View File

@@ -1,17 +1,17 @@
import { loadOrCreateConfig, writeTraefikConfig } from "./application";
import type { Security } from "@/server/api/services/security";
import * as bcrypt from "bcrypt";
import { loadOrCreateConfig, writeTraefikConfig } from "./application";
import type {
BasicAuthMiddleware,
FileConfig,
HttpMiddleware,
} from "./file-types";
import {
addMiddleware,
deleteMiddleware,
loadMiddlewares,
writeMiddleware,
} from "./middleware";
import type {
BasicAuthMiddleware,
FileConfig,
HttpMiddleware,
} from "./file-types";
export const createSecurityMiddleware = async (
appName: string,

View File

@@ -1,11 +1,11 @@
import { join } from "node:path";
import type { MainTraefikConfig } from "./types";
import { loadOrCreateConfig, writeTraefikConfig } from "./application";
import { MAIN_TRAEFIK_PATH } from "@/server/constants";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { dump, load } from "js-yaml";
import type { FileConfig } from "./file-types";
import { join } from "node:path";
import type { Admin } from "@/server/api/services/admin";
import { MAIN_TRAEFIK_PATH } from "@/server/constants";
import { dump, load } from "js-yaml";
import { loadOrCreateConfig, writeTraefikConfig } from "./application";
import type { FileConfig } from "./file-types";
import type { MainTraefikConfig } from "./types";
export const updateServerTraefik = (
admin: Admin | null,