From dc3add495c2adf91e57f6d187d966baa637a08f6 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 18 May 2022 12:32:53 +0200 Subject: [PATCH] wip(fix): traefik --- src/lib/haproxy/index.ts | 21 ++--- src/lib/queues/proxyTcpHttp.ts | 13 ++-- src/routes/services/[id]/minio/start.json.ts | 2 - src/routes/webhooks/traefik/main.json.ts | 80 +++++++++++--------- src/routes/webhooks/traefik/other.json.ts | 6 +- 5 files changed, 64 insertions(+), 58 deletions(-) diff --git a/src/lib/haproxy/index.ts b/src/lib/haproxy/index.ts index dcd2d054d..2c21460c3 100644 --- a/src/lib/haproxy/index.ts +++ b/src/lib/haproxy/index.ts @@ -108,6 +108,7 @@ export async function checkHAProxy(haproxy?: Got): Promise { } export async function stopTcpHttpProxy( + id: string, destinationDocker: DestinationDocker, publicPort: number, forceName: string = null @@ -115,7 +116,7 @@ export async function stopTcpHttpProxy( const { engine } = destinationDocker; const host = getEngine(engine); const settings = await db.listSettings(); - let containerName = `proxy-for-${publicPort}`; + let containerName = `${id}-${publicPort}`; if (!settings.isTraefikUsed) { containerName = `haproxy-for-${publicPort}`; } @@ -141,7 +142,7 @@ export async function startTraefikTCPProxy( const { network, engine } = destinationDocker; const host = getEngine(engine); - const containerName = `proxy-for-${publicPort}`; + const containerName = `${id}-${publicPort}`; const found = await checkContainer(engine, containerName, true); const foundDependentContainer = await checkContainer(engine, id, true); @@ -154,8 +155,8 @@ export async function startTraefikTCPProxy( const tcpProxy = { version: '3.5', services: { - [id]: { - container_name: `proxy-for-${publicPort}`, + [`${id}-${publicPort}`]: { + container_name: containerName, image: 'traefik:v2.6', command: [ `--entrypoints.tcp.address=:${publicPort}`, @@ -241,7 +242,7 @@ export async function startTraefikHTTPProxy( const { network, engine } = destinationDocker; const host = getEngine(engine); - const containerName = `proxy-for-${publicPort}`; + const containerName = `${id}-${publicPort}`; const found = await checkContainer(engine, containerName, true); const foundDependentContainer = await checkContainer(engine, id, true); @@ -251,21 +252,21 @@ export async function startTraefikHTTPProxy( `DOCKER_HOST="${host}" docker network inspect bridge --format '{{json .IPAM.Config }}'` ); const ip = JSON.parse(Config)[0].Gateway; + console.log({ privatePort, publicPort }); const tcpProxy = { version: '3.5', services: { - [id]: { - container_name: `proxy-for-${publicPort}`, + [`${id}-${publicPort}`]: { + container_name: containerName, image: 'traefik:v2.6', command: [ `--entrypoints.http.address=:${publicPort}`, `--providers.http.endpoint=${otherTraefikEndpoint}?id=${id}&privatePort=${privatePort}&publicPort=${publicPort}&type=http`, '--providers.http.pollTimeout=2s', - '--log.level=error' + '--log.level=debug' ], ports: [`${publicPort}:${publicPort}`], extra_hosts: ['host.docker.internal:host-gateway', `host.docker.internal:${ip}`], - volumes: ['/var/run/docker.sock:/var/run/docker.sock'], networks: ['coolify-infra', network] } }, @@ -377,7 +378,7 @@ export async function startTraefikProxy(engine: string): Promise { --certificatesresolvers.letsencrypt.acme.httpchallenge=true \ --certificatesresolvers.letsencrypt.acme.storage=/etc/traefik/acme/acme.json \ --certificatesresolvers.letsencrypt.acme.httpchallenge.entrypoint=web \ - --log.level=error` + --log.level=debug` ); await db.prisma.setting.update({ where: { id }, data: { proxyHash: null } }); await db.setDestinationSettings({ engine, isCoolifyProxyUsed: true }); diff --git a/src/lib/queues/proxyTcpHttp.ts b/src/lib/queues/proxyTcpHttp.ts index e4ed9fe16..136798777 100644 --- a/src/lib/queues/proxyTcpHttp.ts +++ b/src/lib/queues/proxyTcpHttp.ts @@ -21,7 +21,7 @@ export default async function (): Promise { await asyncExecShell(`DOCKER_HOST=${host} docker compose -f ${composeFileDestination} pull`); await asyncExecShell(`DOCKER_HOST=${host} docker compose -f ${composeFileDestination} up -d`); await db.updateMinioService({ id, publicPort }); - await startHttpProxy(destinationDocker, id, publicPort, apiPort); return { status: 200 }; diff --git a/src/routes/webhooks/traefik/main.json.ts b/src/routes/webhooks/traefik/main.json.ts index a5f1b9a55..e887a06d9 100644 --- a/src/routes/webhooks/traefik/main.json.ts +++ b/src/routes/webhooks/traefik/main.json.ts @@ -6,38 +6,10 @@ import { listServicesWithIncludes } from '$lib/database'; import { checkContainer } from '$lib/haproxy'; import type { RequestHandler } from '@sveltejs/kit'; -const traefik = { - http: { - routers: {}, - services: {}, - middlewares: { - 'redirect-to-https': { - redirectscheme: { - scheme: 'https' - } - }, - 'redirect-to-http': { - redirectscheme: { - scheme: 'http' - } - }, - 'redirect-to-non-www': { - redirectregex: { - regex: '^https?://www\\.(.+)', - replacement: 'http://${1}' - } - }, - 'redirect-to-www': { - redirectregex: { - regex: '^https?://(?:www\\.)?(.+)', - replacement: 'http://www.${1}' - } - } - } - } -}; - -function configureMiddleware({ id, port, domain, nakedDomain, isHttps, isWWW, isDualCerts }) { +function configureMiddleware( + { id, port, domain, nakedDomain, isHttps, isWWW, isDualCerts }, + traefik +) { if (isHttps) { traefik.http.routers[id] = { entrypoints: ['web'], @@ -155,6 +127,36 @@ function configureMiddleware({ id, port, domain, nakedDomain, isHttps, isWWW, is } } export const get: RequestHandler = async (event) => { + const traefik = { + http: { + routers: {}, + services: {}, + middlewares: { + 'redirect-to-https': { + redirectscheme: { + scheme: 'https' + } + }, + 'redirect-to-http': { + redirectscheme: { + scheme: 'http' + } + }, + 'redirect-to-non-www': { + redirectregex: { + regex: '^https?://www\\.(.+)', + replacement: 'http://${1}' + } + }, + 'redirect-to-www': { + redirectregex: { + regex: '^https?://(?:www\\.)?(.+)', + replacement: 'http://www.${1}' + } + } + } + } + }; const applications = await db.prisma.application.findMany({ include: { destinationDocker: true, settings: true } }); @@ -230,7 +232,6 @@ export const get: RequestHandler = async (event) => { type, destinationDocker, destinationDockerId, - updatedAt, dualCerts, plausibleAnalytics } = service; @@ -288,11 +289,11 @@ export const get: RequestHandler = async (event) => { }); } for (const application of data.applications) { - configureMiddleware(application); + configureMiddleware(application, traefik); } for (const service of data.services) { const { id, scriptName } = service; - configureMiddleware(service); + configureMiddleware(service, traefik); if (scriptName) { traefik.http.middlewares[`${id}-redir`] = { @@ -309,9 +310,14 @@ export const get: RequestHandler = async (event) => { } } for (const coolify of data.coolify) { - configureMiddleware(coolify); + configureMiddleware(coolify, traefik); + } + if (Object.keys(traefik.http.routers).length === 0) { + traefik.http.routers = null; + } + if (Object.keys(traefik.http.services).length === 0) { + traefik.http.services = null; } - return { status: 200, body: { diff --git a/src/routes/webhooks/traefik/other.json.ts b/src/routes/webhooks/traefik/other.json.ts index ea99e770e..ab0aa7c9e 100644 --- a/src/routes/webhooks/traefik/other.json.ts +++ b/src/routes/webhooks/traefik/other.json.ts @@ -40,14 +40,14 @@ export const get: RequestHandler = async (event) => { traefik = { [type]: { routers: { - [id]: { + [`${id}-${publicPort}`]: { entrypoints: [type], rule: `Host(\`${domain}\`)`, - service: id + service: `${id}-${publicPort}` } }, services: { - [id]: { + [`${id}-${publicPort}`]: { loadbalancer: { servers: [{ url: `http://${id}:${privatePort}` }] }