@take-out/scripts 0.1.16 ā 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/helpers/ssh.ts +31 -8
package/package.json
CHANGED
package/src/helpers/ssh.ts
CHANGED
|
@@ -9,15 +9,38 @@ export async function checkSSHKey(sshKeyPath: string): Promise<void> {
|
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
async function sleep(ms: number) {
|
|
13
|
+
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function testSSHConnection(
|
|
17
|
+
host: string,
|
|
18
|
+
sshKey: string,
|
|
19
|
+
options?: { retries?: number; retryDelayMs?: number }
|
|
20
|
+
): Promise<void> {
|
|
21
|
+
const maxRetries = options?.retries ?? 3
|
|
22
|
+
const retryDelay = options?.retryDelayMs ?? 5000
|
|
23
|
+
|
|
13
24
|
console.info('\nš testing ssh connection...\n')
|
|
14
25
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
26
|
+
let lastError: Error | null = null
|
|
27
|
+
|
|
28
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
29
|
+
try {
|
|
30
|
+
await run(
|
|
31
|
+
`ssh -i ${sshKey} -o StrictHostKeyChecking=no -o ConnectTimeout=10 ${host} "echo 'SSH connection successful'"`,
|
|
32
|
+
{ silent: attempt > 1 }
|
|
33
|
+
)
|
|
34
|
+
console.info('ā
ssh connection verified')
|
|
35
|
+
return
|
|
36
|
+
} catch (err) {
|
|
37
|
+
lastError = err as Error
|
|
38
|
+
if (attempt < maxRetries) {
|
|
39
|
+
console.info(` ssh attempt ${attempt}/${maxRetries} failed, retrying in ${retryDelay / 1000}s...`)
|
|
40
|
+
await sleep(retryDelay)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
22
43
|
}
|
|
44
|
+
|
|
45
|
+
throw new Error(`cannot connect to ${host} after ${maxRetries} attempts - check ssh key: ${sshKey}`)
|
|
23
46
|
}
|