diff --git a/README.md b/README.md index 5045100..3cfd5c5 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,9 @@ Array of security-groups. The AWS region used when accessing ECS and CloudWatch. If nothing is provided falls back to `us-east-1`. The `AWS_DEFAULT_REGION` environment variable has precendence over this setting. +#### tag +This option is a key/value pair defined as `key=value` and can be repeated multiple times. Each pair is passed as a tag of the launched task, where `key` is the name of the tag and `value` is it's value. + ### Example Module Usage ``` diff --git a/bin/ecs-task-runner b/bin/ecs-task-runner index 93181ba..a0036d4 100755 --- a/bin/ecs-task-runner +++ b/bin/ecs-task-runner @@ -61,6 +61,16 @@ const argv = yargs describe: 'Security groups network configuration (awsvpc configuration)', type: 'array' }) + .option('tag', { + array: true, + describe: 'key=value Pass tag to add to the task', + coerce: opts => { + return opts.map(item => { + let pieces = item.split('='); + return { key: pieces[0], value: pieces[1] }; + }) + } + }) .help() .wrap(yargs.terminalWidth()) .argv; @@ -76,7 +86,8 @@ const options = { launchType: argv.launchType, assignPublicIp: argv.assignPublicIp, subnets: argv.subnets, - securityGroups: argv.securityGroups + securityGroups: argv.securityGroups, + tags: argv.tag }; ecsTaskRunner(options, function (err, stream) { diff --git a/index.js b/index.js index 02d44f8..61b99de 100644 --- a/index.js +++ b/index.js @@ -67,7 +67,8 @@ module.exports = function (options, cb) { launchType: options.launchType, assignPublicIp: options.assignPublicIp, subnets: options.subnets, - securityGroups: options.securityGroups + securityGroups: options.securityGroups, + tags: options.tags }; taskRunner.runPromisified = util.promisify(taskRunner.run); diff --git a/lib/taskrunner.js b/lib/taskrunner.js index 895abda..da7a6b0 100644 --- a/lib/taskrunner.js +++ b/lib/taskrunner.js @@ -54,6 +54,10 @@ module.exports = { params.networkConfiguration.awsvpcConfiguration.securityGroups = options.securityGroups; } + if (options.tags !== undefined) { + params.tags = options.tags + } + ecs.runTask(params) .then(data => cb(null, data)) .catch(err => cb(err, null)); diff --git a/test/taskrunner.js b/test/taskrunner.js index 5bfa70b..6021021 100644 --- a/test/taskrunner.js +++ b/test/taskrunner.js @@ -81,6 +81,36 @@ describe('TaskRunner', function () { done(); }); }); + + it('should make a call to AWS.ECS with correct arguments including tags', function (done) { + const options = { + clusterArn: 'cluster.arn', + taskDefinitionArn: 'task-definition.arn', + containerName: 'container name', + cmd: 'mycommand --arg "Woot"', + endOfStreamIdentifier: '1234', + startedBy: 'Bugs Bunny', + tags: [{ key: 'task', value: 'cleanup' }, { key: 'database', value: 'abc' }] + }; + + ecsMock.on(RunTaskCommand).callsFake((params) => { + expect(params.cluster).to.equal(options.clusterArn); + expect(params.taskDefinition).to.equal(options.taskDefinitionArn); + expect(params.startedBy).to.equal(options.startedBy); + expect(params.tags).to.equal(options.tags); + + const cmdOverride = params.overrides.containerOverrides[0]; + expect(cmdOverride.name).to.equal(options.containerName); + expect(cmdOverride.command).to.eql(taskRunner.makeCmd(options)); + + return Promise.resolve({ taskArn: "Yo" }); + }); + + taskRunner.run(options, function (err, _task) { + expect(err).to.equal(null); + done(); + }); + }); }); describe('#stop', function () {