feat: add ssh connection retry

This commit is contained in:
Justin Lin committed 2024-10-14 17:31:35 +08:00
1 parent 251139c159
commit 31301419ad
4 files changed
+64 -44

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
import { Client } from 'ssh2'
import { Duplex } from 'stream'
import config from '../config.js'
export async function connectToSSH(
containerIp: string,
{ token }: { token: string }
): Promise<Duplex> {
const conn = new Client()
return new Promise((resolve, reject) => {
conn
.on('ready', () => {
console.log('SSH connection ready')
conn.shell(
{
env: {
TOKEN: token,
API_ENDPOINT: config.backendUrl,
ZDOTDIR: '/etc/zsh',
NODE_PATH: '/usr/src/node_modules',
},
},
(err, stream) => {
if (err) {
reject(err)
}
stream.on('close', () => {
console.log('Stream :: close')
conn.end()
})
resolve(stream)
}
)
})
.on('error', (err) => {
reject(err)
})
.connect({
host: containerIp,
port: 22,
username: 'commander',
privateKey: config.docker.keypair.privateKey,
debug: console.log,
timeout: 5000,
})
})
}