@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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/helpers/ssh.ts +31 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@take-out/scripts",
3
- "version": "0.1.16",
3
+ "version": "0.1.18",
4
4
  "type": "module",
5
5
  "main": "./src/run.ts",
6
6
  "sideEffects": false,
@@ -9,15 +9,38 @@ export async function checkSSHKey(sshKeyPath: string): Promise<void> {
9
9
  }
10
10
  }
11
11
 
12
- export async function testSSHConnection(host: string, sshKey: string): Promise<void> {
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
- try {
16
- await run(
17
- `ssh -i ${sshKey} -o StrictHostKeyChecking=no -o ConnectTimeout=5 ${host} "echo 'SSH connection successful'"`
18
- )
19
- console.info('āœ… ssh connection verified')
20
- } catch {
21
- throw new Error(`cannot connect to ${host} - check ssh key: ${sshKey}`)
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
  }