Skip to content

Instantly share code, notes, and snippets.

@webframp
Last active January 2, 2016 18:39
Show Gist options
  • Select an option

  • Save webframp/8345331 to your computer and use it in GitHub Desktop.

Select an option

Save webframp/8345331 to your computer and use it in GitHub Desktop.
Auto-enable Local HTTP Caching in Test Kitchen, global t-k config
<%
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
# http://spin.atomicobject.com/2013/09/30/socket-connection-timeout-ruby/
def socket_open?(host, port, timeout = 0.1)
addr = Socket.getaddrinfo(host, nil)
sockaddr = Socket.pack_sockaddr_in(port, addr[0][3])
Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0).tap do |socket|
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
begin
socket.connect_nonblock(sockaddr)
rescue IO::WaitWritable
if IO.select(nil, [socket], nil, timeout)
begin
socket.connect_nonblock(sockaddr)
rescue Errno::EISCONN
true
rescue
socket.close
false
end
else
socket.close
false
end
end
end
end
def local_port ; 8123 ; end
def http_proxy_url ; "http://#{local_ip}:#{local_port}" ; end
def proxy_running?
socket_open?(local_ip, local_port)
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.opscode.com/chef/install.sh | bash'"
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment