UNPKG

2.36 kBJavaScriptView Raw
1'use strict'
2
3const cli = require('heroku-cli-util')
4const co = require('co')
5const configCmd = require('./config')
6const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
7
8function check (val, message) {
9 if (!val) throw new Error(`${message}.\nUSAGE: heroku spaces:vpn:wait --space my-space vpn-connection-name`)
10}
11
12function * run (context, heroku) {
13 const space = context.flags.space
14 check(space, 'Space name required')
15 const name = context.flags.name || context.args.name
16 check(name, 'VPN connection name required')
17
18 const interval = (typeof context.flags.interval !== 'undefined' ? context.flags.interval : 10) * 1000
19 const timeout = (typeof context.flags.timeout !== 'undefined' ? context.flags.timeout : 20 * 60) * 1000
20 const deadline = new Date(new Date().getTime() + timeout)
21
22 let lib = require('../../lib/vpn-connections')(heroku)
23 let info = yield lib.getVPNConnection(space, name)
24 if (info.status === 'active') {
25 cli.log('VPN has been allocated.')
26 return
27 }
28
29 const spinner = new cli.Spinner({ text: `Waiting for VPN Connection ${cli.color.green(name)} to allocate...` })
30
31 spinner.start()
32
33 do {
34 if ((new Date()).getTime() >= deadline) {
35 throw new Error('Timeout waiting for VPN to become allocated.')
36 }
37
38 if (info.status === 'failed') {
39 throw new Error(info.status_message)
40 }
41
42 yield wait(interval)
43 info = yield lib.getVPNConnection(space, name)
44 } while (info.status !== 'active')
45
46 spinner.stop('done\n')
47
48 var config = yield lib.getVPNConnection(space, name)
49 configCmd.displayVPNConfigInfo(space, name, config)
50}
51
52module.exports = {
53 topic: 'spaces',
54 command: 'vpn:wait',
55 description: 'wait for VPN Connection to be created',
56 needsApp: false,
57 needsAuth: true,
58 args: [{ name: 'name', optional: true, hidden: true }],
59 flags: [
60 { name: 'space', char: 's', hasValue: true, description: 'space the vpn connection belongs to' },
61 { name: 'name', char: 'n', hasValue: true, description: 'name or id of the vpn connection to wait for' },
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}