Skip to content

Commit c6f3dfb

Browse files
authored
feat: ability to configure log driver and log options (#255)
* hability to configure log driver * changes from code review * changes from code review
1 parent 2d5d108 commit c6f3dfb

File tree

4 files changed

+117
-5
lines changed

4 files changed

+117
-5
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ input of the second:
6767
cluster: my-cluster
6868
```
6969

70+
Use the following approach to configure your log driver if needed:
71+
72+
```yaml
73+
- name: Render Amazon ECS task definition
74+
id: render-web-container
75+
uses: aws-actions/amazon-ecs-render-task-definition@v1
76+
with:
77+
task-definition: task-definition.json
78+
container-name: web
79+
image: amazon/amazon-ecs-sample:latest
80+
log-configuration-log-driver: awslogs
81+
log-configuration-log-options: |
82+
awslogs-create-group=true
83+
awslogs-group=/ecs/web
84+
awslogs-region=us-east-1
85+
awslogs-stream-prefix=ecs
86+
87+
```
88+
7089
See [action.yml](action.yml) for the full documentation for this action's inputs and outputs.
7190

7291
## License Summary

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ inputs:
1616
environment-variables:
1717
description: 'Variables to add to the container. Each variable is of the form KEY=value, you can specify multiple variables with multi-line YAML strings.'
1818
required: false
19+
log-configuration-log-driver:
20+
description: "Create/Override logDriver inside logConfiguration"
21+
required: false
22+
log-configuration-options:
23+
description: "Create/Override options inside logConfiguration. Each variable is of the form key=value, you can specify multiple variables with multi-line YAML strings."
24+
required: false
1925
outputs:
2026
task-definition:
2127
description: 'The path to the rendered task definition file'

index.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ async function run() {
1212

1313
const environmentVariables = core.getInput('environment-variables', { required: false });
1414

15+
const logConfigurationLogDriver = core.getInput("log-configuration-log-driver", { required: false });
16+
const logConfigurationOptions = core.getInput("log-configuration-options", { required: false });
17+
1518
// Parse the task definition
1619
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
1720
taskDefinitionFile :
@@ -25,7 +28,7 @@ async function run() {
2528
if (!Array.isArray(taskDefContents.containerDefinitions)) {
2629
throw new Error('Invalid task definition format: containerDefinitions section is not present or is not an array');
2730
}
28-
const containerDef = taskDefContents.containerDefinitions.find(function(element) {
31+
const containerDef = taskDefContents.containerDefinitions.find(function (element) {
2932
return element.name == containerName;
3033
});
3134
if (!containerDef) {
@@ -50,7 +53,7 @@ async function run() {
5053
const separatorIdx = trimmedLine.indexOf("=");
5154
// If there's nowhere to split
5255
if (separatorIdx === -1) {
53-
throw new Error(`Cannot parse the environment variable '${trimmedLine}'. Environment variable pairs must be of the form NAME=value.`);
56+
throw new Error(`Cannot parse the environment variable '${trimmedLine}'. Environment variable pairs must be of the form NAME=value.`);
5457
}
5558
// Build object
5659
const variable = {
@@ -70,6 +73,29 @@ async function run() {
7073
})
7174
}
7275

76+
if (logConfigurationLogDriver) {
77+
if (!containerDef.logConfiguration) { containerDef.logConfiguration = {} }
78+
const validDrivers = ["json-file", "syslog", "journald", "logentries", "gelf", "fluentd", "awslogs", "splunk", "awsfirelens"];
79+
if (!validDrivers.includes(logConfigurationLogDriver)) {
80+
throw new Error(`'${logConfigurationLogDriver}' is invalid logConfigurationLogDriver. valid options are ${validDrivers}. More details: https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_LogConfiguration.html`)
81+
}
82+
containerDef.logConfiguration.logDriver = logConfigurationLogDriver
83+
}
84+
85+
if (logConfigurationOptions) {
86+
if (!containerDef.logConfiguration) { containerDef.logConfiguration = {} }
87+
if (!containerDef.logConfiguration.options) { containerDef.logConfiguration.options = {} }
88+
logConfigurationOptions.split("\n").forEach(function (option) {
89+
option = option.trim();
90+
if (option && option.length) { // not a blank line
91+
if (option.indexOf("=") == -1) {
92+
throw new Error(`Can't parse logConfiguration option ${option}. Must be in key=value format, one per line`);
93+
}
94+
const [key, value] = option.split("=");
95+
containerDef.logConfiguration.options[key] = value
96+
}
97+
})
98+
}
7399

