From d284e4ccca2a70261ae1860a1859c7e390fd130e Mon Sep 17 00:00:00 2001 From: Damian Lewis Date: Tue, 14 Aug 2018 16:10:55 +0100 Subject: [PATCH] Allow VM networks to be configured --- README.md | 31 +++++++++++++++++++++++++++++-- Vagrantfile | 42 +++++++++++++++++++++++++++++------------- config.yaml | 11 ++++++++++- 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f46e20d..3e0fb13 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,41 @@ DevBox can be configured using the `config.yaml` configuration file. #### VM -By default, DevBox will create a private network with an automatically assigned IP address. Use `ifconfig` on the VM to determine the IP address. A static IP address for the VM can be given by adding the `ip` property and assigning an address from the [reserved private address space](https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces). Also the default name for the VM is 'devbox', this can be changed by adding the `name` property. The default hostname for the VM is also 'devbox', this can be changed by adding the `hostname` property. +By default, the name for the VM is 'devbox', this can be changed by adding the `name` property. The default hostname for the VM is also 'devbox', this can be changed by adding the `hostname` property. ```yaml -ip: "192.168.22.18" name: vmname hostname: vmhostname ``` +#### Network +By default, DevBox will create a private network with an automatically assigned IP address. Use `ifconfig` on the VM to determine the IP address. To create a bridged (public) network, add a `networks` array with a `type` property of `bridged`. +```yaml +networks: + - type: bridged +``` + +A static IP address for the VM can also be given by adding an `ip` property and assigning an address from the [reserved private address space](https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces). +```yaml +networks: + - type: private + ip: "192.168.178.40" +``` + +```yaml +networks: + - type: bridged + ip: "192.168.178.40" +``` + +By default, DevBox will forward ports 80 and 3306 on the guest machine to ports 8000 and 33060 on the host. To forward additional ports, add a `ports` array with `guest` and `host` properties set to the ports you want to forward. +```yaml +ports: + - guest: 443 + host: 44300 +``` + + #### Folders By default, DevBox will share your project folder `.` with the `/vagrant` folder on the guest machine. To share additional folders with the guest machine `map` the host folder `to` the guest folder in the `folders` array. ```yaml diff --git a/Vagrantfile b/Vagrantfile index c2004a9..f8b6305 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -41,7 +41,6 @@ name = settings["name"] ||= "devbox" # Default ports for forwarding default_ports = { 80 => 8000, - 443 => 44300, 3306 => 33060 } @@ -70,19 +69,41 @@ Vagrant.configure("2") do |config| config.vm.box = settings["box"] ||= "damianlewis/ubuntu-#{settings["ubuntu"] ||= default_ubuntu}-#{webserver == "apache" ? "lamp" : "lemp"}" config.vm.box_version = settings["version"] ||= ">= 1.0" + # Configure default ports + default_ports.each do |guest, host| + config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true + end + + # Configure additional ports to forward + if settings.has_key?("ports") + settings["ports"].each do |port| + config.vm.network "forwarded_port", guest: port["guest"], host: port["host"], auto_correct: true + end + end + # Configure networks if settings.has_key?("networks") settings["networks"].each do |network| - if network.has_key?("ip") - config.vm.network network["type"], ip: network["ip"], bridge: network["bridge"] ||= nil - else - config.vm.network network["type"], bridge: network["bridge"] ||= nil + if network.has_key?("type") + if network["type"] == "private" + if network.has_key?("ip") + config.vm.network "private_network", ip: network["ip"] + else + config.vm.network "private_network", type: "dhcp" + end + end + + if network["type"] == "bridged" + if network.has_key?("ip") + config.vm.network "public_network", ip: network["ip"], bridge: network["bridge"] ||= nil + else + config.vm.network "public_network", bridge: network["bridge"] ||= nil + end + end end end - elsif settings.has_key?("ip") - config.vm.network :private_network, ip: settings["ip"] else - config.vm.network :private_network, type: "dhcp" + config.vm.network "private_network", type: "dhcp" end # Configure VirtualBox settings @@ -98,11 +119,6 @@ Vagrant.configure("2") do |config| end end - # Configure default ports - default_ports.each do |guest, host| - config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true - end - # Configure shared folders if settings.has_key?("folders") settings["folders"].each do |folder| diff --git a/config.yaml b/config.yaml index ebc4e4e..9029a5b 100644 --- a/config.yaml +++ b/config.yaml @@ -1,8 +1,17 @@ --- # VM configurations -ip: "192.168.10.10" #ubuntu: "14.04" +# Networks to configure +#networks: +# - type: bridged +# ip: "192.168.178.40" + +# Additional ports to forward +#ports: +# - guest: 443 +# host: 44300 + # Shared folders to configure #folders: # - map: ~/code