fix: multiplex ssh connections

This commit is contained in:
Andras Bacsai 2022-09-22 09:02:53 +02:00
parent 550150d685
commit 510a748749

View File

@ -113,7 +113,7 @@ export const asyncExecShellStream = async ({
line: `${line.replace('\n', '')}`, line: `${line.replace('\n', '')}`,
buildId, buildId,
applicationId applicationId
}); });
} }
} }
}); });
@ -513,7 +513,7 @@ export async function createRemoteEngineConfiguration(id: string) {
try { try {
await fs.stat(`/tmp/coolify-ssh-agent.pid`); await fs.stat(`/tmp/coolify-ssh-agent.pid`);
await fs.rm(`/tmp/coolify-ssh-agent.pid`); await fs.rm(`/tmp/coolify-ssh-agent.pid`);
} catch (error) {} } catch (error) { }
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}`);
@ -526,9 +526,17 @@ export async function createRemoteEngineConfiguration(id: string) {
await asyncExecShell( await asyncExecShell(
`SSH_AUTH_SOCK=/tmp/coolify-ssh-agent.pid ssh -F /dev/null -o "StrictHostKeyChecking no" -fNL ${localPort}:localhost:${remotePort} ${remoteUser}@${remoteIpAddress}` `SSH_AUTH_SOCK=/tmp/coolify-ssh-agent.pid ssh -F /dev/null -o "StrictHostKeyChecking no" -fNL ${localPort}:localhost:${remotePort} ${remoteUser}@${remoteIpAddress}`
); );
} catch (error) {} } catch (error) { }
} }
const config = sshConfig.parse(''); const config = sshConfig.parse('');
const foundWildcard = config.find({ Host: '*' });
if (!foundWildcard) {
config.append({
Host: '*',
ControlMaster: 'auto',
ControlPath: `${homedir}/.ssh/coolify-%r@%h:%p`,
})
}
const found = config.find({ Host: remoteIpAddress }); const found = config.find({ Host: remoteIpAddress });
if (!found) { if (!found) {
config.append({ config.append({
@ -540,6 +548,7 @@ export async function createRemoteEngineConfiguration(id: string) {
StrictHostKeyChecking: 'no' StrictHostKeyChecking: 'no'
}); });
} }
try { try {
await fs.stat(`${homedir}/.ssh/`); await fs.stat(`${homedir}/.ssh/`);
} catch (error) { } catch (error) {
@ -738,32 +747,32 @@ type DatabaseConfiguration = {
}; };
} }
| { | {
volume: string; volume: string;
image: string; image: string;
command?: string; command?: string;
ulimits: Record<string, unknown>; ulimits: Record<string, unknown>;
privatePort: number; privatePort: number;
environmentVariables: { environmentVariables: {
MONGO_INITDB_ROOT_USERNAME?: string; MONGO_INITDB_ROOT_USERNAME?: string;
MONGO_INITDB_ROOT_PASSWORD?: string; MONGO_INITDB_ROOT_PASSWORD?: string;
MONGODB_ROOT_USER?: string; MONGODB_ROOT_USER?: string;
MONGODB_ROOT_PASSWORD?: string; MONGODB_ROOT_PASSWORD?: string;
}; };
} }
| { | {
volume: string; volume: string;
image: string; image: string;
command?: string; command?: string;
ulimits: Record<string, unknown>; ulimits: Record<string, unknown>;
privatePort: number; privatePort: number;
environmentVariables: { environmentVariables: {
MARIADB_ROOT_USER: string; MARIADB_ROOT_USER: string;
MARIADB_ROOT_PASSWORD: string; MARIADB_ROOT_PASSWORD: string;
MARIADB_USER: string; MARIADB_USER: string;
MARIADB_PASSWORD: string; MARIADB_PASSWORD: string;
MARIADB_DATABASE: string; MARIADB_DATABASE: string;
}; };
} }
| { | {
volume: string; volume: string;
image: string; image: string;
@ -919,9 +928,8 @@ export function generateDatabaseConfiguration(database: any, arch: string): Data
}; };
if (isARM(arch)) { if (isARM(arch)) {
configuration.volume = `${id}-${type}-data:/data`; configuration.volume = `${id}-${type}-data:/data`;
configuration.command = `/usr/local/bin/redis-server --appendonly ${ configuration.command = `/usr/local/bin/redis-server --appendonly ${appendOnly ? 'yes' : 'no'
appendOnly ? 'yes' : 'no' } --requirepass ${dbUserPassword}`;
} --requirepass ${dbUserPassword}`;
} }
return configuration; return configuration;
} else if (type === 'couchdb') { } else if (type === 'couchdb') {
@ -1005,12 +1013,12 @@ export type ComposeFileService = {
command?: string; command?: string;
ports?: string[]; ports?: string[];
build?: build?:
| { | {
context: string; context: string;
dockerfile: string; dockerfile: string;
args?: Record<string, unknown>; args?: Record<string, unknown>;
} }
| string; | string;
deploy?: { deploy?: {
restart_policy?: { restart_policy?: {
condition?: string; condition?: string;
@ -1079,7 +1087,7 @@ export const createDirectories = async ({
let workdirFound = false; let workdirFound = false;
try { try {
workdirFound = !!(await fs.stat(workdir)); workdirFound = !!(await fs.stat(workdir));
} catch (error) {} } catch (error) { }
if (workdirFound) { if (workdirFound) {
await asyncExecShell(`rm -fr ${workdir}`); await asyncExecShell(`rm -fr ${workdir}`);
} }
@ -1428,6 +1436,9 @@ export async function getServiceFromDB({
where: { id, teams: { some: { id: teamId === '0' ? undefined : teamId } } }, where: { id, teams: { some: { id: teamId === '0' ? undefined : teamId } } },
include: includeServices include: includeServices
}); });
if (!body) {
return null
}
let { type } = body; let { type } = body;
type = fixType(type); type = fixType(type);
@ -1595,7 +1606,7 @@ export async function stopBuild(buildId, applicationId) {
} }
} }
count++; count++;
} catch (error) {} } catch (error) { }
}, 100); }, 100);
}); });
} }
@ -1626,7 +1637,7 @@ export async function cleanupDockerStorage(dockerId, lowDiskSpace, force) {
if (images) { if (images) {
await executeDockerCmd({ dockerId, command: `docker rmi -f ${images}" -q | xargs -r` }); await executeDockerCmd({ dockerId, command: `docker rmi -f ${images}" -q | xargs -r` });
} }
} catch (error) {} } catch (error) { }
if (lowDiskSpace || force) { if (lowDiskSpace || force) {
if (isDev) { if (isDev) {
if (!force) console.log(`[DEV MODE] Low disk space: ${lowDiskSpace}`); if (!force) console.log(`[DEV MODE] Low disk space: ${lowDiskSpace}`);
@ -1637,17 +1648,17 @@ export async function cleanupDockerStorage(dockerId, lowDiskSpace, force) {
dockerId, dockerId,
command: `docker container prune -f --filter "label=coolify.managed=true"` command: `docker container prune -f --filter "label=coolify.managed=true"`
}); });
} catch (error) {} } catch (error) { }
try { try {
await executeDockerCmd({ dockerId, command: `docker image prune -f` }); await executeDockerCmd({ dockerId, command: `docker image prune -f` });
} catch (error) {} } catch (error) { }
try { try {
await executeDockerCmd({ dockerId, command: `docker image prune -a -f` }); await executeDockerCmd({ dockerId, command: `docker image prune -a -f` });
} catch (error) {} } catch (error) { }
// Cleanup build caches // Cleanup build caches
try { try {
await executeDockerCmd({ dockerId, command: `docker builder prune -a -f` }); await executeDockerCmd({ dockerId, command: `docker builder prune -a -f` });
} catch (error) {} } catch (error) { }
} }
} }