Vagrant is awesome, but there are times where you might need to specify redundant/alternative URLs in a Vagrantfile. This could be a failover type scenario, although in my case, I needed it to give external developers access to our Vagrantbox, which we made a copy of on the interwebs. The below goes in the Vagrantfile, and does an HTTP HEAD request to see if the Vagrant box is available. If so, it uses it; otherwise, it’ll default to the public Vagrant box.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
box_internal = "http://internal.url/linux.box" box_public = "http://external.url/linux.box" require 'net/http' uri = URI(box_internal) http = Net::HTTP.new(uri.host, uri.port) begin config.vm.box_url = box_internal response = http.request(Net::HTTP::Head.new(uri.request_uri)) rescue StandardError puts 'Could not reach internal Vagrant box; using ' + box_public config.vm.box_url = box_public end |
I hope that helps!
Leave a Reply