1 |
|
2 | 'use strict'
|
3 | let cli = require('heroku-cli-util')
|
4 | let co = require('co')
|
5 | let info = require('./info')
|
6 | let wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
7 |
|
8 | function * run (context, heroku) {
|
9 | const spaceName = context.flags.space || context.args.space
|
10 | if (!spaceName) throw new Error('Space name required.\nUSAGE: heroku spaces:wait my-space')
|
11 |
|
12 | const interval = (typeof context.flags.interval !== 'undefined' ? context.flags.interval : 30) * 1000
|
13 | const timeout = (typeof context.flags.timeout !== 'undefined' ? context.flags.timeout : 25 * 60) * 1000
|
14 | const deadline = new Date(new Date().getTime() + timeout)
|
15 | const spinner = new cli.Spinner({ text: `Waiting for space ${cli.color.green(spaceName)} to allocate...` })
|
16 |
|
17 | spinner.start()
|
18 |
|
19 | let headers = {}
|
20 | if (!context.flags.json) {
|
21 | headers = { 'Accept-Expansion': 'region' }
|
22 | }
|
23 |
|
24 | let space = yield heroku.get(`/spaces/${spaceName}`, { headers })
|
25 | while (space.state === 'allocating') {
|
26 | if ((new Date()).getTime() >= deadline) {
|
27 | throw new Error('Timeout waiting for space to become allocated.')
|
28 | }
|
29 | yield wait(interval)
|
30 | space = yield heroku.get(`/spaces/${spaceName}`, { headers })
|
31 | }
|
32 |
|
33 | space.outbound_ips = yield heroku.get(`/spaces/${spaceName}/nat`)
|
34 | spinner.stop('done\n')
|
35 |
|
36 | info.render(space, context.flags)
|
37 | _notify(context.flags['space'] || context.args['space'])
|
38 | }
|
39 |
|
40 | function _notify (spaceName) {
|
41 | try {
|
42 | const { notify } = require('@heroku-cli/notifications')
|
43 | notify({
|
44 | title: `${spaceName}`,
|
45 | subtitle: `heroku spaces:wait ${spaceName}`,
|
46 | message: 'space was successfully created',
|
47 | sound: true
|
48 | })
|
49 | } catch (err) {
|
50 | cli.warn(err)
|
51 | }
|
52 | }
|
53 |
|
54 | module.exports = {
|
55 | topic: 'spaces',
|
56 | command: 'wait',
|
57 | description: 'wait for a space to be created',
|
58 | needsAuth: true,
|
59 | args: [{ name: 'space', optional: true, hidden: true }],
|
60 | flags: [
|
61 | { name: 'space', char: 's', hasValue: true, description: 'space to get info of' },
|
62 | { name: 'json', description: 'output in json format' },
|
63 | { name: 'interval', char: 'i', hasValue: true, description: 'seconds to wait between poll intervals' },
|
64 | { name: 'timeout', char: 't', hasValue: true, description: 'maximum number of seconds to wait' }
|
65 | ],
|
66 | run: cli.command(co.wrap(run))
|
67 | }
|