Saturday, July 26, 2014

Vagrant proxy on Cygwin

Cygwin + Ruby + Vagrant = Pain!

Vagrantfile. Simple. I was given this by a colleague running Vagrant on a Mac.

 Vagrant.configure("2") do |config|  
  config.vm.box = "base"  
  #Ensure proxy plugin is installed - $ vagrant plugin install vagrant-proxyconf  
  config.proxy.http   = "http://XXX"  
  config.proxy.https  = "http://XXX"  
  config.vm.define :box1 do |box1|  
   box1.vm.network :private_network, :ip => "XXX"  
   box1.vm.hostname = "box1"  
   box1.vm.synced_folder ENV['HOME'], "/home/vagrant/home"  
   box1.vm.provider "virtualbox" do |v|  
    v.customize ['modifyvm', :id, '--memory', '4096']  
    v.customize ['modifyvm', :id, '--cpus', '2']  
   end  
   box1.vm.provision :puppet do |puppet|  
    puppet.manifests_path = "./manifests"  
    puppet.module_path = "./modules"  
    puppet.manifest_file = "site.pp"  
    puppet.options = "--verbose"  
   end  
  end  
 end  

Try to install vagrant-proxyconf, and you get a barrage of errors if your Cygwin doesn't have the required libraries. So I'm gonna take it from the top here.

First thing, getting Ruby. Using Ruby from http://rubyinstaller.org/downloads/ caused headaches with running "gem", where I had to add "gem.bat" to my .bashrc file as an "alias", so I just stuck with the setup.x86_x64.exe Cygwin method.

Next, getting RubyGems. Get it here: https://rubygems.org . Put it in C:/RubyGems
Install RubyGems:
cd C:/RubyGems
ruby setup.rb install

Required Cygwin libraries (setup.86_x64.exe again)
libcrypt-devel
gcc (I had to search gcc then select the whole "Devel" package to make sure I got this, change the dropdown "Devel (Default)" to "Devel (Install)" if not clear)
make

Now, if you run "vagrant plugin install vagrant-proxyconf" now, you'll get this error:

DEBUG [dc362284] Bundler::GemNotFound: Could not find json-1.8.1.gem for installation
DEBUG [dc362284] An error occurred while installing json (1.8.1), and Bundler cannot continue.
DEBUG [dc362284] Make sure that `gem install json -v '1.8.1'` succeeds before bundling.

So run "gem install json -v '1.8.1'. If you get problems with this, it's because you didn't have gcc,make or libcrypt.
Then run "vagrant plugin install vagrant-proxyconf". What I ran into was:
Make sure that `gem install json -v '1.8.1'` succeeds before bundling.

Wtf? But my gem installed successfully! After finding out I had another Cygwin installation (though not on any path), I removed that folder and it still didn't work. I reinstalled Vagrant and it FINALLY...progresses.

It gets to the part to download the box, and then spits another error:
C:/HashiCorp/Vagrant/embedded/lib/ruby/2.0.0/uri/common.rb:176:in `split': bad URI(is not URI?): file://C:\cygwin\home\michart\base (URI::InvalidURIError)
Ruby doesn't parse Windows paths! Or maybe it's Cygwin being stupid. You would expect a widely used scripting language to have figured this out by now. So what I did was add this to common.rb:

    # Returns a split URI against regexp[:ABS_URI]
    def split(uri)
      uri.gsub!('\\','/')
      case uri
      when ''
      ...

Voila, it tries to download the box! But then I hit yet another problem...
An error occurred while downloading the remote file. The error
message, if any, is reproduced below. Please fix this error and try
again.

Couldn't open file /cygwin/home/michart/base

Well this one's probably just worded horribly, but my proxy was inaccessible, corporate VPN client looks like it's down over the weekend...blah.
Do note however, when it's just a simple "vagrant up" with a "vagrant init" Vagrantfile, I had no issues. This looked like extra steps I needed for "vagrant plugin install" to work.
Hope this helps anyone trying to get Vagrant working!

No comments:

Post a Comment