Last active
March 15, 2018 10:01
-
-
Save carlevans719/83cec1bd51dd09b4aa000246786dc6fb to your computer and use it in GitHub Desktop.
Create task definition revisions and update all services which use them AWS ECS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const child_process = require('child_process') | |
const spawn = child_process.spawn | |
const AWS = require('aws-sdk') | |
const ACCESS_KEY_ID = '' | |
const SECRET_ACCESS_KEY = '' | |
const PROFILE = '' | |
const REGION = 'eu-west-1' | |
const CLUSTER_NAME = '' | |
AWS.config.update({ | |
accessKeyId: ACCESS_KEY_ID, | |
secretAccessKey: SECRET_ACCESS_KEY, | |
region: REGION | |
}) | |
const ECS = new AWS.ECS() | |
function getUniqueTaskDefs(defs) { | |
const out = {} | |
for (let i = 0; i < defs.length; i++) { | |
let parts = defs[i].split(':') | |
const revision = parts.pop() | |
const defName = parts.join(':') | |
if (!(defName in out) || out[defName] < revision) { | |
out[defName] = revision | |
} | |
} | |
return Object.keys(out).map(function (defName) { | |
return defName + ':' + out[defName] | |
}) | |
} | |
function spawnAndParse(command, args) { | |
return JSON.parse(child_process.spawnSync(command, args).stdout) | |
} | |
const taskDefs = getUniqueTaskDefs(spawnAndParse('aws', ['--profile', PROFILE, '--region', REGION, 'ecs', 'list-task-definitions']).taskDefinitionArns) | |
console.log(taskDefs) | |
function doNextTaskDef() { | |
if (taskDefs.length === 0) { | |
console.log('Done!') | |
} | |
else { | |
const def = taskDefs.pop() | |
const defData = spawnAndParse('aws', ['--profile', PROFILE, '--region', REGION, 'ecs', 'describe-task-definition', '--task-definition', def]) | |
const taskDefinition = defData.taskDefinition | |
delete taskDefinition.volumes | |
delete taskDefinition.status | |
delete taskDefinition.taskDefinitionArn | |
delete taskDefinition.requiresAttributes | |
delete taskDefinition.revision | |
// | |
// YOUR CODE TO UPDATE THE TASK DEFS GOES HERE | |
// | |
ECS.registerTaskDefinition(taskDefinition, (err, res) => { | |
if (err) { | |
return console.error(err) | |
} | |
console.log(res) | |
const taskDefArn = res.taskDefinition.taskDefinitionArn | |
const name = taskDefArn.split('/').pop().split(':').shift() | |
ECS.updateService({ | |
cluster: CLUSTER_NAME, | |
service: name, | |
desiredCount: 1, | |
taskDefinition: taskDefArn | |
}, (err1, res1) => { | |
console.log('Done processing ' + name) | |
doNextTaskDef() | |
}) | |
}) | |
} | |
} | |
doNextTaskDef() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment