lasthourcloud/src/lib/database/applications.ts

262 lines
6.8 KiB
TypeScript
Raw Normal View History

2022-02-10 15:47:44 +01:00
import { decrypt, encrypt } from '$lib/crypto';
import { asyncExecShell, getEngine } from '$lib/common';
2022-02-10 15:47:44 +01:00
import { getDomain, removeDestinationDocker } from '$lib/common';
import { prisma } from './common';
export async function listApplications(teamId) {
return await prisma.application.findMany({ where: { teams: { some: { id: teamId } } } });
}
export async function newApplication({ name, teamId }) {
return await prisma.application.create({
data: {
name,
teams: { connect: { id: teamId } },
settings: { create: { debug: false, previews: false } }
}
});
}
export async function importApplication({
name,
teamId,
fqdn,
port,
buildCommand,
startCommand,
installCommand
}) {
return await prisma.application.create({
data: {
name,
fqdn,
port,
buildCommand,
startCommand,
installCommand,
teams: { connect: { id: teamId } }
}
});
}
export async function removeApplication({ id, teamId }) {
const { fqdn, destinationDockerId, destinationDocker } = await prisma.application.findUnique({
where: { id },
include: { destinationDocker: true }
});
const domain = getDomain(fqdn);
if (destinationDockerId) {
const host = getEngine(destinationDocker.engine);
const { stdout: containers } = await asyncExecShell(
`DOCKER_HOST=${host} docker ps -a --filter network=${destinationDocker.network} --filter name=${id} --format '{{json .}}'`
);
if (containers) {
const containersArray = containers.trim().split('\n');
for (const container of containersArray) {
const containerObj = JSON.parse(container);
const id = containerObj.ID;
const preview = containerObj.Image.split('-')[1];
await removeDestinationDocker({ id, engine: destinationDocker.engine });
}
}
}
await prisma.applicationSettings.deleteMany({ where: { application: { id } } });
await prisma.buildLog.deleteMany({ where: { applicationId: id } });
await prisma.secret.deleteMany({ where: { applicationId: id } });
await prisma.application.deleteMany({ where: { id, teams: { some: { id: teamId } } } });
}
export async function getApplicationWebhook({ projectId, branch }) {
try {
2022-03-11 22:48:55 +01:00
let application = await prisma.application.findFirst({
2022-03-11 22:36:21 +01:00
where: { projectId, branch, settings: { autodeploy: true } },
2022-02-10 15:47:44 +01:00
include: {
destinationDocker: true,
settings: true,
gitSource: { include: { githubApp: true, gitlabApp: true } },
secrets: true
}
});
2022-03-15 17:22:28 +01:00
if (application?.gitSource?.githubApp?.clientSecret) {
2022-03-11 22:48:55 +01:00
application.gitSource.githubApp.clientSecret = decrypt(
application.gitSource.githubApp.clientSecret
);
}
2022-03-15 17:22:28 +01:00
if (application?.gitSource?.githubApp?.webhookSecret) {
2022-03-11 22:48:55 +01:00
application.gitSource.githubApp.webhookSecret = decrypt(
application.gitSource.githubApp.webhookSecret
);
}
2022-03-15 17:22:28 +01:00
if (application?.gitSource?.githubApp?.privateKey) {
2022-03-11 22:48:55 +01:00
application.gitSource.githubApp.privateKey = decrypt(
application.gitSource.githubApp.privateKey
);
}
if (application?.gitSource?.gitlabApp?.appSecret) {
application.gitSource.gitlabApp.appSecret = decrypt(
application.gitSource.gitlabApp.appSecret
);
}
if (application?.gitSource?.gitlabApp?.webhookToken) {
application.gitSource.gitlabApp.webhookToken = decrypt(
application.gitSource.gitlabApp.webhookToken
);
}
if (application?.secrets.length > 0) {
application.secrets = application.secrets.map((s) => {
s.value = decrypt(s.value);
return s;
});
2022-02-10 15:47:44 +01:00
}
2022-03-11 22:48:55 +01:00
return { ...application };
2022-02-10 15:47:44 +01:00
} catch (e) {
throw { status: 404, body: { message: e.message } };
}
}
export async function getApplicationById({ id }) {
const body = await prisma.application.findFirst({
where: { id },
include: { destinationDocker: true }
});
return { ...body };
}
2022-02-10 15:47:44 +01:00
export async function getApplication({ id, teamId }) {
let body = await prisma.application.findFirst({
where: { id, teams: { some: { id: teamId } } },
include: {
destinationDocker: true,
settings: true,
gitSource: { include: { githubApp: true, gitlabApp: true } },
secrets: true
}
});
2022-03-01 15:30:39 +01:00
if (body?.gitSource?.githubApp?.clientSecret) {
2022-02-10 15:47:44 +01:00
body.gitSource.githubApp.clientSecret = decrypt(body.gitSource.githubApp.clientSecret);
}
2022-03-01 15:30:39 +01:00
if (body?.gitSource?.githubApp?.webhookSecret) {
2022-02-10 15:47:44 +01:00
body.gitSource.githubApp.webhookSecret = decrypt(body.gitSource.githubApp.webhookSecret);
}
2022-03-01 15:30:39 +01:00
if (body?.gitSource?.githubApp?.privateKey) {
2022-02-10 15:47:44 +01:00
body.gitSource.githubApp.privateKey = decrypt(body.gitSource.githubApp.privateKey);
}
if (body?.gitSource?.gitlabApp?.appSecret) {
body.gitSource.gitlabApp.appSecret = decrypt(body.gitSource.gitlabApp.appSecret);
}
if (body?.secrets.length > 0) {
body.secrets = body.secrets.map((s) => {
s.value = decrypt(s.value);
return s;
});
}
return { ...body };
}
2022-03-11 22:36:21 +01:00
export async function configureGitRepository({
id,
repository,
branch,
projectId,
webhookToken,
autodeploy
}) {
2022-02-10 15:47:44 +01:00
if (webhookToken) {
const encryptedWebhookToken = encrypt(webhookToken);
2022-03-11 22:36:21 +01:00
await prisma.application.update({
2022-02-10 15:47:44 +01:00
where: { id },
data: {
repository,
branch,
projectId,
2022-03-11 22:36:21 +01:00
gitSource: { update: { gitlabApp: { update: { webhookToken: encryptedWebhookToken } } } },
settings: { update: { autodeploy } }
2022-02-10 15:47:44 +01:00
}
});
} else {
2022-03-11 22:36:21 +01:00
await prisma.application.update({
2022-02-10 15:47:44 +01:00
where: { id },
2022-03-11 22:36:21 +01:00
data: { repository, branch, projectId, settings: { update: { autodeploy } } }
2022-02-10 15:47:44 +01:00
});
}
2022-03-11 22:36:21 +01:00
if (!autodeploy) {
const applications = await prisma.application.findMany({ where: { branch, projectId } });
for (const application of applications) {
await prisma.applicationSettings.updateMany({
where: { applicationId: application.id },
data: { autodeploy: false }
});
}
}
2022-02-10 15:47:44 +01:00
}
export async function configureBuildPack({ id, buildPack }) {
return await prisma.application.update({ where: { id }, data: { buildPack } });
}
export async function configureApplication({
id,
buildPack,
name,
fqdn,
port,
installCommand,
buildCommand,
startCommand,
baseDirectory,
publishDirectory
}) {
return await prisma.application.update({
where: { id },
data: {
buildPack,
fqdn,
port,
installCommand,
buildCommand,
startCommand,
baseDirectory,
publishDirectory,
name
}
});
}
2022-03-11 22:36:21 +01:00
export async function checkDoubleBranch(branch, projectId) {
const applications = await prisma.application.findMany({ where: { branch, projectId } });
return applications.length > 1;
}
export async function setApplicationSettings({ id, debug, previews, dualCerts, autodeploy }) {
2022-02-10 15:47:44 +01:00
return await prisma.application.update({
where: { id },
2022-03-11 22:36:21 +01:00
data: { settings: { update: { debug, previews, dualCerts, autodeploy } } },
2022-02-10 15:47:44 +01:00
include: { destinationDocker: true }
});
}
export async function createBuild({
id,
applicationId,
destinationDockerId,
gitSourceId,
githubAppId,
gitlabAppId,
type
}) {
return await prisma.build.create({
data: {
id,
applicationId,
destinationDockerId,
gitSourceId,
githubAppId,
gitlabAppId,
status: 'running',
type
}
});
}