diff options
author | Tim Smith <tsmith@chef.io> | 2017-02-10 14:13:18 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-10 14:13:18 -0800 |
commit | 5f04ffaf58e6f457a221b23df4ded4024d998e34 (patch) | |
tree | 20c849305b8f1d2dcd32678a1d6970b374983f55 /lib | |
parent | a0d5aa7942cd0a6af82b69410e83c6a5ac6d6bee (diff) | |
parent | 26f93b65afada37e35d712148ce3b77c71f202cb (diff) | |
download | ohai-5f04ffaf58e6f457a221b23df4ded4024d998e34.tar.gz |
Merge pull request #951 from chef/http_helper
Move duplicate http logic into a helper
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ohai/mixin/ec2_metadata.rb | 27 | ||||
-rw-r--r-- | lib/ohai/mixin/gce_metadata.rb | 33 | ||||
-rw-r--r-- | lib/ohai/mixin/http_helper.rb | 56 | ||||
-rw-r--r-- | lib/ohai/plugins/ec2.rb | 4 | ||||
-rw-r--r-- | lib/ohai/plugins/gce.rb | 4 |
5 files changed, 62 insertions, 62 deletions
diff --git a/lib/ohai/mixin/ec2_metadata.rb b/lib/ohai/mixin/ec2_metadata.rb index 1797105d..7962aaab 100644 --- a/lib/ohai/mixin/ec2_metadata.rb +++ b/lib/ohai/mixin/ec2_metadata.rb @@ -18,7 +18,6 @@ # limitations under the License. require "net/http" -require "socket" module Ohai module Mixin @@ -49,32 +48,6 @@ module Ohai 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" diff --git a/lib/ohai/mixin/gce_metadata.rb b/lib/ohai/mixin/gce_metadata.rb index a42a73bc..10e78ae2 100644 --- a/lib/ohai/mixin/gce_metadata.rb +++ b/lib/ohai/mixin/gce_metadata.rb @@ -15,7 +15,6 @@ # limitations under the License. require "net/http" -require "socket" module Ohai module Mixin @@ -25,38 +24,6 @@ module Ohai 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) diff --git a/lib/ohai/mixin/http_helper.rb b/lib/ohai/mixin/http_helper.rb new file mode 100644 index 00000000..f9c83fa1 --- /dev/null +++ b/lib/ohai/mixin/http_helper.rb @@ -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 diff --git a/lib/ohai/plugins/ec2.rb b/lib/ohai/plugins/ec2.rb index 583480a4..76c718ea 100644 --- a/lib/ohai/plugins/ec2.rb +++ b/lib/ohai/plugins/ec2.rb @@ -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" @@ -79,7 +81,7 @@ Ohai.plugin(:EC2) do # 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 diff --git a/lib/ohai/plugins/gce.rb b/lib/ohai/plugins/gce.rb index e913070c..f8fdb3a5 100644 --- a/lib/ohai/plugins/gce.rb +++ b/lib/ohai/plugins/gce.rb @@ -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" @@ -27,7 +29,7 @@ Ohai.plugin(:GCE) do # 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 |