summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRenato Covarrubias <rnt@rnt.cl>2023-02-14 22:10:34 +0100
committerGitHub <noreply@github.com>2023-02-14 13:10:34 -0800
commitdb9c5e48a741f6106a6575d5cb41db3fe0d1fb77 (patch)
treed0d38b78040bed44b5c9542dcb00dea3d827fb4a /lib
parentaf1405ed41ca25bcc2967bcf97a0448d5cba208d (diff)
downloadohai-db9c5e48a741f6106a6575d5cb41db3fe0d1fb77.tar.gz
Add cloud provider oci (#1780)
ohai cloud fail if the vm is in Oracle Cloud Infrastructure (OCI). This PR add support to this cloud provider to cloud plugin. It also adds OCI detection to the cloud plugin. Signed-off-by: Renato Covarrubias <rnt@rnt.cl>
Diffstat (limited to 'lib')
-rw-r--r--lib/ohai/mixin/oci_metadata.rb69
-rw-r--r--lib/ohai/plugins/cloud.rb22
-rw-r--r--lib/ohai/plugins/oci.rb94
3 files changed, 185 insertions, 0 deletions
diff --git a/lib/ohai/mixin/oci_metadata.rb b/lib/ohai/mixin/oci_metadata.rb
new file mode 100644
index 00000000..b04243a3
--- /dev/null
+++ b/lib/ohai/mixin/oci_metadata.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+#
+# Author:: Renato Covarrubias (<rnt@rnt.cl>)
+# 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 "net/http" unless defined?(Net::HTTP)
+
+module Ohai
+ module Mixin
+ module OCIMetadata
+ OCI_METADATA_ADDR = "169.254.169.254"
+ OCI_METADATA_URL = "/opc/v2"
+ CHASSIS_ASSET_TAG_FILE = "/sys/devices/virtual/dmi/id/chassis_asset_tag"
+
+ # fetch the meta content with a timeout and the required header
+ def http_get(uri)
+ conn = Net::HTTP.start(OCI_METADATA_ADDR)
+ conn.read_timeout = 6
+ conn.get(
+ uri,
+ {
+ "Authorization" => "Bearer Oracle",
+ "User-Agent" => "chef-ohai/#{Ohai::VERSION}",
+ }
+ )
+ end
+
+ # parse JSON data from a String to a Hash
+ #
+ # @param [String] response_body json as string to parse
+ #
+ # @return [Hash]
+ def parse_json(response_body)
+ data = String(response_body)
+ parser = FFI_Yajl::Parser.new
+ parser.parse(data)
+ rescue FFI_Yajl::ParseError
+ logger.warn("Mixin OciMetadata: Metadata response is NOT valid JSON")
+ nil
+ end
+
+ # Fetch metadata from api
+ def fetch_metadata(metadata = "instance")
+ response = http_get("#{OCI_METADATA_URL}/#{metadata}")
+ return nil unless response.code == "200"
+
+ if response.code == "200"
+ parse_json(response.body)
+ else
+ logger.warn("Mixin OciMetadata: Received response code #{response.code} requesting metadata")
+ nil
+ end
+ end
+ end
+ end
+end
diff --git a/lib/ohai/plugins/cloud.rb b/lib/ohai/plugins/cloud.rb
index 8e79a824..503516ab 100644
--- a/lib/ohai/plugins/cloud.rb
+++ b/lib/ohai/plugins/cloud.rb
@@ -28,6 +28,7 @@ Ohai.plugin(:Cloud) do
depends "azure"
depends "digital_ocean"
depends "softlayer"
+ depends "oci"
# Class to help enforce the interface exposed to node[:cloud] (OHAI-542)
#
@@ -336,6 +337,26 @@ Ohai.plugin(:Cloud) do
@cloud_attr_obj.provider = "softlayer"
end
+ # ----------------------------------------
+ # OCI
+ # ----------------------------------------
+
+ # Is current Oracle Cloud Infrastructure?
+ #
+ # === Return
+ # true:: If oci Hash is defined
+ # false:: Otherwise
+ def on_oci?
+ oci != nil
+ end
+
+ # Fill cloud hash with OCI values
+ def oci_values
+ oci["metadata"]["network"]["interface"].each { |vnic| @cloud_attr_obj.add_ipv4_addr(vnic["privateIp"], :private) }
+ @cloud_attr_obj.local_hostname = oci["metadata"]["compute"]["hostname"]
+ @cloud_attr_obj.provider = "oci"
+ end
+
collect_data do
require "ipaddr" unless defined?(IPAddr)
@@ -351,6 +372,7 @@ Ohai.plugin(:Cloud) do
get_digital_ocean_values if on_digital_ocean?
get_softlayer_values if on_softlayer?
get_alibaba_values if on_alibaba?
+ oci_values if on_oci?
cloud @cloud_attr_obj.cloud_mash
end
diff --git a/lib/ohai/plugins/oci.rb b/lib/ohai/plugins/oci.rb
new file mode 100644
index 00000000..04e83ba5
--- /dev/null
+++ b/lib/ohai/plugins/oci.rb
@@ -0,0 +1,94 @@
+# frozen_string_literal: true
+
+#
+# Author:: Renato Covarrubias (<rnt@rnt.cl>)
+# 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.
+#
+
+Ohai.plugin(:Oci) do
+ require_relative "../mixin/oci_metadata"
+ require_relative "../mixin/http_helper"
+
+ include Ohai::Mixin::OCIMetadata
+ include Ohai::Mixin::HttpHelper
+
+ provides "oci"
+
+ collect_data do
+ oci_metadata_from_hints = hint?("oci")
+ if oci_metadata_from_hints
+ logger.trace("Plugin OCI: oci hint is present. Parsing any hint data.")
+ oci Mash.new
+ oci_metadata_from_hints.each { |k, v| oci[k] = v }
+ oci["metadata"] = parse_metadata
+ elsif oci_chassis_asset_tag?
+ logger.trace("Plugin oci: No hints present, but system appears to be on oci.")
+ oci Mash.new
+ oci["metadata"] = parse_metadata
+ else
+ logger.trace("Plugin oci: No hints present and doesn't appear to be on oci.")
+ false
+ end
+ end
+
+ def oci_chassis_asset_tag?
+ has_oci_chassis_asset_tag = false
+ if file_exist?(Ohai::Mixin::OCIMetadata::CHASSIS_ASSET_TAG_FILE)
+ file_open(Ohai::Mixin::OCIMetadata::CHASSIS_ASSET_TAG_FILE).each do |line|
+ next unless /OracleCloud.com/.match?(line)
+
+ logger.trace("Plugin oci: Found OracleCloud.com chassis_asset_tag used by oci.")
+ has_oci_chassis_asset_tag = true
+ break
+ end
+ end
+ has_oci_chassis_asset_tag
+ end
+
+ def parse_metadata
+ return nil unless can_socket_connect?(Ohai::Mixin::OCIMetadata::OCI_METADATA_ADDR, 80)
+
+ instance_data = fetch_metadata("instance")
+ return nil if instance_data.nil?
+
+ metadata = Mash.new
+ metadata["compute"] = Mash.new
+
+ instance_data.each do |k, v|
+ metadata["compute"][k] = v
+ end
+
+ vnics_data = fetch_metadata("vnics")
+
+ unless vnics_data.nil?
+ metadata["network"] = Mash.new
+ metadata["network"]["interface"] = []
+ vnics_data.each do |v|
+ metadata["network"]["interface"].append(v)
+ end
+ end
+
+ volume_attachments_data = fetch_metadata("volumeAttachments")
+
+ unless volume_attachments_data.nil?
+ metadata["volumes"] = Mash.new
+ volume_attachments_data.each do |k, v|
+ metadata["volumes"][k] = v
+ end
+ end
+
+ metadata
+ end
+end