diff options
author | Tim Smith <tsmith84@gmail.com> | 2016-05-09 12:33:05 -0700 |
---|---|---|
committer | Tim Smith <tsmith84@gmail.com> | 2016-05-09 12:38:17 -0700 |
commit | 5912cc394fb86bcc1e501582a902e6320ff71b9b (patch) | |
tree | 5737d992b7783d00b9eae698e0ad3fdb2c8c922b | |
parent | 780f7c5fbdebd9d65c4605f47bad9111d1176fcc (diff) | |
download | ohai-5912cc394fb86bcc1e501582a902e6320ff71b9b.tar.gz |
Properly poll Openstack detection + other Openstack improvements
The existing Openstack metadata logic failed as it was hardcoded to use metadata from 2013 that doesn't seem to exist in standard installs. Instead lets use the logic in the EC2 metadata mixin to find the latest version we support.
Wrap metadata fetching around socket connection check from the EC2 metadata plugin. This prevents hung Ohai runs on providers that don't support the metadata endpoint
Grab the Openstack data correctly in the Cloud plugins
Detect a system as being on Openstack based on DMI data
Remove HP cloud logic since that's dead
Add debug logging so we can follow what's happening in the logs better
-rw-r--r-- | lib/ohai/plugins/cloud.rb | 14 | ||||
-rw-r--r-- | lib/ohai/plugins/cloud_v2.rb | 8 | ||||
-rw-r--r-- | lib/ohai/plugins/openstack.rb | 79 |
3 files changed, 53 insertions, 48 deletions
diff --git a/lib/ohai/plugins/cloud.rb b/lib/ohai/plugins/cloud.rb index 23084631..bb2b531a 100644 --- a/lib/ohai/plugins/cloud.rb +++ b/lib/ohai/plugins/cloud.rb @@ -183,13 +183,13 @@ Ohai.plugin(:Cloud) do # Fill cloud hash with openstack values def get_openstack_values - cloud[:public_ips] << openstack["public_ipv4"] - cloud[:private_ips] << openstack["local_ipv4"] - cloud[:public_ipv4] = openstack["public_ipv4"] - cloud[:public_hostname] = openstack["public_hostname"] - cloud[:local_ipv4] = openstack["local_ipv4"] - cloud[:local_hostname] = openstack["local_hostname"] - cloud[:provider] = openstack["provider"] + cloud[:public_ips] << openstack["metadata"]["public_ipv4"] + cloud[:private_ips] << openstack["metadata"]["local_ipv4"] + cloud[:public_ipv4] = openstack["metadata"]["public_ipv4"] + cloud[:public_hostname] = openstack["metadata"]["public_hostname"] + cloud[:local_ipv4] = openstack["metadata"]["local_ipv4"] + cloud[:local_hostname] = openstack["metadata"]["local_hostname"] + cloud[:provider] = "openstack" end # ---------------------------------------- diff --git a/lib/ohai/plugins/cloud_v2.rb b/lib/ohai/plugins/cloud_v2.rb index 7c3f3b1a..19a491ff 100644 --- a/lib/ohai/plugins/cloud_v2.rb +++ b/lib/ohai/plugins/cloud_v2.rb @@ -242,10 +242,10 @@ Ohai.plugin(:CloudV2) do # Fill cloud hash with openstack values def get_openstack_values - @cloud_attr_obj.add_ipv4_addr(openstack["public_ipv4"], :public) - @cloud_attr_obj.add_ipv4_addr(openstack["local_ipv4"], :private) - @cloud_attr_obj.public_hostname = openstack["public_hostname"] - @cloud_attr_obj.local_hostname = openstack["local_hostname"] + @cloud_attr_obj.add_ipv4_addr(openstack["metadata"]["public_ipv4"], :public) + @cloud_attr_obj.add_ipv4_addr(openstack["metadata"]["local_ipv4"], :private) + @cloud_attr_obj.public_hostname = openstack["metadata"]["public_hostname"] + @cloud_attr_obj.local_hostname = openstack["metadata"]["local_hostname"] @cloud_attr_obj.provider = "openstack" end diff --git a/lib/ohai/plugins/openstack.rb b/lib/ohai/plugins/openstack.rb index b7ac8973..305bd96d 100644 --- a/lib/ohai/plugins/openstack.rb +++ b/lib/ohai/plugins/openstack.rb @@ -1,5 +1,6 @@ # # Author:: Matt Ray (<matt@chef.io>) +# Author:: Tim Smith (<tsmith@chef.io>) # Copyright:: Copyright (c) 2012-2016 Chef Software, Inc. # License:: Apache License, Version 2.0 # @@ -18,53 +19,57 @@ require "ohai/mixin/ec2_metadata" Ohai.plugin(:Openstack) do + include Ohai::Mixin::Ec2Metadata + provides "openstack" + depends "dmi" - include Ohai::Mixin::Ec2Metadata + # do we have the openstack dmi data + def openstack_dmi? + # detect a manufacturer of OpenStack Foundation + if dmi[:system][:all_records][0][:Manufacturer] =~ /OpenStack/ + Ohai::Log.debug("Plugin Openstack: has_openstack_dmi? == true") + true + end + rescue NoMethodError + Ohai::Log.debug("Plugin Openstack: has_openstack_dmi? == false") + false + end + + # check for the ohai hint and log debug messaging + def openstack_hint? + if hint?("openstack") + Ohai::Log.debug("Plugin Openstack: openstack hint present") + return true + else + Ohai::Log.debug("Plugin Openstack: openstack hint not present") + return false + end + end - def collect_openstack_metadata(addr = Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, api_version = "2013-04-04") - path = "/openstack/#{api_version}/meta_data.json" - uri = "http://#{addr}#{path}" - begin - response = http_client.get_response(URI.parse(uri), nil, nil) - case response.code - when "200" - FFI_Yajl::Parser.parse(response.body) - when "404" - Ohai::Log.debug("Encountered 404 response retreiving OpenStack specific metadata path: #{path} ; continuing.") - nil - else - raise "Encountered error retrieving OpenStack specific metadata (#{path} returned #{response.code} response)" + # grab metadata and return a mash. if we can't connect return nil + def openstack_metadata + if can_metadata_connect?("169.254.169.254", 80) + metadata = Mash.new + fetch_metadata.each do |k, v| + metadata[k] = v end - rescue => e - Ohai::Log.debug("Encountered error retrieving OpenStack specific metadata (#{uri}), due to #{e.class}") + Ohai::Log.debug("Plugin Openstack: Successfully fetched Openstack metadata from the metadata endpoint") + metadata + else + Ohai::Log.warn("Plugin Openstack: Timed out connecting to Openstack metadata endpoint. Skipping metadata.") nil end end collect_data do - # Adds openstack Mash - if hint?("openstack") || hint?("hp") - Ohai::Log.debug("ohai openstack") - - if can_metadata_connect?(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80) - openstack Mash.new - Ohai::Log.debug("connecting to the OpenStack metadata service") - fetch_metadata.each { |k, v| openstack[k] = v } - - if hint?("hp") - openstack["provider"] = "hp" - else - openstack["provider"] = "openstack" - Ohai::Log.debug("connecting to the OpenStack specific metadata service") - openstack["metadata"] = collect_openstack_metadata - end - - else - Ohai::Log.debug("unable to connect to the OpenStack metadata service") - end + # fetch data if we look like openstack + if openstack_hint? || openstack_dmi? + openstack Mash.new + openstack[:provider] = "openstack" # for now this is our only provider + openstack[:metadata] = openstack_metadata # fetch metadata or set this to nil else - Ohai::Log.debug("NOT ohai openstack") + Ohai::Log.debug("Plugin Openstack: Node does not appear to be an Openstack node") end end end |