-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
144 lines (122 loc) · 4.88 KB
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const exec = require('child_process').exec;
const app = express();
const REALM = process.env.REALM;
const STAGE = process.env.STAGE;
var PREFIX = ( REALM === "growth" ) ? "gr-" : "" ;
const commands = [
`bash base-image.sh ${REALM} ${STAGE}`,
`bash custom-image.sh ${REALM} ${STAGE}`
];
function checkServiceWhitelist( appName )
{
var whitelist = [ "ecs", "image", "pag", "page", "recommendation", "search", "user-activity", "web", "mini", "follow", "category-datasync", "social", "pwg", "library", "event", "notification", "content", "cms", "report", "init","oasis" ];
if( STAGE === "devo" ) {
whitelist.push( "pratilipi" );
whitelist.push( "author" );
whitelist.push( "user" );
whitelist.push( "auth" );
}
if( STAGE === "prod" && REALM === "product" ) {
whitelist.push( "datastore-util" );
}
if( whitelist.indexOf( appName ) === -1 ) {
console.log( `${appName}***** service: ${appName} is not whitelisted.` );
return false;
} else {
console.log( `${appName}***** service: ${appName} whitelisted.` );
return true;
}
}
function getServiceCommand( appName, callback )
{
var state = "";
if( checkServiceWhitelist( appName ) ) {
var command = `aws ecs list-services --cluster ${PREFIX}${STAGE}-ecs | jq '.serviceArns[]' | jq 'split(":")[5]' | jq 'split("/")[1]'`;
console.log( `${appName}***** Running command: ${command}` );
var cmdProcess = exec( command, { maxBuffer: 2 * 1024 * 1024 }, function( error, stdout, stderr ) {
if( error != null ) {
console.error( `${appName}***** Failed to execute command: ${command}` );
console.error( String( error ) );
callback( error, null );
} else {
var services = stdout.split( "\n" );
services.pop();
if( services.indexOf( `\"${appName}\"` ) === -1 ) {
console.log( `${appName}***** service: ${appName} not exists.` );
console.log( `${appName}***** service: creating ${appName}.` );
state = "create";
} else {
console.log( `${appName}***** service: ${appName} exists.` );
console.log( `${appName}***** service: updating ${appName}.` );
state = "update";
}
callback( null, state );
}
} );
cmdProcess.stdout.pipe( process.stdout );
cmdProcess.stderr.pipe( process.stdout );
} else {
callback( null, state );
}
}
(function run() {
if( commands.length == 0 ) {
console.log("***** ecs:sleeping for 5 seconds. TATA Good Night");
return setTimeout( run, 5 * 1000 );
}
commands.reverse();
var command = commands.pop();
commands.reverse();
console.log( `***** Running command: ${command}` );
var cmdProcess = exec( command, { maxBuffer: 2 * 1024 * 1024 }, function( error, stdout, stderr ) {
if( error != null ) {
console.error( `Failed to execute command: ${command}` );
console.error( String( error ) );
}
run();
} );
cmdProcess.stdout.pipe( process.stdout );
cmdProcess.stderr.pipe( process.stdout );
} )();
app.use( bodyParser.urlencoded({ extended:true }) );
app.use( bodyParser.json() );
app.get( '/health', function ( req, res ) {
res.send( 'Realm:' + REALM + ', Stage:' + STAGE );
} );
app.post( '/*', function ( req, res ) {
req.body = JSON.parse( req.body.payload );
if( ( STAGE == 'devo' && req.body.ref != 'refs/heads/devo' )
|| ( STAGE == 'gamma' && req.body.ref != 'refs/heads/gamma' )
|| ( STAGE == 'prod' && req.body.ref != 'refs/heads/master') ) {
res.status( 400 ).send( `No deployment in ${REALM}/${STAGE} for commit in ${req.body.repository.name}/${req.body.ref.substr(11)} branch.` );
return;
}
if( req.body.deleted ) {
res.status( 400 ).send( `No deployment in ${REALM}/${STAGE} for deleted ${req.body.repository.name}/${req.body.ref.substr(11)} branch.` );
return;
}
var appName = req.body.repository.name;
if( appName === 'pratilipi' ) {
res.status( 400 ).send( `No deployment in ${REALM}/${STAGE} for repository: ${appName}.` );
return;
}
if( appName.startsWith( 'ecs-' ) ) {
appName = appName.substr( 4 );
}
var appVersion = Math.round( new Date().getTime() / 1000 / 60 );
getServiceCommand( appName, function( error, command ) {
if( error != null ) {
res.status( 400 ).send( `Not Deploying to ${REALM}/${STAGE}/${appName} from ${req.body.ref.substr(11)} branch due to an error.` );
} else {
if( command === "" ) {
res.status( 400 ).send( `Not Deploying to ${REALM}/${STAGE}/${appName} from ${req.body.ref.substr(11)} branch due to service: ${appName} not being in whitelist.` );
} else {
commands.push( `bash app-deploy.sh ${command} ${REALM} ${STAGE} ${appName} ${appVersion}` );
res.send( `Deploying to ${REALM}/${STAGE}/${appName} from ${req.body.ref.substr(11)} branch.` );
}
}
} );
} );
app.listen( 80 );