Skip to content

Instantly share code, notes, and snippets.

@mikeverbeck
Created September 18, 2013 20:45
Show Gist options
  • Save mikeverbeck/6615411 to your computer and use it in GitHub Desktop.
Save mikeverbeck/6615411 to your computer and use it in GitHub Desktop.
chef-rbenv ohai plugin recipe
include_recipe "ohai"
bin_path = ::File.join(node['rbenv']['root_path'], "bin", "rbenv")
template "#{node[:ohai][:plugin_path]}/rbenv.rb" do
source 'plugins/rbenv.rb.erb'
owner 'root'
group 'root'
mode 0755
variables(
:rbenv_bin => bin_path
)
notifies :reload, "ohai[custom_plugins]", :immediately
end
provides "languages/ruby"
require_plugin "languages"
def rbenv_command(*args)
run_command(:command => "<%= @rbenv_bin %> #{args.join(' ')}", :no_status_check => true)
end
def run_ruby(command)
rbenv_ruby = rbenv_command("which ruby")
cmd = "#{rbenv_ruby} -e \"require 'rbconfig'; #{command}\""
so = shell_out(cmd)
so.stdout.strip
end
collect_data do
languages[:ruby] = Mash.new
values = {
:platform => "RUBY_PLATFORM",
:version => "RUBY_VERSION",
:release_date => "RUBY_RELEASE_DATE",
:target => "::Config::CONFIG['target']",
:target_cpu => "::Config::CONFIG['target_cpu']",
:target_vendor => "::Config::CONFIG['target_vendor']",
:target_os => "::Config::CONFIG['target_os']",
:host => "::Config::CONFIG['host']",
:host_cpu => "::Config::CONFIG['host_cpu']",
:host_os => "::Config::CONFIG['host_os']",
:host_vendor => "::Config::CONFIG['host_vendor']",
:bin_dir => "::Config::CONFIG['bindir']",
:ruby_bin => "::File.join(::Config::CONFIG['bindir'], ::Config::CONFIG['ruby_install_name'])"
}
# Create a query string from above hash
env_string = ""
values.keys.each do |v|
env_string << "#{v}=\#{#{values[v]}},"
end
# Query the system ruby
result = run_ruby "puts %Q(#{env_string})"
# Parse results to plugin hash
result.split(',').each do |entry|
key, value = entry.split('=')
languages[:ruby][key.to_sym] = value || ""
end
# Perform one more (conditional) query
bin_dir = languages[:ruby][:bin_dir]
ruby_bin = languages[:ruby][:ruby_bin]
gem_binaries = [
run_ruby("require 'rubygems'; puts ::Gem.default_exec_format % 'gem'"),
"gem"
].map {|bin| ::File.join(bin_dir, bin)}
gem_binary = gem_binaries.find {|bin| ::File.exists? bin }
if gem_binary
languages[:ruby][:gems_dir] = run_ruby "puts %x{#{ruby_bin} #{gem_binary} env gemdir}.chomp!"
languages[:ruby][:gem_bin] = gem_binary
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment