Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly poll Openstack metadata + other Openstack improvements #818

Merged
merged 8 commits into from
May 11, 2016
14 changes: 7 additions & 7 deletions lib/ohai/plugins/cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ def on_openstack?

# 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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this can sometimes be "dreamhost"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this one gets a bit weird and here's why I went with openstack. When you're in the context of the cloud plugin says provider you'd think openstack (right? maybe...). When you're in the context of the openstack plugin you know you're openstack so when someone says provider you're thinking about what openstack provider and that could be dreamhost. I guess it could really go either way though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be breaking for some users. Though I understand your argument, I think it's safer to keep it as it was.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous behavior was HP or Openstack, but I'll switch this one back. People on Dreamhost can figure it out probably.

end

# ----------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions lib/ohai/plugins/cloud_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ def on_openstack?

# 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

Expand Down
89 changes: 52 additions & 37 deletions lib/ohai/plugins/openstack.rb
Original file line number Diff line number Diff line change
@@ -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
#
Expand All @@ -18,53 +19,67 @@
require "ohai/mixin/ec2_metadata"

Ohai.plugin(:Openstack) do
include Ohai::Mixin::Ec2Metadata

provides "openstack"
depends "dmi"
depends "etc"

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}"
# dreamhost systems hae the dhc-user on them
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

def openstack_provider
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)"
return "dreamhost" if etc["passwd"]["dhc-user"]
rescue NoMethodError
# handle etc not existing on non-linux systems
end
return "openstack"
end
Copy link
Contributor

@thommay thommay May 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def openstack_provider
  if  etc["passwd"]["dhc-user"]
    "dreamhost"
  else 
    "openstack"
  end
rescue NoMethodError
end

might be cleaner?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it that way and it ends up returning nil when it can't find the key, which isn't what you want.

Scenarios:
etc plugin doesn't exist at all and the rescue is necessary -> openstack
etc plugin exists, but dhc-user doesn't exist -> openstack
etc plugin exists and dhc-user does exist -> dreamhost


# grab metadata and return a mash. if we can't connect return nil
def openstack_metadata
metadata = Mash.new
if can_metadata_connect?("169.254.169.254", 80)
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}")
nil
Ohai::Log.debug("Plugin Openstack: Successfully fetched Openstack metadata from the metadata endpoint")
else
Ohai::Log.debug("Plugin Openstack: Timed out connecting to Openstack metadata endpoint. Skipping metadata.")
end
metadata
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_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
Loading