Skip to content
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

Make Terraform exec timeout configurable #134

Merged
merged 1 commit into from
Jun 4, 2020
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
13 changes: 13 additions & 0 deletions commands/serve_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"
"syscall"
"time"

lsctx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/langserver"
Expand All @@ -25,6 +26,7 @@ type ServeCommand struct {
logFilePath string
tfExecPath string
tfExecLogPath string
tfExecTimeout string
}

func (c *ServeCommand) flags() *flag.FlagSet {
Expand All @@ -34,6 +36,7 @@ func (c *ServeCommand) flags() *flag.FlagSet {
fs.StringVar(&c.logFilePath, "log-file", "", "path to a file to log into with support "+
"for variables (e.g. Timestamp, Pid, Ppid) via Go template syntax {{.VarName}}")
fs.StringVar(&c.tfExecPath, "tf-exec", "", "path to Terraform binary")
fs.StringVar(&c.tfExecTimeout, "tf-exec-timeout", "", "Overrides Terraform execution timeout (e.g. 30s)")
fs.StringVar(&c.tfExecLogPath, "tf-log-file", "", "path to a file for Terraform executions"+
" to be logged into with support for variables (e.g. Timestamp, Pid, Ppid) via Go template"+
" syntax {{.VarName}}")
Expand Down Expand Up @@ -79,6 +82,16 @@ func (c *ServeCommand) Run(args []string) int {
"(interpolated at the time of execution)", c.tfExecLogPath)
}

if c.tfExecTimeout != "" {
d, err := time.ParseDuration(c.tfExecTimeout)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to parse Terraform timeout: %s", err))
return 1
}
ctx = lsctx.WithTerraformExecTimeout(d, ctx)
logger.Printf("Terraform execution timeout set to %s", d)
}

srv := langserver.NewLangServer(ctx, handlers.NewSession)
srv.SetLogger(logger)

Expand Down
11 changes: 11 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package context

import (
"context"
"time"

"github.com/hashicorp/terraform-ls/internal/filesystem"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
Expand All @@ -27,6 +28,7 @@ var (
ctxTfVersion = &contextKey{"terraform version"}
ctxTfVersionSetter = &contextKey{"terraform version setter"}
ctxTfExecLogPath = &contextKey{"terraform executor log path"}
ctxTfExecTimeout = &contextKey{"terraform execution timeout"}
)

func missingContextErr(ctxKey *contextKey) *MissingContextErr {
Expand Down Expand Up @@ -147,3 +149,12 @@ func TerraformExecLogPath(ctx context.Context) (string, bool) {
path, ok := ctx.Value(ctxTfExecLogPath).(string)
return path, ok
}

func WithTerraformExecTimeout(timeout time.Duration, ctx context.Context) context.Context {
return context.WithValue(ctx, ctxTfExecTimeout, timeout)
}

func TerraformExecTimeout(ctx context.Context) (time.Duration, bool) {
path, ok := ctx.Value(ctxTfExecTimeout).(time.Duration)
return path, ok
}
4 changes: 3 additions & 1 deletion internal/terraform/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/hashicorp/terraform-ls/logging"
)

var defaultExecTimeout = 30 * time.Second

// Environment variables to pass through to Terraform
var passthroughEnvVars = []string{
// This allows Terraform to find custom-built providers
Expand Down Expand Up @@ -56,7 +58,7 @@ type command struct {
func NewExecutor(ctx context.Context, path string) *Executor {
return &Executor{
ctx: ctx,
timeout: 10 * time.Second,
timeout: defaultExecTimeout,
execPath: path,
logger: log.New(ioutil.Discard, "", 0),
cmdCtxFunc: func(ctx context.Context, path string, arg ...string) *exec.Cmd {
Expand Down
6 changes: 6 additions & 0 deletions langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
if path, ok := lsctx.TerraformExecLogPath(svc.srvCtx); ok {
tf.SetExecLogPath(path)
}

// Timeout is set via CLI flag, hence in the server context
if timeout, ok := lsctx.TerraformExecTimeout(svc.srvCtx); ok {
tf.SetTimeout(timeout)
}

tf.SetLogger(svc.logger)

ctx = lsctx.WithTerraformExecutor(tf, ctx)
Expand Down