/
asgmanager.go
97 lines (87 loc) · 2.53 KB
/
asgmanager.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
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
package asgmanager
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/morfien101/asg-health/ec2metadatareader"
"os"
)
func awsSession() (*session.Session, error) {
region, ok := os.LookupEnv("AWS_REGION")
if !ok {
guess, err := ec2metadatareader.Region()
if err != nil {
return nil, err
}
region = guess
}
return session.NewSession(&aws.Config{Region: aws.String(region)})
}
func asgSession(session *session.Session) *autoscaling.AutoScaling {
return autoscaling.New(session)
}
func newAWSSession() (*autoscaling.AutoScaling, error) {
basicSession, err := awsSession()
if err != nil {
return nil, err
}
asgSession := asgSession(basicSession)
return asgSession, nil
}
func SetUnhealthy(instanceID string) error {
asgSession, err := newAWSSession()
if err != nil {
return err
}
currentHealth, err := getCurrentHealth(asgSession, instanceID)
if currentHealth == "Unhealthy" {
return nil
}
return setUnhealthy(asgSession, instanceID)
}
// IsInService return ok bool, current state string and an error
func IsInService(instanceID string) (bool, string, error) {
asgSession, err := newAWSSession()
if err != nil {
return false, "Not_Available", err
}
details, err := getInstanceDetails(asgSession, instanceID)
if err != nil {
return false, "Not_Available", err
}
if *details.LifecycleState == "InService" {
return true, *details.LifecycleState, nil
}
return false, *details.LifecycleState, nil
}
func getCurrentHealth(asgSession *autoscaling.AutoScaling, instanceID string) (string, error) {
instanceDetails, err := getInstanceDetails(asgSession, instanceID)
if err != nil {
return "", err
}
return *instanceDetails.LifecycleState, nil
}
func getInstanceDetails(asgSession *autoscaling.AutoScaling, instanceID string) (*autoscaling.InstanceDetails, error) {
input := &autoscaling.DescribeAutoScalingInstancesInput{
InstanceIds: []*string{aws.String(instanceID)},
}
output, err := asgSession.DescribeAutoScalingInstances(input)
if err != nil {
return nil, err
}
if len(output.AutoScalingInstances) == 0 {
return nil, fmt.Errorf("No health status found for instance %s", instanceID)
}
return output.AutoScalingInstances[0], nil
}
func setUnhealthy(asgSession *autoscaling.AutoScaling, instanceID string) error {
_, err := asgSession.SetInstanceHealth(&autoscaling.SetInstanceHealthInput{
HealthStatus: aws.String("Unhealthy"),
InstanceId: aws.String(instanceID),
})
if err != nil {
return err
}
return nil
}