Skip to content

Commit 08faae1

Browse files
committed
add environment variable support
1 parent d749a3b commit 08faae1

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ extract the archive. Then copy the binary to [the terraform plugin directory].
1313
```hcl
1414
resource "vagrant_vm" "my_vagrant_vm" {
1515
vagrantfile_dir = "path/to/dir"
16+
env = {
17+
"key": "value",
18+
}
1619
}
1720
```
1821

@@ -21,6 +24,9 @@ Vagrantfile must exist when terraform runs or else it will throw an error. This
2124
option defaults to `.`, ie, the current directory and you may set this value to
2225
absolute or relative paths.
2326

27+
**env** is a map of additional environment variables to pass to the Vagrantfile.
28+
The environment variables set by the calling process are always passed.
29+
2430
### Outputs
2531
* `ssh_config.#` - SSH connection info. Since a Vagrantfile may create multiple
2632
machines, this is a list with the following variables:
@@ -33,5 +39,11 @@ absolute or relative paths.
3339
* `ssh_config.*.agent` - whether or not to use the agent for authentication
3440
(always "false" for now).
3541

42+
If there is only one machine built by the Vagrantfile, the connection info will
43+
be set in the `resource` block so you can include provisioners without any
44+
additional configuration. However, if there are more than one machines, the
45+
connection info will not be set; you'll need to create some `null_resources` to
46+
do your provisioning.
47+
3648
[the latest release]: https://github.com/bmatcuk/terraform-provider-vagrant/releases/latest
3749
[the terraform plugin directory]: https://www.terraform.io/docs/configuration/providers.html#third-party-plugins

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
99
github.com/bgentry/speakeasy v0.1.0 // indirect
1010
github.com/blang/semver v3.5.1+incompatible // indirect
11-
github.com/bmatcuk/go-vagrant v1.1.2
11+
github.com/bmatcuk/go-vagrant v1.2.1
1212
github.com/fatih/color v1.7.0 // indirect
1313
github.com/hashicorp/errwrap v0.0.0-20180715044906-d6c0cd880357 // indirect
1414
github.com/hashicorp/go-cleanhttp v0.0.0-20171218145408-d5fe4b57a186 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ github.com/bmatcuk/go-vagrant v1.1.1 h1:rZ+zCTv3xJM1LINJwVtVGkXs+L6HXstA2QnX8QLf
1818
github.com/bmatcuk/go-vagrant v1.1.1/go.mod h1:wybyCOf1R4TB2OXU8l6ghwqlCUJvBhWS5TeVpNj1doc=
1919
github.com/bmatcuk/go-vagrant v1.1.2 h1:ZZsbvNSg3DmhzNSoPbd0MvbPwWSnkSfVfJdPbtBmQiA=
2020
github.com/bmatcuk/go-vagrant v1.1.2/go.mod h1:wybyCOf1R4TB2OXU8l6ghwqlCUJvBhWS5TeVpNj1doc=
21+
github.com/bmatcuk/go-vagrant v1.2.0 h1:z8tZAwribzCSs9HEPv7snqqXYghH/3VXBTlD4zGFTeE=
22+
github.com/bmatcuk/go-vagrant v1.2.0/go.mod h1:wybyCOf1R4TB2OXU8l6ghwqlCUJvBhWS5TeVpNj1doc=
23+
github.com/bmatcuk/go-vagrant v1.2.1 h1:GQddoSzp8qtNzQAlob5wHvAUPQL7gy3VapBf7SgJ0Bk=
24+
github.com/bmatcuk/go-vagrant v1.2.1/go.mod h1:wybyCOf1R4TB2OXU8l6ghwqlCUJvBhWS5TeVpNj1doc=
2125
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
2226
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
2327
github.com/go-ini/ini v1.25.4 h1:Mujh4R/dH6YL8bxuISne3xX2+qcQ9p0IxKAP6ExWoUo=

resource_vagrant_vm.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/hashicorp/terraform/helper/schema"
66

77
"context"
8+
"fmt"
89
"io/ioutil"
910
"os"
1011
"path/filepath"
@@ -32,6 +33,24 @@ func resourceVagrantVM() *schema.Resource {
3233
ValidateFunc: resourceVagrantVMPathToVagrantfileValidate,
3334
},
3435

