Skip to content

feat: Add destroy to workspace provision job #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,8 @@ func convertWorkspaceTransition(transition database.WorkspaceTransition) (sdkpro
return sdkproto.WorkspaceTransition_START, nil
case database.WorkspaceTransitionStop:
return sdkproto.WorkspaceTransition_STOP, nil
case database.WorkspaceTransitionDelete:
return sdkproto.WorkspaceTransition_DESTROY, nil
default:
return 0, xerrors.Errorf("unrecognized transition: %q", transition)
}
Expand Down
7 changes: 5 additions & 2 deletions provisioner/terraform/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (t *terraform) runTerraformApply(ctx context.Context, terraform *tfexec.Ter
}()

t.logger.Debug(ctx, "running apply", slog.F("vars", len(vars)), slog.F("env", len(env)))
err := runApplyCommand(ctx, t.shutdownCtx, terraform.ExecPath(), terraform.WorkingDir(), writer, env, vars)
err := runApplyCommand(ctx, t.shutdownCtx, request.Metadata.WorkspaceTransition, terraform.ExecPath(), terraform.WorkingDir(), writer, env, vars)
if err != nil {
errorMessage := err.Error()
// Terraform can fail and apply and still need to store it's state.
Expand Down Expand Up @@ -440,7 +440,7 @@ func (t *terraform) runTerraformApply(ctx context.Context, terraform *tfexec.Ter

// This couldn't use terraform-exec, because it doesn't support cancellation, and there didn't appear
// to be a straight-forward way to add it.
func runApplyCommand(ctx, shutdownCtx context.Context, bin, dir string, stdout io.Writer, env, vars []string) error {
func runApplyCommand(ctx, shutdownCtx context.Context, transition proto.WorkspaceTransition, bin, dir string, stdout io.Writer, env, vars []string) error {
args := []string{
"apply",
"-no-color",
Expand All @@ -449,6 +449,9 @@ func runApplyCommand(ctx, shutdownCtx context.Context, bin, dir string, stdout i
"-json",
"-refresh=true",
}
if transition == proto.WorkspaceTransition_DESTROY {
args = append(args, "-destroy")
}
Comment on lines +452 to +454
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know this flag was available - I thought it needed terraform destroy - TIL. This flag is certainly cleaner 👍

for _, variable := range vars {
args = append(args, "-var", variable)
}
Expand Down
50 changes: 27 additions & 23 deletions provisionersdk/proto/provisioner.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions provisionersdk/proto/provisioner.proto
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ message Parse {
enum WorkspaceTransition {
START = 0;
STOP = 1;
DESTROY = 2;
}

// Provision consumes source-code from a directory to produce resources.
Expand Down