This commit is contained in:
Andras Bacsai 2022-02-28 16:55:02 +01:00
parent f340ca9d05
commit 2daa043840
2 changed files with 64 additions and 35 deletions

View File

@ -214,23 +214,27 @@ export async function checkProxyConfigurations() {
console.log(error.response.body); console.log(error.response.body);
} }
} }
export async function configureHAProxy(fqdn, id, port, containerRunning, engine) { export async function configureHAProxy(
haproxy,
transactionId,
fqdn,
id,
port,
containerRunning,
engine
) {
const domain = getDomain(fqdn); const domain = getDomain(fqdn);
const isHttps = fqdn.startsWith('https://'); const isHttps = fqdn.startsWith('https://');
const isWWW = fqdn.includes('www.'); const isWWW = fqdn.includes('www.');
const redirectValue = `${isHttps ? 'https://' : 'http://'}${domain}%[capture.req.uri]`; const redirectValue = `${isHttps ? 'https://' : 'http://'}${domain}%[capture.req.uri]`;
const contTest = `{ req.hdr(host) -i ${isWWW ? domain.replace('www.', '') : `www.${domain}`} }`; const contTest = `{ req.hdr(host) -i ${isWWW ? domain.replace('www.', '') : `www.${domain}`} }`;
console.log({ application: true, fqdn, domain, id, port, containerRunning, isHttps, isWWW }); // console.log({ fqdn, domain, id, port, containerRunning, isHttps, isWWW });
const haproxy = await haproxyInstance();
await checkHAProxy(haproxy);
let transactionId;
if (!containerRunning) { if (!containerRunning) {
try { try {
await haproxy.get(`v2/services/haproxy/configuration/backends/${domain}`).json(); await haproxy.get(`v2/services/haproxy/configuration/backends/${domain}`).json();
console.log('removing', domain);
transactionId = await getNextTransactionId(); transactionId = await getNextTransactionId();
await haproxy await haproxy
.delete(`v2/services/haproxy/configuration/backends/${domain}`, { .delete(`v2/services/haproxy/configuration/backends/${domain}`, {
@ -240,10 +244,16 @@ export async function configureHAProxy(fqdn, id, port, containerRunning, engine)
}) })
.json(); .json();
} catch (error) { } catch (error) {
if (error?.response?.body) {
const json = JSON.parse(error.response.body);
if (json.code === 400 && json.message.includes('could not resolve address')) {
await stopCoolifyProxy(engine);
await startCoolifyProxy(engine);
}
}
// //
} }
try { try {
if (!transactionId) await getNextTransactionId();
let rules: any; let rules: any;
// Force SSL off // Force SSL off
rules = await haproxy rules = await haproxy
@ -259,6 +269,7 @@ export async function configureHAProxy(fqdn, id, port, containerRunning, engine)
rule.cond_test.includes(`{ hdr(host) -i ${domain} } !{ ssl_fc }`) rule.cond_test.includes(`{ hdr(host) -i ${domain} } !{ ssl_fc }`)
); );
if (rule) { if (rule) {
if (!transactionId) transactionId = await getNextTransactionId();
await haproxy await haproxy
.delete(`v2/services/haproxy/configuration/http_request_rules/${rule.index}`, { .delete(`v2/services/haproxy/configuration/http_request_rules/${rule.index}`, {
searchParams: { searchParams: {
@ -283,6 +294,7 @@ export async function configureHAProxy(fqdn, id, port, containerRunning, engine)
if (rules.data.length > 0) { if (rules.data.length > 0) {
const rule = rules.data.find((rule) => rule.redir_value.includes(redirectValue)); const rule = rules.data.find((rule) => rule.redir_value.includes(redirectValue));
if (rule) { if (rule) {
if (!transactionId) transactionId = await getNextTransactionId();
await haproxy await haproxy
.delete(`v2/services/haproxy/configuration/http_request_rules/${rule.index}`, { .delete(`v2/services/haproxy/configuration/http_request_rules/${rule.index}`, {
searchParams: { searchParams: {
@ -299,10 +311,7 @@ export async function configureHAProxy(fqdn, id, port, containerRunning, engine)
// //
} finally { } finally {
try { try {
if (transactionId) { if (transactionId) return transactionId;
console.log(transactionId);
await haproxy.put(`v2/services/haproxy/transactions/${transactionId}`);
}
} catch (error) { } catch (error) {
if (error?.response?.body) { if (error?.response?.body) {
const json = JSON.parse(error.response.body); const json = JSON.parse(error.response.body);
@ -315,7 +324,6 @@ export async function configureHAProxy(fqdn, id, port, containerRunning, engine)
} }
return; return;
} else { } else {
console.log('adding ', domain);
let serverConfigured = false; let serverConfigured = false;
let backendAvailable: any = null; let backendAvailable: any = null;
try { try {
@ -346,13 +354,15 @@ export async function configureHAProxy(fqdn, id, port, containerRunning, engine)
} }
} catch (error) { } catch (error) {
// //
console.log(error);
} }
if (serverConfigured) { if (serverConfigured) {
console.log('server configured'); console.log('server configured', domain);
return; return;
} }
if (!transactionId) transactionId = await getNextTransactionId();
if (backendAvailable) { if (backendAvailable) {
if (!transactionId) transactionId = await getNextTransactionId();
await haproxy await haproxy
.delete(`v2/services/haproxy/configuration/backends/${domain}`, { .delete(`v2/services/haproxy/configuration/backends/${domain}`, {
searchParams: { searchParams: {
@ -363,6 +373,7 @@ export async function configureHAProxy(fqdn, id, port, containerRunning, engine)
} }
try { try {
console.log('adding ', domain); console.log('adding ', domain);
if (!transactionId) transactionId = await getNextTransactionId();
await haproxy.post('v2/services/haproxy/configuration/backends', { await haproxy.post('v2/services/haproxy/configuration/backends', {
searchParams: { searchParams: {
transaction_id: transactionId transaction_id: transactionId
@ -373,7 +384,6 @@ export async function configureHAProxy(fqdn, id, port, containerRunning, engine)
name: domain name: domain
} }
}); });
await haproxy.post('v2/services/haproxy/configuration/servers', { await haproxy.post('v2/services/haproxy/configuration/servers', {
searchParams: { searchParams: {
transaction_id: transactionId, transaction_id: transactionId,
@ -386,7 +396,6 @@ export async function configureHAProxy(fqdn, id, port, containerRunning, engine)
port: port port: port
} }
}); });
let rules: any; let rules: any;
// Force SSL off // Force SSL off
@ -414,9 +423,7 @@ export async function configureHAProxy(fqdn, id, port, containerRunning, engine)
.json(); .json();
} }
} }
// Generate SSL && force SSL on // Generate SSL && force SSL on
if (isHttps) { if (isHttps) {
await letsEncrypt(domain, id, false); await letsEncrypt(domain, id, false);
rules = await haproxy rules = await haproxy
@ -492,13 +499,9 @@ export async function configureHAProxy(fqdn, id, port, containerRunning, engine)
.json(); .json();
} catch (error) { } catch (error) {
console.log(error); console.log(error);
throw error?.response?.body || error;
} finally { } finally {
try { try {
if (transactionId) { if (transactionId) return transactionId;
console.log('Committing transaction');
await haproxy.put(`v2/services/haproxy/transactions/${transactionId}`);
}
} catch (error) { } catch (error) {
if (error?.response?.body) { if (error?.response?.body) {
const json = JSON.parse(error.response.body); const json = JSON.parse(error.response.body);

View File

@ -2,16 +2,21 @@ import * as db from '$lib/database';
import { getDomain } from '$lib/common'; import { getDomain } from '$lib/common';
import { import {
checkContainer, checkContainer,
checkHAProxy,
checkProxyConfigurations, checkProxyConfigurations,
configureCoolifyProxyOn, configureCoolifyProxyOn,
configureHAProxy, configureHAProxy,
forceSSLOnApplication, forceSSLOnApplication,
haproxyInstance,
setWwwRedirection, setWwwRedirection,
startCoolifyProxy, startCoolifyProxy,
startHttpProxy startHttpProxy
} from '$lib/haproxy'; } from '$lib/haproxy';
export default async function () { export default async function () {
const haproxy = await haproxyInstance();
await checkHAProxy(haproxy);
let transactionId;
try { try {
await checkProxyConfigurations(); await checkProxyConfigurations();
} catch (error) { } catch (error) {
@ -30,7 +35,15 @@ export default async function () {
destinationDocker: { engine } destinationDocker: { engine }
} = application; } = application;
const containerRunning = await checkContainer(engine, id); const containerRunning = await checkContainer(engine, id);
await configureHAProxy(fqdn, id, port, containerRunning, engine); transactionId = await configureHAProxy(
haproxy,
transactionId,
fqdn,
id,
port,
containerRunning,
engine
);
} }
const services = await db.prisma.service.findMany({ const services = await db.prisma.service.findMany({
@ -50,12 +63,23 @@ export default async function () {
type, type,
destinationDocker: { engine } destinationDocker: { engine }
} = service; } = service;
console.log({ fqdn, id, type, engine });
const found = db.supportedServiceTypesAndVersions.find((a) => a.name === type); const found = db.supportedServiceTypesAndVersions.find((a) => a.name === type);
if (found) { if (found) {
console.log(found);
const port = found.ports.main; const port = found.ports.main;
const publicPort = service[type]?.publicPort; const publicPort = service[type]?.publicPort;
const containerRunning = await checkContainer(engine, id); const containerRunning = await checkContainer(engine, id);
await configureHAProxy(fqdn, id, port, containerRunning, engine); console.log(containerRunning);
transactionId = await configureHAProxy(
haproxy,
transactionId,
fqdn,
id,
port,
containerRunning,
engine
);
if (publicPort) { if (publicPort) {
const containerFound = await checkContainer( const containerFound = await checkContainer(
service.destinationDocker.engine, service.destinationDocker.engine,
@ -67,16 +91,18 @@ export default async function () {
} }
} }
} }
console.log(transactionId);
if (transactionId) await haproxy.put(`v2/services/haproxy/transactions/${transactionId}`);
// Check Coolify FQDN and configure proxy if needed // Check Coolify FQDN and configure proxy if needed
const { fqdn } = await db.listSettings(); // const { fqdn } = await db.listSettings();
if (fqdn) { // if (fqdn) {
const domain = getDomain(fqdn); // const domain = getDomain(fqdn);
await startCoolifyProxy('/var/run/docker.sock'); // await startCoolifyProxy('/var/run/docker.sock');
await configureCoolifyProxyOn(fqdn); // await configureCoolifyProxyOn(fqdn);
await setWwwRedirection(fqdn); // await setWwwRedirection(fqdn);
const isHttps = fqdn.startsWith('https://'); // const isHttps = fqdn.startsWith('https://');
if (isHttps) await forceSSLOnApplication(domain); // if (isHttps) await forceSSLOnApplication(domain);
} // }
} catch (error) { } catch (error) {
throw error; throw error;
} }