github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/aws/ec2-syslog.go (about)

     1  package aws
     2  
     3  import (
     4  	"encoding/base64"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  	"github.com/gruntwork-io/terratest/modules/logger"
    11  	"github.com/gruntwork-io/terratest/modules/retry"
    12  	"github.com/gruntwork-io/terratest/modules/testing"
    13  )
    14  
    15  // (Deprecated) See the FetchContentsOfFileFromInstance method for a more powerful solution.
    16  //
    17  // GetSyslogForInstance gets the syslog for the Instance with the given ID in the given region. This should be available ~1 minute after an
    18  // Instance boots and is very useful for debugging boot-time issues, such as an error in User Data.
    19  func GetSyslogForInstance(t testing.TestingT, instanceID string, awsRegion string) string {
    20  	out, err := GetSyslogForInstanceE(t, instanceID, awsRegion)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	return out
    25  }
    26  
    27  // (Deprecated) See the FetchContentsOfFileFromInstanceE method for a more powerful solution.
    28  //
    29  // GetSyslogForInstanceE gets the syslog for the Instance with the given ID in the given region. This should be available ~1 minute after an
    30  // Instance boots and is very useful for debugging boot-time issues, such as an error in User Data.
    31  func GetSyslogForInstanceE(t testing.TestingT, instanceID string, region string) (string, error) {
    32  	description := fmt.Sprintf("Fetching syslog for Instance %s in %s", instanceID, region)
    33  	maxRetries := 120
    34  	timeBetweenRetries := 5 * time.Second
    35  
    36  	logger.Log(t, description)
    37  
    38  	client, err := NewEc2ClientE(t, region)
    39  	if err != nil {
    40  		return "", err
    41  	}
    42  
    43  	input := ec2.GetConsoleOutputInput{
    44  		InstanceId: aws.String(instanceID),
    45  	}
    46  
    47  	syslogB64, err := retry.DoWithRetryE(t, description, maxRetries, timeBetweenRetries, func() (string, error) {
    48  		out, err := client.GetConsoleOutput(&input)
    49  		if err != nil {
    50  			return "", err
    51  		}
    52  
    53  		syslog := aws.StringValue(out.Output)
    54  		if syslog == "" {
    55  			return "", fmt.Errorf("Syslog is not yet available for instance %s in %s", instanceID, region)
    56  		}
    57  
    58  		return syslog, nil
    59  	})
    60  
    61  	if err != nil {
    62  		return "", err
    63  	}
    64  
    65  	syslogBytes, err := base64.StdEncoding.DecodeString(syslogB64)
    66  	if err != nil {
    67  		return "", err
    68  	}
    69  
    70  	return string(syslogBytes), nil
    71  }
    72  
    73  // (Deprecated) See the FetchContentsOfFilesFromAsg method for a more powerful solution.
    74  //
    75  // GetSyslogForInstancesInAsg gets the syslog for each of the Instances in the given ASG in the given region. These logs should be available ~1
    76  // minute after the Instance boots and are very useful for debugging boot-time issues, such as an error in User Data.
    77  // Returns a map of Instance Id -> Syslog for that Instance.
    78  func GetSyslogForInstancesInAsg(t testing.TestingT, asgName string, awsRegion string) map[string]string {
    79  	out, err := GetSyslogForInstancesInAsgE(t, asgName, awsRegion)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	return out
    84  }
    85  
    86  // (Deprecated) See the FetchContentsOfFilesFromAsgE method for a more powerful solution.
    87  //
    88  // GetSyslogForInstancesInAsgE gets the syslog for each of the Instances in the given ASG in the given region. These logs should be available ~1
    89  // minute after the Instance boots and are very useful for debugging boot-time issues, such as an error in User Data.
    90  // Returns a map of Instance Id -> Syslog for that Instance.
    91  func GetSyslogForInstancesInAsgE(t testing.TestingT, asgName string, awsRegion string) (map[string]string, error) {
    92  	logger.Logf(t, "Fetching syslog for each Instance in ASG %s in %s", asgName, awsRegion)
    93  
    94  	instanceIDs, err := GetEc2InstanceIdsByTagE(t, awsRegion, "aws:autoscaling:groupName", asgName)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	logs := map[string]string{}
   100  	for _, id := range instanceIDs {
   101  		syslog, err := GetSyslogForInstanceE(t, id, awsRegion)
   102  		if err != nil {
   103  			return nil, err
   104  		}
   105  		logs[id] = syslog
   106  	}
   107  
   108  	return logs, nil
   109  }