Search code examples
vagrantchef-infratest-kitchen

Changing where kitchen-vagrant downloads chef from


I'm doing some Chef recipe testing using test-kitchen on vagrant virtuals. Each time I run kitchen test, it is downloading a copy of the chef omnibus installer from the standard place (Amazon). The problem is that I'm at home, and each of those downloads takes time, and adds to my monthly network usage.

Is there a way to configure test-kitchen / kitchen-vagrant / whatever so that the downloads come from a local cache? If I set up a caching proxy webserver on my PC, can I get test-kitchen to configure the vagrant instance to use it for the chef downloads?


Solution

  • Yes - the process is documented in this gist, but in short:

    Use ERB in your .kitchen.yml:

    <%
    require 'socket'
    
    def local_ip
      @local_ip ||= begin
        # turn off reverse DNS resolution temporarily
        orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
    
        UDPSocket.open do |s|
          s.connect '64.233.187.99', 1
          s.addr.last
        end
      ensure
        Socket.do_not_reverse_lookup = orig
      end
    end
    
    def local_port ; 8123 ; end
    def http_proxy_url ; "http://#{local_ip}:#{local_port}" ; end
    
    def proxy_running?
      socket = TCPSocket.new(local_ip, local_port)
      true
    rescue SocketError, Errno::ECONNREFUSED,
      Errno::EHOSTUNREACH, Errno::ENETUNREACH, IOError
      false
    rescue Errno::EPERM, Errno::ETIMEDOUT
      false
    ensure
      socket && socket.close
    end
    %>
    ---
    <% if proxy_running? %>
    driver:
      http_proxy: <%= http_proxy_url %>
      https_proxy: <%= http_proxy_url %>
      provision_command: "env http_proxy=<%= http_proxy_url %> bash -c 'curl -L http://www.getchef.com/chef/install.sh | bash'"
    
    provisioner:
      chef_omnibus_url: http://www.getchef.com/chef/install.sh
    <% end %>
    

    The instructions for running a proxy use polipo, but you could use any caching proxy you would like.