74100
// Write out a new task definition file
75101
var updatedTaskDefFile = tmp.fileSync({
@@ -92,5 +118,5 @@ module.exports = run;
92118

93119
/* istanbul ignore next */
94120
if (require.main === module) {
95-
run();
121+
run();
96122
}

index.test.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('Render task definition', () => {
7171
postfix: '.json',
7272
keep: true,
7373
discardDescriptor: true
74-
});
74+
});
7575
expect(fs.writeFileSync).toHaveBeenNthCalledWith(1, 'new-task-def-file-name',
7676
JSON.stringify({
7777
family: 'task-def-family',
@@ -129,7 +129,7 @@ describe('Render task definition', () => {
129129
postfix: '.json',
130130
keep: true,
131131
discardDescriptor: true
132-
});
132+
});
133133
expect(fs.writeFileSync).toHaveBeenNthCalledWith(1, 'new-task-def-file-name',
134134
JSON.stringify({
135135
family: 'task-def-family',
@@ -150,6 +150,67 @@ describe('Render task definition', () => {
150150
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'task-definition', 'new-task-def-file-name');
151151
});
152152

153+
test('renders logConfiguration on the task definition', async () => {
154+
core.getInput = jest
155+
.fn()
156+
.mockReturnValueOnce('task-definition.json')
157+
.mockReturnValueOnce('web')
158+
.mockReturnValueOnce('nginx:latest')
159+
.mockReturnValueOnce('FOO=bar\nHELLO=world')
160+
.mockReturnValueOnce('awslogs')
161+
.mockReturnValueOnce(`awslogs-create-group=true\nawslogs-group=/ecs/web\nawslogs-region=us-east-1\nawslogs-stream-prefix=ecs`);
162+
163+
await run()
164+
165+
expect(tmp.fileSync).toHaveBeenNthCalledWith(1, {
166+
tmpdir: '/home/runner/work/_temp',
167+
prefix: 'task-definition-',
168+
postfix: '.json',
169+
keep: true,
170+
discardDescriptor: true
171+
});
172+
173+
174+
expect(fs.writeFileSync).toHaveBeenNthCalledWith(1, 'new-task-def-file-name',
175+
JSON.stringify({
176+
family: 'task-def-family',
177+
containerDefinitions: [
178+
{
179+
name: "web",
180+
image: "nginx:latest",
181+
environment: [
182+
{
183+
name: "FOO",
184+
value: "bar"
185+
},
186+
{
187+
name: "DONT-TOUCH",
188+
value: "me"
189+
},
190+
{
191+
name: "HELLO",
192+
value: "world"
193+
}
194+
],
195+
logConfiguration: {
196+
logDriver: "awslogs",
197+
options: {
198+
"awslogs-create-group": "true",
199+
"awslogs-group": "/ecs/web",
200+
"awslogs-region": "us-east-1",
201+
"awslogs-stream-prefix": "ecs"
202+
}
203+
}
204+
},
205+
{
206+
name: "sidecar",
207+
image: "hello"
208+
}
209+
]
210+
}, null, 2)
211+
);
212+
});
213+
153214
test('error returned for missing task definition file', async () => {
154215
fs.existsSync.mockReturnValue(false);
155216
core.getInput = jest

0 commit comments

Comments
 (0)