-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_apply_ansible_playbooks_log_summarizer.go
132 lines (122 loc) · 3.48 KB
/
aws_apply_ansible_playbooks_log_summarizer.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
package awsapplyansibleplaybookslogsummarizer
import (
"fmt"
"sort"
"time"
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
ansiblelogparser "github.com/yukihiko-shinoda/go-ansible-log-parser"
ansiblelogparserforcloudwatch "github.com/yukihiko-shinoda/go-ansible-log-parser-for-cloudwatch"
"github.com/yukihiko-shinoda/go-aws-api-util/cloudwatch/logs"
"github.com/yukihiko-shinoda/go-aws-api-util/ec2/instance"
"github.com/yukihiko-shinoda/go-aws-api-util/ssm/command"
)
type structFailed struct {
instanceId string
tagName string
errorMessage string
}
type structSucceed struct {
instanceId string
tagName string
timestamp time.Time
log string
playRecap *ansiblelogparser.StructPlayRecap
}
func ViewLog(ssmClient command.ListCommandsListCommandInvocationsAPI, ec2Client instance.DescribeInstancesAPI, cloudwatchlogsClient logs.GetLogEventsAPI, commandId *string) error {
commandInvocations, err := command.GetApplyAnsiblePlaybooksInvocations(ssmClient, commandId)
if err != nil {
return err
}
succeed, failed := checkCloudWatchLogs(ec2Client, cloudwatchlogsClient, commandInvocations)
report(failed, succeed)
return nil
}
func checkCloudWatchLogs(ec2Client instance.DescribeInstancesAPI, cloudwatchlogsClient logs.GetLogEventsAPI, commandInvocations []types.CommandInvocation) ([]structSucceed, []structFailed) {
var failed []structFailed
var succeed []structSucceed
for _, commandInvocation := range commandInvocations {
logStreamName := command.BuildLogStreamNameRunShellScriptStdout(commandInvocation)
tagName, err := instance.GetNameFromTag(ec2Client, commandInvocation)
if err != nil {
errorString := err.Error()
tagName = &errorString
}
events, err := logs.GetAllLogs(cloudwatchlogsClient, "/aws/systems-manager/run-command", logStreamName)
if err != nil {
failed = append(failed, structFailed{
*commandInvocation.InstanceId,
*tagName,
err.Error(),
})
continue
}
playRecap, err := ansiblelogparserforcloudwatch.PickupNumberPlayRecap(events)
if err != nil {
failed = append(failed, structFailed{
*commandInvocation.InstanceId,
*tagName,
err.Error(),
})
continue
}
message := ansiblelogparserforcloudwatch.PickupMessage(events)
succeed = append(succeed, structSucceed{
*commandInvocation.InstanceId,
*tagName,
time.Unix(*events[0].Timestamp/int64(time.Microsecond), 0).Local(),
message,
playRecap,
})
}
return sortSucceed(succeed), sortFailed(failed)
}
func sortSucceed(succeed []structSucceed) []structSucceed {
sort.Slice(succeed, func(i, j int) bool {
playRecapI := succeed[i].playRecap
playRecapJ := succeed[j].playRecap
if playRecapI == nil {
return true
}
if playRecapJ == nil {
return false
}
if playRecapI.Failed > playRecapJ.Failed {
return true
}
if playRecapI.Failed < playRecapJ.Failed {
return false
}
if playRecapI.Changed > playRecapJ.Changed {
return true
}
if playRecapI.Changed < playRecapJ.Changed {
return false
}
return succeed[i].tagName > succeed[j].tagName
})
return succeed
}
func sortFailed(failed []structFailed) []structFailed {
sort.Slice(failed, func(i, j int) bool {
return failed[i].tagName > failed[j].tagName
})
return failed
}
func report(failed []structFailed, succeed []structSucceed) {
for _, f := range failed {
fmt.Println(
f.instanceId,
f.tagName,
f.errorMessage,
)
}
for _, s := range succeed {
fmt.Printf(
"%s\t%s\t%s\n%s\n",
s.instanceId,
s.tagName,
s.timestamp,
s.log,
)
}
}