2022-02-10 15:47:44 +01:00
|
|
|
import child from 'child_process';
|
|
|
|
import util from 'util';
|
|
|
|
import { dev } from '$app/env';
|
|
|
|
import * as Sentry from '@sentry/node';
|
2022-03-31 15:11:51 +02:00
|
|
|
import { uniqueNamesGenerator, adjectives, colors, animals } from 'unique-names-generator';
|
|
|
|
import type { Config } from 'unique-names-generator';
|
2022-02-10 15:47:44 +01:00
|
|
|
|
|
|
|
import * as db from '$lib/database';
|
|
|
|
import { buildLogQueue } from './queues';
|
|
|
|
|
|
|
|
import { version as currentVersion } from '../../package.json';
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
import Cookie from 'cookie';
|
2022-02-14 09:28:37 +01:00
|
|
|
import os from 'os';
|
2022-04-05 20:11:19 +02:00
|
|
|
import type { RequestEvent } from '@sveltejs/kit/types/internal';
|
|
|
|
import type { Job } from 'bullmq';
|
2022-02-10 15:47:44 +01:00
|
|
|
|
|
|
|
try {
|
2022-02-14 09:28:56 +01:00
|
|
|
if (!dev) {
|
2022-02-10 15:47:44 +01:00
|
|
|
Sentry.init({
|
|
|
|
dsn: process.env['COOLIFY_SENTRY_DSN'],
|
|
|
|
tracesSampleRate: 0,
|
2022-02-14 09:28:56 +01:00
|
|
|
environment: 'production',
|
2022-02-14 09:28:37 +01:00
|
|
|
debug: true,
|
|
|
|
release: currentVersion,
|
|
|
|
initialScope: {
|
|
|
|
tags: {
|
|
|
|
appId: process.env['COOLIFY_APP_ID'],
|
|
|
|
'os.arch': os.arch(),
|
|
|
|
'os.platform': os.platform(),
|
|
|
|
'os.release': os.release()
|
|
|
|
}
|
|
|
|
}
|
2022-02-10 15:47:44 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.log('Could not initialize Sentry, no worries.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const customConfig: Config = {
|
|
|
|
dictionaries: [adjectives, colors, animals],
|
|
|
|
style: 'capital',
|
|
|
|
separator: ' ',
|
|
|
|
length: 3
|
|
|
|
};
|
|
|
|
|
|
|
|
export const version = currentVersion;
|
|
|
|
export const asyncExecShell = util.promisify(child.exec);
|
2022-04-05 20:11:19 +02:00
|
|
|
export const asyncSleep = (delay: number): Promise<unknown> =>
|
|
|
|
new Promise((resolve) => setTimeout(resolve, delay));
|
2022-02-10 15:47:44 +01:00
|
|
|
export const sentry = Sentry;
|
|
|
|
|
2022-04-05 20:11:19 +02:00
|
|
|
export const uniqueName = (): string => uniqueNamesGenerator(customConfig);
|
|
|
|
|
|
|
|
export const saveBuildLog = async ({
|
|
|
|
line,
|
|
|
|
buildId,
|
|
|
|
applicationId
|
|
|
|
}: {
|
|
|
|
line: string;
|
|
|
|
buildId: string;
|
|
|
|
applicationId: string;
|
|
|
|
}): Promise<Job> => {
|
2022-04-08 10:54:40 +02:00
|
|
|
if (line) {
|
|
|
|
if (line.includes('ghs_')) {
|
|
|
|
const regex = /ghs_.*@/g;
|
|
|
|
line = line.replace(regex, '<SENSITIVE_DATA_DELETED>@');
|
|
|
|
}
|
|
|
|
const addTimestamp = `${generateTimestamp()} ${line}`;
|
|
|
|
return await buildLogQueue.add(buildId, { buildId, line: addTimestamp, applicationId });
|
2022-04-05 10:21:40 +02:00
|
|
|
}
|
2022-02-10 15:47:44 +01:00
|
|
|
};
|
|
|
|
|
2022-04-05 20:11:19 +02:00
|
|
|
export const getTeam = (event: RequestEvent): string | null => {
|
2022-02-18 13:59:23 +01:00
|
|
|
const cookies = Cookie.parse(event.request.headers.get('cookie'));
|
2022-03-15 17:04:15 +01:00
|
|
|
if (cookies?.teamId) {
|
2022-02-10 15:47:44 +01:00
|
|
|
return cookies.teamId;
|
2022-03-15 17:21:18 +01:00
|
|
|
} else if (event.locals.session.data?.teamId) {
|
2022-02-10 15:47:44 +01:00
|
|
|
return event.locals.session.data.teamId;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-04-05 20:11:19 +02:00
|
|
|
export const getUserDetails = async (
|
|
|
|
event: RequestEvent,
|
|
|
|
isAdminRequired = true
|
|
|
|
): Promise<{
|
|
|
|
teamId: string;
|
|
|
|
userId: string;
|
|
|
|
permission: string;
|
|
|
|
status: number;
|
|
|
|
body: { message: string };
|
|
|
|
}> => {
|
2022-02-10 15:47:44 +01:00
|
|
|
const teamId = getTeam(event);
|
2022-04-02 23:48:56 +02:00
|
|
|
const userId = event?.locals?.session?.data?.userId || null;
|
2022-02-10 15:47:44 +01:00
|
|
|
const { permission = 'read' } = await db.prisma.permission.findFirst({
|
|
|
|
where: { teamId, userId },
|
|
|
|
select: { permission: true },
|
|
|
|
rejectOnNotFound: true
|
|
|
|
});
|
|
|
|
const payload = {
|
|
|
|
teamId,
|
|
|
|
userId,
|
|
|
|
permission,
|
|
|
|
status: 200,
|
|
|
|
body: {
|
|
|
|
message: 'OK'
|
|
|
|
}
|
|
|
|
};
|
2022-04-07 15:23:32 +02:00
|
|
|
|
2022-02-10 15:47:44 +01:00
|
|
|
if (isAdminRequired && permission !== 'admin' && permission !== 'owner') {
|
|
|
|
payload.status = 401;
|
|
|
|
payload.body.message =
|
|
|
|
'You do not have permission to do this. \nAsk an admin to modify your permissions.';
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload;
|
|
|
|
};
|
|
|
|
|
2022-04-05 20:11:19 +02:00
|
|
|
export function getEngine(engine: string): string {
|
2022-02-26 15:08:26 +01:00
|
|
|
return engine === '/var/run/docker.sock' ? 'unix:///var/run/docker.sock' : engine;
|
2022-02-10 15:47:44 +01:00
|
|
|
}
|
|
|
|
|
2022-04-05 20:11:19 +02:00
|
|
|
export async function removeContainer(id: string, engine: string): Promise<void> {
|
2022-02-10 15:47:44 +01:00
|
|
|
const host = getEngine(engine);
|
|
|
|
try {
|
|
|
|
const { stdout } = await asyncExecShell(
|
|
|
|
`DOCKER_HOST=${host} docker inspect --format '{{json .State}}' ${id}`
|
|
|
|
);
|
|
|
|
if (JSON.parse(stdout).Running) {
|
|
|
|
await asyncExecShell(`DOCKER_HOST=${host} docker stop -t 0 ${id}`);
|
|
|
|
await asyncExecShell(`DOCKER_HOST=${host} docker rm ${id}`);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 20:11:19 +02:00
|
|
|
export const removeDestinationDocker = async ({
|
|
|
|
id,
|
|
|
|
engine
|
|
|
|
}: {
|
|
|
|
id: string;
|
|
|
|
engine: string;
|
|
|
|
}): Promise<void> => {
|
2022-02-10 15:47:44 +01:00
|
|
|
return await removeContainer(id, engine);
|
|
|
|
};
|
|
|
|
|
2022-04-05 20:11:19 +02:00
|
|
|
export const createDirectories = async ({
|
|
|
|
repository,
|
|
|
|
buildId
|
|
|
|
}: {
|
|
|
|
repository: string;
|
|
|
|
buildId: string;
|
|
|
|
}): Promise<{ workdir: string; repodir: string }> => {
|
2022-03-31 15:42:15 +02:00
|
|
|
const repodir = `/tmp/build-sources/${repository}/`;
|
|
|
|
const workdir = `/tmp/build-sources/${repository}/${buildId}`;
|
2022-02-10 15:47:44 +01:00
|
|
|
|
|
|
|
await asyncExecShell(`mkdir -p ${workdir}`);
|
|
|
|
|
|
|
|
return {
|
|
|
|
workdir,
|
|
|
|
repodir
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-04-05 20:11:19 +02:00
|
|
|
export function generateTimestamp(): string {
|
2022-02-10 15:47:44 +01:00
|
|
|
return `${dayjs().format('HH:mm:ss.SSS')} `;
|
|
|
|
}
|
|
|
|
|
2022-04-05 20:11:19 +02:00
|
|
|
export function getDomain(domain: string): string {
|
2022-02-10 15:47:44 +01:00
|
|
|
return domain?.replace('https://', '').replace('http://', '');
|
|
|
|
}
|