110 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-02-28 16:06:44 +01:00
import * as db from '$lib/database';
2022-02-10 15:47:44 +01:00
import { getDomain } from '$lib/common';
import {
checkContainer,
2022-02-28 16:55:02 +01:00
checkHAProxy,
2022-02-27 11:52:05 +01:00
checkProxyConfigurations,
2022-02-10 15:47:44 +01:00
configureCoolifyProxyOn,
2022-02-28 16:06:44 +01:00
configureHAProxy,
2022-02-10 15:47:44 +01:00
forceSSLOnApplication,
2022-02-28 16:55:02 +01:00
haproxyInstance,
2022-02-13 22:56:37 +01:00
setWwwRedirection,
startCoolifyProxy,
startHttpProxy
2022-02-10 15:47:44 +01:00
} from '$lib/haproxy';
export default async function () {
2022-02-28 16:55:02 +01:00
const haproxy = await haproxyInstance();
await checkHAProxy(haproxy);
let transactionId;
2022-02-27 11:52:05 +01:00
try {
await checkProxyConfigurations();
} catch (error) {
console.log(error);
}
2022-02-10 15:47:44 +01:00
try {
2022-02-28 16:06:44 +01:00
const applications = await db.prisma.application.findMany({
include: { destinationDocker: true }
});
for (const application of applications) {
const {
fqdn,
id,
port,
destinationDocker: { engine }
} = application;
const containerRunning = await checkContainer(engine, id);
2022-02-28 16:55:02 +01:00
transactionId = await configureHAProxy(
haproxy,
transactionId,
fqdn,
id,
port,
containerRunning,
engine
);
2022-02-28 16:06:44 +01:00
}
const services = await db.prisma.service.findMany({
include: {
destinationDocker: true,
minio: true,
plausibleAnalytics: true,
vscodeserver: true,
wordpress: true
}
});
for (const service of services) {
const {
fqdn,
id,
type,
destinationDocker: { engine }
} = service;
2022-02-28 16:55:02 +01:00
console.log({ fqdn, id, type, engine });
2022-02-28 16:06:44 +01:00
const found = db.supportedServiceTypesAndVersions.find((a) => a.name === type);
if (found) {
2022-02-28 16:55:02 +01:00
console.log(found);
2022-02-28 16:06:44 +01:00
const port = found.ports.main;
const publicPort = service[type]?.publicPort;
const containerRunning = await checkContainer(engine, id);
2022-02-28 16:55:02 +01:00
console.log(containerRunning);
transactionId = await configureHAProxy(
haproxy,
transactionId,
fqdn,
id,
port,
containerRunning,
engine
);
2022-02-28 16:06:44 +01:00
if (publicPort) {
const containerFound = await checkContainer(
service.destinationDocker.engine,
`haproxy-for-${publicPort}`
);
if (!containerFound) {
await startHttpProxy(service.destinationDocker, id, publicPort, 9000);
2022-02-10 15:47:44 +01:00
}
}
}
}
2022-02-28 16:55:02 +01:00
console.log(transactionId);
if (transactionId) await haproxy.put(`v2/services/haproxy/transactions/${transactionId}`);
2022-02-10 15:47:44 +01:00
// Check Coolify FQDN and configure proxy if needed
2022-02-28 16:55:02 +01:00
// const { fqdn } = await db.listSettings();
// if (fqdn) {
// const domain = getDomain(fqdn);
// await startCoolifyProxy('/var/run/docker.sock');
// await configureCoolifyProxyOn(fqdn);
// await setWwwRedirection(fqdn);
// const isHttps = fqdn.startsWith('https://');
// if (isHttps) await forceSSLOnApplication(domain);
// }
2022-02-10 15:47:44 +01:00
} catch (error) {
throw error;
}
}