Skip to content

Commit

Permalink
Make Terraform exec timeout configurable (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Jun 4, 2020
1 parent 3b8e2da commit bc69f99
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
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

0 comments on commit bc69f99

Please sign in to comment.