-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.mjs
96 lines (90 loc) · 2.24 KB
/
commands.mjs
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
import clientAutoScaling from "@aws-sdk/client-auto-scaling";
import clientECS from "@aws-sdk/client-ecs";
const { AutoScalingClient, CompleteLifecycleActionCommand } = clientAutoScaling;
const {
ECSClient,
ListContainerInstancesCommand,
UpdateContainerInstancesStateCommand,
ContainerInstanceStatus,
} = clientECS;
export const getContainerInstanceId = async ({
instanceId,
clusterName,
region,
}) => {
const client = new ECSClient({ region });
const command = new ListContainerInstancesCommand({
cluster: clusterName,
filter: `attribute:instanceId==${instanceId}`,
});
var response;
try {
response = await client.send(command);
const [containerInstanceArn] = response.containerInstanceArns;
return containerInstanceArn;
} catch (error) {
console.log(error);
response = null;
}
return response;
};
export const updateContainerInstance = async ({
clusterName,
containerInstanceId,
status,
region,
}) => {
const client = new ECSClient({ region });
const command = new UpdateContainerInstancesStateCommand({
cluster: clusterName,
containerInstances: [containerInstanceId],
status,
});
var response;
try {
response = await client.send(command);
if (response.failures.length > 0) {
response = null;
}
} catch (error) {
console.log(error);
response = null;
}
return response;
};
export const drainContainerInstance = async ({
clusterName,
containerInstanceId,
region,
}) => {
return updateContainerInstance({
clusterName,
containerInstanceId,
status: ContainerInstanceStatus.DRAINING,
region,
});
};
export const completeLifecycleAction = async ({
region,
autoScalingGroupName,
lifecycleHookName,
ec2InstanceId,
lifecycleActionToken,
}) => {
const client = new AutoScalingClient({ region });
const command = new CompleteLifecycleActionCommand({
AutoScalingGroupName: autoScalingGroupName,
LifecycleHookName: lifecycleHookName,
InstanceId: ec2InstanceId,
LifecycleActionToken: lifecycleActionToken,
/* required */ LifecycleActionResult: "CONTINUE",
});
var response;
try {
response = await client.send(command);
} catch (error) {
console.log(error);
response = null;
}
return response;
};