-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathwait.ts
103 lines (91 loc) · 3.19 KB
/
wait.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
102
103
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import heredoc from 'tsheredoc'
import Spinner from '@oclif/core/lib/cli-ux/action/spinner'
import debug from 'debug'
import {renderInfo} from '../../lib/spaces/spaces'
import {Notification, notify} from '@heroku-cli/notifications'
import {IncomingHttpHeaders} from 'node:http'
import {SpaceNat} from '../../lib/types/fir'
import {SpaceWithOutboundIps} from '../../lib/types/spaces'
const spacesDebug = debug('spaces:wait')
export default class Wait extends Command {
static topic = 'spaces'
static description = 'wait for a space to be created'
static flags = {
space: flags.string({char: 's', description: 'space to get info of'}),
json: flags.boolean({description: 'output in json format'}),
interval: flags.integer({
char: 'i',
description: 'seconds to wait between poll intervals',
default: 30,
}),
timeout: flags.integer({
char: 't',
description: 'maximum number of seconds to wait',
default: 25 * 60,
}),
}
static args = {
space: Args.string({hidden: true}),
}
public async run(): Promise<void> {
const {flags, args} = await this.parse(Wait)
const spaceName = flags.space || args.space
if (!spaceName) {
ux.error(heredoc(`
Error: Missing 1 required arg:
space
See more help with --help
`))
}
const interval = flags.interval * 1000
const timeout = flags.timeout * 1000
const deadline = new Date(Date.now() + timeout)
const action = new Spinner()
action.start(`Waiting for space ${color.green(spaceName as string)} to allocate`)
const headers: IncomingHttpHeaders = {
Accept: 'application/vnd.heroku+json; version=3.fir',
}
if (!flags.json) {
headers['Accept-Expansion'] = 'region'
}
let {body: space} = await this.heroku.get<SpaceWithOutboundIps>(`/spaces/${spaceName}`, {headers})
while (space.state === 'allocating') {
if (new Date() > deadline) {
throw new Error('Timeout waiting for space to become allocated.')
}
await this.wait(interval)
const {body: updatedSpace} = await this.heroku.get<SpaceWithOutboundIps>(`/spaces/${spaceName}`, {headers})
space = updatedSpace
}
try {
const {body: nat} = await this.heroku.get<SpaceNat>(`/spaces/${spaceName}/nat`, {headers: {Accept: 'application/vnd.heroku+json; version=3.fir'}})
space.outbound_ips = nat
} catch (error) {
spacesDebug(`Retrieving NAT details for the space failed with ${error}`)
}
action.stop()
renderInfo(space, flags.json)
this.notify(spaceName as string)
}
protected wait(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}
protected notify(spaceName: string) {
try {
const notification: Notification & {
sound?: boolean, message?: string, title?: string, subtitle?: string
} = {
title: spaceName,
subtitle: `heroku spaces:wait ${spaceName}`,
message: 'space was successfully created',
sound: true,
}
notify(notification)
} catch (error: any) {
ux.warn(error)
}
}
}