36+
"env": {
37+
Description: "Environment variables to pass to the Vagrantfile.",
38+
Type: schema.TypeMap,
39+
Optional: true,
40+
Elem: &schema.Schema{
41+
Type: schema.TypeString,
42+
},
43+
},
44+
45+
"machine_names": {
46+
Description: "Names of the vagrant machines from the Vagrantfile. Names are in the same order as ssh_config.",
47+
Type: schema.TypeList,
48+
Computed: true,
49+
Elem: &schema.Schema{
50+
Type: schema.TypeString,
51+
},
52+
},
53+
3554
"ssh_config": {
3655
Description: "SSH connection information.",
3756
Type: schema.TypeList,
@@ -91,6 +110,8 @@ func resourceVagrantVMCreate(d *schema.ResourceData, m interface{}) error {
91110

92111
cmd := client.Up()
93112
cmd.Context = ctx
113+
cmd.Env = buildEnvironment(d.Get("env").(map[string]string))
114+
cmd.Parallel = true
94115
if err := cmd.Run(); err != nil {
95116
return err
96117
}
@@ -123,6 +144,7 @@ func resourceVagrantVMUpdate(d *schema.ResourceData, m interface{}) error {
123144

124145
cmd := client.Reload()
125146
cmd.Context = ctx
147+
cmd.Env = buildEnvironment(d.Get("env").(map[string]string))
126148
if err := cmd.Run(); err != nil {
127149
return nil
128150
}
@@ -141,6 +163,7 @@ func resourceVagrantVMDelete(d *schema.ResourceData, m interface{}) error {
141163

142164
cmd := client.Destroy()
143165
cmd.Context = ctx
166+
cmd.Env = buildEnvironment(d.Get("env").(map[string]string))
144167
return cmd.Run()
145168
}
146169

@@ -155,6 +178,7 @@ func resourceVagrantVMExists(d *schema.ResourceData, m interface{}) (bool, error
155178

156179
cmd := client.Status()
157180
cmd.Context = ctx
181+
cmd.Env = buildEnvironment(d.Get("env").(map[string]string))
158182
if err := cmd.Run(); err != nil {
159183
return false, err
160184
}
@@ -207,13 +231,15 @@ func buildId(info map[string]*vagrant.VMInfo) string {
207231
func readVagrantInfo(ctx context.Context, client *vagrant.VagrantClient, d *schema.ResourceData) error {
208232
cmd := client.SSHConfig()
209233
cmd.Context = ctx
234+
cmd.Env = buildEnvironment(d.Get("env").(map[string]string))
210235
if err := cmd.Run(); err != nil {
211236
return err
212237
}
213238

214239
sshConfigs := make([]map[string]string, len(cmd.Configs))
240+
keys := make([]string, len(cmd.Configs))
215241
i := 0
216-
for _, config := range cmd.Configs {
242+
for key, config := range cmd.Configs {
217243
sshConfig := make(map[string]string, 6)
218244
sshConfig["type"] = "ssh"
219245
sshConfig["user"] = config.User
@@ -224,13 +250,30 @@ func readVagrantInfo(ctx context.Context, client *vagrant.VagrantClient, d *sche
224250
}
225251
sshConfig["agent"] = "false"
226252
sshConfigs[i] = sshConfig
253+
keys[i] = key
227254
i++
228255
}
256+
229257
d.Set("ssh_config", sshConfigs)
258+
d.Set("machine_names", keys)
230259

231260
if len(sshConfigs) == 1 {
232261
d.SetConnInfo(sshConfigs[0])
233262
}
234263

235264
return nil
236265
}
266+
267+
func buildEnvironment(env map[string]string) []string {
268+
if len(env) == 0 {
269+
return nil
270+
}
271+
272+
envArray := make([]string, len(env))
273+
i := 0
274+
for key, value := range env {
275+
envArray[i] = fmt.Sprintf("%v=%v", key, value)
276+
i++
277+
}
278+
return envArray
279+
}

0 commit comments

Comments
 (0)