mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: add ssh connection retry
This commit is contained in:
1 parent
251139c159
commit
31301419ad
4 files changed
+64
-44
No files matched your search
@@ -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,
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user