summaryrefslogtreecommitdiff
path: root/lib/chef
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2015-09-08 18:18:09 -0500
committerJay Mundrawala <jdmundrawala@gmail.com>2015-09-08 18:25:50 -0500
commitbc0daaae67b2e6bf032e11edbc3057c7531dfc36 (patch)
tree795e8ba1c599c6f93a5d536192578328cb8744d0 /lib/chef
parenteac543b05437f6ee1d9f98cc2656e55a189c2934 (diff)
downloadchef-bc0daaae67b2e6bf032e11edbc3057c7531dfc36.tar.gz
Add monkey patch for webrickjdm/webrick-monkey
Ruby 2.1 introduces a regression on Windows in WEBrick. create_listeners does not throw an exception when we're already listening on a port. This seems to only be an issue on Windows. This patch reverts it back to what it was in Ruby 2.0 It seems the regression was introduced in https://github.com/ruby/ruby/commit/b1f493dcd1092fe17cccec998e175516ed5c6e47#diff-4b178393150b2b3a5ec9d77eced1f09e
Diffstat (limited to 'lib/chef')
-rw-r--r--lib/chef/local_mode.rb5
-rw-r--r--lib/chef/monkey_patches/webrick-utils.rb51
2 files changed, 56 insertions, 0 deletions
diff --git a/lib/chef/local_mode.rb b/lib/chef/local_mode.rb
index 79fb750dd8..fbb72cd6cb 100644
--- a/lib/chef/local_mode.rb
+++ b/lib/chef/local_mode.rb
@@ -15,6 +15,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
require 'chef/config'
+if Chef::Platform.windows?
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.1')
+ require 'chef/monkey_patches/webrick-utils'
+ end
+end
class Chef
module LocalMode
diff --git a/lib/chef/monkey_patches/webrick-utils.rb b/lib/chef/monkey_patches/webrick-utils.rb
new file mode 100644
index 0000000000..57f6db54ed
--- /dev/null
+++ b/lib/chef/monkey_patches/webrick-utils.rb
@@ -0,0 +1,51 @@
+require 'webrick/utils'
+
+module WEBrick
+ module Utils
+ ##
+ # Creates TCP server sockets bound to +address+:+port+ and returns them.
+ #
+ # It will create IPV4 and IPV6 sockets on all interfaces.
+ #
+ # NOTE: We need to monkey patch this method because
+ # create_listeners on Windows with Ruby > 2.0.0 does not
+ # raise an error if we're already listening on a port.
+ #
+ def create_listeners(address, port, logger=nil)
+ #
+ # utils.rb -- Miscellaneous utilities
+ #
+ # Author: IPR -- Internet Programming with Ruby -- writers
+ # Copyright (c) 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
+ # Copyright (c) 2002 Internet Programming with Ruby writers. All rights
+ # reserved.
+ #
+ # $IPR: utils.rb,v 1.10 2003/02/16 22:22:54 gotoyuzo Exp $
+ unless port
+ raise ArgumentError, "must specify port"
+ end
+ res = Socket::getaddrinfo(address, port,
+ Socket::AF_UNSPEC, # address family
+ Socket::SOCK_STREAM, # socket type
+ 0, # protocol
+ Socket::AI_PASSIVE) # flag
+ last_error = nil
+ sockets = []
+ res.each{|ai|
+ begin
+ logger.debug("TCPServer.new(#{ai[3]}, #{port})") if logger
+ sock = TCPServer.new(ai[3], port)
+ port = sock.addr[1] if port == 0
+ Utils::set_close_on_exec(sock)
+ sockets << sock
+ rescue => ex
+ logger.warn("TCPServer Error: #{ex}") if logger
+ last_error = ex
+ end
+ }
+ raise last_error if sockets.empty?
+ return sockets
+ end
+ module_function :create_listeners
+ end
+end