Skip to content

Commit bd2c5c8

Browse files
committed
upgrades to terraform sdk v2; adds forwarded ports
1 parent 04e9999 commit bd2c5c8

File tree

16 files changed

+573
-230
lines changed

16 files changed

+573
-230
lines changed

.gitignore

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13-
terraform.tfstate*
14-
terraform-provider-vagrant
1513
config.ign.merged
16-
.terraform
1714
coverage.txt
1815
dist
1916

20-
.idea
17+
.idea
18+
19+
.terraform
20+
.terraform.lock.hcl
21+
terraform.d
22+
terraform.tfstate*
23+
terraform-provider-vagrant
24+
.vagrant

.goreleaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ builds:
1919
- goarch: '386'
2020
goos: darwin
2121
ldflags:
22-
- -s -w -X version.ProviderVersion={{.Version}}
22+
- -s -w -X main.version={{.Version}}
2323
mod_timestamp: '{{ .CommitTimestamp }}'
2424
archives:
2525
- name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'

README.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Go Report Card](https://goreportcard.com/badge/github.com/bmatcuk/terraform-provider-vagrant)](https://goreportcard.com/report/github.com/bmatcuk/terraform-provider-vagrant)
44

55
# terraform-provider-vagrant
6-
A Vagrant provider for terraform.
6+
A Vagrant provider for terraform 0.12+.
77

88
A note about lippertmarkus/vagrant in the registry: when I originally wrote
99
this provider, the terraform registry didn't exist. My terraform needs waned
@@ -20,7 +20,7 @@ terraform {
2020
required_providers {
2121
vagrant = {
2222
source = "bmatcuk/vagrant"
23-
version = "~> 3.0.0"
23+
version = "~> 4.0.0"
2424
}
2525
}
2626
}
@@ -33,6 +33,7 @@ resource "vagrant_vm" "my_vagrant_vm" {
3333
env = {
3434
KEY = "value",
3535
}
36+
get_ports = true
3637
}
3738
```
3839

@@ -44,6 +45,14 @@ absolute or relative paths.
4445
**env** is a map of additional environment variables to pass to the Vagrantfile.
4546
The environment variables set by the calling process are always passed.
4647

48+
**get_ports** if `true`, information about forwarded ports will be filled in
49+
(see `ports` below). This is `false` by default because it may take some time
50+
to run.
51+
52+
If you have multiple Vagrantfiles, provide an `alias` in the `provider` block
53+
and use the `provider` meta-argument in the resource/data-source
54+
configurations.
55+
4756
### Outputs
4857
* `machine_names.#` - a list of machine names as defined in the Vagrantfile.
4958
* `ssh_config.#` - SSH connection info. Since a Vagrantfile may create multiple
@@ -62,10 +71,16 @@ The environment variables set by the calling process are always passed.
6271
any additional configuration. However, if there is more than one machine, the
6372
connection info will not be set; you'll need to create some `null_resources`
6473
to do your provisioning.
74+
* `ports.#` - information about forwarded ports if `get_ports` is `true`. This
75+
is a list of lists: for each machine in the Vagrantfile, `ports` will have a
76+
list with the following variables:
6577

66-
Note that `machine_names` and `ssh_config` are guaranteed to be in the same
67-
order (ie, `ssh_config[0]` is the corresponding config for the machine named
68-
`machine_names[0]`), but the order is undefined (ie, don't count on
78+
* `ports.*.*.guest` - the port on the guest VM
79+
* `ports.*.*.host` - the host port forwarded to the guest VM
80+
81+
Note that `machine_names`, `ssh_config`, and `ports` are guaranteed to be in
82+
the same order (ie, `ssh_config[0]` is the corresponding config for the machine
83+
named `machine_names[0]`), but the order is undefined (ie, don't count on
6984
`machine_names[0]` being the first machine defined in the Vagrantfile).
7085

7186
## Forcing an Update
@@ -80,7 +95,7 @@ something like this:
8095
resource "vagrant_vm" "my_vagrant_vm" {
8196
vagrantfile_dir = "path/to/dir"
8297
env = {
83-
VAGRANTFILE_HASH = "${md5(file("path/to/dir/Vagrantfile"))}",
98+
VAGRANTFILE_HASH = md5(file("path/to/dir/Vagrantfile")),
8499
}
85100
}
86101
```
@@ -113,4 +128,19 @@ env TF_LOG=TRACE terraform apply ...
113128

114129
And, of course, you can always run vagrant on your Vagrantfile directly.
115130

131+
## Local Development
132+
The example in `examples/resources/vagrant_vm` is fully functioning, but you'll
133+
need to compile this provider and put it in a place terraform can find it:
134+
135+
```bash
136+
go build
137+
mkdir -p examples/resources/vagrant_vm/terraform.d/plugins/registry.terraform.io/bmatcuk/vagrant/4.0.0/darwin_amd64
138+
mv terraform-provider-vagrant examples/resources/vagrant_vm/terraform.d/plugins/registry.terraform.io/bmatcuk/vagrant/4.0.0/darwin_amd64/
139+
cd examples/resources/vagrant_vm
140+
terraform init
141+
terraform apply
142+
```
143+
144+
Adjust `darwin_amd64` to match your system.
145+
116146
[required_providers]: https://www.terraform.io/docs/language/providers/requirements.html#requiring-providers

docs/index.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: |-
55
---
66

77
# Vagrant Provider
8-
A Vagrant provider for terraform.
8+
A Vagrant provider for terraform 0.12+.
99

1010
A note about lippertmarkus/vagrant in the registry: when I originally wrote
1111
this provider, the terraform registry didn't exist. My terraform needs waned
@@ -22,7 +22,7 @@ terraform {
2222
required_providers {
2323
vagrant = {
2424
source = "bmatcuk/vagrant"
25-
version = "~> 3.0.0"
25+
version = "~> 4.0.0"
2626
}
2727
}
2828
}
@@ -36,8 +36,20 @@ provider "vagrant" {
3636
}
3737
```
3838

39-
<!-- schema generated by tfplugindocs -->
40-
## Schema
39+
## Removing Machines
40+
Sadly, due to some limitations in vagrant, it's not possible to automatically
41+
remove a portion of machines from a Vagrantfile. In other words, if your
42+
Vagrantfile defines 5 machines and you remove 2 of them from the Vagrantfile,
43+
they will be left running in your vagrant provider (ie, virtualbox or whatever)
44+
with no way of removing them via vagrant (or terraform).
45+
46+
If you intend of removing some machines, you should manually run `vagrant
47+
destroy MACHINE_NAME` on those machines you wish to remove *before* editing the
48+
Vagrantfile. Then update your Vagrantfile and allow terraform to do the rest.
49+
50+
If you forget, you can manually cleanup these old VMs by launching your vagrant
51+
provider's UI and deleting the machines. Then run `vagrant global-status
52+
--prune` to cleanup vagrant's cache of these machines.
4153

4254
## Debugging
4355
If terrafrom is failing on the vagrant step, you can get additional output by

docs/resources/vm.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ and terraform will ask for an update.
2121
resource "vagrant_vm" "my_vagrant_vm" {
2222
env = {
2323
# force terraform to re-run vagrant if the Vagrantfile changes
24-
VAGRANTFILE_HASH = "${md5(file("path/to/Vagrantfile"))}",
24+
VAGRANTFILE_HASH = md5(file("./Vagrantfile")),
2525
}
26+
get_ports = true
2627
# see schema for additional options
2728
}
2829
```
@@ -33,12 +34,14 @@ resource "vagrant_vm" "my_vagrant_vm" {
3334
### Optional
3435

3536
- **env** (Map of String) Environment variables to pass to the Vagrantfile.
37+
- **get_ports** (Boolean) Whether or not to retrieve forwarded port information. See `ports`. Defaults to `false`.
3638
- **id** (String) The ID of this resource.
37-
- **vagrantfile_dir** (String) Path to the directory where the Vagrantfile can be found. Defaults to the current directory.
39+
- **vagrantfile_dir** (String) Path to the directory where the Vagrantfile can be found. Defaults to `.`.
3840

3941
### Read-Only
4042

4143
- **machine_names** (List of String) Names of the vagrant machines from the Vagrantfile. Names are in the same order as ssh_config.
44+
- **ports** (List of List of Object) Forwarded ports per machine. Only set if `get_ports` is true.
4245
- **ssh_config** (List of Object) SSH connection information. (see [below for nested schema](#nestedatt--ssh_config))
4346

4447
<a id="nestedatt--ssh_config"></a>
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,31 @@
11
# -*- mode: ruby -*-
22
# vi: set ft=ruby :
33

4-
required_plugins = %w(vagrant-ignition)
5-
plugins_to_install = required_plugins.reject(&Vagrant.method(:has_plugin?))
6-
unless plugins_to_install.empty?
7-
puts "Installing plugins: #{plugins_to_install.join(', ')}"
8-
if system "vagrant plugin install #{plugins_to_install.join(' ')}"
9-
exec "vagrant #{ARGV.join(' ')}"
10-
else
11-
abort 'Installation of one or more plugins failed.'
12-
end
13-
end
14-
154
$vm_cpus = 1
165
$vm_memory = 1024
176

187
Vagrant.configure("2") do |config|
8+
config.vm.box = "minimal/trusty64"
9+
1910
config.ssh.insert_key = false
2011
config.ssh.forward_agent = true
2112

22-
config.vm.box = 'coreos-stable'
23-
config.vm.box_url = 'https://stable.release.core-os.net/amd64-usr/current/coreos_production_vagrant_virtualbox.json'
13+
config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
14+
15+
config.vm.synced_folder ".", "/vagrant", disabled: true
2416

2517
if Vagrant.has_plugin? 'vagrant-vbguest'
2618
config.vbguest.auto_update = false
2719
end
2820

2921
config.vm.provider :virtualbox do |vb|
22+
vb.customize ["modifyvm", :id, "--usb", "on"]
23+
vb.customize ["modifyvm", :id, "--usbehci", "off"]
24+
3025
vb.gui = false
3126
vb.cpus = $vm_cpus
3227
vb.memory = $vm_memory
3328
vb.check_guest_additions = false
3429
vb.functional_vboxsf = false
35-
config.ignition.enabled = true
36-
config.ignition.config_obj = vb
3730
end
3831
end

examples/resources/vagrant_vm/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
terraform {
2+
required_providers {
3+
vagrant = {
4+
source = "bmatcuk/vagrant"
5+
}
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "host_port" {
2+
value = vagrant_vm.my_vagrant_vm.ports[0][0].host
3+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
resource "vagrant_vm" "my_vagrant_vm" {
22
env = {
33
# force terraform to re-run vagrant if the Vagrantfile changes
4-
VAGRANTFILE_HASH = "${md5(file("path/to/Vagrantfile"))}",
4+
VAGRANTFILE_HASH = md5(file("./Vagrantfile")),
55
}
6+
get_ports = true
67
# see schema for additional options
78
}

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ module github.com/bmatcuk/terraform-provider-vagrant
22

33
require (
44
github.com/bmatcuk/go-vagrant v1.4.2
5-
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce // indirect
65
github.com/hashicorp/terraform-plugin-docs v0.4.0 // indirect
7-
github.com/hashicorp/terraform-plugin-sdk v1.0.0
6+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.3
7+
github.com/mattn/go-colorable v0.1.8 // indirect
8+
github.com/zclconf/go-cty v1.7.1 // indirect
89
)
910

1011
go 1.13

0 commit comments

Comments
 (0)