fix: WIP Traefik

This commit is contained in:
Andras Bacsai 2022-05-18 16:54:04 +02:00
parent b006fe8f68
commit eb62888c39
6 changed files with 73 additions and 80 deletions

View File

@ -122,6 +122,7 @@ export async function stopTcpHttpProxy(
}
if (forceName) containerName = forceName;
const found = await checkContainer(engine, containerName);
try {
if (found) {
return await asyncExecShell(
@ -137,15 +138,15 @@ export async function startTraefikTCPProxy(
id: string,
publicPort: number,
privatePort: number,
volume?: string
type?: string
): Promise<{ stdout: string; stderr: string } | Error> {
const { network, engine } = destinationDocker;
const host = getEngine(engine);
const containerName = `${id}-${publicPort}`;
const found = await checkContainer(engine, containerName, true);
const foundDependentContainer = await checkContainer(engine, id, true);
let dependentId = id;
if (type === 'wordpressftp') dependentId = `${id}-ftp`;
const foundDependentContainer = await checkContainer(engine, dependentId, true);
try {
if (foundDependentContainer && !found) {
const { stdout: Config } = await asyncExecShell(
@ -210,7 +211,6 @@ export async function startTcpProxy(
const containerName = `haproxy-for-${publicPort}`;
const found = await checkContainer(engine, containerName, true);
const foundDependentContainer = await checkContainer(engine, id, true);
try {
if (foundDependentContainer && !found) {
const { stdout: Config } = await asyncExecShell(

View File

@ -71,7 +71,7 @@ export default async function (): Promise<void | {
ftpPublicPort,
`haproxy-for-${ftpPublicPort}`
);
await startTraefikTCPProxy(destinationDocker, `${id}-ftp`, ftpPublicPort, 22);
await startTraefikTCPProxy(destinationDocker, id, ftpPublicPort, 22, 'wordpressftp');
} else {
await stopTcpHttpProxy(id, destinationDocker, ftpPublicPort, `${id}-${ftpPublicPort}`);
await startTcpProxy(destinationDocker, `${id}-ftp`, ftpPublicPort, 22);

View File

@ -36,49 +36,51 @@ export const post: RequestHandler = async (event) => {
});
const {
service: { destinationDockerId, destinationDocker },
ftpPublicPort: oldPublicPort,
ftpPublicPort,
ftpUser: user,
ftpPassword: savedPassword,
ftpHostKey,
ftpHostKeyPrivate
} = data;
if (user) ftpUser = user;
if (savedPassword) ftpPassword = decrypt(savedPassword);
const { network, engine } = destinationDocker;
const settings = await db.prisma.setting.findFirst();
const host = getEngine(engine);
if (ftpEnabled) {
if (user) ftpUser = user;
if (savedPassword) ftpPassword = decrypt(savedPassword);
const { stdout: password } = await asyncExecShell(
`echo ${ftpPassword} | openssl passwd -1 -stdin`
);
if (destinationDockerId) {
try {
await fs.stat(hostkeyDir);
} catch (error) {
await asyncExecShell(`mkdir -p ${hostkeyDir}`);
}
if (!ftpHostKey) {
await asyncExecShell(
`ssh-keygen -t ed25519 -f ssh_host_ed25519_key -N "" -q -f ${hostkeyDir}/${id}.ed25519`
);
const { stdout: ftpHostKey } = await asyncExecShell(`cat ${hostkeyDir}/${id}.ed25519`);
await db.prisma.wordpress.update({
where: { serviceId: id },
data: { ftpHostKey: encrypt(ftpHostKey) }
});
} else {
await asyncExecShell(`echo "${decrypt(ftpHostKey)}" > ${hostkeyDir}/${id}.ed25519`);
}
if (!ftpHostKeyPrivate) {
await asyncExecShell(`ssh-keygen -t rsa -b 4096 -N "" -f ${hostkeyDir}/${id}.rsa`);
const { stdout: ftpHostKeyPrivate } = await asyncExecShell(`cat ${hostkeyDir}/${id}.rsa`);
await db.prisma.wordpress.update({
where: { serviceId: id },
data: { ftpHostKeyPrivate: encrypt(ftpHostKeyPrivate) }
});
} else {
await asyncExecShell(`echo "${decrypt(ftpHostKeyPrivate)}" > ${hostkeyDir}/${id}.rsa`);
}
const { stdout: password } = await asyncExecShell(
`echo ${ftpPassword} | openssl passwd -1 -stdin`
);
if (destinationDockerId) {
try {
await fs.stat(hostkeyDir);
} catch (error) {
await asyncExecShell(`mkdir -p ${hostkeyDir}`);
}
if (!ftpHostKey) {
await asyncExecShell(
`ssh-keygen -t ed25519 -f ssh_host_ed25519_key -N "" -q -f ${hostkeyDir}/${id}.ed25519`
);
const { stdout: ftpHostKey } = await asyncExecShell(`cat ${hostkeyDir}/${id}.ed25519`);
await db.prisma.wordpress.update({
where: { serviceId: id },
data: { ftpHostKey: encrypt(ftpHostKey) }
});
} else {
await asyncExecShell(`echo "${decrypt(ftpHostKey)}" > ${hostkeyDir}/${id}.ed25519`);
}
if (!ftpHostKeyPrivate) {
await asyncExecShell(`ssh-keygen -t rsa -b 4096 -N "" -f ${hostkeyDir}/${id}.rsa`);
const { stdout: ftpHostKeyPrivate } = await asyncExecShell(`cat ${hostkeyDir}/${id}.rsa`);
await db.prisma.wordpress.update({
where: { serviceId: id },
data: { ftpHostKeyPrivate: encrypt(ftpHostKeyPrivate) }
});
} else {
await asyncExecShell(`echo "${decrypt(ftpHostKeyPrivate)}" > ${hostkeyDir}/${id}.rsa`);
}
const { network, engine } = destinationDocker;
const host = getEngine(engine);
if (ftpEnabled) {
await db.prisma.wordpress.update({
where: { serviceId: id },
data: {
@ -147,28 +149,7 @@ export const post: RequestHandler = async (event) => {
await asyncExecShell(
`DOCKER_HOST=${host} docker compose -f ${hostkeyDir}/${id}-docker-compose.yml up -d`
);
const settings = await db.prisma.setting.findFirst();
if (settings.isTraefikUsed) {
await startTraefikTCPProxy(destinationDocker, `${id}-ftp`, publicPort, 22);
} else {
await startTcpProxy(destinationDocker, `${id}-ftp`, publicPort, 22);
}
} else {
await db.prisma.wordpress.update({
where: { serviceId: id },
data: { ftpPublicPort: null }
});
try {
await asyncExecShell(
`DOCKER_HOST=${host} docker stop -t 0 ${id}-ftp && docker rm ${id}-ftp`
);
} catch (error) {
//
}
await stopTcpHttpProxy(destinationDocker, oldPublicPort);
}
}
if (ftpEnabled) {
return {
status: 201,
body: {
@ -178,6 +159,18 @@ export const post: RequestHandler = async (event) => {
}
};
} else {
await db.prisma.wordpress.update({
where: { serviceId: id },
data: { ftpPublicPort: null }
});
try {
await asyncExecShell(
`DOCKER_HOST=${host} docker stop -t 0 ${id}-ftp && docker rm ${id}-ftp`
);
} catch (error) {
//
}
await stopTcpHttpProxy(id, destinationDocker, ftpPublicPort);
return {
status: 200,
body: {}

View File

@ -166,7 +166,7 @@
await post(`/update.json`, { type: to });
const data = await get(`/settings.json`);
$isTraefikUsed = data.settings.isTraefikUsed;
return toast.push('Proxy migration completed.');
return toast.push('Proxy migration started, it takes a few seconds.');
} catch ({ error }) {
return errorNotification(error);
} finally {

View File

@ -64,12 +64,12 @@ export const post: RequestHandler = async (event) => {
}
} else if (type === 'traefik') {
try {
const found = await checkContainer('/var/run/docker.sock', 'coolify-haproxy');
if (found) {
await asyncExecShell(`docker stop -t 0 coolify-haproxy`);
await asyncExecShell(`docker rm coolify-haproxy`);
}
await startTraefikProxy('/var/run/docker.sock');
// const found = await checkContainer('/var/run/docker.sock', 'coolify-haproxy');
// if (found) {
// await asyncExecShell(`docker stop -t 0 coolify-haproxy`);
// await asyncExecShell(`docker rm coolify-haproxy`);
// }
// await startTraefikProxy('/var/run/docker.sock');
await db.prisma.setting.update({
where: { id: settings.id },
data: { isTraefikUsed: true }
@ -83,12 +83,12 @@ export const post: RequestHandler = async (event) => {
}
} else if (type === 'haproxy') {
try {
const found = await checkContainer('/var/run/docker.sock', 'coolify-proxy');
if (found) {
await asyncExecShell(`docker stop -t 0 coolify-proxy`);
await asyncExecShell(`docker rm coolify-proxy`);
}
await startCoolifyProxy('/var/run/docker.sock');
// const found = await checkContainer('/var/run/docker.sock', 'coolify-proxy');
// if (found) {
// await asyncExecShell(`docker stop -t 0 coolify-proxy`);
// await asyncExecShell(`docker rm coolify-proxy`);
// }
// await startCoolifyProxy('/var/run/docker.sock');
await db.prisma.setting.update({
where: { id: settings.id },
data: { isTraefikUsed: false }

View File

@ -40,14 +40,14 @@ export const get: RequestHandler = async (event) => {
traefik = {
[type]: {
routers: {
[`${id}-${publicPort}`]: {
[id]: {
entrypoints: [type],
rule: `Host(\`${domain}\`)`,
service: `${id}-${publicPort}`
service: id
}
},
services: {
[`${id}-${publicPort}`]: {
[id]: {
loadbalancer: {
servers: [{ url: `http://${id}:${privatePort}` }]
}