From eb0aa20fe14bf4092e4d181c8b178b98e80a5d60 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 26 Oct 2022 10:27:33 +0200 Subject: [PATCH] fixes --- apps/api/src/lib.ts | 4 +- apps/api/src/lib/common.ts | 10 ++--- .../src/routes/api/v1/services/handlers.ts | 21 ++++++----- .../src/routes/webhooks/traefik/handlers.ts | 5 +-- apps/ui/src/routes/services/[id]/index.svelte | 37 ++++++++----------- 5 files changed, 34 insertions(+), 43 deletions(-) diff --git a/apps/api/src/lib.ts b/apps/api/src/lib.ts index 208e2a484..a5d5cff5a 100644 --- a/apps/api/src/lib.ts +++ b/apps/api/src/lib.ts @@ -1,5 +1,5 @@ import cuid from "cuid"; -import { decrypt, encrypt, generatePassword, getDomain, prisma } from "./lib/common"; +import { decrypt, encrypt, fixType, generatePassword, getDomain, prisma } from "./lib/common"; import { getTemplates } from "./lib/services"; import { includeServices } from "./lib/services/common"; @@ -13,7 +13,7 @@ export async function migrateServicesToNewTemplate() { if (!service.type) { continue; } - let template = templates.find(t => t.name.toLowerCase() === service.type.toLowerCase()); + let template = templates.find(t => fixType(t.name) === fixType(service.type)); if (template) { template = JSON.parse(JSON.stringify(template).replaceAll('$$id', service.id)) if (service.type === 'plausibleanalytics' && service.plausibleAnalytics) await plausibleAnalytics(service, template) diff --git a/apps/api/src/lib/common.ts b/apps/api/src/lib/common.ts index 9f8212fb1..d497fa24c 100644 --- a/apps/api/src/lib/common.ts +++ b/apps/api/src/lib/common.ts @@ -1457,8 +1457,8 @@ export async function getServiceFromDB({ if (!body) { return null } - let { type } = body; - type = fixType(type); + body.type = fixType(body.type); + if (body?.serviceSecret.length > 0) { body.serviceSecret = body.serviceSecret.map((s) => { s.value = decrypt(s.value); @@ -1466,7 +1466,6 @@ export async function getServiceFromDB({ }); } - // body[type] = { ...body[type], ...getUpdateableFields(type, body[type]) }; return { ...body, settings }; } @@ -1532,10 +1531,7 @@ export function getUpdateableFields(type: string, data: any) { } export function fixType(type) { - // Hack to fix the type case sensitivity... - if (type === 'plausibleanalytics') type = 'plausibleAnalytics'; - if (type === 'meilisearch') type = 'meiliSearch'; - return type; + return type?.replaceAll(' ', '').toLowerCase() || null; } export const getServiceMainPort = (service: string) => { diff --git a/apps/api/src/routes/api/v1/services/handlers.ts b/apps/api/src/routes/api/v1/services/handlers.ts index ed265c487..31637307c 100644 --- a/apps/api/src/routes/api/v1/services/handlers.ts +++ b/apps/api/src/routes/api/v1/services/handlers.ts @@ -115,7 +115,7 @@ export async function getServiceStatus(request: FastifyRequest) { } export async function parseAndFindServiceTemplates(service: any, workdir?: string, isDeploy: boolean = false) { const templates = await getTemplates() - const foundTemplate = templates.find(t => t.name.toLowerCase() === service.type.toLowerCase()) + const foundTemplate = templates.find(t => fixType(t.name) === service.type) let parsedTemplate = {} if (foundTemplate) { if (!isDeploy) { @@ -249,7 +249,7 @@ export async function saveServiceType(request: FastifyRequest, const { id } = request.params; const { type } = request.body; const templates = await getTemplates() - let foundTemplate = templates.find(t => t.name === type) + let foundTemplate = templates.find(t => fixType(t.name) === fixType(type)) if (foundTemplate) { foundTemplate = JSON.parse(JSON.stringify(foundTemplate).replaceAll('$$id', id)) if (foundTemplate.variables.length > 0) { @@ -495,7 +495,7 @@ export async function saveService(request: FastifyRequest, reply: F } const templates = await getTemplates() const service = await prisma.service.findUnique({ where: { id } }) - const foundTemplate = templates.find(t => t.name.toLowerCase() === service.type.toLowerCase()) + const foundTemplate = templates.find(t => fixType(t.name) === service.type) for (const setting of serviceSetting) { let { id: settingId, name, value, changed = false, isNew = false, variableName } = setting if (changed) { @@ -679,14 +679,17 @@ export async function activatePlausibleUsers(request: FastifyRequest, re const { destinationDockerId, destinationDocker, - plausibleAnalytics: { postgresqlUser, postgresqlPassword, postgresqlDatabase } + serviceSecret } = await getServiceFromDB({ id, teamId }); if (destinationDockerId) { - await executeDockerCmd({ - dockerId: destinationDocker.id, - command: `docker exec ${id}-postgresql psql -H postgresql://${postgresqlUser}:${postgresqlPassword}@localhost:5432/${postgresqlDatabase} -c "UPDATE users SET email_verified = true;"` - }) - return await reply.code(201).send() + const databaseUrl = serviceSecret.find((secret) => secret.name === 'DATABASE_URL'); + if (databaseUrl) { + await executeDockerCmd({ + dockerId: destinationDocker.id, + command: `docker exec ${id}-postgresql psql -H ${databaseUrl.value} -c "UPDATE users SET email_verified = true;"` + }) + return await reply.code(201).send() + } } throw { status: 500, message: 'Could not activate users.' } } catch ({ status, message }) { diff --git a/apps/api/src/routes/webhooks/traefik/handlers.ts b/apps/api/src/routes/webhooks/traefik/handlers.ts index b4fc4695d..e741b5f6f 100644 --- a/apps/api/src/routes/webhooks/traefik/handlers.ts +++ b/apps/api/src/routes/webhooks/traefik/handlers.ts @@ -1,5 +1,5 @@ import { FastifyRequest } from "fastify"; -import { errorHandler, getDomain, isDev, prisma, executeDockerCmd } from "../../../lib/common"; +import { errorHandler, getDomain, isDev, prisma, executeDockerCmd, fixType } from "../../../lib/common"; import { supportedServiceTypesAndVersions } from "../../../lib/services/supportedVersions"; import { includeServices } from "../../../lib/services/common"; import { TraefikOtherConfiguration } from "./types"; @@ -378,8 +378,7 @@ export async function traefikConfiguration(request, reply) { } = service; if (destinationDockerId) { const templates = await getTemplates(); - let found = templates.find((a) => a.name === type); - type = type.toLowerCase(); + let found = templates.find((a) => fixType(a.name) === fixType(type)); if (found) { found = JSON.parse(JSON.stringify(found).replaceAll('$$id', id)); for (const oneService of Object.keys(found.services)) { diff --git a/apps/ui/src/routes/services/[id]/index.svelte b/apps/ui/src/routes/services/[id]/index.svelte index 4c596a722..53d9281a6 100644 --- a/apps/ui/src/routes/services/[id]/index.svelte +++ b/apps/ui/src/routes/services/[id]/index.svelte @@ -157,7 +157,7 @@ try { await post(`/services/${id}/${service.type}/cleanup`, { id: service.id }); return addToast({ - message: 'Cleared DB Logs', + message: 'Cleared unnecessary database logs.', type: 'success' }); } catch (error) { @@ -166,28 +166,25 @@ loading.cleanup = false; } } - function doNothing() { - return; - } onMount(async () => { if (browser && window.location.hostname === 'demo.coolify.io' && !service.fqdn) { service.fqdn = `http://${cuid()}.demo.coolify.io`; - if (service.type === 'wordpress') { - service.wordpress.mysqlDatabase = 'db'; - } + // if (service.type === 'wordpress') { + // service.wordpress.mysqlDatabase = 'db'; + // } // if (service.type === 'plausibleanalytics') { // service.plausibleAnalytics.email = 'noreply@demo.com'; // service.plausibleAnalytics.username = 'admin'; // } - if (service.type === 'minio') { - service.minio.apiFqdn = `http://${cuid()}.demo.coolify.io`; - } - if (service.type === 'ghost') { - service.ghost.mariadbDatabase = 'db'; - } - if (service.type === 'fider') { - service.fider.emailNoreply = 'noreply@demo.com'; - } + // if (service.type === 'minio') { + // service.minio.apiFqdn = `http://${cuid()}.demo.coolify.io`; + // } + // if (service.type === 'ghost') { + // service.ghost.mariadbDatabase = 'db'; + // } + // if (service.type === 'fider') { + // service.fider.emailNoreply = 'noreply@demo.com'; + // } // await handleSubmit(); } }); @@ -214,8 +211,7 @@ : $t('forms.save')} {/if} - + {/if} {#if service.type === 'appwrite' && $status.service.isRunning}