forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2-syslog.go
109 lines (91 loc) · 3.61 KB
/
ec2-syslog.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
package aws
import (
"encoding/base64"
"fmt"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/retry"
)
// (Deprecated) See the FetchContentsOfFileFromInstance method for a more powerful solution.
//
// GetSyslogForInstance gets the syslog for the Instance with the given ID in the given region. This should be available ~1 minute after an
// Instance boots and is very useful for debugging boot-time issues, such as an error in User Data.
func GetSyslogForInstance(t *testing.T, instanceID string, awsRegion string) string {
out, err := GetSyslogForInstanceE(t, instanceID, awsRegion)
if err != nil {
t.Fatal(err)
}
return out
}
// (Deprecated) See the FetchContentsOfFileFromInstanceE method for a more powerful solution.
//
// GetSyslogForInstanceE gets the syslog for the Instance with the given ID in the given region. This should be available ~1 minute after an
// Instance boots and is very useful for debugging boot-time issues, such as an error in User Data.
func GetSyslogForInstanceE(t *testing.T, instanceID string, region string) (string, error) {
description := fmt.Sprintf("Fetching syslog for Instance %s in %s", instanceID, region)
maxRetries := 120
timeBetweenRetries := 5 * time.Second
logger.Log(t, description)
client, err := NewEc2ClientE(t, region)
if err != nil {
return "", err
}
input := ec2.GetConsoleOutputInput{
InstanceId: aws.String(instanceID),
}
syslogB64, err := retry.DoWithRetryE(t, description, maxRetries, timeBetweenRetries, func() (string, error) {
out, err := client.GetConsoleOutput(&input)
if err != nil {
return "", err
}
syslog := aws.StringValue(out.Output)
if syslog == "" {
return "", fmt.Errorf("Syslog is not yet available for instance %s in %s", instanceID, region)
}
return syslog, nil
})
if err != nil {
return "", err
}
syslogBytes, err := base64.StdEncoding.DecodeString(syslogB64)
if err != nil {
return "", err
}
return string(syslogBytes), nil
}
// (Deprecated) See the FetchContentsOfFilesFromAsg method for a more powerful solution.
//
// GetSyslogForInstancesInAsg gets the syslog for each of the Instances in the given ASG in the given region. These logs should be available ~1
// minute after the Instance boots and are very useful for debugging boot-time issues, such as an error in User Data.
// Returns a map of Instance Id -> Syslog for that Instance.
func GetSyslogForInstancesInAsg(t *testing.T, asgName string, awsRegion string) map[string]string {
out, err := GetSyslogForInstancesInAsgE(t, asgName, awsRegion)
if err != nil {
t.Fatal(err)
}
return out
}
// (Deprecated) See the FetchContentsOfFilesFromAsgE method for a more powerful solution.
//
// GetSyslogForInstancesInAsgE gets the syslog for each of the Instances in the given ASG in the given region. These logs should be available ~1
// minute after the Instance boots and are very useful for debugging boot-time issues, such as an error in User Data.
// Returns a map of Instance Id -> Syslog for that Instance.
func GetSyslogForInstancesInAsgE(t *testing.T, asgName string, awsRegion string) (map[string]string, error) {
logger.Logf(t, "Fetching syslog for each Instance in ASG %s in %s", asgName, awsRegion)
instanceIDs, err := GetEc2InstanceIdsByTagE(t, awsRegion, "aws:autoscaling:groupName", asgName)
if err != nil {
return nil, err
}
logs := map[string]string{}
for _, id := range instanceIDs {
syslog, err := GetSyslogForInstanceE(t, id, awsRegion)
if err != nil {
return nil, err
}
logs[id] = syslog
}
return logs, nil
}