-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (124 loc) · 3.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const AWS = require("aws-sdk");
const splitArnName = require("./util/splitarnname");
/**
*
* @param {string} profile
* @param {string} region
*/
const initEcsProfile = (profile, region) => {
const creds = new AWS.SharedIniFileCredentials({
profile
});
AWS.config.update({
credentials: creds,
region: region
});
return new AWS.ECS();
};
const ecs = initEcsProfile("HA", "us-east-1");
/**
* List container instnaces of a ecs cluster ex:'dev'
* @param {string} clusterName
* @returns {{containerInstanceArns:Array,clusterName:string}}
*/
const listContainerInstances = async clusterName => {
return new Promise((resolve, reject) => {
const params = {
cluster: clusterName
};
ecs.listContainerInstances(params, (err, data) => {
if (err) reject(err);
else {
if (data.containerInstanceArns.length > 0) {
resolve({
containerInstanceArns: data.containerInstanceArns,
clusterName: clusterName
});
} else {
// TODO: Find a more sane solutions to handle rejections
new Promise((_, reject) => reject()).catch(() =>
console.log(`no instance present in ${clusterName}`)
);
}
}
});
});
};
/**
*
* @param {Array} ecsInstances
* @param {string} clusterName
* @returns {object}
*/
const describeContainerInstances = async (containerInstances, clusterName) => {
return new Promise((resolve, reject) => {
const params = {
containerInstances: containerInstances,
cluster: clusterName
};
ecs.describeContainerInstances(params, (err, data) => {
if (err) reject(err);
else resolve(data.containerInstances);
});
});
};
/**
*
* @param {string} clusterName
*/
const getRemainingResources = async clusterName => {
const clusterInstances = await listContainerInstances(clusterName);
const clusterIntancesDecription = await describeContainerInstances(
clusterInstances.containerInstanceArns,
clusterInstances.clusterName
);
for (const instance of clusterIntancesDecription) {
console.log(`Remaining ecs instances resources in ${clusterName}`);
console.log(
`----------------------------------------------------------------`
);
for (const resource of instance.remainingResources) {
if (resource.name === "CPU" || resource.name === "MEMORY") {
console.log(`${resource.name}: ${resource.integerValue}`);
}
}
console.log("\n");
}
};
const getAllRemainingResources = async clusterName => {
const allClusterNames = await getClusters().then(data =>
data.map(name => splitArnName(name))
);
// console.log(allClusterNames);
allClusterNames.forEach(element => {
getRemainingResources(element);
});
};
/**
* @returns {Array}
*/
const getClusters = async () => {
return new Promise((resolve, reject) => {
const params = {};
ecs.listClusters(params, (err, data) => {
if (err) reject(err);
else resolve(data.clusterArns);
});
});
};
const main = async () => {
try {
// const clusters = await getClusters();
// const clusterInstances = await listContainerInstances("dev");
// const clusterIntancesDecription = await describeContainerInstances(clusterInstances.containerInstanceArns, clusterInstances.clusterName);
// console.log(clusters);
//console.log(clusterInstances);
//console.log(clusterIntancesDecription)
//await getRemainingResources('dev')
await getAllRemainingResources();
// return clusters;
} catch (error) {
console.log(error);
}
};
main();