-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.js
160 lines (149 loc) · 4.26 KB
/
cli.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env node
// Create a source tarball, upload it, build a slug and deploy it on Heroku.
// See https://devcenter.heroku.com/articles/build-and-release-using-the-api
require('console-stamp')(console, 'yyyy-mm-dd HH:MM:ss.l')
var ArgumentParser = require('argparse').ArgumentParser
var child_process = require('child_process')
var fs = require('fs')
var Heroku = require('heroku-client')
var Q = require('q')
var util = require('util')
function displayInfo (app_id, version, source, timeout, interval) {
console.info(util.format('app id: %s', app_id))
console.info(util.format('version: %s', version))
console.info(util.format('source tarball: %s', source))
console.info(util.format('timeout: %d s', timeout))
console.info(util.format('interval: %d s', interval))
}
function exec (command) {
console.log(command)
return Q.nfbind(child_process.exec)(command)
.then(function (out) {
if (out[1]) {
var lines = out[1].split('\n')
for (var i = 0; i < lines.length; i++) {
console.log(lines[i])
}
}
})
}
function createSourceTarball (source, directory) {
return exec(util.format('rm -f %s', source))
.then(function () {
return exec(util.format('tar czvf %s -C %s/ $(ls %s/)', source, directory, directory))
.then(function () {
return Q.nfbind(fs.stat)(source)
})
})
}
function uploadSourceTarball (url, source) {
return exec(util.format('curl "%s" -X PUT -H "Content-Type:" --data-binary @%s', url, source))
}
function createBuild (app, url, version) {
var data = {
source_blob: {
url: url,
version: version
}
}
return app.builds().create(data)
}
function waitForBuild (app, build_id, timeout, interval) {
return app.builds(build_id).info()
.then(function (build) {
console.log(util.format('build status: %s', build.status))
if (timeout === 0) {
throw new Error('Build did not succeed in time')
}
switch (build.status) {
case 'failed':
throw new Error('Build failed')
case 'pending':
return Q.delay(interval)
.then(function () {
return waitForBuild(app, build_id, timeout - interval, interval)
})
}
return build
})
}
function slugify (app_id, version, directory, source_file, timeout, interval) {
var heroku = new Heroku({token: process.env.HEROKU_API_KEY})
var app = heroku.apps(app_id)
displayInfo(app_id, version, source_file, timeout, interval)
return createSourceTarball(source_file, directory)
.then(function () {
return app.sources().create()
})
.then(function (source) {
return uploadSourceTarball(source.source_blob.put_url, source_file)
.then(function () {
return createBuild(app, source.source_blob.get_url, version)
})
.then(function (build) {
console.dir(build)
return waitForBuild(app, build.id, timeout * 1000, interval * 1000)
})
})
.fail(function (error) {
console.error(error.stack)
if (error.body.message) {
console.error(error.body.message)
}
process.exit(1)
})
}
function main () {
var parser = new ArgumentParser({
addHelp: true,
description: 'Deploy a new Heroku slug',
epilog: 'Create a source tarball, upload it, build a slug and deploy it on Heroku.'
})
parser.addArgument(
[ '-a', '--app' ],
{
help: 'Heroku app id',
required: true
}
)
parser.addArgument(
[ '-v', '--version' ],
{
help: 'Version to deploy',
required: true
}
)
parser.addArgument(
[ '-d', '--directory' ],
{
defaultValue: 'heroku',
help: 'Name of the directory containing the files for the tarball (default: heroku)'
}
)
parser.addArgument(
[ '-s', '--source' ],
{
defaultValue: 'source.tar.gz',
help: 'File name of source tarball (default: source.tar.gz)'
}
)
parser.addArgument(
[ '-t', '--timeout' ],
{
defaultValue: 120,
help: 'Timeout for build in seconds (default: 120 s)',
type: 'int'
}
)
parser.addArgument(
[ '-i', '--interval' ],
{
defaultValue: 10,
help: 'Interval to check build status in seconds (default: 10 s)',
type: 'int'
}
)
var args = parser.parseArgs()
return slugify(args.app, args.version, args.directory, args.source, args.timeout, args.interval)
}
main()