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

ohai & $load_path #520

Closed
svmastersamurai opened this issue Apr 6, 2015 · 5 comments
Closed

ohai & $load_path #520

svmastersamurai opened this issue Apr 6, 2015 · 5 comments
Labels
Expeditor: Skip Version Bump Used to skip built_in:bump_version

Comments

@svmastersamurai
Copy link

I'm trying to collect some information about the system via an ohai plugin I've written. The plugin utilizes some custom ruby classes I've developed to make things more modular. To get this to work I have a cookbook that will append to the $LOAD_PATH so it can find the classes I've written. This works when plugins are reloaded, but does not work on subsequent runs. Rather than having to reload all the plugins every chef run I am curious how to get ohai to see my custom classes without needing to reload plugins every time. I've also tried to set the local ruby environment variables RUBYLIB and RUBYLIB_PREFIX within a cookbook but to no avail =(

Ideally I would like to collect information like this

require 'my_custom_information_collector'

Ohai.plugin(:default) do
  provides 'custom_info'
   collect_data do
    my_node['custom_info'] = my_custom_information_collector.new.get_some_info
  end
end

There of course is the argument to have the classes located within the plugin, which is how I was previously doing things. However, I want to be able to abstract things out and allow this library to support multiple platforms, of which I will have different members on my team contribute code to their respective operating system that they know best. Given this requirement, a particular plugin file could grow to a large size and be difficult to follow since as we expand support to more platforms.

Is this a supported use case? Would a custom gem be a better alternative than appending to the $LOAD_PATH during runtime? If so, would what would be a good way to vendor this gem?

Cheers!

@svmastersamurai
Copy link
Author

Digging through the code a bit
https://github.com/chef/chef/blob/master/lib/chef/client.rb

def run
  runlock = RunLock.new(Chef::Config.lockfile)
...
    Chef::Log.info("*** Chef #{Chef::VERSION} ***")
    Chef::Log.info "Chef-client pid: #{Process.pid}"
    Chef::Log.debug("Chef-client request_id: #{request_id}")
    enforce_path_sanity
    run_ohai
...

The enforce_path_sanity method call seems to make a call that strips bare the ENV['PATH'] environment variable which might be what is causing me trouble. I am going to speculate that when the plugins are reloaded within the context of a chef run this enables them to see the updated load path I am setting

@mcquin
Copy link
Contributor

mcquin commented Jul 14, 2015

Sorry for the delay, @svmastersamurai. Any luck? Have you tried using require_relative to require your custom classes? For example, if your plugin is at /etc/chef/ohai_plugins/foo.rb and your Ruby class is stored on the node at /etc/chef/ohai_plugins/lib/foo_collector.rb you can write

# /etc/chef/ohai_plugins/foo.rb
require_relative '../lib/foo_collector.rb'

@svmastersamurai
Copy link
Author

Not a problem @mcquin, currently I am going through some training at work and will be away from my chef duties for a few more weeks, but I will give this a try and update this as soon as I can take a look at it!

@thommay
Copy link
Contributor

thommay commented Feb 17, 2016

Hi,
Very sorry this has been sat around for so long. Assuming you didn't come up with a solution, what's happening is that when you reload ohai, that's happening after your cookbook has changed the $load_path, so everything work ok. But in a normal run, ohai gets run before any cookbook has been compiled or run, so the load_path will be unchanged. Lamont has just pointed out some RFCs that will make this process better in the future, but right now you should use a chef_gem resource to get the gem installed.
Thanks for the report

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Expeditor: Skip Version Bump Used to skip built_in:bump_version
Projects
None yet
Development

No branches or pull requests

5 participants