forked from sevagh/goat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eni.go
81 lines (68 loc) · 1.78 KB
/
eni.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
package main
import (
log "github.com/sirupsen/logrus"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
//GoatEni runs Goat for your ENIs - attach
func GoatEni(debug bool, tagPrefix string) {
log.Printf("WELCOME TO GOAT")
log.Printf("1: COLLECTING EC2 INFO")
ec2Instance := GetEC2InstanceData(tagPrefix)
log.Printf("2: COLLECTING ENI INFO")
ec2Instance.FindEnis(tagPrefix)
log.Printf("3: ATTACHING ENIS")
if len(ec2Instance.Enis) == 0 {
log.Warn("Empty enis, nothing to do")
os.Exit(0)
}
ec2Instance.AttachEnis()
}
//FindEnis returns a list of all ENIs that should be attached to this EC2 instance
func (e *EC2Instance) FindEnis(tagPrefix string) {
log.Info("Searching for ENIs")
params := &ec2.DescribeNetworkInterfacesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:" + tagPrefix + ":Prefix"),
Values: []*string{
aws.String(e.Prefix),
},
},
{
Name: aws.String("tag:" + tagPrefix + ":NodeId"),
Values: []*string{
aws.String(e.NodeID),
},
},
{
Name: aws.String("availability-zone"),
Values: []*string{
aws.String(e.Az),
},
},
},
}
enis := []string{}
result, err := e.EC2Client.DescribeNetworkInterfaces(params)
if err != nil {
log.Fatalf("Error when searching for ENIs: %v", err)
}
for _, eni := range result.NetworkInterfaces {
attachedID := ""
if eni.Attachment != nil {
attachedID = *eni.Attachment.InstanceId
}
if attachedID != "" {
if attachedID != e.InstanceID {
log.Fatalf("Eni %s attached to different instance-id: %s", *eni.NetworkInterfaceId, attachedID)
} else {
log.Infof("Eni %s already attached to this instance, skipping", *eni.NetworkInterfaceId)
continue
}
}
enis = append(enis, *eni.NetworkInterfaceId)
}
e.Enis = enis
}