-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package clierrors | ||
|
||
type CliError struct { | ||
message string | ||
} | ||
|
||
var ( | ||
//DeployCanceledError | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package watchstack | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"time" | ||
|
||
"github.com/fatih/color" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/cloudformation" | ||
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface" | ||
) | ||
|
||
// WatchLog represents a CloudFormation events watcher. It will stream events | ||
// to the configured destination until canceled. Adapted from Tyler Brock's | ||
// github.com/TylerBrock/saw repository. | ||
type WatchStack struct { | ||
cfn cloudformationiface.CloudFormationAPI | ||
PollInterval time.Duration | ||
W io.Writer | ||
} | ||
|
||
func New(cfn cloudformationiface.CloudFormationAPI) *WatchStack { | ||
return &WatchStack{ | ||
cfn: cfn, | ||
PollInterval: time.Second, | ||
W: os.Stdout, | ||
} | ||
} | ||
|
||
func (w *WatchStack) StreamEvents(ctx context.Context, stackId string) error { | ||
var ( | ||
lastSeenTime time.Time | ||
seenEvents map[string]struct{} | ||
) | ||
|
||
input := &cloudformation.DescribeStackEventsInput{ | ||
StackName: aws.String(stackId), | ||
} | ||
pageHandler := func(output *cloudformation.DescribeStackEventsOutput, last bool) bool { | ||
for _, event := range output.StackEvents { | ||
// Filter out any events that occurred before that last seen timestamp. | ||
// If we see a new timestamp, then we set the last seen timestamp to that | ||
// new timestamp. We can also clear the set of seen events. | ||
if event.Timestamp.Before(lastSeenTime) { | ||
continue | ||
} else if event.Timestamp.After(lastSeenTime) { | ||
lastSeenTime = *event.Timestamp | ||
seenEvents = make(map[string]struct{}) | ||
} | ||
|
||
// If we have already seen this event, don't output it. Otherwise, we | ||
// can output the event. | ||
if _, ok := seenEvents[*event.EventId]; ok { | ||
continue | ||
} | ||
|
||
fmt.Fprintf(w.W, "[%s] %s (%s): %s\n", | ||
color.WhiteString(event.Timestamp.Format(time.RFC3339)), | ||
*event.LogicalResourceId, | ||
*event.ResourceType, | ||
color.GreenString(*event.ResourceStatus), | ||
) | ||
seenEvents[*event.EventId] = struct{}{} | ||
} | ||
return !last | ||
} | ||
|
||
for { | ||
err := w.cfn.DescribeStackEventsPages(input, pageHandler) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
time.Sleep(w.PollInterval) | ||
} | ||
} |