forked from gewv-tu-dresden/balena-add-ssh-key
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
101 lines (80 loc) · 2.52 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { spawn, exec, ChildProcess } from 'child_process'
import fs from 'fs'
import { NodeSSH } from 'node-ssh'
const ssh = new NodeSSH()
type balenaTunnelOptions = {
uuid: string,
onClose: (uuid: string, proc: ChildProcess, code: number | null) => Promise<void>,
}
type closeTunnelOptions = {
tunnel: ChildProcess,
signal?: number | NodeJS.Signals
}
export async function createFolder() {
const folders = [
'./tmp/balena',
'./tmp/configs',
]
for (const folder of folders) {
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder, { recursive: true })
}
}
}
export async function connectSSH(privatKeyPath: string) {
return ssh.connect({
host: 'localhost',
port: 22222,
username: 'root',
privateKey: fs.readFileSync(privatKeyPath, 'utf8'),
})
}
export async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export async function loginBalenaShell(token: string): Promise<void> {
return new Promise((res, rej) => {
const child = exec(`balena login --token ${token}`)
child.on('exit', function () {
res()
})
child.on('error', function (error) {
rej(error.message)
})
})
}
export async function createBalenaTunnel({ uuid, onClose }: balenaTunnelOptions): Promise<ChildProcess> {
return new Promise((res, rej) => {
const bTunnel = spawn('balena', ['tunnel', uuid, '-p 22222'], { detached: true })
bTunnel.stdout.on("data", data => {
// console.log(`[tunnel - ${uuid.slice(0, 7)}]${data}`);
if (data.includes("Waiting for connections...")) {
res(bTunnel)
}
});
bTunnel.stderr.on("data", data => {
// console.log(`[error - tunnel - ${uuid.slice(0, 7)}]${data}`);
if (data.includes("No ports are valid for tunnelling")) {
rej("The target port 22222 is already in use!")
}
});
bTunnel.on('error', (error) => {
rej(error.message)
});
bTunnel.on('exit', (code) => {
if (bTunnel.pid != null) {
process.kill(-bTunnel.pid)
}
})
bTunnel.on("close", code => {
onClose(uuid, bTunnel, code)
});
})
}
export async function closeTunnel({ tunnel, signal }: closeTunnelOptions): Promise<void> {
tunnel.kill(signal)
while (!tunnel.killed) {
await sleep(100)
}
return
}