Skip to content

Instantly share code, notes, and snippets.

@aslakhellesoy
Created March 27, 2015 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aslakhellesoy/5c6b98d10214c90f31a3 to your computer and use it in GitHub Desktop.
Save aslakhellesoy/5c6b98d10214c90f31a3 to your computer and use it in GitHub Desktop.
# This script sets up (or removes) port forwarding on VirtualBox
# for all ports defined in fig.yml
require 'yaml'
op = ARGV[0]
if op.nil? || !( /(up|down)/ =~ op )
$stderr.puts "Usage:\n ruby #{__FILE__} up|down"
exit 1
end
system('boot2docker stop')
fig = YAML.load_file('fig.yml')
fig.each do |container, config|
config['ports'].each do |mapping|
host_port, container_port = *mapping.split(':')
mapping_name = "tcp-port-#{container}-#{host_port}"
if op == 'up'
puts "Forwarding #{mapping_name} to #{container_port}"
system %Q{VBoxManage modifyvm "boot2docker-vm" --natpf1 "#{mapping_name},tcp,,#{host_port},,#{host_port}"}
else
puts "Unforwarding #{mapping_name} to #{container_port}"
system %Q{VBoxManage modifyvm "boot2docker-vm" --natpf1 delete "#{mapping_name}"}
end
end
end
system('boot2docker start')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment