-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
107 lines (76 loc) · 2.95 KB
/
index.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
'use strict';
var AWS = require('aws-sdk');
var git = require('git-rev');
var fs = require('fs');
var Q = require('q');
var _ = require('lodash');
var gutil = require('gulp-util');
var through = require('through2');
module.exports = function(config) {
config = _.extend({
region: 'us-east-1'
}, config);
function deploy(file, enc, callback) {
if (config.profile) {
AWS.config.credentials = new AWS.SharedIniFileCredentials({ profile: config.profile });
gutil.log('Using credentials from profile \'' + config.profile + '\'');
}
var sts = new AWS.STS();
sts.getCallerIdentity({}, function(err, data) {
if (err) {
callback(new gutil.PluginError('gulp-eb-deploy', err));
} else {
git.short(function(rev) {
var date = new Date();
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
var version = String(y) +
String(m = (m < 10) ? ('0' + m) : m) +
String(d = (d < 10) ? ('0' + d) : d) +
'-' + rev +
'-' + Math.floor((Math.random() * 899999) + 100000);
var label = config.application + '-' + config.environment + '-' + version;
var account = data.Account;
var bucket = 'elasticbeanstalk-' + config.region + '-' + account;
var s3obj = new AWS.S3({ params: { Bucket: bucket, Key: label + '.zip' } });
gutil.log('Uploading application bundle');
s3obj.upload({ Body: file.contents }).send(function(err, data) {
if (err) {
callback(new gutil.PluginError('gulp-eb-deploy', err));
} else {
gutil.log('Creating application version \'' + label + '\'');
var eb = new AWS.ElasticBeanstalk({ region: config.region });
eb.createApplicationVersion({
ApplicationName: config.application,
VersionLabel: label,
SourceBundle: {
S3Bucket: bucket,
S3Key: label + '.zip'
}
}, function (err, data) {
if (err) {
callback(new gutil.PluginError('gulp-eb-deploy', err));
} else {
gutil.log('Updating environment \'' + config.environment + '\' to version \'' + label + '\'');
eb.updateEnvironment({
EnvironmentName: config.environment,
VersionLabel: label
}, function (err, data) {
if (err) {
callback(new gutil.PluginError('gulp-eb-deploy', err));
} else {
gutil.log('Environment update running, please check AWS console for progress');
callback();
}
});
}
});
}
});
});
}
});
}
return through.obj(deploy);
}