180 lines
5.5 KiB
TypeScript
Raw Normal View History

2022-02-10 15:47:44 +01:00
import { decrypt, encrypt } from '$lib/crypto';
import cuid from 'cuid';
import { generatePassword } from '.';
import { prisma } from './common';
2022-02-10 15:47:44 +01:00
export async function listServices(teamId) {
return await prisma.service.findMany({ where: { teams: { some: { id: teamId } } } });
}
export async function newService({ name, teamId }) {
return await prisma.service.create({ data: { name, teams: { connect: { id: teamId } } } });
}
export async function getService({ id, teamId }) {
const body = await prisma.service.findFirst({
where: { id, teams: { some: { id: teamId } } },
include: {
destinationDocker: true,
plausibleAnalytics: true,
minio: true,
vscodeserver: true,
2022-03-04 15:14:25 +01:00
wordpress: true,
serviceSecret: true
2022-02-10 15:47:44 +01:00
}
});
if (body.plausibleAnalytics?.postgresqlPassword)
body.plausibleAnalytics.postgresqlPassword = decrypt(
body.plausibleAnalytics.postgresqlPassword
);
if (body.plausibleAnalytics?.password)
body.plausibleAnalytics.password = decrypt(body.plausibleAnalytics.password);
if (body.plausibleAnalytics?.secretKeyBase)
body.plausibleAnalytics.secretKeyBase = decrypt(body.plausibleAnalytics.secretKeyBase);
if (body.minio?.rootUserPassword)
body.minio.rootUserPassword = decrypt(body.minio.rootUserPassword);
if (body.vscodeserver?.password) body.vscodeserver.password = decrypt(body.vscodeserver.password);
if (body.wordpress?.mysqlPassword)
body.wordpress.mysqlPassword = decrypt(body.wordpress.mysqlPassword);
if (body.wordpress?.mysqlRootUserPassword)
body.wordpress.mysqlRootUserPassword = decrypt(body.wordpress.mysqlRootUserPassword);
2022-03-04 15:14:25 +01:00
if (body?.serviceSecret.length > 0) {
body.serviceSecret = body.serviceSecret.map((s) => {
s.value = decrypt(s.value);
return s;
});
}
2022-02-10 15:47:44 +01:00
return { ...body };
}
export async function configureServiceType({ id, type }) {
if (type === 'plausibleanalytics') {
const password = encrypt(generatePassword());
const postgresqlUser = cuid();
const postgresqlPassword = encrypt(generatePassword());
const postgresqlDatabase = 'plausibleanalytics';
const secretKeyBase = encrypt(generatePassword(64));
await prisma.service.update({
where: { id },
data: {
type,
plausibleAnalytics: {
create: {
postgresqlDatabase,
postgresqlUser,
postgresqlPassword,
password,
secretKeyBase
}
}
}
});
} else if (type === 'nocodb') {
await prisma.service.update({
where: { id },
data: { type }
});
} else if (type === 'minio') {
const rootUser = cuid();
const rootUserPassword = encrypt(generatePassword());
await prisma.service.update({
where: { id },
data: { type, minio: { create: { rootUser, rootUserPassword } } }
});
} else if (type === 'vscodeserver') {
const password = encrypt(generatePassword());
await prisma.service.update({
where: { id },
data: { type, vscodeserver: { create: { password } } }
});
} else if (type === 'wordpress') {
const mysqlUser = cuid();
const mysqlPassword = encrypt(generatePassword());
const mysqlRootUser = cuid();
const mysqlRootUserPassword = encrypt(generatePassword());
await prisma.service.update({
where: { id },
data: {
type,
wordpress: { create: { mysqlPassword, mysqlRootUserPassword, mysqlRootUser, mysqlUser } }
}
});
2022-02-11 15:31:25 +01:00
} else if (type === 'vaultwarden') {
await prisma.service.update({
where: { id },
data: {
type
}
});
2022-03-02 11:57:03 +01:00
} else if (type === 'languagetool') {
await prisma.service.update({
where: { id },
data: {
type
}
});
2022-03-27 13:49:04 +02:00
} else if (type === 'n8n') {
await prisma.service.update({
where: { id },
data: {
type
}
});
2022-02-10 15:47:44 +01:00
}
}
export async function setServiceVersion({ id, version }) {
2022-02-10 15:47:44 +01:00
return await prisma.service.update({
where: { id },
data: { version }
});
}
export async function setServiceSettings({ id, dualCerts }) {
return await prisma.service.update({
where: { id },
data: { dualCerts }
});
}
2022-02-10 15:47:44 +01:00
export async function updatePlausibleAnalyticsService({ id, fqdn, email, username, name }) {
await prisma.plausibleAnalytics.update({ where: { serviceId: id }, data: { email, username } });
await prisma.service.update({ where: { id }, data: { name, fqdn } });
}
2022-03-27 13:49:04 +02:00
export async function updateService({ id, fqdn, name }) {
2022-02-10 15:47:44 +01:00
return await prisma.service.update({ where: { id }, data: { fqdn, name } });
}
2022-03-02 11:57:03 +01:00
export async function updateLanguageToolService({ id, fqdn, name }) {
return await prisma.service.update({ where: { id }, data: { fqdn, name } });
}
2022-02-11 15:31:25 +01:00
export async function updateVaultWardenService({ id, fqdn, name }) {
return await prisma.service.update({ where: { id }, data: { fqdn, name } });
}
2022-02-10 15:47:44 +01:00
export async function updateVsCodeServer({ id, fqdn, name }) {
return await prisma.service.update({ where: { id }, data: { fqdn, name } });
}
export async function updateWordpress({ id, fqdn, name, mysqlDatabase, extraConfig }) {
return await prisma.service.update({
where: { id },
data: { fqdn, name, wordpress: { update: { mysqlDatabase, extraConfig } } }
});
}
export async function updateMinioService({ id, publicPort }) {
return await prisma.minio.update({ where: { serviceId: id }, data: { publicPort } });
}
export async function removeService({ id }) {
await prisma.plausibleAnalytics.deleteMany({ where: { serviceId: id } });
await prisma.minio.deleteMany({ where: { serviceId: id } });
await prisma.vscodeserver.deleteMany({ where: { serviceId: id } });
await prisma.wordpress.deleteMany({ where: { serviceId: id } });
2022-03-04 15:14:25 +01:00
await prisma.serviceSecret.deleteMany({ where: { serviceId: id } });
2022-02-10 15:47:44 +01:00
await prisma.service.delete({ where: { id } });
}