wip: trpc

This commit is contained in:
Andras Bacsai
2022-12-21 13:06:44 +01:00
parent 4fa0f2d04a
commit 9c6f412f04
26 changed files with 3383 additions and 39 deletions

View File

@@ -34,6 +34,7 @@
"fastify": "4.10.2",
"fastify-plugin": "4.4.0",
"got": "^12.5.3",
"is-ip": "5.0.0",
"is-port-reachable": "4.0.0",
"js-yaml": "4.1.0",
"jsonwebtoken": "8.5.1",

View File

@@ -2,6 +2,7 @@ import type { Permission, Setting, Team, TeamInvitation, User } from '@prisma/cl
import { prisma } from '../prisma';
import bcrypt from 'bcryptjs';
import crypto from 'crypto';
import { promises as dns } from 'dns';
import fs from 'fs/promises';
import { uniqueNamesGenerator, adjectives, colors, animals } from 'unique-names-generator';
import type { Config } from 'unique-names-generator';
@@ -181,3 +182,347 @@ export async function saveDockerRegistryCredentials({ url, username, password, w
await fs.writeFile(`${location}/config.json`, payload);
return location;
}
export function getDomain(domain: string): string {
if (domain) {
return domain?.replace('https://', '').replace('http://', '');
} else {
return '';
}
}
export async function isDomainConfigured({
id,
fqdn,
checkOwn = false,
remoteIpAddress = undefined
}: {
id: string;
fqdn: string;
checkOwn?: boolean;
remoteIpAddress?: string;
}): Promise<boolean> {
const domain = getDomain(fqdn);
const nakedDomain = domain.replace('www.', '');
const foundApp = await prisma.application.findFirst({
where: {
OR: [
{ fqdn: { endsWith: `//${nakedDomain}` } },
{ fqdn: { endsWith: `//www.${nakedDomain}` } },
{ dockerComposeConfiguration: { contains: `//${nakedDomain}` } },
{ dockerComposeConfiguration: { contains: `//www.${nakedDomain}` } }
],
id: { not: id },
destinationDocker: {
remoteIpAddress
}
},
select: { fqdn: true }
});
const foundService = await prisma.service.findFirst({
where: {
OR: [
{ fqdn: { endsWith: `//${nakedDomain}` } },
{ fqdn: { endsWith: `//www.${nakedDomain}` } }
],
id: { not: checkOwn ? undefined : id },
destinationDocker: {
remoteIpAddress
}
},
select: { fqdn: true }
});
const coolifyFqdn = await prisma.setting.findFirst({
where: {
OR: [
{ fqdn: { endsWith: `//${nakedDomain}` } },
{ fqdn: { endsWith: `//www.${nakedDomain}` } }
],
id: { not: id }
},
select: { fqdn: true }
});
return !!(foundApp || foundService || coolifyFqdn);
}
export async function checkExposedPort({
id,
configuredPort,
exposePort,
engine,
remoteEngine,
remoteIpAddress
}: {
id: string;
configuredPort?: number;
exposePort: number;
engine: string;
remoteEngine: boolean;
remoteIpAddress?: string;
}) {
if (exposePort < 1024 || exposePort > 65535) {
throw { status: 500, message: `Exposed Port needs to be between 1024 and 65535.` };
}
if (configuredPort) {
if (configuredPort !== exposePort) {
const availablePort = await getFreeExposedPort(
id,
exposePort,
engine,
remoteEngine,
remoteIpAddress
);
if (availablePort.toString() !== exposePort.toString()) {
throw { status: 500, message: `Port ${exposePort} is already in use.` };
}
}
} else {
const availablePort = await getFreeExposedPort(
id,
exposePort,
engine,
remoteEngine,
remoteIpAddress
);
if (availablePort.toString() !== exposePort.toString()) {
throw { status: 500, message: `Port ${exposePort} is already in use.` };
}
}
}
export async function getFreeExposedPort(id, exposePort, engine, remoteEngine, remoteIpAddress) {
const { default: checkPort } = await import('is-port-reachable');
if (remoteEngine) {
const applicationUsed = await (
await prisma.application.findMany({
where: {
exposePort: { not: null },
id: { not: id },
destinationDocker: { remoteIpAddress }
},
select: { exposePort: true }
})
).map((a) => a.exposePort);
const serviceUsed = await (
await prisma.service.findMany({
where: {
exposePort: { not: null },
id: { not: id },
destinationDocker: { remoteIpAddress }
},
select: { exposePort: true }
})
).map((a) => a.exposePort);
const usedPorts = [...applicationUsed, ...serviceUsed];
if (usedPorts.includes(exposePort)) {
return false;
}
const found = await checkPort(exposePort, { host: remoteIpAddress });
if (!found) {
return exposePort;
}
return false;
} else {
const applicationUsed = await (
await prisma.application.findMany({
where: { exposePort: { not: null }, id: { not: id }, destinationDocker: { engine } },
select: { exposePort: true }
})
).map((a) => a.exposePort);
const serviceUsed = await (
await prisma.service.findMany({
where: { exposePort: { not: null }, id: { not: id }, destinationDocker: { engine } },
select: { exposePort: true }
})
).map((a) => a.exposePort);
const usedPorts = [...applicationUsed, ...serviceUsed];
if (usedPorts.includes(exposePort)) {
return false;
}
const found = await checkPort(exposePort, { host: 'localhost' });
if (!found) {
return exposePort;
}
return false;
}
}
export async function checkDomainsIsValidInDNS({ hostname, fqdn, dualCerts }): Promise<any> {
const { isIP } = await import('is-ip');
const domain = getDomain(fqdn);
const domainDualCert = domain.includes('www.') ? domain.replace('www.', '') : `www.${domain}`;
const { DNSServers } = await listSettings();
if (DNSServers) {
dns.setServers([...DNSServers.split(',')]);
}
let resolves = [];
try {
if (isIP(hostname)) {
resolves = [hostname];
} else {
resolves = await dns.resolve4(hostname);
}
} catch (error) {
throw { status: 500, message: `Could not determine IP address for ${hostname}.` };
}
if (dualCerts) {
try {
const ipDomain = await dns.resolve4(domain);
const ipDomainDualCert = await dns.resolve4(domainDualCert);
let ipDomainFound = false;
let ipDomainDualCertFound = false;
for (const ip of ipDomain) {
if (resolves.includes(ip)) {
ipDomainFound = true;
}
}
for (const ip of ipDomainDualCert) {
if (resolves.includes(ip)) {
ipDomainDualCertFound = true;
}
}
if (ipDomainFound && ipDomainDualCertFound) return { status: 200 };
throw {
status: 500,
message: `DNS not set correctly or propogated.<br>Please check your DNS settings.`
};
} catch (error) {
throw {
status: 500,
message: `DNS not set correctly or propogated.<br>Please check your DNS settings.`
};
}
} else {
try {
const ipDomain = await dns.resolve4(domain);
let ipDomainFound = false;
for (const ip of ipDomain) {
if (resolves.includes(ip)) {
ipDomainFound = true;
}
}
if (ipDomainFound) return { status: 200 };
throw {
status: 500,
message: `DNS not set correctly or propogated.<br>Please check your DNS settings.`
};
} catch (error) {
throw {
status: 500,
message: `DNS not set correctly or propogated.<br>Please check your DNS settings.`
};
}
}
}
export const setDefaultConfiguration = async (data: any) => {
let {
buildPack,
port,
installCommand,
startCommand,
buildCommand,
publishDirectory,
baseDirectory,
dockerFileLocation,
dockerComposeFileLocation,
denoMainFile
} = data;
//@ts-ignore
const template = scanningTemplates[buildPack];
if (!port) {
port = template?.port || 3000;
if (buildPack === 'static') port = 80;
else if (buildPack === 'node') port = 3000;
else if (buildPack === 'php') port = 80;
else if (buildPack === 'python') port = 8000;
}
if (!installCommand && buildPack !== 'static' && buildPack !== 'laravel')
installCommand = template?.installCommand || 'yarn install';
if (!startCommand && buildPack !== 'static' && buildPack !== 'laravel')
startCommand = template?.startCommand || 'yarn start';
if (!buildCommand && buildPack !== 'static' && buildPack !== 'laravel')
buildCommand = template?.buildCommand || null;
if (!publishDirectory) publishDirectory = template?.publishDirectory || null;
if (baseDirectory) {
if (!baseDirectory.startsWith('/')) baseDirectory = `/${baseDirectory}`;
if (baseDirectory.endsWith('/') && baseDirectory !== '/')
baseDirectory = baseDirectory.slice(0, -1);
}
if (dockerFileLocation) {
if (!dockerFileLocation.startsWith('/')) dockerFileLocation = `/${dockerFileLocation}`;
if (dockerFileLocation.endsWith('/')) dockerFileLocation = dockerFileLocation.slice(0, -1);
} else {
dockerFileLocation = '/Dockerfile';
}
if (dockerComposeFileLocation) {
if (!dockerComposeFileLocation.startsWith('/'))
dockerComposeFileLocation = `/${dockerComposeFileLocation}`;
if (dockerComposeFileLocation.endsWith('/'))
dockerComposeFileLocation = dockerComposeFileLocation.slice(0, -1);
} else {
dockerComposeFileLocation = '/Dockerfile';
}
if (!denoMainFile) {
denoMainFile = 'main.ts';
}
return {
buildPack,
port,
installCommand,
startCommand,
buildCommand,
publishDirectory,
baseDirectory,
dockerFileLocation,
dockerComposeFileLocation,
denoMainFile
};
};
export const scanningTemplates = {
'@sveltejs/kit': {
buildPack: 'nodejs'
},
astro: {
buildPack: 'astro'
},
'@11ty/eleventy': {
buildPack: 'eleventy'
},
svelte: {
buildPack: 'svelte'
},
'@nestjs/core': {
buildPack: 'nestjs'
},
next: {
buildPack: 'nextjs'
},
nuxt: {
buildPack: 'nuxtjs'
},
'react-scripts': {
buildPack: 'react'
},
'parcel-bundler': {
buildPack: 'static'
},
'@vue/cli-service': {
buildPack: 'vuejs'
},
vuejs: {
buildPack: 'vuejs'
},
gatsby: {
buildPack: 'gatsby'
},
'preact-cli': {
buildPack: 'react'
}
};

View File

@@ -16,7 +16,7 @@ export function createContext({ req }: CreateFastifyContextOptions) {
if (token) {
user = jwt.verify(token, env.COOLIFY_SECRET_KEY) as User;
}
return { user };
return { user, hostname: req.hostname };
}
export type Context = inferAsyncReturnType<typeof createContext>;

View File

@@ -10,11 +10,306 @@ import {
formatLabelsOnDocker,
removeContainer
} from '../../../lib/docker';
import { deployApplication, generateConfigHash, getApplicationFromDB } from './lib';
import {
deployApplication,
generateConfigHash,
getApplicationFromDB,
setDefaultBaseImage
} from './lib';
import cuid from 'cuid';
import { createDirectories, saveDockerRegistryCredentials } from '../../../lib/common';
import {
checkDomainsIsValidInDNS,
checkExposedPort,
createDirectories,
decrypt,
encrypt,
getDomain,
isDev,
isDomainConfigured,
saveDockerRegistryCredentials,
setDefaultConfiguration
} from '../../../lib/common';
export const applicationsRouter = router({
getStorages: privateProcedure
.input(
z.object({
id: z.string()
})
)
.query(async ({ input }) => {
const { id } = input;
const persistentStorages = await prisma.applicationPersistentStorage.findMany({
where: { applicationId: id }
});
return {
success: true,
data: {
persistentStorages
}
};
}),
deleteStorage: privateProcedure
.input(
z.object({
id: z.string(),
path: z.string()
})
)
.mutation(async ({ input }) => {
const { id, path } = input;
await prisma.applicationPersistentStorage.deleteMany({ where: { applicationId: id, path } });
}),
updateStorage: privateProcedure
.input(
z.object({
id: z.string(),
path: z.string(),
storageId: z.string(),
newStorage: z.boolean().optional().default(false)
})
)
.mutation(async ({ input }) => {
const { id, path, newStorage, storageId } = input;
if (newStorage) {
await prisma.applicationPersistentStorage.create({
data: { path, application: { connect: { id } } }
});
} else {
await prisma.applicationPersistentStorage.update({
where: { id: storageId },
data: { path }
});
}
}),
deleteSecret: privateProcedure
.input(
z.object({
id: z.string(),
name: z.string()
})
)
.mutation(async ({ input }) => {
const { id, name } = input;
await prisma.secret.deleteMany({ where: { applicationId: id, name } });
}),
updateSecret: privateProcedure
.input(
z.object({
id: z.string(),
name: z.string(),
value: z.string(),
isBuildSecret: z.boolean().optional().default(false),
isPreview: z.boolean().optional().default(false)
})
)
.mutation(async ({ input }) => {
const { id, name, value, isBuildSecret, isPreview } = input;
console.log({ isBuildSecret });
await prisma.secret.updateMany({
where: { applicationId: id, name, isPRMRSecret: isPreview },
data: { value: encrypt(value.trim()), isBuildSecret }
});
}),
newSecret: privateProcedure
.input(
z.object({
id: z.string(),
name: z.string(),
value: z.string(),
isBuildSecret: z.boolean().optional().default(false)
})
)
.mutation(async ({ input }) => {
const { id, name, value, isBuildSecret } = input;
const found = await prisma.secret.findMany({ where: { applicationId: id, name } });
if (found.length > 0) {
throw { message: 'Secret already exists.' };
}
await prisma.secret.create({
data: {
name,
value: encrypt(value.trim()),
isBuildSecret,
isPRMRSecret: false,
application: { connect: { id } }
}
});
await prisma.secret.create({
data: {
name,
value: encrypt(value.trim()),
isBuildSecret,
isPRMRSecret: true,
application: { connect: { id } }
}
});
}),
getSecrets: privateProcedure
.input(
z.object({
id: z.string()
})
)
.query(async ({ input }) => {
const { id } = input;
let secrets = await prisma.secret.findMany({
where: { applicationId: id, isPRMRSecret: false },
orderBy: { createdAt: 'asc' }
});
let previewSecrets = await prisma.secret.findMany({
where: { applicationId: id, isPRMRSecret: true },
orderBy: { createdAt: 'asc' }
});
secrets = secrets.map((secret) => {
secret.value = decrypt(secret.value);
return secret;
});
previewSecrets = previewSecrets.map((secret) => {
secret.value = decrypt(secret.value);
return secret;
});
return {
success: true,
data: {
previewSecrets: previewSecrets.sort((a, b) => {
return ('' + a.name).localeCompare(b.name);
}),
secrets: secrets.sort((a, b) => {
return ('' + a.name).localeCompare(b.name);
})
}
};
}),
checkDomain: privateProcedure
.input(
z.object({
id: z.string(),
domain: z.string()
})
)
.query(async ({ input, ctx }) => {
const { id, domain } = input;
const {
fqdn,
settings: { dualCerts }
} = await prisma.application.findUnique({ where: { id }, include: { settings: true } });
return await checkDomainsIsValidInDNS({ hostname: domain, fqdn, dualCerts });
}),
checkDNS: privateProcedure
.input(
z.object({
id: z.string(),
fqdn: z.string(),
forceSave: z.boolean(),
dualCerts: z.boolean(),
exposePort: z.number().nullable().optional()
})
)
.mutation(async ({ input, ctx }) => {
let { id, exposePort, fqdn, forceSave, dualCerts } = input;
if (!fqdn) {
return {};
} else {
fqdn = fqdn.toLowerCase();
}
if (exposePort) exposePort = Number(exposePort);
const {
destinationDocker: { engine, remoteIpAddress, remoteEngine },
exposePort: configuredPort
} = await prisma.application.findUnique({
where: { id },
include: { destinationDocker: true }
});
const { isDNSCheckEnabled } = await prisma.setting.findFirst({});
const found = await isDomainConfigured({ id, fqdn, remoteIpAddress });
if (found) {
throw {
status: 500,
message: `Domain ${getDomain(fqdn).replace('www.', '')} is already in use!`
};
}
if (exposePort)
await checkExposedPort({
id,
configuredPort,
exposePort,
engine,
remoteEngine,
remoteIpAddress
});
if (isDNSCheckEnabled && !isDev && !forceSave) {
let hostname = ctx.hostname.split(':')[0];
if (remoteEngine) hostname = remoteIpAddress;
return await checkDomainsIsValidInDNS({ hostname, fqdn, dualCerts });
}
}),
saveSettings: privateProcedure
.input(
z.object({
id: z.string(),
previews: z.boolean().optional(),
debug: z.boolean().optional(),
dualCerts: z.boolean().optional(),
isBot: z.boolean().optional(),
autodeploy: z.boolean().optional(),
isDBBranching: z.boolean().optional(),
isCustomSSL: z.boolean().optional()
})
)
.mutation(async ({ ctx, input }) => {
const { id, debug, previews, dualCerts, autodeploy, isBot, isDBBranching, isCustomSSL } =
input;
await prisma.application.update({
where: { id },
data: {
fqdn: isBot ? null : undefined,
settings: {
update: { debug, previews, dualCerts, autodeploy, isBot, isDBBranching, isCustomSSL }
}
},
include: { destinationDocker: true }
});
}),
getImages: privateProcedure
.input(z.object({ buildPack: z.string(), deploymentType: z.string().nullable() }))
.query(async ({ ctx, input }) => {
const { buildPack, deploymentType } = input;
let publishDirectory = undefined;
let port = undefined;
const { baseImage, baseBuildImage, baseBuildImages, baseImages } = setDefaultBaseImage(
buildPack,
deploymentType
);
if (buildPack === 'nextjs') {
if (deploymentType === 'static') {
publishDirectory = 'out';
port = '80';
} else {
publishDirectory = '';
port = '3000';
}
}
if (buildPack === 'nuxtjs') {
if (deploymentType === 'static') {
publishDirectory = 'dist';
port = '80';
} else {
publishDirectory = '';
port = '3000';
}
}
return {
success: true,
data: { baseImage, baseImages, baseBuildImage, baseBuildImages, publishDirectory, port }
};
}),
getApplicationById: privateProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
@@ -32,17 +327,140 @@ export const applicationsRouter = router({
save: privateProcedure
.input(
z.object({
id: z.string()
id: z.string(),
name: z.string(),
buildPack: z.string(),
fqdn: z.string().nullable().optional(),
port: z.number(),
exposePort: z.number().nullable().optional(),
installCommand: z.string(),
buildCommand: z.string(),
startCommand: z.string(),
baseDirectory: z.string().nullable().optional(),
publishDirectory: z.string().nullable().optional(),
pythonWSGI: z.string().nullable().optional(),
pythonModule: z.string().nullable().optional(),
pythonVariable: z.string().nullable().optional(),
dockerFileLocation: z.string(),
denoMainFile: z.string().nullable().optional(),
denoOptions: z.string().nullable().optional(),
gitCommitHash: z.string(),
baseImage: z.string(),
baseBuildImage: z.string(),
deploymentType: z.string().nullable().optional(),
baseDatabaseBranch: z.string().nullable().optional(),
dockerComposeFile: z.string().nullable().optional(),
dockerComposeFileLocation: z.string().nullable().optional(),
dockerComposeConfiguration: z.string().nullable().optional(),
simpleDockerfile: z.string().nullable().optional(),
dockerRegistryImageName: z.string().nullable().optional()
})
)
.mutation(async ({ ctx, input }) => {
const { id } = input;
const teamId = ctx.user?.teamId;
// const buildId = await deployApplication(id, teamId);
return {
// buildId
};
.mutation(async ({ input }) => {
let {
id,
name,
buildPack,
fqdn,
port,
exposePort,
installCommand,
buildCommand,
startCommand,
baseDirectory,
publishDirectory,
pythonWSGI,
pythonModule,
pythonVariable,
dockerFileLocation,
denoMainFile,
denoOptions,
gitCommitHash,
baseImage,
baseBuildImage,
deploymentType,
baseDatabaseBranch,
dockerComposeFile,
dockerComposeFileLocation,
dockerComposeConfiguration,
simpleDockerfile,
dockerRegistryImageName
} = input;
const {
destinationDocker: { engine, remoteEngine, remoteIpAddress },
exposePort: configuredPort
} = await prisma.application.findUnique({
where: { id },
include: { destinationDocker: true }
});
if (exposePort)
await checkExposedPort({
id,
configuredPort,
exposePort,
engine,
remoteEngine,
remoteIpAddress
});
if (denoOptions) denoOptions = denoOptions.trim();
const defaultConfiguration = await setDefaultConfiguration({
buildPack,
port,
installCommand,
startCommand,
buildCommand,
publishDirectory,
baseDirectory,
dockerFileLocation,
dockerComposeFileLocation,
denoMainFile
});
if (baseDatabaseBranch) {
await prisma.application.update({
where: { id },
data: {
name,
fqdn,
exposePort,
pythonWSGI,
pythonModule,
pythonVariable,
denoOptions,
baseImage,
gitCommitHash,
baseBuildImage,
deploymentType,
dockerComposeFile,
dockerComposeConfiguration,
simpleDockerfile,
dockerRegistryImageName,
...defaultConfiguration,
connectedDatabase: { update: { hostedDatabaseDBName: baseDatabaseBranch } }
}
});
} else {
await prisma.application.update({
where: { id },
data: {
name,
fqdn,
exposePort,
pythonWSGI,
pythonModule,
gitCommitHash,
pythonVariable,
denoOptions,
baseImage,
baseBuildImage,
deploymentType,
dockerComposeFile,
dockerComposeConfiguration,
simpleDockerfile,
dockerRegistryImageName,
...defaultConfiguration
}
});
}
}),
status: privateProcedure.input(z.object({ id: z.string() })).query(async ({ ctx, input }) => {
const id: string = input.id;
@@ -210,26 +628,26 @@ export const applicationsRouter = router({
if (pullmergeRequestId) {
const isSecretFound = secrets.filter((s) => s.name === secret.name && s.isPRMRSecret);
if (isSecretFound.length > 0) {
if (isSecretFound[0].value.includes('\\n') || isSecretFound[0].value.includes("'")) {
envs.push(`${secret.name}=${isSecretFound[0].value}`);
} else {
envs.push(`${secret.name}='${isSecretFound[0].value}'`);
}
if (isSecretFound[0].value.includes('\\n') || isSecretFound[0].value.includes("'")) {
envs.push(`${secret.name}=${isSecretFound[0].value}`);
} else {
envs.push(`${secret.name}='${isSecretFound[0].value}'`);
}
} else {
if (secret.value.includes('\\n')|| secret.value.includes("'")) {
envs.push(`${secret.name}=${secret.value}`);
} else {
envs.push(`${secret.name}='${secret.value}'`);
}
if (secret.value.includes('\\n') || secret.value.includes("'")) {
envs.push(`${secret.name}=${secret.value}`);
} else {
envs.push(`${secret.name}='${secret.value}'`);
}
}
} else {
if (!secret.isPRMRSecret) {
if (secret.value.includes('\\n')|| secret.value.includes("'")) {
envs.push(`${secret.name}=${secret.value}`);
} else {
envs.push(`${secret.name}='${secret.value}'`);
}
}
if (secret.value.includes('\\n') || secret.value.includes("'")) {
envs.push(`${secret.name}=${secret.value}`);
} else {
envs.push(`${secret.name}='${secret.value}'`);
}
}
}
});
}