Last active
March 2, 2022 16:31
-
-
Save EvanBoyle/9e4f9ce95a40c288274566d2c3134de0 to your computer and use it in GitHub Desktop.
Code snippet to refresh a stack in a stuck state using Automation API
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
package main | |
import ( | |
"context" | |
"fmt" | |
"os" | |
"path/filepath" | |
"github.com/pulumi/pulumi/sdk/v3/go/auto" | |
"github.com/pulumi/pulumi/sdk/v3/go/auto/optrefresh" | |
) | |
func reset(ctx context.Context, stackName, workDir string) { | |
s, err := auto.UpsertStackLocalSource(ctx, stackName, workDir) | |
if err != nil { | |
fmt.Printf("Failed to create or select stack: %v\n", err) | |
os.Exit(1) | |
} | |
s.SetConfig(ctx, "aws:region", auto.ConfigValue{Value: "us-west-2"}) | |
fmt.Println("☁️ canceling any active updates...") | |
// we ignore the error on a cancel as it errors if there is no update running :(... | |
_ = s.Cancel(ctx) | |
fmt.Println("☁️ exporting stack...") | |
exp, err := s.Workspace().ExportStack(ctx, s.Name()) | |
if err != nil { | |
fmt.Println("failed to reset stack, could not export stack") | |
os.Exit(1) | |
} | |
fmt.Println("☁️ importing stack...") | |
err = s.Workspace().ImportStack(ctx, s.Name(), exp) | |
if err != nil { | |
fmt.Println("failed to reset stack, could not import stack") | |
os.Exit(1) | |
} | |
fmt.Println("☁️ refreshing stack...") | |
_, err = s.Refresh(ctx, optrefresh.ProgressStreams(os.Stdout)) | |
if err != nil { | |
fmt.Printf("Failed to refresh stack: %v\n", err) | |
os.Exit(1) | |
} | |
fmt.Println("☁️ refresh complete!") | |
fmt.Println("☁️ done! stack has been reset!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment