-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheroku.js
More file actions
85 lines (75 loc) · 1.7 KB
/
heroku.js
File metadata and controls
85 lines (75 loc) · 1.7 KB
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
const Heroku = require('heroku-client')
const heroku = new Heroku({ token: process.env.HEROKU_API_TOKEN })
const log = require("debug")("dynos:log")
const maintenance = (name, status) => {
log(`Maintenance: ${status} - ${name}`)
return heroku.patch(`/apps/${name}`, {
body: {
maintenance: status
}
})
}
const scaleToZero = name => {
log(`Stopping ${name}`)
return heroku.patch(`/apps/${name}/formation`, {
body: {
updates: [
{ quantity: 0, type: 'web' }
]
}
}).then(() => maintenance(name, true))
}
const scaleToOne = name => {
log(`Starting ${name}`)
return heroku.patch(`/apps/${name}/formation`, {
body: {
updates: [
{ quantity: 1, type: 'web' }
]
}
}).then(() => maintenance(name, false))
}
const stop = (names) => {
return Promise.all(names.map(scaleToZero))
}
const start = (names) => {
return Promise.all(names.map(scaleToOne))
}
const size = (names, size) => {
const resize = name => {
log(`Resizing ${name}`)
return heroku.patch(`/apps/${name}/formation`, {
body: {
updates: [
{ size, type: 'web' }
]
}
})
}
return Promise.all(names.map(resize))
}
const config = (names) => {
return new Promise((resolve, reject) => {
const get = name => heroku.get(`/apps/${name}/config-vars`)
if (!Array.isArray(names)) {
names = [names]
}
return Promise.all(names.map(get)).then(
results => {
const hash = results.reduce((hash, result, index) => {
hash[names[index]] = result
return hash
}, {})
resolve(hash)
},
reject
)
})
}
module.exports = {
stop,
start,
size,
maintenance,
config
}