-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
102 lines (86 loc) · 2.51 KB
/
Copy pathmain.go
File metadata and controls
102 lines (86 loc) · 2.51 KB
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
package main
import (
"context"
"errors"
"flag"
"log"
"math"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/retry"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
)
const (
DefaultSleepInterval = 15 * time.Second
)
type Config struct {
SleepInterval time.Duration
}
func parseArgs() (*Config, error) {
sleepInterval := flag.Int("sleepInterval", int(DefaultSleepInterval/time.Second), "Sleep Interval")
flag.Parse()
if *sleepInterval <= 0 {
return nil, errors.New("sleepInterval must be a positive integer")
}
return &Config{
SleepInterval: time.Duration(*sleepInterval) * time.Second,
}, nil
}
func main() {
cfg, err := parseArgs()
if err != nil {
log.Fatal(err)
}
ctx := context.TODO()
awscfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
log.Fatalf("%v", err)
}
var instanceIdentity *imds.GetInstanceIdentityDocumentOutput
for {
// Get instance identity
// Retry as needed. For some reason, configuring Retryer options here doesn't work.
i := imds.NewFromConfig(awscfg)
instanceIdentity, err = i.GetInstanceIdentityDocument(context.TODO(), &imds.GetInstanceIdentityDocumentInput{})
if err == nil {
break
}
log.Printf("Unable to load instance identity: %v - will retry in %d seconds", err, cfg.SleepInterval / time.Second)
time.Sleep(cfg.SleepInterval)
}
log.Printf("Instance ID is %s\n", instanceIdentity.InstanceID)
log.Println("Polling Auto Scaling status...")
svc := autoscaling.NewFromConfig(awscfg, func(options *autoscaling.Options) {
options.Region = instanceIdentity.Region
})
// Main loop
for {
instances, err := svc.DescribeAutoScalingInstances(ctx, &autoscaling.DescribeAutoScalingInstancesInput{
InstanceIds: []string{
instanceIdentity.InstanceID,
},
}, func(options *autoscaling.Options) {
// Retry transient errors
options.Retryer = retry.AddWithMaxAttempts(options.Retryer, math.MaxInt64)
})
if err != nil {
log.Fatalf("%v", err)
}
if len(instances.AutoScalingInstances) == 0 {
log.Println("Instance is not in an Auto Scaling Group. Exiting successfully.")
os.Exit(0)
}
lifecycleState := aws.ToString(instances.AutoScalingInstances[0].LifecycleState)
if os.Getenv("DEBUG") != "" {
log.Printf("Lifecycle state: %s\n", lifecycleState)
}
if lifecycleState == "InService" {
log.Println("Instance is now InService. Exiting successfully.")
os.Exit(0)
}
time.Sleep(cfg.SleepInterval)
}
}