Skip to content

Instantly share code, notes, and snippets.

@dpneumo
Last active Oct 20, 2018
Embed
What would you like to do?
Adjust Vagrant network setup to environment at vm startup
class BridgedInterfaces
def initialize( print_output: false )
@cmd = 'VBoxManage.exe list bridgedifs'
@directory = 'C:\Program Files\Oracle\VirtualBox'
@print_output = print_output
end
def self.preferred
new
.bridged_ifcs
.sort_by {|ifc| ifc['Wireless'] }
.first
end
def bridged_ifcs
@bridged_ifcs || bridged_ifc_list
end
private
def bridged_ifc_list
list = run_command
last_key, _ = list[-2].split(':')
bifs, ifc = [], {}
list.each do |item|
next if item.empty?
k, v = item.split(':')
ifc[k] = v.strip
if k == last_key
bifs << ifc if usable(ifc)
ifc = {}
end
end
bifs
end
def run_command
out = IO.popen(@cmd, err: %i[child out], chdir: @directory) do |io|
begin
out = ''
loop do
chunk = io.readpartial(4096)
print chunk if @print_output
out += chunk
end
rescue EOFError; end
out
end
$?.exitstatus.zero? || (raise "Error running command #{@cmd}")
out.split("\n")
.map { |line| line.tr("\r\n", '') }
end
def usable(ifc)
ifc['Status'] == 'Up' && !ifc['Wireless'].empty?
end
end
# Defines our Vagrant environment
#
# -*- mode: ruby -*-
# vi: set ft=ruby :
require_relative 'bridged_interfaces'
Vagrant.configure("2") do |config|
# create wisp node
config.vm.define :wisp do |wisp|
wisp.vm.box = "geerlingguy/centos7"
wisp.vm.box_version = "1.2.10"
wisp.vm.hostname = "wisp"
wisp.vm.synced_folder "C:/Users/loco/My Projects/ansible/puff",
"/home/vagrant/puff",
mount_options: ["dmode=755", "fmode=644"]
wisp.vm.network "forwarded_port", guest: 3000, host: 3000
wisp.vm.network "public_network", bridge: BridgedInterfaces.preferred['Name']
wisp.vm.provider "virtualbox" do |vb|
vb.memory = "256"
end
wisp.vm.provision :shell,
run: "always",
inline: <<-SHELL
sudo sed -i 's/DEFROUTE="yes"/DEFROUTE="no"/g' /etc/sysconfig/network-scripts/ifcfg-enp0s3
sudo systemctl restart network
ip route get 8.8.8.8 | awk '{print $7}' | xargs -I IPADDR echo "The bridged IP is IPADDR"
netstat -rn
SHELL
wisp.vm.provision :shell,
path: "bootstrap-centos-wisp.sh",
privileged: false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment