-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws.go
169 lines (142 loc) · 4.89 KB
/
aws.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
log "github.com/sirupsen/logrus"
)
// getInstanceTagValue - Get the string value for a given tag
func getInstanceTagValue(inst *ec2.Instance, name string, missingValue string) string {
for _, tag := range inst.Tags {
if (name != "") && (*tag.Key == name) {
return *tag.Value
}
}
return missingValue
}
// getInstanceStatus Get list of instances w/ event data
func getRegionInstanceStatus(awsRegion string) (*ec2.DescribeInstanceStatusOutput, error) {
sess := session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String(awsRegion), Credentials: credentials.NewSharedCredentials("", application.Config.AWS.Profile)},
Profile: application.Config.AWS.Profile,
}))
svc := ec2.New(sess)
params := &ec2.DescribeInstanceStatusInput{
Filters: []*ec2.Filter{
{
Name: aws.String("event.code"),
Values: []*string{
aws.String("instance-reboot"),
aws.String("instance-stop"),
aws.String("instance-retirement"),
aws.String("system-reboot"),
aws.String("system-maintenance"),
},
},
},
}
resp, err := svc.DescribeInstanceStatus(params)
if err != nil {
return resp, err
}
return resp, nil
}
// getInstanceData - Get actual instance data , such as tags, vpc, ect
func getInstanceData(awsRegion string, instanceID string) (*ec2.Instance, error) {
sess := session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String(awsRegion), Credentials: credentials.NewSharedCredentials("", application.Config.AWS.Profile)},
Profile: application.Config.AWS.Profile,
}))
svc := ec2.New(sess)
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("instance-id"),
Values: []*string{aws.String(instanceID)},
},
},
}
resp, err := svc.DescribeInstances(params)
if err != nil {
log.WithFields(log.Fields{
"instanceID": instanceID,
"awsProfile": application.Config.AWS.Profile,
"awsRegion": awsRegion,
}).Error(fmt.Printf("there was an error listing instances in %s", err.Error()))
}
return resp.Reservations[0].Instances[0], nil
}
func fetchRegionList() ([]string, error) {
awsSession := session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String("us-west-2"), Credentials: credentials.NewSharedCredentials("", application.Config.AWS.Profile)},
Profile: application.Config.AWS.Profile,
}))
svc := ec2.New(awsSession)
awsRegions, err := svc.DescribeRegions(&ec2.DescribeRegionsInput{})
if err != nil {
return nil, err
}
regions := make([]string, 0, len(awsRegions.Regions))
for _, region := range awsRegions.Regions {
regions = append(regions, *region.RegionName)
}
return regions, nil
}
func processInstance(resp *ec2.DescribeInstanceStatusOutput, awsRegion string) {
// Iterate over them
for _, instance := range resp.InstanceStatuses {
for _, event := range instance.Events {
// If instance ID does not exist in K/V store then its assumed to be a new event and we must act on it
// Find out if mainteance is complete and parse string
newInstance, err := newInstanceEvent(*instance.InstanceId, awsRegion)
if err != nil {
log.Fatal(err)
}
if newInstance == true {
isComplete, msg := maintenanceComplete(*event.Description, awsRegion, *instance.InstanceId)
if isComplete {
log.WithFields(log.Fields{
"instanceID": *instance.InstanceId,
"awsProfile": application.Config.AWS.Profile,
"awsRegion": awsRegion,
}).Warn("Maintance completed, no ticket to create")
break
}
log.WithFields(log.Fields{
"instanceID": *instance.InstanceId,
"awsProfile": application.Config.AWS.Profile,
"awsRegion": awsRegion,
"completed": isComplete,
"eventCode": *event.Code,
"notBefore": *event.NotBefore,
}).Info(msg)
issue := buildInstanceTicket(*event, *instance, awsRegion)
issueKey := createJiraIssue(issue)
_, err := application.DB.HSet([]byte(*instance.InstanceId), []byte("code"), []byte(*event.Code))
if err != nil {
log.Fatal(err)
}
_, err = application.DB.HSet([]byte(*instance.InstanceId), []byte("description"), []byte(*event.Description))
if err != nil {
log.Fatal(err)
}
_, err = application.DB.HSet([]byte(*instance.InstanceId), []byte("issue"), []byte(issueKey))
if err != nil {
log.Fatal(err)
}
_, err = application.DB.HExpire([]byte(*instance.InstanceId), application.Config.Ledis.KeyTimeExpireInSeconds)
if err != nil {
log.Fatal(err)
}
} else {
log.WithFields(log.Fields{
"instanceID": *instance.InstanceId,
"awsProfile": application.Config.AWS.Profile,
"awsRegion": awsRegion,
}).Warn("Instance event already processed")
}
}
}
}