From 61eb155d1354f90ea4ea386469b696045fd9f4c9 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 4 Oct 2022 14:05:01 +0200 Subject: [PATCH 01/13] chore: version++ --- apps/api/src/lib/common.ts | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/api/src/lib/common.ts b/apps/api/src/lib/common.ts index 18b5aab23..6ccd2f71a 100644 --- a/apps/api/src/lib/common.ts +++ b/apps/api/src/lib/common.ts @@ -20,7 +20,7 @@ import { scheduler } from './scheduler'; import { supportedServiceTypesAndVersions } from './services/supportedVersions'; import { includeServices } from './services/common'; -export const version = '3.10.13'; +export const version = '3.10.14'; export const isDev = process.env.NODE_ENV === 'development'; const algorithm = 'aes-256-ctr'; diff --git a/package.json b/package.json index 741d79460..6f580d533 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "coolify", "description": "An open-source & self-hostable Heroku / Netlify alternative.", - "version": "3.10.13", + "version": "3.10.14", "license": "Apache-2.0", "repository": "github:coollabsio/coolify", "scripts": { From a3af21275a1d133c38ca83bcc9e93842d2f930a9 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 4 Oct 2022 14:05:11 +0200 Subject: [PATCH 02/13] fix: meilisearch data dir --- apps/api/src/lib/services/handlers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/api/src/lib/services/handlers.ts b/apps/api/src/lib/services/handlers.ts index ee9ab88ec..3dcf0d5e8 100644 --- a/apps/api/src/lib/services/handlers.ts +++ b/apps/api/src/lib/services/handlers.ts @@ -919,7 +919,7 @@ async function startMeilisearchService(request: FastifyRequest const config = { meilisearch: { image: `${image}:${version}`, - volumes: [`${id}-datams:/data.ms`], + volumes: [`${id}-datams:/meili_data/data.ms`, `${id}-data:/meili_data `], environmentVariables: { MEILI_MASTER_KEY: masterKey } From bd27afe0da892dda63d1b37bc1e2aa42e7815604 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 4 Oct 2022 15:01:47 +0200 Subject: [PATCH 03/13] fix: verify and configure remote docker engines --- apps/api/src/index.ts | 34 +++++++++---- .../routes/api/v1/destinations/handlers.ts | 49 +++++++++++++------ .../api/src/routes/api/v1/servers/handlers.ts | 13 ++++- .../destinations/[id]/_RemoteDocker.svelte | 35 +++++++------ 4 files changed, 86 insertions(+), 45 deletions(-) diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index 6b43db8b7..49ab78187 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -10,6 +10,7 @@ import { asyncExecShell, createRemoteEngineConfiguration, getDomain, isDev, list import { scheduler } from './lib/scheduler'; import { compareVersions } from 'compare-versions'; import Graceful from '@ladjs/graceful' +import { verifyRemoteDockerEngineFn } from './routes/api/v1/destinations/handlers'; declare module 'fastify' { interface FastifyInstance { config: { @@ -27,7 +28,8 @@ declare module 'fastify' { const port = isDev ? 3001 : 3000; const host = '0.0.0.0'; -prisma.setting.findFirst().then(async (settings) => { +(async () => { + const settings = prisma.setting.findFirst() const fastify = Fastify({ logger: settings?.isAPIDebuggingEnabled || false, trustProxy: true @@ -117,11 +119,8 @@ prisma.setting.findFirst().then(async (settings) => { // console.log('not allowed', request.headers.host) } }) - fastify.listen({ port, host }, async (err: any, address: any) => { - if (err) { - console.error(err); - process.exit(1); - } + try { + await fastify.listen({ port, host }) console.log(`Coolify's API is listening on ${host}:${port}`); await initServer(); @@ -162,19 +161,28 @@ prisma.setting.findFirst().then(async (settings) => { getIPAddress(), configureRemoteDockers(), ]) - }); -}) + } catch (error) { + console.error(error); + process.exit(1); + } + + + +})(); + async function getIPAddress() { const { publicIpv4, publicIpv6 } = await import('public-ip') try { const settings = await listSettings(); if (!settings.ipv4) { + console.log(`Getting public IPv4 address...`); const ipv4 = await publicIpv4({ timeout: 2000 }) await prisma.setting.update({ where: { id: settings.id }, data: { ipv4 } }) } if (!settings.ipv6) { + console.log(`Getting public IPv6 address...`); const ipv6 = await publicIpv6({ timeout: 2000 }) await prisma.setting.update({ where: { id: settings.id }, data: { ipv6 } }) } @@ -183,6 +191,7 @@ async function getIPAddress() { } async function initServer() { try { + console.log(`Initializing server...`); await asyncExecShell(`docker network create --attachable coolify`); } catch (error) { } try { @@ -196,6 +205,7 @@ async function getArch() { try { const settings = await prisma.setting.findFirst({}) if (settings && !settings.arch) { + console.log(`Getting architecture...`); await prisma.setting.update({ where: { id: settings.id }, data: { arch: process.arch } }) } } catch (error) { } @@ -207,9 +217,13 @@ async function configureRemoteDockers() { where: { remoteVerified: true, remoteEngine: true } }); if (remoteDocker.length > 0) { + console.log(`Verifying Remote Docker Engines...`); for (const docker of remoteDocker) { - await createRemoteEngineConfiguration(docker.id) + console.log('Verifying:', docker.remoteIpAddress) + await verifyRemoteDockerEngineFn(docker.id); } } - } catch (error) { } + } catch (error) { + console.log(error) + } } diff --git a/apps/api/src/routes/api/v1/destinations/handlers.ts b/apps/api/src/routes/api/v1/destinations/handlers.ts index e4f038881..83373edd8 100644 --- a/apps/api/src/routes/api/v1/destinations/handlers.ts +++ b/apps/api/src/routes/api/v1/destinations/handlers.ts @@ -4,7 +4,7 @@ import sshConfig from 'ssh-config' import fs from 'fs/promises' import os from 'os'; -import { asyncExecShell, createRemoteEngineConfiguration, decrypt, errorHandler, executeDockerCmd, listSettings, prisma, startTraefikProxy, stopTraefikProxy } from '../../../../lib/common'; +import { asyncExecShell, createRemoteEngineConfiguration, decrypt, errorHandler, executeDockerCmd, executeSSHCmd, listSettings, prisma, startTraefikProxy, stopTraefikProxy } from '../../../../lib/common'; import { checkContainer } from '../../../../lib/docker'; import type { OnlyId } from '../../../../types'; @@ -202,25 +202,44 @@ export async function assignSSHKey(request: FastifyRequest) { return errorHandler({ status, message }) } } -export async function verifyRemoteDockerEngine(request: FastifyRequest, reply: FastifyReply) { +export async function verifyRemoteDockerEngineFn(id: string) { + await createRemoteEngineConfiguration(id); + const { remoteIpAddress, remoteUser, network, isCoolifyProxyUsed } = await prisma.destinationDocker.findFirst({ where: { id } }) + const host = `ssh://${remoteUser}@${remoteIpAddress}` + const { stdout } = await asyncExecShell(`DOCKER_HOST=${host} docker network ls --filter 'name=${network}' --no-trunc --format "{{json .}}"`); + if (!stdout) { + await asyncExecShell(`DOCKER_HOST=${host} docker network create --attachable ${network}`); + } + const { stdout: coolifyNetwork } = await asyncExecShell(`DOCKER_HOST=${host} docker network ls --filter 'name=coolify-infra' --no-trunc --format "{{json .}}"`); + if (!coolifyNetwork) { + await asyncExecShell(`DOCKER_HOST=${host} docker network create --attachable coolify-infra`); + } + if (isCoolifyProxyUsed) await startTraefikProxy(id); + const { stdout: daemonJson } = await executeSSHCmd({ dockerId: id, command: `cat /etc/docker/daemon.json` }); try { - const { id } = request.params; - await createRemoteEngineConfiguration(id); - const { remoteIpAddress, remoteUser, network, isCoolifyProxyUsed } = await prisma.destinationDocker.findFirst({ where: { id } }) - const host = `ssh://${remoteUser}@${remoteIpAddress}` - const { stdout } = await asyncExecShell(`DOCKER_HOST=${host} docker network ls --filter 'name=${network}' --no-trunc --format "{{json .}}"`); - if (!stdout) { - await asyncExecShell(`DOCKER_HOST=${host} docker network create --attachable ${network}`); + let daemonJsonParsed = JSON.parse(daemonJson); + if (!daemonJsonParsed['live-restore'] || daemonJsonParsed['live-restore'] !== true) { + daemonJsonParsed['live-restore'] = true + await executeSSHCmd({ dockerId: id, command: `echo '${JSON.stringify(daemonJsonParsed)}' > /etc/docker/daemon.json` }); + await executeSSHCmd({ dockerId: id, command: `systemctl restart docker` }); } - const { stdout: coolifyNetwork } = await asyncExecShell(`DOCKER_HOST=${host} docker network ls --filter 'name=coolify-infra' --no-trunc --format "{{json .}}"`); - if (!coolifyNetwork) { - await asyncExecShell(`DOCKER_HOST=${host} docker network create --attachable coolify-infra`); + } catch (error) { + const daemonJsonParsed = { + "live-restore": true } - if (isCoolifyProxyUsed) await startTraefikProxy(id); - await prisma.destinationDocker.update({ where: { id }, data: { remoteVerified: true } }) + console.log(JSON.stringify(daemonJsonParsed)) + await executeSSHCmd({ dockerId: id, command: `echo '${JSON.stringify(daemonJsonParsed)}' > /etc/docker/daemon.json` }); + await executeSSHCmd({ dockerId: id, command: `systemctl restart docker` }); + } + await prisma.destinationDocker.update({ where: { id }, data: { remoteVerified: true } }) +} +export async function verifyRemoteDockerEngine(request: FastifyRequest, reply: FastifyReply) { + const { id } = request.params; + try { + await verifyRemoteDockerEngineFn(id); return reply.code(201).send() - } catch ({ status, message }) { + await prisma.destinationDocker.update({ where: { id }, data: { remoteVerified: false } }) return errorHandler({ status, message }) } } diff --git a/apps/api/src/routes/api/v1/servers/handlers.ts b/apps/api/src/routes/api/v1/servers/handlers.ts index d9705e9ef..874f5a9f7 100644 --- a/apps/api/src/routes/api/v1/servers/handlers.ts +++ b/apps/api/src/routes/api/v1/servers/handlers.ts @@ -8,7 +8,16 @@ export async function listServers(request: FastifyRequest) { try { const userId = request.user.userId; const teamId = request.user.teamId; - const servers = await prisma.destinationDocker.findMany({ where: { teams: { some: { id: teamId === '0' ? undefined : teamId } }}, distinct: ['remoteIpAddress', 'engine'] }) + let servers = await prisma.destinationDocker.findMany({ where: { teams: { some: { id: teamId === '0' ? undefined : teamId } } }, distinct: ['remoteIpAddress', 'engine'] }) + servers = servers.filter((server) => { + if (server.remoteEngine) { + if (server.remoteVerified) { + return server + } + } else { + return server + } + }) return { servers } @@ -78,7 +87,7 @@ export async function showUsage(request: FastifyRequest) { freeMemPercentage: (parsed.totalMemoryKB - parsed.usedMemoryKB) / parsed.totalMemoryKB * 100 }, cpu: { - load: [0,0,0], + load: [0, 0, 0], usage: cpuUsage, count: cpus }, diff --git a/apps/ui/src/routes/destinations/[id]/_RemoteDocker.svelte b/apps/ui/src/routes/destinations/[id]/_RemoteDocker.svelte index e9c444e3d..4c65e7796 100644 --- a/apps/ui/src/routes/destinations/[id]/_RemoteDocker.svelte +++ b/apps/ui/src/routes/destinations/[id]/_RemoteDocker.svelte @@ -175,24 +175,23 @@ disabled={loading.save} >{$t('forms.save')} - {#if !destination.remoteVerified} - - {:else} - - {/if} + + + {/if}
From 8eb9ca0260d5279038f7e50fbe8584ee6f12b786 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 4 Oct 2022 15:06:09 +0200 Subject: [PATCH 04/13] fix: add buildkit features --- .../routes/api/v1/destinations/handlers.ts | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/apps/api/src/routes/api/v1/destinations/handlers.ts b/apps/api/src/routes/api/v1/destinations/handlers.ts index 83373edd8..729426b2e 100644 --- a/apps/api/src/routes/api/v1/destinations/handlers.ts +++ b/apps/api/src/routes/api/v1/destinations/handlers.ts @@ -218,20 +218,34 @@ export async function verifyRemoteDockerEngineFn(id: string) { const { stdout: daemonJson } = await executeSSHCmd({ dockerId: id, command: `cat /etc/docker/daemon.json` }); try { let daemonJsonParsed = JSON.parse(daemonJson); + let isUpdated = false; if (!daemonJsonParsed['live-restore'] || daemonJsonParsed['live-restore'] !== true) { + isUpdated = true; daemonJsonParsed['live-restore'] = true + + } + if (!daemonJsonParsed?.features?.buildkit) { + isUpdated = true; + daemonJsonParsed.features = { + buildkit: true + } + } + if (isUpdated) { await executeSSHCmd({ dockerId: id, command: `echo '${JSON.stringify(daemonJsonParsed)}' > /etc/docker/daemon.json` }); await executeSSHCmd({ dockerId: id, command: `systemctl restart docker` }); } } catch (error) { const daemonJsonParsed = { - "live-restore": true + "live-restore": true, + "features": { + "buildkit": true + } } - console.log(JSON.stringify(daemonJsonParsed)) await executeSSHCmd({ dockerId: id, command: `echo '${JSON.stringify(daemonJsonParsed)}' > /etc/docker/daemon.json` }); await executeSSHCmd({ dockerId: id, command: `systemctl restart docker` }); + } finally { + await prisma.destinationDocker.update({ where: { id }, data: { remoteVerified: true } }) } - await prisma.destinationDocker.update({ where: { id }, data: { remoteVerified: true } }) } export async function verifyRemoteDockerEngine(request: FastifyRequest, reply: FastifyReply) { const { id } = request.params; From 772c0d1e416b73b7b77eda895eae1c59954845c2 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 4 Oct 2022 15:14:52 +0200 Subject: [PATCH 05/13] fix: nope if you are not logged in --- apps/api/src/routes/api/v1/base/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/api/src/routes/api/v1/base/index.ts b/apps/api/src/routes/api/v1/base/index.ts index f1b64baf3..76854af8a 100644 --- a/apps/api/src/routes/api/v1/base/index.ts +++ b/apps/api/src/routes/api/v1/base/index.ts @@ -2,12 +2,13 @@ import { FastifyPluginAsync } from 'fastify'; import { errorHandler, listSettings, version } from '../../../../lib/common'; const root: FastifyPluginAsync = async (fastify): Promise => { - fastify.get('/', async () => { + fastify.get('/', async (request) => { + const teamId = request.user?.teamId; const settings = await listSettings() try { return { - ipv4: settings.ipv4, - ipv6: settings.ipv6, + ipv4: teamId ? settings.ipv4 : 'nope', + ipv6: teamId ? settings.ipv6 : 'nope', version, whiteLabeled: process.env.COOLIFY_WHITE_LABELED === 'true', whiteLabeledIcon: process.env.COOLIFY_WHITE_LABELED_ICON, From bbe0690056a22fbeb684514bd131f6f767d08ac9 Mon Sep 17 00:00:00 2001 From: vvvctr <1019862+vvvctr@users.noreply.github.com> Date: Tue, 4 Oct 2022 16:27:56 +0200 Subject: [PATCH 06/13] Proper capitalization for WordPress service type. --- apps/api/src/lib/services/supportedVersions.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/api/src/lib/services/supportedVersions.ts b/apps/api/src/lib/services/supportedVersions.ts index 485e89ecf..64f049961 100644 --- a/apps/api/src/lib/services/supportedVersions.ts +++ b/apps/api/src/lib/services/supportedVersions.ts @@ -63,7 +63,7 @@ export const supportedServiceTypesAndVersions = [ }, { name: 'wordpress', - fancyName: 'Wordpress', + fancyName: 'WordPress', baseImage: 'wordpress', images: ['bitnami/mysql:5.7'], versions: ['latest', 'php8.1', 'php8.0', 'php7.4', 'php7.3'], @@ -255,4 +255,4 @@ export const supportedServiceTypesAndVersions = [ main: 8080 } }, -]; \ No newline at end of file +]; From 9d08421f01c1c76f6c79ecf2d65d10def2ae2e01 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 4 Oct 2022 21:46:01 +0200 Subject: [PATCH 07/13] dev intervals --- apps/api/src/index.ts | 102 +++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index 49ab78187..e8c648724 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -29,9 +29,9 @@ declare module 'fastify' { const port = isDev ? 3001 : 3000; const host = '0.0.0.0'; (async () => { - const settings = prisma.setting.findFirst() + // const settings = prisma.setting.findFirst() const fastify = Fastify({ - logger: settings?.isAPIDebuggingEnabled || false, + logger: false, trustProxy: true }); @@ -101,66 +101,66 @@ const host = '0.0.0.0'; }); fastify.register(cookie) fastify.register(cors); - fastify.addHook('onRequest', async (request, reply) => { - let allowedList = ['coolify:3000']; - const { ipv4, ipv6, fqdn } = await prisma.setting.findFirst({}) + // fastify.addHook('onRequest', async (request, reply) => { + // let allowedList = ['coolify:3000']; + // const { ipv4, ipv6, fqdn } = await prisma.setting.findFirst({}) - ipv4 && allowedList.push(`${ipv4}:3000`); - ipv6 && allowedList.push(ipv6); - fqdn && allowedList.push(getDomain(fqdn)); - isDev && allowedList.push('localhost:3000') && allowedList.push('localhost:3001') && allowedList.push('host.docker.internal:3001'); - const remotes = await prisma.destinationDocker.findMany({ where: { remoteEngine: true, remoteVerified: true } }) - if (remotes.length > 0) { - remotes.forEach(remote => { - allowedList.push(`${remote.remoteIpAddress}:3000`); - }) - } - if (!allowedList.includes(request.headers.host)) { - // console.log('not allowed', request.headers.host) - } - }) + // ipv4 && allowedList.push(`${ipv4}:3000`); + // ipv6 && allowedList.push(ipv6); + // fqdn && allowedList.push(getDomain(fqdn)); + // isDev && allowedList.push('localhost:3000') && allowedList.push('localhost:3001') && allowedList.push('host.docker.internal:3001'); + // const remotes = await prisma.destinationDocker.findMany({ where: { remoteEngine: true, remoteVerified: true } }) + // if (remotes.length > 0) { + // remotes.forEach(remote => { + // allowedList.push(`${remote.remoteIpAddress}:3000`); + // }) + // } + // if (!allowedList.includes(request.headers.host)) { + // // console.log('not allowed', request.headers.host) + // } + // }) try { await fastify.listen({ port, host }) console.log(`Coolify's API is listening on ${host}:${port}`); await initServer(); - const graceful = new Graceful({ brees: [scheduler] }); - graceful.listen(); + // const graceful = new Graceful({ brees: [scheduler] }); + // graceful.listen(); - setInterval(async () => { - if (!scheduler.workers.has('deployApplication')) { - scheduler.run('deployApplication'); - } - if (!scheduler.workers.has('infrastructure')) { - scheduler.run('infrastructure'); - } - }, 2000) + // setInterval(async () => { + // if (!scheduler.workers.has('deployApplication')) { + // scheduler.run('deployApplication'); + // } + // if (!scheduler.workers.has('infrastructure')) { + // scheduler.run('infrastructure'); + // } + // }, 2000) // autoUpdater - setInterval(async () => { - scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:autoUpdater") - }, isDev ? 5000 : 60000 * 15) + // setInterval(async () => { + // scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:autoUpdater") + // }, 60000 * 15) - // cleanupStorage - setInterval(async () => { - scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:cleanupStorage") - }, isDev ? 6000 : 60000 * 10) + // // cleanupStorage + // setInterval(async () => { + // scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:cleanupStorage") + // }, 60000 * 10) - // checkProxies and checkFluentBit - setInterval(async () => { - scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:checkProxies") - scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:checkFluentBit") - }, 10000) + // // checkProxies and checkFluentBit + // setInterval(async () => { + // scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:checkProxies") + // scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:checkFluentBit") + // }, 10000) - setInterval(async () => { - scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:copySSLCertificates") - }, 2000) + // setInterval(async () => { + // scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:copySSLCertificates") + // }, 2000) - await Promise.all([ - getArch(), - getIPAddress(), - configureRemoteDockers(), - ]) + // await Promise.all([ + // getArch(), + // getIPAddress(), + // configureRemoteDockers(), + // ]) } catch (error) { console.error(error); process.exit(1); @@ -178,13 +178,13 @@ async function getIPAddress() { if (!settings.ipv4) { console.log(`Getting public IPv4 address...`); const ipv4 = await publicIpv4({ timeout: 2000 }) - await prisma.setting.update({ where: { id: settings.id }, data: { ipv4 } }) + // await prisma.setting.update({ where: { id: settings.id }, data: { ipv4 } }) } if (!settings.ipv6) { console.log(`Getting public IPv6 address...`); const ipv6 = await publicIpv6({ timeout: 2000 }) - await prisma.setting.update({ where: { id: settings.id }, data: { ipv6 } }) + // await prisma.setting.update({ where: { id: settings.id }, data: { ipv6 } }) } } catch (error) { } From 430d51866c3ebcab9b007e1653d726def9a327da Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 4 Oct 2022 21:46:23 +0200 Subject: [PATCH 08/13] test: remove prisma --- apps/api/package.json | 3 ++- apps/api/src/lib/common.ts | 24 +++--------------------- apps/api/src/lib/scheduler.ts | 8 ++++---- 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/apps/api/package.json b/apps/api/package.json index 3bc32ff2b..f37501f84 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -11,7 +11,8 @@ "build": "rimraf build && esbuild `find src \\( -name '*.ts' \\)| grep -v client/` --minify=true --platform=node --outdir=build --format=cjs", "format": "prettier --write 'src/**/*.{js,ts,json,md}'", "lint": "prettier --check 'src/**/*.{js,ts,json,md}' && eslint --ignore-path .eslintignore .", - "start": "NODE_ENV=production npx -y prisma migrate deploy && npx prisma generate && npx prisma db seed && node index.js" + "predev": "npx -y prisma migrate deploy && npx prisma generate && npx prisma db seed", + "start": "NODE_ENV=production node index.js" }, "dependencies": { "@breejs/ts-worker": "2.0.0", diff --git a/apps/api/src/lib/common.ts b/apps/api/src/lib/common.ts index 6ccd2f71a..28cba7853 100644 --- a/apps/api/src/lib/common.ts +++ b/apps/api/src/lib/common.ts @@ -140,27 +140,9 @@ export const asyncExecShellStream = async ({ export const asyncSleep = (delay: number): Promise => new Promise((resolve) => setTimeout(resolve, delay)); -export const prisma = new PrismaClient({ - errorFormat: 'minimal' - // log: [ - // { - // emit: 'event', - // level: 'query', - // }, - // { - // emit: 'stdout', - // level: 'error', - // }, - // { - // emit: 'stdout', - // level: 'info', - // }, - // { - // emit: 'stdout', - // level: 'warn', - // }, - // ], -}); +export const prisma = () => { + return null +} // prisma.$on('query', (e) => { // console.log({e}) diff --git a/apps/api/src/lib/scheduler.ts b/apps/api/src/lib/scheduler.ts index 743463757..cec129bef 100644 --- a/apps/api/src/lib/scheduler.ts +++ b/apps/api/src/lib/scheduler.ts @@ -18,10 +18,10 @@ const options: any = { } } }, - jobs: [ - { name: 'infrastructure' }, - { name: 'deployApplication' }, - ], + // jobs: [ + // { name: 'infrastructure' }, + // { name: 'deployApplication' }, + // ], }; if (isDev) options.root = path.join(__dirname, '../jobs'); From 702798c275d7be97203f1a696072aa82dc223dfa Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 4 Oct 2022 22:00:50 +0200 Subject: [PATCH 09/13] revert things --- apps/api/package.json | 2 +- apps/api/src/index.ts | 102 +++++++++++++++++----------------- apps/api/src/lib/common.ts | 24 +++++++- apps/api/src/lib/scheduler.ts | 8 +-- 4 files changed, 77 insertions(+), 59 deletions(-) diff --git a/apps/api/package.json b/apps/api/package.json index f37501f84..3e6759220 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -11,7 +11,7 @@ "build": "rimraf build && esbuild `find src \\( -name '*.ts' \\)| grep -v client/` --minify=true --platform=node --outdir=build --format=cjs", "format": "prettier --write 'src/**/*.{js,ts,json,md}'", "lint": "prettier --check 'src/**/*.{js,ts,json,md}' && eslint --ignore-path .eslintignore .", - "predev": "npx -y prisma migrate deploy && npx prisma generate && npx prisma db seed", + "prestart": "npx -y prisma migrate deploy && npx prisma generate && npx prisma db seed", "start": "NODE_ENV=production node index.js" }, "dependencies": { diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index e8c648724..a40ea38f2 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -29,9 +29,9 @@ declare module 'fastify' { const port = isDev ? 3001 : 3000; const host = '0.0.0.0'; (async () => { - // const settings = prisma.setting.findFirst() + const settings = await prisma.setting.findFirst() const fastify = Fastify({ - logger: false, + logger: settings?.isAPIDebuggingEnabled || false, trustProxy: true }); @@ -101,66 +101,66 @@ const host = '0.0.0.0'; }); fastify.register(cookie) fastify.register(cors); - // fastify.addHook('onRequest', async (request, reply) => { - // let allowedList = ['coolify:3000']; - // const { ipv4, ipv6, fqdn } = await prisma.setting.findFirst({}) + fastify.addHook('onRequest', async (request, reply) => { + let allowedList = ['coolify:3000']; + const { ipv4, ipv6, fqdn } = await prisma.setting.findFirst({}) - // ipv4 && allowedList.push(`${ipv4}:3000`); - // ipv6 && allowedList.push(ipv6); - // fqdn && allowedList.push(getDomain(fqdn)); - // isDev && allowedList.push('localhost:3000') && allowedList.push('localhost:3001') && allowedList.push('host.docker.internal:3001'); - // const remotes = await prisma.destinationDocker.findMany({ where: { remoteEngine: true, remoteVerified: true } }) - // if (remotes.length > 0) { - // remotes.forEach(remote => { - // allowedList.push(`${remote.remoteIpAddress}:3000`); - // }) - // } - // if (!allowedList.includes(request.headers.host)) { - // // console.log('not allowed', request.headers.host) - // } - // }) + ipv4 && allowedList.push(`${ipv4}:3000`); + ipv6 && allowedList.push(ipv6); + fqdn && allowedList.push(getDomain(fqdn)); + isDev && allowedList.push('localhost:3000') && allowedList.push('localhost:3001') && allowedList.push('host.docker.internal:3001'); + const remotes = await prisma.destinationDocker.findMany({ where: { remoteEngine: true, remoteVerified: true } }) + if (remotes.length > 0) { + remotes.forEach(remote => { + allowedList.push(`${remote.remoteIpAddress}:3000`); + }) + } + if (!allowedList.includes(request.headers.host)) { + // console.log('not allowed', request.headers.host) + } + }) try { await fastify.listen({ port, host }) console.log(`Coolify's API is listening on ${host}:${port}`); await initServer(); - // const graceful = new Graceful({ brees: [scheduler] }); - // graceful.listen(); + const graceful = new Graceful({ brees: [scheduler] }); + graceful.listen(); - // setInterval(async () => { - // if (!scheduler.workers.has('deployApplication')) { - // scheduler.run('deployApplication'); - // } - // if (!scheduler.workers.has('infrastructure')) { - // scheduler.run('infrastructure'); - // } - // }, 2000) + setInterval(async () => { + if (!scheduler.workers.has('deployApplication')) { + scheduler.run('deployApplication'); + } + if (!scheduler.workers.has('infrastructure')) { + scheduler.run('infrastructure'); + } + }, 2000) // autoUpdater - // setInterval(async () => { - // scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:autoUpdater") - // }, 60000 * 15) + setInterval(async () => { + scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:autoUpdater") + }, 60000 * 15) - // // cleanupStorage - // setInterval(async () => { - // scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:cleanupStorage") - // }, 60000 * 10) + // cleanupStorage + setInterval(async () => { + scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:cleanupStorage") + }, 60000 * 10) - // // checkProxies and checkFluentBit - // setInterval(async () => { - // scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:checkProxies") - // scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:checkFluentBit") - // }, 10000) + // checkProxies and checkFluentBit + setInterval(async () => { + scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:checkProxies") + scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:checkFluentBit") + }, 10000) - // setInterval(async () => { - // scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:copySSLCertificates") - // }, 2000) + setInterval(async () => { + scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:copySSLCertificates") + }, 2000) - // await Promise.all([ - // getArch(), - // getIPAddress(), - // configureRemoteDockers(), - // ]) + await Promise.all([ + getArch(), + getIPAddress(), + configureRemoteDockers(), + ]) } catch (error) { console.error(error); process.exit(1); @@ -178,13 +178,13 @@ async function getIPAddress() { if (!settings.ipv4) { console.log(`Getting public IPv4 address...`); const ipv4 = await publicIpv4({ timeout: 2000 }) - // await prisma.setting.update({ where: { id: settings.id }, data: { ipv4 } }) + await prisma.setting.update({ where: { id: settings.id }, data: { ipv4 } }) } if (!settings.ipv6) { console.log(`Getting public IPv6 address...`); const ipv6 = await publicIpv6({ timeout: 2000 }) - // await prisma.setting.update({ where: { id: settings.id }, data: { ipv6 } }) + await prisma.setting.update({ where: { id: settings.id }, data: { ipv6 } }) } } catch (error) { } diff --git a/apps/api/src/lib/common.ts b/apps/api/src/lib/common.ts index 28cba7853..6ccd2f71a 100644 --- a/apps/api/src/lib/common.ts +++ b/apps/api/src/lib/common.ts @@ -140,9 +140,27 @@ export const asyncExecShellStream = async ({ export const asyncSleep = (delay: number): Promise => new Promise((resolve) => setTimeout(resolve, delay)); -export const prisma = () => { - return null -} +export const prisma = new PrismaClient({ + errorFormat: 'minimal' + // log: [ + // { + // emit: 'event', + // level: 'query', + // }, + // { + // emit: 'stdout', + // level: 'error', + // }, + // { + // emit: 'stdout', + // level: 'info', + // }, + // { + // emit: 'stdout', + // level: 'warn', + // }, + // ], +}); // prisma.$on('query', (e) => { // console.log({e}) diff --git a/apps/api/src/lib/scheduler.ts b/apps/api/src/lib/scheduler.ts index cec129bef..743463757 100644 --- a/apps/api/src/lib/scheduler.ts +++ b/apps/api/src/lib/scheduler.ts @@ -18,10 +18,10 @@ const options: any = { } } }, - // jobs: [ - // { name: 'infrastructure' }, - // { name: 'deployApplication' }, - // ], + jobs: [ + { name: 'infrastructure' }, + { name: 'deployApplication' }, + ], }; if (isDev) options.root = path.join(__dirname, '../jobs'); From 7489f172a1774ea8c5b5f12b50d5d935dfc2e0da Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 4 Oct 2022 22:14:16 +0200 Subject: [PATCH 10/13] test prestart --- .npmrc | 1 + 1 file changed, 1 insertion(+) create mode 100644 .npmrc diff --git a/.npmrc b/.npmrc new file mode 100644 index 000000000..b7425b9ee --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +enable-pre-post-scripts=true \ No newline at end of file From 6cd1c5de38bcf77048adf6af9f97d0f5287ab819 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 4 Oct 2022 22:22:29 +0200 Subject: [PATCH 11/13] Dockerfile update --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index b7bd56212..633934afe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,6 +38,7 @@ COPY --from=build /app/apps/ui/build/ ./public COPY --from=build /app/apps/api/prisma/ ./prisma COPY --from=build /app/apps/api/package.json . COPY --from=build /app/docker-compose.yaml . +COPY --from=build /app/.npmrc . RUN pnpm install -p From a901388887574eb2e82e8a61db32553614f9a354 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 4 Oct 2022 23:06:46 +0200 Subject: [PATCH 12/13] revert --- .npmrc | 1 - Dockerfile | 1 - apps/api/package.json | 3 +-- 3 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 .npmrc diff --git a/.npmrc b/.npmrc deleted file mode 100644 index b7425b9ee..000000000 --- a/.npmrc +++ /dev/null @@ -1 +0,0 @@ -enable-pre-post-scripts=true \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 633934afe..b7bd56212 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,7 +38,6 @@ COPY --from=build /app/apps/ui/build/ ./public COPY --from=build /app/apps/api/prisma/ ./prisma COPY --from=build /app/apps/api/package.json . COPY --from=build /app/docker-compose.yaml . -COPY --from=build /app/.npmrc . RUN pnpm install -p diff --git a/apps/api/package.json b/apps/api/package.json index 3e6759220..3bc32ff2b 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -11,8 +11,7 @@ "build": "rimraf build && esbuild `find src \\( -name '*.ts' \\)| grep -v client/` --minify=true --platform=node --outdir=build --format=cjs", "format": "prettier --write 'src/**/*.{js,ts,json,md}'", "lint": "prettier --check 'src/**/*.{js,ts,json,md}' && eslint --ignore-path .eslintignore .", - "prestart": "npx -y prisma migrate deploy && npx prisma generate && npx prisma db seed", - "start": "NODE_ENV=production node index.js" + "start": "NODE_ENV=production npx -y prisma migrate deploy && npx prisma generate && npx prisma db seed && node index.js" }, "dependencies": { "@breejs/ts-worker": "2.0.0", From 86eebb35cb410d17ec8f003cc3e3e05a0f7d1107 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 5 Oct 2022 08:58:14 +0200 Subject: [PATCH 13/13] fix: do not use npx --- apps/api/package.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/api/package.json b/apps/api/package.json index 3bc32ff2b..c1c6e5028 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -3,6 +3,7 @@ "description": "Coolify's Fastify API", "license": "Apache-2.0", "scripts": { + "db:generate":"prisma generate", "db:push": "prisma db push && prisma generate", "db:seed": "prisma db seed", "db:studio": "prisma studio", @@ -11,7 +12,7 @@ "build": "rimraf build && esbuild `find src \\( -name '*.ts' \\)| grep -v client/` --minify=true --platform=node --outdir=build --format=cjs", "format": "prettier --write 'src/**/*.{js,ts,json,md}'", "lint": "prettier --check 'src/**/*.{js,ts,json,md}' && eslint --ignore-path .eslintignore .", - "start": "NODE_ENV=production npx -y prisma migrate deploy && npx prisma generate && npx prisma db seed && node index.js" + "start": "NODE_ENV=production pnpm prisma migrate deploy && pnpm prisma generate && pnpm prisma db seed && node index.js" }, "dependencies": { "@breejs/ts-worker": "2.0.0", @@ -25,6 +26,7 @@ "@iarna/toml": "2.2.5", "@ladjs/graceful": "3.0.2", "@prisma/client": "4.4.0", + "prisma": "4.4.0", "axios": "0.27.2", "bcryptjs": "2.4.3", "bree": "9.1.2", @@ -66,7 +68,7 @@ "eslint-plugin-prettier": "4.2.1", "nodemon": "2.0.20", "prettier": "2.7.1", - "prisma": "4.4.0", + "rimraf": "3.0.2", "tsconfig-paths": "4.1.0", "typescript": "4.8.4"