This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
list.go
61 lines (50 loc) · 1.74 KB
/
list.go
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
package ecs
import (
"fmt"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/magicpool-co/pool/pkg/aws"
"github.com/magicpool-co/pool/types"
)
func GetContainerByInstanceID(client *aws.Client, cluster, instanceID string) (string, error) {
ecsSvc := ecs.New(client.Session())
containers, err := ecsSvc.ListContainerInstances(&ecs.ListContainerInstancesInput{
Cluster: types.StringPtr(cluster),
Filter: types.StringPtr(fmt.Sprintf(`ec2InstanceId == '%s'`, instanceID)),
})
if err != nil {
return "", err
} else if len(containers.ContainerInstanceArns) == 0 {
return "", fmt.Errorf("unable to find container by instance %s in cluster %s", instanceID, cluster)
}
return types.StringValue(containers.ContainerInstanceArns[0]), nil
}
func toggleDrainInstance(client *aws.Client, cluster, instance string, drain bool) error {
state := "ACTIVE"
if drain {
state = "DRAINING"
}
ecsSvc := ecs.New(client.Session())
res, err := ecsSvc.UpdateContainerInstancesState(&ecs.UpdateContainerInstancesStateInput{
Cluster: types.StringPtr(cluster),
ContainerInstances: []*string{
types.StringPtr(instance),
},
Status: types.StringPtr(state),
})
if err != nil {
return err
}
for _, failure := range res.Failures {
arn := types.StringValue(failure.Arn)
detail := types.StringValue(failure.Detail)
reason := types.StringValue(failure.Reason)
return fmt.Errorf("failed for ARN (%s): %s: %s", arn, detail, reason)
}
return nil
}
func DrainClusterContainerInstance(client *aws.Client, cluster, instance string) error {
return toggleDrainInstance(client, cluster, instance, true)
}
func ActivateClusterContainerInstance(client *aws.Client, cluster, instance string) error {
return toggleDrainInstance(client, cluster, instance, false)
}