Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 8 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ This will attempt to stop an existing task, making way for the blue/green rollou

If you're only running a single task you'll experience some down time. **Use at your own risk.**

#### Usage
## Usage

ecs-task-deploy [options]

Expand All @@ -66,7 +66,7 @@ If you're only running a single task you'll experience some down time. **Use at
**Hint: You can pass a service OR a task definition name. If a service is passed
that service will be updated, otherwise only the given task definition.**

##### Node
### CLI

To run via cli.

Expand All @@ -87,10 +87,14 @@ ecs-task-deploy \

To run in code.

npm install ecs-task-deploy --save
```sh
npm install @valiton/ecs-task-deploy
```

### Node API

```javascript
const ecsTaskDeploy = require('ecs-task-deploy')
const ecsTaskDeploy = require('@valiton/ecs-task-deploy');

ecsTaskDeploy({
awsAccessKey: 'ABCD',
Expand All @@ -110,26 +114,3 @@ ecsTaskDeploy({
)
```

##### Docker

Run with arguments.

docker run --rm stead/ecs-task-deploy \
-k <key> \
-s <secret> \
-r <region> \
-c <cluster> \
-n <service-name> \
-i <image-name>

Run with standard AWS environment variables.

docker run --rm \
-e AWS_DEFAULT_REGION=<region> \
-e AWS_ACCESS_KEY_ID=<key> \
-e AWS_SECRET_ACCESS_KEY=<secret> \
stead/ecs-task-deploy \
-c <cluster> \
-n <service-name> \
-i <image-name>

8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"name": "@valiton/ecs-task-deploy",
"version": "1.4.1",
"version": "1.4.2",
"description": "Update an ECS task definition with Docker image and trigger blue/green deployment",
"keywords": [
"aws",
"ecs",
"deploy",
"docker"
"deploy"
],
"files": [
"src/"
Expand All @@ -24,10 +23,6 @@
"bugs": {
"url": "https://github.com/valiton/ecs-task-deploy/issues"
},
"engines": {
"node": ">=4.0.0",
"npm": ">=2.14.2"
},
"main": "src/index.js",
"bin": {
"ecs-task-deploy": "src/run.js"
Expand All @@ -36,8 +31,7 @@
"lint": "eslint src"
},
"dependencies": {
"aws-sdk": "^2.28.0",
"aws-sdk-promise": "0.0.2",
"aws-sdk": "^2.625.0",
"chalk": "^1.1.3",
"commander": "^2.9.0"
},
Expand Down
20 changes: 10 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const AWS = require('aws-sdk-promise')
const AWS = require('aws-sdk')
const assert = require('assert')

module.exports = execute
Expand Down Expand Up @@ -46,7 +46,7 @@ function deployTaskDefinition(options) {
assert.ok(options.service, 'ECS service name missing')
console.info('service given, updating service')
return getService(ecs, options)
.then(service => getTaskDefinition(ecs, service, options))
.then(service => getTaskDefinition(ecs, service.taskDefinition, options))
.then(taskDef => addNewTaskDefinition(ecs, taskDef, options))
.then(taskDef => updateService(ecs, taskDef, options))
.then(service => checkForTaskKill(ecs, service, options))
Expand All @@ -55,7 +55,7 @@ function deployTaskDefinition(options) {

function getService(ecs, options) {
return ecs.describeServices({ cluster: options.cluster, services: [ options.service ] }).promise()
.then(res => res.data.services.find(service => service.serviceName == options.service))
.then(res => res.services.find(service => service.serviceName == options.service))
.then(service => {
assert.ok(service, `Service ${options.service} not found, aborting.`)
return service
Expand All @@ -64,13 +64,13 @@ function getService(ecs, options) {

function getTaskDefinition(ecs, taskDef, options) {
if (options.verbose) console.info('get task definition')
return ecs.describeTaskDefinition({ taskDefinition: taskDef }).promise().then(res => res.data.taskDefinition)
return ecs.describeTaskDefinition({ taskDefinition: taskDef }).promise().then(res => res.taskDefinition)
}

function addNewTaskDefinition(ecs, template, options) {
if (options.verbose) console.info(`registering new task definition with image '${options.image.uri}'`)
const newTaskDef = newTaskDefinition(template, options)
return ecs.registerTaskDefinition(newTaskDef).promise().then(res => res.data.taskDefinition)
return ecs.registerTaskDefinition(newTaskDef).promise().then(res => res.taskDefinition)
}

function newTaskDefinition(template, options) {
Expand Down Expand Up @@ -117,7 +117,7 @@ function parseImagePath(uri) {
function updateService(ecs, taskDef, options) {
if (options.verbose) console.info(`update service with new task definition '${taskDef.taskDefinitionArn}'`)
const serviceOptions = createServiceOptions(taskDef, options)
return ecs.updateService(serviceOptions).promise().then(res => ({ info: res.data.service, taskDef }))
return ecs.updateService(serviceOptions).promise().then(res => ({ info: res.service, taskDef }))
}

function createServiceOptions(taskDef, options) {
Expand All @@ -132,8 +132,8 @@ function checkForTaskKill(ecs, service, options) {
if (options.killTask) {
if (options.verbose) console.info('searching for running task to stop')
return ecs.listTasks({ cluster: options.cluster, serviceName: options.service }).promise().then(res => {
if (res.data && res.data.taskArns && res.data.taskArns.length) {
const task = res.data.taskArns[0]
if (res.taskArns && res.taskArns.length) {
const task = res.taskArns[0]
if (options.verbose) console.info(`stopping task '${task}'`)
return ecs.stopTask({ cluster: options.cluster, task, reason: 'Making room for blue/green deployment' }).promise()
.then(() => {
Expand Down Expand Up @@ -169,13 +169,13 @@ function waitForServiceUpdate(ecs, service, options) {
serviceName: service.info.serviceName,
desiredStatus: 'RUNNING'
}).promise().then(res => {
const tasks = res.data.taskArns || []
const tasks = res.taskArns || []
if (tasks.length) {
ecs.describeTasks({
tasks,
cluster: service.info.clusterArn
}).promise().then(res => {
const task = res.data.tasks.find(task => task.taskDefinitionArn === service.taskDef.taskDefinitionArn)
const task = res.tasks.find(task => task.taskDefinitionArn === service.taskDef.taskDefinitionArn)
if (task) {
resolve(task)
} else if (Date.now() - START_TIME > MAX_TIMEOUT) {
Expand Down