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

View File

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

View File

@ -36,49 +36,51 @@ export const post: RequestHandler = async (event) => {
}); });
const { const {
service: { destinationDockerId, destinationDocker }, service: { destinationDockerId, destinationDocker },
ftpPublicPort: oldPublicPort, ftpPublicPort,
ftpUser: user, ftpUser: user,
ftpPassword: savedPassword, ftpPassword: savedPassword,
ftpHostKey, ftpHostKey,
ftpHostKeyPrivate ftpHostKeyPrivate
} = data; } = data;
if (user) ftpUser = user; const { network, engine } = destinationDocker;
if (savedPassword) ftpPassword = decrypt(savedPassword); 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({ await db.prisma.wordpress.update({
where: { serviceId: id }, where: { serviceId: id },
data: { data: {
@ -147,28 +149,7 @@ export const post: RequestHandler = async (event) => {
await asyncExecShell( await asyncExecShell(
`DOCKER_HOST=${host} docker compose -f ${hostkeyDir}/${id}-docker-compose.yml up -d` `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 { return {
status: 201, status: 201,
body: { body: {
@ -178,6 +159,18 @@ export const post: RequestHandler = async (event) => {
} }
}; };
} else { } 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 { return {
status: 200, status: 200,
body: {} body: {}

View File

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

View File

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

View File

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