Skip to content

Commit

Permalink
provisioner/local-exec: Allow passing env vars commands
Browse files Browse the repository at this point in the history
  • Loading branch information
cnicolov committed Sep 27, 2017
1 parent a28b5d2 commit 384abc4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
21 changes: 19 additions & 2 deletions builtin/provisioners/local-exec/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ func Provisioner() terraform.ResourceProvisioner {
Type: schema.TypeString,
Required: true,
},

"interpreter": &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
"environment": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
},
},

ApplyFunc: applyFn,
Expand All @@ -45,11 +48,20 @@ func applyFn(ctx context.Context) error {
o := ctx.Value(schema.ProvOutputKey).(terraform.UIOutput)

command := data.Get("command").(string)

if command == "" {
return fmt.Errorf("local-exec provisioner command must be a non-empty string")
}

// Execute the command with env
environment := data.Get("environment").(map[string]interface{})

var env []string
env = make([]string, len(environment))
for k := range environment {
entry := fmt.Sprintf("%s=%s", k, environment[k].(string))
env = append(env, entry)
}

// Execute the command using a shell
interpreter := data.Get("interpreter").([]interface{})

Expand Down Expand Up @@ -78,10 +90,15 @@ func applyFn(ctx context.Context) error {
return fmt.Errorf("failed to initialize pipe for output: %s", err)
}

var cmdEnv []string
cmdEnv = os.Environ()
cmdEnv = append(cmdEnv, env...)

// Setup the command
cmd := exec.CommandContext(ctx, cmdargs[0], cmdargs[1:]...)
cmd.Stderr = pw
cmd.Stdout = pw
cmd.Env = cmdEnv

output, _ := circbuf.NewBuffer(maxBufSize)

Expand Down
24 changes: 24 additions & 0 deletions builtin/provisioners/local-exec/resource_provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,27 @@ func TestResourceProvider_ApplyCustomInterpreter(t *testing.T) {
t.Errorf("wrong output\ngot: %s\nwant: %s", got, want)
}
}

func TestResourceProvider_ApplyCustomEnv(t *testing.T) {
c := testConfig(t, map[string]interface{}{
"command": "echo $FOO $BAR $BAZ",
"environment": map[string]interface{}{
"FOO": "BAR",
"BAR": 1,
"BAZ": "true",
},
})

output := new(terraform.MockUIOutput)
p := Provisioner()

if err := p.Apply(output, nil, c); err != nil {
t.Fatalf("err: %v", err)
}

got := strings.TrimSpace(output.OutputMessage)
want := "BAR 1 true"
if got != want {
t.Errorf("wrong output\ngot: %s\nwant: %s", got, want)
}
}
19 changes: 19 additions & 0 deletions website/docs/provisioners/local-exec.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ The following arguments are supported:
form "/bin/bash", "-c", "echo foo". If `interpreter` is unspecified,
sensible defaults will be chosen based on the system OS.

* `environment` - (Optional) block of key value pairs representing the
environment of the executed command. inherits the current process environment.

### Interpreter Examples

```hcl
Expand All @@ -66,3 +69,19 @@ resource "null_resource" "example2" {
}
}
```

```hcl
resource "aws_instance" "web" {
# ...
provisioner "local-exec" {
command = "echo $FOO $BAR $BAZ >> env_vars.txt"
environment {
FOO = "bar"
BAR = 1
BAZ = "true"
}
}
}
```

0 comments on commit 384abc4

Please sign in to comment.