From 132707caa71555516ad92ad97d152a7e2d1da634 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 7 Dec 2022 14:46:12 +0100 Subject: [PATCH] fix: rde --- apps/api/src/lib/common.ts | 4 +- .../routes/api/v1/destinations/handlers.ts | 47 +++++++++++-------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/apps/api/src/lib/common.ts b/apps/api/src/lib/common.ts index abf967c30..70173a31e 100644 --- a/apps/api/src/lib/common.ts +++ b/apps/api/src/lib/common.ts @@ -565,9 +565,9 @@ export async function executeCommand({ command, dockerId = null, sshCommand = fa } if (sshCommand) { if (shell) { - return execaCommand(`ssh ${remoteIpAddress}-remote ${command}`, { shell: true, stdio: 'inherit' }); + return execaCommand(`ssh ${remoteIpAddress}-remote ${command}`); } - return await execa('ssh', [`${remoteIpAddress}-remote`, ...dockerArgs]); + return await execa('ssh', [`${remoteIpAddress}-remote`, dockerCommand, ...dockerArgs]); } if (stream) { return await new Promise(async (resolve, reject) => { diff --git a/apps/api/src/routes/api/v1/destinations/handlers.ts b/apps/api/src/routes/api/v1/destinations/handlers.ts index e376d33ea..f5ccb2614 100644 --- a/apps/api/src/routes/api/v1/destinations/handlers.ts +++ b/apps/api/src/routes/api/v1/destinations/handlers.ts @@ -103,7 +103,7 @@ export async function newDestination(request: FastifyRequest, re return reply.code(201).send({ id: destination.id }); } else { const destination = await prisma.destinationDocker.create({ - data: { name, teams: { connect: { id: teamId } }, engine, network, isCoolifyProxyUsed, remoteEngine: true, remoteIpAddress, remoteUser, remotePort } + data: { name, teams: { connect: { id: teamId } }, engine, network, isCoolifyProxyUsed, remoteEngine: true, remoteIpAddress, remoteUser, remotePort: Number(remotePort) } }); return reply.code(201).send({ id: destination.id }) } @@ -203,22 +203,31 @@ export async function assignSSHKey(request: FastifyRequest) { } } export async function verifyRemoteDockerEngineFn(id: string) { - await createRemoteEngineConfiguration(id); const { remoteIpAddress, network, isCoolifyProxyUsed } = await prisma.destinationDocker.findFirst({ where: { id } }) - const host = `ssh://${remoteIpAddress}-remote` - const { stdout } = await executeCommand({ command: `docker network ls --filter 'name=${network}' --no-trunc --format "{{json .}}"`, dockerId: id }); - if (!stdout) { + const daemonJson = `daemon-${id}.json` + try { + await executeCommand({ sshCommand: true, command: `docker network inspect ${network}`, dockerId: id }); + } catch (error) { await executeCommand({ command: `docker network create --attachable ${network}`, dockerId: id }); } - const { stdout: coolifyNetwork } = await executeCommand({ command: `docker network ls --filter 'name=coolify-infra' --no-trunc --format "{{json .}}"`, dockerId: id }); - if (!coolifyNetwork) { + + try { + await executeCommand({ sshCommand: true, command: `docker network inspect coolify-infra`, dockerId: id }); + } catch (error) { await executeCommand({ command: `docker network create --attachable coolify-infra`, dockerId: id }); } + if (isCoolifyProxyUsed) await startTraefikProxy(id); + let isUpdated = false; + let daemonJsonParsed = { + "live-restore": true, + "features": { + "buildkit": true + } + }; try { const { stdout: daemonJson } = await executeCommand({ sshCommand: true, dockerId: id, command: `cat /etc/docker/daemon.json` }); - let daemonJsonParsed = JSON.parse(daemonJson); - let isUpdated = false; + daemonJsonParsed = JSON.parse(daemonJson); if (!daemonJsonParsed['live-restore'] || daemonJsonParsed['live-restore'] !== true) { isUpdated = true; daemonJsonParsed['live-restore'] = true @@ -230,21 +239,19 @@ export async function verifyRemoteDockerEngineFn(id: string) { buildkit: true } } + } catch (error) { + isUpdated = true; + } + try { if (isUpdated) { - await executeCommand({ sshCommand: true, shell: true, dockerId: id, command: `echo '${JSON.stringify(daemonJsonParsed)}' > /etc/docker/daemon.json` }); + await executeCommand({ shell: true, command: `echo '${JSON.stringify(daemonJsonParsed, null, 2)}' > /tmp/${daemonJson}` }) + await executeCommand({ dockerId: id, command: `scp /tmp/${daemonJson} ${remoteIpAddress}-remote:/etc/docker/daemon.json` }); + await executeCommand({ command: `rm /tmp/${daemonJson}` }) await executeCommand({ sshCommand: true, dockerId: id, command: `systemctl restart docker` }); } - } catch (error) { - const daemonJsonParsed = { - "live-restore": true, - "features": { - "buildkit": true - } - } - await executeCommand({ sshCommand: true, shell: true, dockerId: id, command: `echo '${JSON.stringify(daemonJsonParsed)}' > /etc/docker/daemon.json` }); - await executeCommand({ sshCommand: true, dockerId: id, command: `systemctl restart docker` }); - } finally { await prisma.destinationDocker.update({ where: { id }, data: { remoteVerified: true } }) + } catch (error) { + throw new Error('Error while verifying remote docker engine') } } export async function verifyRemoteDockerEngine(request: FastifyRequest, reply: FastifyReply) {