-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws.js
More file actions
80 lines (73 loc) · 2.37 KB
/
Copy pathaws.js
File metadata and controls
80 lines (73 loc) · 2.37 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
if ( !process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY || !process.env.AWS_STACK_ID ){
console.log('see README.md...');
process.exit();
}
const AWS = require('aws-sdk'),
opsworks = new AWS.OpsWorks( {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAcessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: 'us-east-1'
} ),
slack = require(__dirname + '/slack.js');
module.exports = {
checkDeploymentStatus: function( cb ){
console.log( 'checking deployment status...' );
opsworks.describeDeployments({
StackId: process.env.AWS_STACK_ID,
}, function(err, data) {
if (err){
slack.postSlackMessage( process.env.SLACK_CHANNEL_ID, 'Unable to check deployment status.' );
console.log( err );
};
let isDeploying = false;
if ( data && data.Deployments ){
data.Deployments.forEach( function( deployment ){
if ( deployment && deployment.Status && deployment.Status === 'running' ){
isDeploying = true;
}
} );
if ( cb ){
cb( isDeploying );
}
}
});
},
runDeployment: function( cb ){
var helper = this;
opsworks.createDeployment( {
Command: {
Name: 'deploy'
},
StackId: process.env.AWS_STACK_ID,
AppId: process.env.AWS_APPLICATION_ID,
Comment: 'Running automated stage deploy.'
}, function( err, data ) {
if ( err ){
console.log( err );
slack.postSlackMessage( process.env.SLACK_CHANNEL_ID, 'There was an error while running deployment.' );
} else {
console.log( 'running deployment...', data );
helper.notifyWhenDeploymentIsFinished();
slack.postSlackMessage( process.env.SLACK_CHANNEL_ID, 'Deploying stage.', function( err ){
console.log( err );
} );
}
if ( cb ){
cb( err, data );
}
});
},
notifyWhenDeploymentIsFinished: function( cb ){
var helper = this;
helper.checkDeploymentStatus( function( isDeploying ) {
console.log( `checking deployment status: ${isDeploying}` );
if ( isDeploying ){
setTimeout( function(){
helper.notifyWhenDeploymentIsFinished();
}, 5000 );
} else {
slack.postSlackMessage( process.env.SLACK_CHANNEL_ID, 'Deployment is finished.' );
}
});
}
};