Skip to content

Commit

Permalink
upgrades to terraform sdk v2; adds forwarded ports
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatcuk committed Apr 14, 2021
1 parent 04e9999 commit bd2c5c8
Show file tree
Hide file tree
Showing 16 changed files with 573 additions and 230 deletions.
12 changes: 8 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
terraform.tfstate*
terraform-provider-vagrant
config.ign.merged
.terraform
coverage.txt
dist

.idea
.idea

.terraform
.terraform.lock.hcl
terraform.d
terraform.tfstate*
terraform-provider-vagrant
.vagrant
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ builds:
- goarch: '386'
goos: darwin
ldflags:
- -s -w -X version.ProviderVersion={{.Version}}
- -s -w -X main.version={{.Version}}
mod_timestamp: '{{ .CommitTimestamp }}'
archives:
- name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'
Expand Down
42 changes: 36 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/bmatcuk/terraform-provider-vagrant)](https://goreportcard.com/report/github.com/bmatcuk/terraform-provider-vagrant)

# terraform-provider-vagrant
A Vagrant provider for terraform.
A Vagrant provider for terraform 0.12+.

A note about lippertmarkus/vagrant in the registry: when I originally wrote
this provider, the terraform registry didn't exist. My terraform needs waned
Expand All @@ -20,7 +20,7 @@ terraform {
required_providers {
vagrant = {
source = "bmatcuk/vagrant"
version = "~> 3.0.0"
version = "~> 4.0.0"
}
}
}
Expand All @@ -33,6 +33,7 @@ resource "vagrant_vm" "my_vagrant_vm" {
env = {
KEY = "value",
}
get_ports = true
}
```

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

**get_ports** if `true`, information about forwarded ports will be filled in
(see `ports` below). This is `false` by default because it may take some time
to run.

If you have multiple Vagrantfiles, provide an `alias` in the `provider` block
and use the `provider` meta-argument in the resource/data-source
configurations.

### Outputs
* `machine_names.#` - a list of machine names as defined in the Vagrantfile.
* `ssh_config.#` - SSH connection info. Since a Vagrantfile may create multiple
Expand All @@ -62,10 +71,16 @@ The environment variables set by the calling process are always passed.
any additional configuration. However, if there is more than one machine, the
connection info will not be set; you'll need to create some `null_resources`
to do your provisioning.
* `ports.#` - information about forwarded ports if `get_ports` is `true`. This
is a list of lists: for each machine in the Vagrantfile, `ports` will have a
list with the following variables:

Note that `machine_names` and `ssh_config` are guaranteed to be in the same
order (ie, `ssh_config[0]` is the corresponding config for the machine named
`machine_names[0]`), but the order is undefined (ie, don't count on
* `ports.*.*.guest` - the port on the guest VM
* `ports.*.*.host` - the host port forwarded to the guest VM

Note that `machine_names`, `ssh_config`, and `ports` are guaranteed to be in
the same order (ie, `ssh_config[0]` is the corresponding config for the machine
named `machine_names[0]`), but the order is undefined (ie, don't count on
`machine_names[0]` being the first machine defined in the Vagrantfile).

## Forcing an Update
Expand All @@ -80,7 +95,7 @@ something like this:
resource "vagrant_vm" "my_vagrant_vm" {
vagrantfile_dir = "path/to/dir"
env = {
VAGRANTFILE_HASH = "${md5(file("path/to/dir/Vagrantfile"))}",
VAGRANTFILE_HASH = md5(file("path/to/dir/Vagrantfile")),
}
}
```
Expand Down Expand Up @@ -113,4 +128,19 @@ env TF_LOG=TRACE terraform apply ...

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

## Local Development
The example in `examples/resources/vagrant_vm` is fully functioning, but you'll
need to compile this provider and put it in a place terraform can find it:

```bash
go build
mkdir -p examples/resources/vagrant_vm/terraform.d/plugins/registry.terraform.io/bmatcuk/vagrant/4.0.0/darwin_amd64
mv terraform-provider-vagrant examples/resources/vagrant_vm/terraform.d/plugins/registry.terraform.io/bmatcuk/vagrant/4.0.0/darwin_amd64/
cd examples/resources/vagrant_vm
terraform init
terraform apply
```

Adjust `darwin_amd64` to match your system.

[required_providers]: https://www.terraform.io/docs/language/providers/requirements.html#requiring-providers
20 changes: 16 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: |-
---

# Vagrant Provider
A Vagrant provider for terraform.
A Vagrant provider for terraform 0.12+.

A note about lippertmarkus/vagrant in the registry: when I originally wrote
this provider, the terraform registry didn't exist. My terraform needs waned
Expand All @@ -22,7 +22,7 @@ terraform {
required_providers {
vagrant = {
source = "bmatcuk/vagrant"
version = "~> 3.0.0"
version = "~> 4.0.0"
}
}
}
Expand All @@ -36,8 +36,20 @@ provider "vagrant" {
}
```

<!-- schema generated by tfplugindocs -->
## Schema
## Removing Machines
Sadly, due to some limitations in vagrant, it's not possible to automatically
remove a portion of machines from a Vagrantfile. In other words, if your
Vagrantfile defines 5 machines and you remove 2 of them from the Vagrantfile,
they will be left running in your vagrant provider (ie, virtualbox or whatever)
with no way of removing them via vagrant (or terraform).

If you intend of removing some machines, you should manually run `vagrant
destroy MACHINE_NAME` on those machines you wish to remove *before* editing the
Vagrantfile. Then update your Vagrantfile and allow terraform to do the rest.

If you forget, you can manually cleanup these old VMs by launching your vagrant
provider's UI and deleting the machines. Then run `vagrant global-status
--prune` to cleanup vagrant's cache of these machines.

## Debugging
If terrafrom is failing on the vagrant step, you can get additional output by
Expand Down
7 changes: 5 additions & 2 deletions docs/resources/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ and terraform will ask for an update.
resource "vagrant_vm" "my_vagrant_vm" {
env = {
# force terraform to re-run vagrant if the Vagrantfile changes
VAGRANTFILE_HASH = "${md5(file("path/to/Vagrantfile"))}",
VAGRANTFILE_HASH = md5(file("./Vagrantfile")),
}
get_ports = true
# see schema for additional options
}
```
Expand All @@ -33,12 +34,14 @@ resource "vagrant_vm" "my_vagrant_vm" {
### Optional

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

### Read-Only

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

<a id="nestedatt--ssh_config"></a>
Expand Down
23 changes: 8 additions & 15 deletions examples/resources/vagrant_vm/Vagrantfile
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

required_plugins = %w(vagrant-ignition)
plugins_to_install = required_plugins.reject(&Vagrant.method(:has_plugin?))
unless plugins_to_install.empty?
puts "Installing plugins: #{plugins_to_install.join(', ')}"
if system "vagrant plugin install #{plugins_to_install.join(' ')}"
exec "vagrant #{ARGV.join(' ')}"
else
abort 'Installation of one or more plugins failed.'
end
end

$vm_cpus = 1
$vm_memory = 1024

Vagrant.configure("2") do |config|
config.vm.box = "minimal/trusty64"

config.ssh.insert_key = false
config.ssh.forward_agent = true

config.vm.box = 'coreos-stable'
config.vm.box_url = 'https://stable.release.core-os.net/amd64-usr/current/coreos_production_vagrant_virtualbox.json'
config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true

config.vm.synced_folder ".", "/vagrant", disabled: true

if Vagrant.has_plugin? 'vagrant-vbguest'
config.vbguest.auto_update = false
end

config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--usb", "on"]
vb.customize ["modifyvm", :id, "--usbehci", "off"]

vb.gui = false
vb.cpus = $vm_cpus
vb.memory = $vm_memory
vb.check_guest_additions = false
vb.functional_vboxsf = false
config.ignition.enabled = true
config.ignition.config_obj = vb
end
end
7 changes: 7 additions & 0 deletions examples/resources/vagrant_vm/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
required_providers {
vagrant = {
source = "bmatcuk/vagrant"
}
}
}
3 changes: 3 additions & 0 deletions examples/resources/vagrant_vm/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "host_port" {
value = vagrant_vm.my_vagrant_vm.ports[0][0].host
}
3 changes: 2 additions & 1 deletion examples/resources/vagrant_vm/resource.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
resource "vagrant_vm" "my_vagrant_vm" {
env = {
# force terraform to re-run vagrant if the Vagrantfile changes
VAGRANTFILE_HASH = "${md5(file("path/to/Vagrantfile"))}",
VAGRANTFILE_HASH = md5(file("./Vagrantfile")),
}
get_ports = true
# see schema for additional options
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module github.com/bmatcuk/terraform-provider-vagrant

require (
github.com/bmatcuk/go-vagrant v1.4.2
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce // indirect
github.com/hashicorp/terraform-plugin-docs v0.4.0 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.0.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.3
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/zclconf/go-cty v1.7.1 // indirect
)

go 1.13
Loading

0 comments on commit bd2c5c8

Please sign in to comment.