8000 fix: Allow terraform provisions to be gracefully cancelled by mafredri · Pull Request #3526 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix: Allow terraform provisions to be gracefully cancelled #3526

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 14 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: Add ExitTimeout to terraform.ServeOptions
  • Loading branch information
mafredri committed Aug 17, 2022
commit 3e4005ad10853c35fb7d127f91f3c874b90d8685
8 changes: 1 addition & 7 deletions provisioner/terraform/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import (
"github.com/coder/coder/provisionersdk/proto"
)

const (
// Define how long we will wait for Terraform to exit cleanly
// after receiving an interrupt (if the provision was stopped).
terraformCancelTimeout = 5 * time.Minute
)

// Provision executes `terraform apply` or `terraform plan` for dry runs.
func (s *server) Provision(stream proto.DRPCProvisioner_ProvisionStream) error {
request, err := stream.Recv()
Expand Down Expand Up @@ -54,7 +48,7 @@ func (s *server) Provision(stream proto.DRPCProvisioner_ProvisionStream) error {
// part of graceful server shutdown procedure. Waiting on a
// process here should delay provisioner/coder shutdown.
select {
case <-time.After(terraformCancelTimeout):
case <-time.After(s.exitTimeout):
kill()
case <-killCtx.Done():
}
Expand Down
28 changes: 25 additions & 3 deletions provisioner/terraform/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"path/filepath"
"sync"
"time"

"github.com/cli/safeexec"
"github.com/hashicorp/go-version"
Expand All @@ -26,6 +27,10 @@ var (
terraformMinorVersionMismatch = xerrors.New("Terraform binary minor version mismatch.")
)

const (
defaultExitTimeout = 5 * time.Minute
)

type ServeOptions struct {
*provisionersdk.ServeOptions

Expand All @@ -34,6 +39,17 @@ type ServeOptions struct {
BinaryPath string
CachePath string
Logger slog.Logger

// ExitTimeout defines how long we will wait for a running Terraform
// command to exit (cleanly) if the provision was stopped. This only
// happens when the command is still running after the provision
// stream is closed. If the provision is canceled via RPC, this
// timeout will not be used.
//
// This is a no-op on Windows where the process can't be interrupted.
//
// Default value: 5 minutes.
ExitTimeout time.Duration
}

func absoluteBinaryPath(ctx context.Context) (string, error) {
Expand Down Expand Up @@ -92,10 +108,14 @@ func Serve(ctx context.Context, options *ServeOptions) error {
options.BinaryPath = absoluteBinary
}
}
if options.ExitTimeout == 0 {
options.ExitTimeout = defaultExitTimeout
}
return provisionersdk.Serve(ctx, &server{
binaryPath: options.BinaryPath,
cachePath: options.CachePath,
logger: options.Logger,
binaryPath: options.BinaryPath,
cachePath: options.CachePath,
logger: options.Logger,
exitTimeout: options.ExitTimeout,
}, options.ServeOptions)
}

Expand All @@ -107,6 +127,8 @@ type server struct {
binaryPath string
cachePath string
logger slog.Logger

exitTimeout time.Duration
}

func (s *server) executor(workdir string) executor {
Expand Down
0