summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2017-02-03 09:57:26 +0000
committerTim Smith <tsmith@chef.io>2017-02-03 09:57:26 +0000
commit26f93b65afada37e35d712148ce3b77c71f202cb (patch)
tree146563f08a1715e828c12733dafff7701482cb82
parentec980071ce34627701f039c0deae37022b4742d2 (diff)
downloadohai-http_helper.tar.gz
Move duplicate http logic into a helperhttp_helper
We probably want to use this in a few other places, but we certainly want to encourage people to use this in their plugins vs. reinventing the wheel. Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/ohai/mixin/ec2_metadata.rb27
-rw-r--r--lib/ohai/mixin/gce_metadata.rb33
-rw-r--r--lib/ohai/mixin/http_helper.rb56
-rw-r--r--lib/ohai/plugins/ec2.rb4
-rw-r--r--lib/ohai/plugins/gce.rb4
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