debug more

This commit is contained in:
Andras Bacsai 2022-09-05 12:09:13 +02:00
parent b24a5d9aca
commit 26d86cbcb5
3 changed files with 43 additions and 9 deletions

View File

@ -5,7 +5,7 @@ import env from '@fastify/env';
import cookie from '@fastify/cookie'; import cookie from '@fastify/cookie';
import path, { join } from 'path'; import path, { join } from 'path';
import autoLoad from '@fastify/autoload'; 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 { scheduler } from './lib/scheduler';
import { compareVersions } from 'compare-versions'; import { compareVersions } from 'compare-versions';
import Graceful from '@ladjs/graceful' 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") // scheduler.workers.has('infrastructure') && scheduler.workers.get('infrastructure').postMessage("action:cleanupPrismaEngines")
// }, 60000) // }, 60000)
await getArch(); await Promise.all([
await getIPAddress(); getArch(),
getIPAddress(),
// configureRemoteDockers(),
])
}); });
async function getIPAddress() { async function getIPAddress() {
console.log('getIPAddress')
const { publicIpv4, publicIpv6 } = await import('public-ip') const { publicIpv4, publicIpv6 } = await import('public-ip')
try { try {
const settings = await listSettings(); const settings = await listSettings();
@ -167,6 +171,7 @@ async function initServer() {
} catch (error) { } } catch (error) { }
} }
async function getArch() { async function getArch() {
console.log('getArch')
try { try {
const settings = await prisma.setting.findFirst({}) const settings = await prisma.setting.findFirst({})
if (settings && !settings.arch) { 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)
}
}

View File

@ -1,7 +1,7 @@
import { parentPort } from 'node:worker_threads'; import { parentPort } from 'node:worker_threads';
import axios from 'axios'; import axios from 'axios';
import { compareVersions } from 'compare-versions'; 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() { async function autoUpdater() {
try { try {
@ -59,15 +59,22 @@ async function checkProxies() {
} }
// Coolify Proxy remote // Coolify Proxy remote
const remoteDocker = await prisma.destinationDocker.findMany({ const remoteDocker = await prisma.destinationDocker.findMany({
where: { engine, isCoolifyProxyUsed: true, remoteEngine: true } where: { remoteEngine: true, remoteVerified: true }
}); });
if (remoteDocker.length > 0) { if (remoteDocker.length > 0) {
for (const docker of remoteDocker) { for (const docker of remoteDocker) {
if (docker.isCoolifyProxyUsed) {
portReachable = await isReachable(80, { host: docker.remoteIpAddress }) portReachable = await isReachable(80, { host: docker.remoteIpAddress })
if (!portReachable) { if (!portReachable) {
await startTraefikProxy(docker.id); await startTraefikProxy(docker.id);
} }
} }
try {
await createRemoteEngineConfiguration(docker.id)
} catch (error) {
console.log({ error })
}
}
} }
// TCP Proxies // TCP Proxies
const databasesWithPublicPort = await prisma.database.findMany({ const databasesWithPublicPort = await prisma.database.findMany({

View File

@ -451,19 +451,24 @@ export async function getFreeSSHLocalPort(id: string): Promise<number | boolean>
} }
export async function createRemoteEngineConfiguration(id: string) { export async function createRemoteEngineConfiguration(id: string) {
const homedir = os.homedir(); const homedir = os.homedir();
const sshKeyFile = `/tmp/id_rsa-${id}` const sshKeyFile = `/tmp/id_rsa-${id}`
console.log(sshKeyFile)
const localPort = await getFreeSSHLocalPort(id); const localPort = await getFreeSSHLocalPort(id);
console.log(localPort)
const { sshKey: { privateKey }, remoteIpAddress, remotePort, remoteUser } = await prisma.destinationDocker.findFirst({ where: { id }, include: { sshKey: true } }) 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 }) await fs.writeFile(sshKeyFile, decrypt(privateKey) + '\n', { encoding: 'utf8', mode: 400 })
// Needed for remote docker compose // 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`) 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) { if (numberOfSSHAgentsRunning !== '' && Number(numberOfSSHAgentsRunning.trim()) == 0) {
await asyncExecShell(`eval $(ssh-agent -sa /tmp/coolify-ssh-agent.pid)`) 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}`) 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`) 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) { if (numberOfSSHTunnelsRunning !== '' && Number(numberOfSSHTunnelsRunning.trim()) == 0) {
try { try {
await asyncExecShell(`SSH_AUTH_SOCK=/tmp/coolify-ssh-agent.pid ssh -F /dev/null -o "StrictHostKeyChecking no" -fNL ${localPort}:localhost:${remotePort} ${remoteUser}@${remoteIpAddress}`) 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) { } catch (error) {
await fs.mkdir(`${homedir}/.ssh/`) await fs.mkdir(`${homedir}/.ssh/`)
} }
console.log(config.toString())
return await fs.writeFile(`${homedir}/.ssh/config`, sshConfig.stringify(config)) 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<any> { export async function executeDockerCmd({ debug, buildId, applicationId, dockerId, command }: { debug?: boolean, buildId?: string, applicationId?: string, dockerId: string, command: string }): Promise<any> {