From 26d86cbcb563e69f62e8d6852bfbb9be189f976f Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 5 Sep 2022 12:09:13 +0200 Subject: [PATCH] debug more --- apps/api/src/index.ts | 27 ++++++++++++++++++++++++--- apps/api/src/jobs/infrastructure.ts | 19 +++++++++++++------ apps/api/src/lib/common.ts | 6 ++++++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index 8c81b0350..9befdd981 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -5,7 +5,7 @@ import env from '@fastify/env'; import cookie from '@fastify/cookie'; import path, { join } from 'path'; import autoLoad from '@fastify/autoload'; -import { asyncExecShell, isDev, listSettings, prisma, version } from './lib/common'; +import { asyncExecShell, createRemoteEngineConfiguration, isDev, listSettings, prisma, version } from './lib/common'; import { scheduler } from './lib/scheduler'; import { compareVersions } from 'compare-versions'; import Graceful from '@ladjs/graceful' @@ -136,10 +136,14 @@ fastify.listen({ port, host }, async (err: any, address: any) => { // scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:cleanupPrismaEngines") // }, 60000) - await getArch(); - await getIPAddress(); + await Promise.all([ + getArch(), + getIPAddress(), + // configureRemoteDockers(), + ]) }); async function getIPAddress() { + console.log('getIPAddress') const { publicIpv4, publicIpv6 } = await import('public-ip') try { const settings = await listSettings(); @@ -167,6 +171,7 @@ async function initServer() { } catch (error) { } } async function getArch() { + console.log('getArch') try { const settings = await prisma.setting.findFirst({}) if (settings && !settings.arch) { @@ -176,3 +181,19 @@ async function getArch() { } + +async function configureRemoteDockers() { + console.log('configureRemoteDockers') + try { + const remoteDocker = await prisma.destinationDocker.findMany({ + where: { remoteVerified: true, remoteEngine: true } + }); + if (remoteDocker.length > 0) { + for (const docker of remoteDocker) { + await createRemoteEngineConfiguration(docker.id) + } + } + } catch (error) { + console.log(error) + } +} diff --git a/apps/api/src/jobs/infrastructure.ts b/apps/api/src/jobs/infrastructure.ts index 0dda946b7..97369fb3a 100644 --- a/apps/api/src/jobs/infrastructure.ts +++ b/apps/api/src/jobs/infrastructure.ts @@ -1,7 +1,7 @@ import { parentPort } from 'node:worker_threads'; import axios from 'axios'; import { compareVersions } from 'compare-versions'; -import { asyncExecShell, cleanupDockerStorage, executeDockerCmd, isDev, prisma, startTraefikTCPProxy, generateDatabaseConfiguration, startTraefikProxy, listSettings, version } from '../lib/common'; +import { asyncExecShell, cleanupDockerStorage, executeDockerCmd, isDev, prisma, startTraefikTCPProxy, generateDatabaseConfiguration, startTraefikProxy, listSettings, version, createRemoteEngineConfiguration } from '../lib/common'; async function autoUpdater() { try { @@ -45,7 +45,7 @@ async function checkProxies() { let portReachable; const { arch, ipv4, ipv6 } = await listSettings(); - + // Coolify Proxy local const engine = '/var/run/docker.sock'; const localDocker = await prisma.destinationDocker.findFirst({ @@ -59,13 +59,20 @@ async function checkProxies() { } // Coolify Proxy remote const remoteDocker = await prisma.destinationDocker.findMany({ - where: { engine, isCoolifyProxyUsed: true, remoteEngine: true } + where: { remoteEngine: true, remoteVerified: true } }); if (remoteDocker.length > 0) { for (const docker of remoteDocker) { - portReachable = await isReachable(80, { host: docker.remoteIpAddress }) - if (!portReachable) { - await startTraefikProxy(docker.id); + if (docker.isCoolifyProxyUsed) { + portReachable = await isReachable(80, { host: docker.remoteIpAddress }) + if (!portReachable) { + await startTraefikProxy(docker.id); + } + } + try { + await createRemoteEngineConfiguration(docker.id) + } catch (error) { + console.log({ error }) } } } diff --git a/apps/api/src/lib/common.ts b/apps/api/src/lib/common.ts index 6b8a84b0c..b97a6dfbf 100644 --- a/apps/api/src/lib/common.ts +++ b/apps/api/src/lib/common.ts @@ -451,19 +451,24 @@ export async function getFreeSSHLocalPort(id: string): Promise } export async function createRemoteEngineConfiguration(id: string) { + const homedir = os.homedir(); const sshKeyFile = `/tmp/id_rsa-${id}` + console.log(sshKeyFile) const localPort = await getFreeSSHLocalPort(id); + console.log(localPort) const { sshKey: { privateKey }, remoteIpAddress, remotePort, remoteUser } = await prisma.destinationDocker.findFirst({ where: { id }, include: { sshKey: true } }) await fs.writeFile(sshKeyFile, decrypt(privateKey) + '\n', { encoding: 'utf8', mode: 400 }) // Needed for remote docker compose const { stdout: numberOfSSHAgentsRunning } = await asyncExecShell(`ps ax | grep [s]sh-agent | grep coolify-ssh-agent.pid | grep -v grep | wc -l`) + console.log({ numberOfSSHAgentsRunning }) if (numberOfSSHAgentsRunning !== '' && Number(numberOfSSHAgentsRunning.trim()) == 0) { await asyncExecShell(`eval $(ssh-agent -sa /tmp/coolify-ssh-agent.pid)`) } await asyncExecShell(`SSH_AUTH_SOCK=/tmp/coolify-ssh-agent.pid ssh-add -q ${sshKeyFile}`) const { stdout: numberOfSSHTunnelsRunning } = await asyncExecShell(`ps ax | grep 'ssh -F /dev/null -o StrictHostKeyChecking no -fNL ${localPort}:localhost:${remotePort}' | grep -v grep | wc -l`) + console.log({ numberOfSSHTunnelsRunning }) if (numberOfSSHTunnelsRunning !== '' && Number(numberOfSSHTunnelsRunning.trim()) == 0) { try { await asyncExecShell(`SSH_AUTH_SOCK=/tmp/coolify-ssh-agent.pid ssh -F /dev/null -o "StrictHostKeyChecking no" -fNL ${localPort}:localhost:${remotePort} ${remoteUser}@${remoteIpAddress}`) @@ -490,6 +495,7 @@ export async function createRemoteEngineConfiguration(id: string) { } catch (error) { await fs.mkdir(`${homedir}/.ssh/`) } + console.log(config.toString()) return await fs.writeFile(`${homedir}/.ssh/config`, sshConfig.stringify(config)) } export async function executeDockerCmd({ debug, buildId, applicationId, dockerId, command }: { debug?: boolean, buildId?: string, applicationId?: string, dockerId: string, command: string }): Promise {