Skip to content

Instantly share code, notes, and snippets.

@EvanBoyle
Last active March 2, 2022 16:31
Show Gist options
  • Save EvanBoyle/9e4f9ce95a40c288274566d2c3134de0 to your computer and use it in GitHub Desktop.
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
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