2022-02-10 15:47:44 +01:00
|
|
|
import { getDomain } from '$lib/common';
|
2022-02-14 09:28:37 +01:00
|
|
|
import { getApplicationById, prisma } from '$lib/database';
|
2022-02-10 15:47:44 +01:00
|
|
|
import { dockerInstance } from '$lib/docker';
|
|
|
|
import {
|
|
|
|
checkContainer,
|
|
|
|
configureCoolifyProxyOn,
|
|
|
|
configureProxyForApplication,
|
|
|
|
forceSSLOnApplication,
|
|
|
|
reloadHaproxy,
|
2022-02-13 22:56:37 +01:00
|
|
|
setWwwRedirection,
|
2022-02-10 15:47:44 +01:00
|
|
|
startCoolifyProxy
|
|
|
|
} from '$lib/haproxy';
|
|
|
|
import * as db from '$lib/database';
|
|
|
|
|
|
|
|
export default async function () {
|
|
|
|
try {
|
|
|
|
// Check destination containers and configure proxy if needed
|
|
|
|
const destinationDockers = await prisma.destinationDocker.findMany({});
|
|
|
|
for (const destination of destinationDockers) {
|
|
|
|
if (destination.isCoolifyProxyUsed) {
|
|
|
|
const docker = dockerInstance({ destinationDocker: destination });
|
|
|
|
const containers = await docker.engine.listContainers();
|
|
|
|
const configurations = containers.filter(
|
|
|
|
(container) => container.Labels['coolify.managed']
|
|
|
|
);
|
|
|
|
for (const configuration of configurations) {
|
|
|
|
const parsedConfiguration = JSON.parse(
|
|
|
|
Buffer.from(configuration.Labels['coolify.configuration'], 'base64').toString()
|
|
|
|
);
|
|
|
|
if (configuration.Labels['coolify.type'] === 'standalone-application') {
|
|
|
|
const { fqdn, applicationId, port, pullmergeRequestId } = parsedConfiguration;
|
|
|
|
if (fqdn) {
|
2022-02-14 09:28:37 +01:00
|
|
|
const found = await getApplicationById({ id: applicationId });
|
|
|
|
if (found) {
|
|
|
|
const domain = getDomain(fqdn);
|
|
|
|
await configureProxyForApplication({
|
|
|
|
domain,
|
|
|
|
imageId: pullmergeRequestId
|
|
|
|
? `${applicationId}-${pullmergeRequestId}`
|
|
|
|
: applicationId,
|
|
|
|
applicationId,
|
|
|
|
port
|
|
|
|
});
|
|
|
|
const isHttps = fqdn.startsWith('https://');
|
|
|
|
if (isHttps) await forceSSLOnApplication({ domain });
|
|
|
|
await setWwwRedirection(fqdn);
|
|
|
|
}
|
2022-02-10 15:47:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check Coolify FQDN and configure proxy if needed
|
|
|
|
const { fqdn } = await db.listSettings();
|
|
|
|
if (fqdn) {
|
|
|
|
const domain = getDomain(fqdn);
|
2022-02-15 15:55:55 +01:00
|
|
|
await startCoolifyProxy('/var/run/docker.sock');
|
|
|
|
await configureCoolifyProxyOn(fqdn);
|
2022-02-13 22:56:37 +01:00
|
|
|
await setWwwRedirection(fqdn);
|
2022-02-10 15:47:44 +01:00
|
|
|
const isHttps = fqdn.startsWith('https://');
|
|
|
|
if (isHttps) await forceSSLOnApplication({ domain });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
// await reloadHaproxy('/var/run/docker.sock');
|
|
|
|
}
|
|
|
|
}
|