Created
September 3, 2019 19:05
-
-
Save nathanpeck/dbd1ed6467167d11de2ec9353b75ba2d to your computer and use it in GitHub Desktop.
This file contains 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
import AWS = require('aws-sdk'); | |
const ecs_sdk = new AWS.ECS(); | |
function chunk(array: Array<any>, size: number) { | |
const chunked_arr = []; | |
for (let i = 0; i < array.length; i++) { | |
const last = chunked_arr[chunked_arr.length - 1]; | |
if (!last || last.length === size) { | |
chunked_arr.push([array[i]]); | |
} else { | |
last.push(array[i]); | |
} | |
} | |
return chunked_arr; | |
} | |
export interface ServiceDesiredCount { | |
[details: string] : number | undefined; | |
} | |
let serviceDesiredCounts: ServiceDesiredCount; | |
serviceDesiredCounts = {}; | |
async function fetchDesiredCounts() { | |
var nextToken = null; | |
let clusterArns: Array<string>; | |
clusterArns = []; | |
// Enumerate all clusters | |
do { | |
let resp = await ecs_sdk.listClusters().promise(); | |
if (resp.clusterArns) { | |
clusterArns = clusterArns.concat(resp.clusterArns); | |
} | |
nextToken = resp.nextToken; | |
} while(nextToken) | |
// Enumerate all services | |
for (let cluster of clusterArns) { | |
do { | |
let resp = await ecs_sdk.listServices({ cluster }).promise(); | |
if (resp.serviceArns) { | |
// Fetch desired count of all services in all clusters | |
let chunked: Array<Array<string>> | |
chunked = chunk(resp.serviceArns, 10); | |
for (let chunk of chunked) { | |
let resp = await ecs_sdk.describeServices({ cluster: cluster, services: chunk }).promise(); | |
if (resp.services) { | |
for (let serviceDetail of resp.services) { | |
serviceDesiredCounts[cluster + '|' + serviceDetail.serviceName] = serviceDetail.desiredCount; | |
} | |
} | |
} | |
} | |
nextToken = resp.nextToken; | |
} while(nextToken) | |
} | |
console.log(serviceDesiredCounts); | |
} | |
function desiredCountForService(cluster: string, serviceName: string) { | |
return serviceDesiredCounts[cluster + '|' + serviceName]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment