-
Notifications
You must be signed in to change notification settings - Fork 63
/
index.js
156 lines (142 loc) · 5.08 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
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
'use strict';
var AWS = require('aws-sdk');
var ecs = new AWS.ECS();
// Reading environment variables
const ecsClusterArn = process.env.ecsClusterArn;
const ecsTaskDefinationArn = process.env.ecsTaskDefinationArn;
const ecsContainerName = process.env.ecsContainerName;
const recordingArtifactsBucket = process.env.recordingArtifactsBucket;
let responseBody = {
message: '',
input: ''
};
let response = {
statusCode: 200,
headers: {},
body: ''
};
exports.handler = function(event, context, callback) {
let meetingURL = "";
let taskId = "";
let recordingAction = "";
console.log(event);
responseBody.input = event;
if(event.queryStringParameters && event.queryStringParameters.recordingAction) {
console.log("Recording action: " + event.queryStringParameters.recordingAction);
recordingAction = event.queryStringParameters.recordingAction;
}
switch(recordingAction.toLowerCase()) {
case 'start':
if(event.queryStringParameters && event.queryStringParameters.meetingURL) {
console.log("Meeting URL: " + event.queryStringParameters.meetingURL);
meetingURL = decodeURIComponent(event.queryStringParameters.meetingURL);
return startRecording(event, context, callback, meetingURL);
} else {
responseBody = {
message: "Missing parameter: meetingURL",
input: event
};
response = {
statusCode: 400,
headers: {},
body: JSON.stringify(responseBody, null, ' ')
};
context.succeed(response);
}
case 'stop':
if(event.queryStringParameters && event.queryStringParameters.taskId) {
console.log("ECS task ID: " + event.queryStringParameters.taskId);
taskId = event.queryStringParameters.taskId;
return stopRecording(event, context, taskId);
} else {
responseBody = {
message: "Missing parameter: taskId",
input: event
};
response = {
statusCode: 400,
headers: {},
body: JSON.stringify(responseBody, null, ' ')
};
context.succeed(response);
}
default:
responseBody = {
message: "Invalid parameter: recordingAction. Valid values 'start' & 'stop'",
input: event
};
response = {
statusCode: 400,
headers: {},
body: JSON.stringify(responseBody)
};
}
console.log("response: " + JSON.stringify(response));
callback(null, response);
};
function startRecording(event, context, callback, meetingUrl) {
let ecsRunTaskParams = {
cluster: ecsClusterArn,
launchType: "EC2",
count: 1,
overrides: {
containerOverrides: [
{
environment: [
{
name: "MEETING_URL",
value: meetingUrl
},
{
name: "RECORDING_ARTIFACTS_BUCKET",
value: recordingArtifactsBucket
}
],
name: ecsContainerName
}
],
},
placementConstraints: [{
type: "distinctInstance"
}],
taskDefinition: ecsTaskDefinationArn
};
ecs.runTask(ecsRunTaskParams, function(err, data) {
if (err) {
console.log(err); // an error occurred
response.statusCode = err.statusCode;
response.body = JSON.stringify(err, null, ' ');
context.succeed(response);
}
else {
console.log(data); // successful response
response.statusCode = 200;
response.body = JSON.stringify((data.tasks.length && data.tasks[0].taskArn) ? data.tasks[0].taskArn : data, null, ' ');
context.succeed(response);
}
});
}
function stopRecording(event, context, taskId) {
let ecsStopTaskParam = {
cluster: ecsClusterArn,
task: taskId
};
ecs.stopTask(ecsStopTaskParam, function(err, data) {
if (err) {
console.log(err); // an error occurred
response.statusCode = err.statusCode;
response.body = JSON.stringify(err, null, ' ');
context.succeed(response);
}
else {
console.log(data); // successful response
response.statusCode = 200;
responseBody = data;
response.body = JSON.stringify(data, null, ' ');
console.log("Stop task succeeded.", response);
context.succeed(response);
}
});
}