forked from barnybug/cli53
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instances.go
151 lines (135 loc) · 3.28 KB
/
instances.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package cli53
import (
"context"
"fmt"
"regexp"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/route53"
)
type instancesArgs struct {
name string
off string
regions []string
wait bool
ttl int
match string
internal bool
aRecord bool
dryRun bool
}
type InstanceRecord struct {
name string
value string
}
func instances(ctx context.Context, args instancesArgs, config *aws.Config) {
zone := lookupZone(ctx, args.name)
fmt.Println("Getting DNS records")
describeInstancesInput := ec2.DescribeInstancesInput{}
if args.off == "" {
filter := ec2.Filter{
Name: aws.String("instance-state-name"),
Values: []*string{aws.String("running")},
}
describeInstancesInput.Filters = []*ec2.Filter{&filter}
}
var reMatch *regexp.Regexp
if args.match != "" {
var err error
reMatch, err = regexp.Compile(args.match)
if err != nil {
fatalIfErr(err)
}
}
insts := map[string]*ec2.Instance{}
for _, region := range args.regions {
ec2conn := ec2.New(session.New(), config.WithRegion(region))
for {
// paginated
output, err := ec2conn.DescribeInstances(&describeInstancesInput)
fatalIfErr(err)
for _, r := range output.Reservations {
for _, i := range r.Instances {
for _, tag := range i.Tags {
// limit to instances with a Name tag
if *tag.Key == "Name" {
if reMatch != nil && !reMatch.MatchString(*tag.Value) {
continue
}
insts[*tag.Value] = i
continue
}
}
}
}
if output.NextToken == nil {
break
}
describeInstancesInput.NextToken = output.NextToken
}
}
if len(insts) == 0 {
fmt.Println("No instances found")
}
var rtype string
if args.aRecord {
rtype = "A"
} else {
rtype = "CNAME"
}
suffix := "." + *zone.Name
suffix = strings.TrimSuffix(suffix, ".")
upserts := []*route53.Change{}
for name, instance := range insts {
var value *string
if *instance.State.Name != "running" {
value = &args.off
} else if args.aRecord {
if args.internal {
value = instance.PrivateIpAddress
} else {
value = instance.PublicIpAddress
}
} else {
if args.internal {
value = aws.String(*instance.PrivateDnsName + ".")
} else {
value = aws.String(*instance.PublicDnsName + ".")
}
}
// add domain suffix if missing
dnsname := name
if !strings.HasSuffix(dnsname, suffix) {
dnsname += suffix
}
rr := route53.ResourceRecord{
Value: value,
}
rrset := route53.ResourceRecordSet{
Name: &dnsname,
TTL: aws.Int64(int64(args.ttl)),
Type: &rtype,
ResourceRecords: []*route53.ResourceRecord{&rr},
}
change := route53.Change{
Action: aws.String("UPSERT"),
ResourceRecordSet: &rrset,
}
upserts = append(upserts, &change)
}
if args.dryRun {
fmt.Println("Dry-run, upserts that would be made:")
for _, upsert := range upserts {
rr := upsert.ResourceRecordSet
fmt.Printf("+ %s %s %v\n", *rr.Name, *rr.Type, *rr.ResourceRecords[0].Value)
}
} else {
resp := batchChanges(ctx, upserts, []*route53.Change{}, zone)
fmt.Printf("%d records upserted\n", len(upserts))
if args.wait && resp != nil {
waitForChange(ctx, resp.ChangeInfo)
}
}
}