forked from aws-actions/amazon-ecs-render-task-definition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
233 lines (210 loc) · 8.53 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const path = require('path');
const core = require('@actions/core');
const tmp = require('tmp');
const fs = require('fs');
const env = require('env-var')
async function run() {
try {
// Get inputs
const taskDefinitionFile = core.getInput('task-definition', { required: true });
const containerName = core.getInput('container-name', { required: true });
const imageURI = core.getInput('image', { required: true });
const logGroup = core.getInput('log-group', { required: false });
const serviceFamily = core.getInput('service-family', { required: false });
let envList = core.getInput('env-list', { required: false });
if (envList) {
envList = JSON.parse(envList)
}
console.log(envList)
const environmentVariables = core.getInput('environment-variables', { required: false });
const envFiles = core.getInput('env-files', { required: false });
const logConfigurationLogDriver = core.getInput("log-configuration-log-driver", { required: false });
const logConfigurationOptions = core.getInput("log-configuration-options", { required: false });
const dockerLabels = core.getInput('docker-labels', { required: false });
const command = core.getInput('command', { required: false });
// Parse the task definition
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
taskDefinitionFile :
path.join(process.env.GITHUB_WORKSPACE, taskDefinitionFile);
if (!fs.existsSync(taskDefPath)) {
throw new Error(`Task definition file does not exist: ${taskDefinitionFile}`);
}
const taskDefContents = require(taskDefPath);
// Insert the image URI
if (!Array.isArray(taskDefContents.containerDefinitions)) {
throw new Error('Invalid task definition format: containerDefinitions section is not present or is not an array');
}
const containerDef = taskDefContents.containerDefinitions.find(function (element) {
return element.name == containerName;
});
if (!containerDef) {
throw new Error('Invalid task definition: Could not find container definition with matching name');
}
containerDef.image = imageURI;
if (logGroup) {
containerDef.logConfiguration.options["awslogs-group"] = logGroup;
}
if (serviceFamily) {
taskDefContents.family = serviceFamily;
}
if (envList) {
const environmentMap = new Map(containerDef.environment.map(e => [e.name, e.value]))
envList.forEach(variable => {
console.log(variable)
try {
environmentMap.set(variable, env.get(variable).required().asString())
} catch (e) {
console.log(e)
}
})
containerDef.environment = Array.from(environmentMap.entries()).map(([name, value]) => ({ name, value }))
} else {
containerDef.environment = containerDef.environment.map(object => ({
name: object.name,
value: env.get(object.name).required(false).asString() || object.value
}))
}
console.log(containerDef.environment);
if (environmentVariables) {
// If environment array is missing, create it
if (!Array.isArray(containerDef.environment)) {
containerDef.environment = [];
}
// Get pairs by splitting on newlines
environmentVariables.split('\n').forEach(function (line) {
// Trim whitespace
const trimmedLine = line.trim();
// Skip if empty
if (trimmedLine.length === 0) { return; }
// Split on =
const separatorIdx = trimmedLine.indexOf("=");
// If there's nowhere to split
if (separatorIdx === -1) {
throw new Error(`Cannot parse the environment variable '${trimmedLine}'. Environment variable pairs must be of the form NAME=value.`);
}
// Build object
const variable = {
name: trimmedLine.substring(0, separatorIdx),
value: trimmedLine.substring(separatorIdx + 1),
};
// Search container definition environment for one matching name
const variableDef = containerDef.environment.find((e) => e.name == variable.name);
if (variableDef) {
// If found, update
variableDef.value = variable.value;
} else {
// Else, create
containerDef.environment.push(variable);
}
})
}
if (command) {
containerDef.command = command.split(' ')
}
if (envFiles) {
containerDef.environmentFiles = [];
envFiles.split('\n').forEach(function (line) {
// Trim whitespace
const trimmedLine = line.trim();
// Skip if empty
if (trimmedLine.length === 0) { return; }
// Build object
const variable = {
value: trimmedLine,
type: "s3",
};
containerDef.environmentFiles.push(variable);
})
}
if (environmentVariables) {
// If environment array is missing, create it
if (!Array.isArray(containerDef.environment)) {
containerDef.environment = [];
}
// Get pairs by splitting on newlines
environmentVariables.split('\n').forEach(function (line) {
// Trim whitespace
const trimmedLine = line.trim();
// Skip if empty
if (trimmedLine.length === 0) { return; }
// Split on =
const separatorIdx = trimmedLine.indexOf("=");
// If there's nowhere to split
if (separatorIdx === -1) {
throw new Error(`Cannot parse the environment variable '${trimmedLine}'. Environment variable pairs must be of the form NAME=value.`);
}
// Build object
const variable = {
name: trimmedLine.substring(0, separatorIdx),
value: trimmedLine.substring(separatorIdx + 1),
};
// Search container definition environment for one matching name
const variableDef = containerDef.environment.find((e) => e.name == variable.name);
if (variableDef) {
// If found, update
variableDef.value = variable.value;
} else {
// Else, create
containerDef.environment.push(variable);
}
})
}
if (logConfigurationLogDriver) {
if (!containerDef.logConfiguration) { containerDef.logConfiguration = {} }
const validDrivers = ["json-file", "syslog", "journald", "logentries", "gelf", "fluentd", "awslogs", "splunk", "awsfirelens"];
if (!validDrivers.includes(logConfigurationLogDriver)) {
throw new Error(`'${logConfigurationLogDriver}' is invalid logConfigurationLogDriver. valid options are ${validDrivers}. More details: https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_LogConfiguration.html`)
}
containerDef.logConfiguration.logDriver = logConfigurationLogDriver
}
if (logConfigurationOptions) {
if (!containerDef.logConfiguration) { containerDef.logConfiguration = {} }
if (!containerDef.logConfiguration.options) { containerDef.logConfiguration.options = {} }
logConfigurationOptions.split("\n").forEach(function (option) {
option = option.trim();
if (option && option.length) { // not a blank line
if (option.indexOf("=") == -1) {
throw new Error(`Can't parse logConfiguration option ${option}. Must be in key=value format, one per line`);
}
const [key, value] = option.split("=");
containerDef.logConfiguration.options[key] = value
}
})
}
if (dockerLabels) {
// If dockerLabels object is missing, create it
if (!containerDef.dockerLabels) { containerDef.dockerLabels = {} }
// Get pairs by splitting on newlines
dockerLabels.split('\n').forEach(function (label) {
// Trim whitespace
label = label.trim();
if (label && label.length) {
if (label.indexOf("=") == -1 ) {
throw new Error(`Can't parse logConfiguration option ${label}. Must be in key=value format, one per line`);
}
const [key, value] = label.split("=");
containerDef.dockerLabels[key] = value;
}
})
}
// Write out a new task definition file
var updatedTaskDefFile = tmp.fileSync({
tmpdir: process.env.RUNNER_TEMP,
prefix: 'task-definition-',
postfix: '.json',
keep: true,
discardDescriptor: true
});
const newTaskDefContents = JSON.stringify(taskDefContents, null, 2);
fs.writeFileSync(updatedTaskDefFile.name, newTaskDefContents);
core.setOutput('task-definition', updatedTaskDefFile.name);
}
catch (error) {
core.setFailed(error.message);
}
}
module.exports = run;
/* istanbul ignore next */
if (require.main === module) {
run();
}