Skip to content

Commit

Permalink
Merge pull request #951 from chef/http_helper
Browse files Browse the repository at this point in the history
Move duplicate http logic into a helper
  • Loading branch information
tas50 committed Feb 10, 2017
2 parents a0d5aa7 + 26f93b6 commit 5f04ffa
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 62 deletions.
27 changes: 0 additions & 27 deletions lib/ohai/mixin/ec2_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# limitations under the License.

require "net/http"
require "socket"

module Ohai
module Mixin
Expand Down Expand Up @@ -49,32 +48,6 @@ module Ec2Metadata
EC2_ARRAY_DIR = %w{network/interfaces/macs}
EC2_JSON_DIR = %w{iam}

def can_metadata_connect?(addr, port, timeout = 2)
t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
saddr = Socket.pack_sockaddr_in(port, addr)
connected = false

begin
t.connect_nonblock(saddr)
rescue Errno::EINPROGRESS
r, w, e = IO.select(nil, [t], nil, timeout)
if !w.nil?
connected = true
else
begin
t.connect_nonblock(saddr)
rescue Errno::EISCONN
t.close
connected = true
rescue SystemCallError
end
end
rescue SystemCallError
end
Ohai::Log.debug("ec2 metadata mixin: can_metadata_connect? == #{connected}")
connected
end

def best_api_version
response = http_client.get("/")
if response.code == "404"
Expand Down
33 changes: 0 additions & 33 deletions lib/ohai/mixin/gce_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# limitations under the License.

require "net/http"
require "socket"

module Ohai
module Mixin
Expand All @@ -25,38 +24,6 @@ module GCEMetadata
GCE_METADATA_ADDR = "metadata.google.internal." unless defined?(GCE_METADATA_ADDR)
GCE_METADATA_URL = "/computeMetadata/v1/?recursive=true" unless defined?(GCE_METADATA_URL)

def can_metadata_connect?(addr, port, timeout = 2)
t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
begin
saddr = Socket.pack_sockaddr_in(port, addr)
rescue SocketError => e # occurs when non-GCE systems try to resolve metadata.google.internal
Ohai::Log.debug("Mixin GCE: can_metadata_connect? failed setting up socket: #{e}")
return false
end

connected = false

begin
t.connect_nonblock(saddr)
rescue Errno::EINPROGRESS
r, w, e = IO.select(nil, [t], nil, timeout)
if !w.nil?
connected = true
else
begin
t.connect_nonblock(saddr)
rescue Errno::EISCONN
t.close
connected = true
rescue SystemCallError
end
end
rescue SystemCallError
end
Ohai::Log.debug("Mixin GCE: can_metadata_connect? == #{connected}")
connected
end

# fetch the meta content with a timeout and the required header
def http_get(uri)
conn = Net::HTTP.start(GCE_METADATA_ADDR)
Expand Down
56 changes: 56 additions & 0 deletions lib/ohai/mixin/http_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#
# Copyright:: 2017, Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "socket"

module Ohai
module Mixin
module HttpHelper

def can_socket_connect?(addr, port, timeout = 2)
t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
begin
saddr = Socket.pack_sockaddr_in(port, addr)
rescue SocketError => e # generally means dns resolution error
Ohai::Log.debug("Mixin HttpHelper: can_socket_connect? failed setting up socket connection: #{e}")
return false
end

connected = false

begin
t.connect_nonblock(saddr)
rescue Errno::EINPROGRESS
r, w, e = IO.select(nil, [t], nil, timeout)
if !w.nil?
connected = true
else
begin
t.connect_nonblock(saddr)
rescue Errno::EISCONN
t.close
connected = true
rescue SystemCallError
end
end
rescue SystemCallError
end
Ohai::Log.debug("Mixin HttpHelper: can_socket_connect? == #{connected}")
connected
end
end
end
end
4 changes: 3 additions & 1 deletion lib/ohai/plugins/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
# 4. Kernel data mentioned Amazon. This catches Windows HVM & paravirt instances

require "ohai/mixin/ec2_metadata"
require "ohai/mixin/http_helper"
require "base64"

Ohai.plugin(:EC2) do
include Ohai::Mixin::Ec2Metadata
include Ohai::Mixin::HttpHelper

provides "ec2"

Expand Down Expand Up @@ -79,7 +81,7 @@ def looks_like_ec2?

# Even if it looks like EC2 try to connect first
if has_ec2_xen_uuid? || has_ec2_dmi? || has_amazon_org?
return true if can_metadata_connect?(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80)
return true if can_socket_connect?(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/ohai/plugins/gce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
# limitations under the License.

require "ohai/mixin/gce_metadata"
require "ohai/mixin/http_helper"

Ohai.plugin(:GCE) do
include Ohai::Mixin::GCEMetadata
include Ohai::Mixin::HttpHelper

provides "gce"

Expand All @@ -27,7 +29,7 @@
# true:: If gce metadata server found
# false:: Otherwise
def has_gce_metadata?
can_metadata_connect?(Ohai::Mixin::GCEMetadata::GCE_METADATA_ADDR, 80)
can_socket_connect?(Ohai::Mixin::GCEMetadata::GCE_METADATA_ADDR, 80)
end

# Identifies gce
Expand Down

0 comments on commit 5f04ffa

Please sign in to comment.