-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
146 lines (117 loc) · 4.55 KB
/
deploy.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
// Copyright 2022 unload.sh
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const AWS = require('aws-sdk');
const cfn = require('cfn-response');
const codedeploy = new AWS.CodeDeploy({apiVersion: '2014-10-06'});
var lambda = new AWS.Lambda();
async function warmHook(concurrency = null) {
let invocations = []
let funcName = process.env.WEB_FUNCTION;
let funcConcurrency = concurrency || process.env.WEB_CONCURRENCY;
if (funcConcurrency > 0) {
console.log('Warming function: ' + funcName);
console.log('Warming concurrency: ' + funcConcurrency);
for (let i=0; i <= funcConcurrency; i++) {
let params = {
FunctionName: funcName,
InvocationType: 'Event',
LogType: 'None',
Payload: Buffer.from(JSON.stringify({
warmer: true
}))
};
invocations.push(lambda.invoke(params).promise());
}
let result = await Promise.all(invocations).then(() => true);
console.log('Warming finished: ' + result);
}
}
async function execDeployCli() {
var cliFunction = process.env.CLI_FUNCTION;
var deployCommands = process.env.CLI_COMMAND.split("\n").filter(Boolean);
console.log("BeforeAllowTraffic cliFunction: " + cliFunction);
console.log("BeforeAllowTraffic deployCommands: " + deployCommands);
var lambdaResult = "Succeeded";
for (const command of deployCommands) {
console.log("Running: " + command);
var cliParams = {
FunctionName: cliFunction,
Payload: JSON.stringify(command),
LogType: 'Tail',
};
// Invoke the updated Lambda function.
var response = await lambda.invoke(cliParams).promise();
var payload = JSON.parse(response.Payload);
var logResult = Buffer.from(response.LogResult, 'base64');
var output = logResult.toString('ascii');
console.log("Output: " + output);
console.log("Payload: " + JSON.stringify(payload));
// Check if the status code returned by the updated
// function is 400. If it is, then it failed. If
// is not, then it succeeded.
if (payload.errorType){
lambdaResult = "Failed";
break;
}
}
return lambdaResult;
}
async function deployHook(deploymentId, lifecycleEventHookExecutionId) {
const execResult = await execDeployCli();
// Complete the PreTraffic Hook by sending CodeDeploy the validation status
var params = {
deploymentId: deploymentId,
lifecycleEventHookExecutionId: lifecycleEventHookExecutionId,
status: execResult // status can be 'Succeeded' or 'Failed'
};
// Pass CodeDeploy the prepared validation test results.
return await codedeploy.putLifecycleEventHookExecutionStatus(params).promise();
}
async function cloudformationHook(event, context) {
if (event.RequestType !== "Create") {
cfn.send(event, context, cfn.SUCCESS, {});
return;
}
const execResult = await execDeployCli();
if (execResult === 'Succeeded') {
cfn.send(event, context, cfn.SUCCESS, {});
} else {
cfn.send(event, context, cfn.FAILED, {});
}
}
exports.handler = async (event, context, callback) => {
let deploymentId = event.DeploymentId;
let stackId = event.StackId;
let lifecycleEventHookExecutionId = event.LifecycleEventHookExecutionId;
if (deploymentId) {
await deployHook(deploymentId, lifecycleEventHookExecutionId);
await warmHook();
callback(null, "deployHook function finished");
return;
}
if (stackId) {
await cloudformationHook(event, context);
await warmHook();
callback(null, "cloudformationHook function finished");
return;
}
var warm = event.Warmer;
var warmConcurrency = event.WarmerConcurrency;
if (warm) {
await warmHook(warmConcurrency);
callback(null, "warmHook function finished");
}
throw new Error("Unknown event: " + JSON.encode(event));
}