Skip to content

Instantly share code, notes, and snippets.

@vinyar
Forked from mcquin/apache.rb
Last active August 29, 2015 14:06
Show Gist options
  • Save vinyar/c4e7999ffa5a9f820242 to your computer and use it in GitHub Desktop.
Save vinyar/c4e7999ffa5a9f820242 to your computer and use it in GitHub Desktop.
# plugins/apache/modules.rb
# :ApacheModules good, you might have some other plugin providing modules
# and you wouldn't want to overlap on the :Modules namespace.
Ohai.plugin(:ApacheModules) do
require 'mixlib/shellout'
provides "apache/modules"
collect_data(:default) do
string = Mixlib::ShellOut.new("apachectl -t -D DUMP_MODULES")
string.run_command
apache Mash.new unless apache # don't override data["apache"] in global ohai data
apache[:modules] = string.stdout
end
end
# plugins/apache/sites.rb
Ohai.plugin(:ApacheSites) do
provides "apache/sites"
collect_data do # if you don't say :default it defaults to :default
apache Mash.new unless apache # "append or create"
apache[:sites] = my_sites
end
end
# plugins/apache.rb
Ohai.plugin(:Apache) do
provides "apache"
depends "apache/modules" # This ensures that :ApacheModules gets run first
depends "apache/sites"
collect_data do
apache Mash.new unless apache # This would cause all other apache data to disappear :(
end
end
# What goes on inside Ohai when you add attributes with Mash.new
# 1. Your ohai object gets created
# it has some internal state:
# o = Ohai::System.new
# o.data => {}
# 2. attribute Mash.new
# o.data["attribute"] = {}
# External => internal equivalent
# apache[:modules] => o.data["apache"][:modules]
# Running Apache plugin:
# 0. Assume no other plugins have run yet, clean ohai object, o
# 1. Looks up the plugin dependencies, sees it depends on "apache/modules" and "apache/sites"
# 2. Run the :ApacheModules plugin (assuming it hasn't run before):
# A. We see there are no dependencies, so we'll move onto collect_data
# o.data = {}
# B. `apache Mash.new unless apache`:
# o.data = {"apache" => {}}
# C. `apache[:modules] = thing`
# o.data = {"apache" => {"modules" => thing}}
# 3. Run the :ApacheSites plugin (assume it hasn't run before):
# A. Same as 2A, except
# o.data = {"apache" => {"modules" => thing}}
# B. if we did `apache Mash.new` then o.data = {"apache" => {}} and we'd have lost our data from :ApacheModules, :(
# but instead we have `unless apache` so we get o.data = {"apache" => {"modules" => thing}} :)
# C. `apache[:sites] = other_thing`
# o.data = {"apache" => {"modules" => thing}, {"sites" => other_thing}}
# 4. :Apache has no more dependencies that are unrun so we can execute its collect_data block.
# Use the actual plugin name to disable plugins from running
# Ohai[:disabled_plugins] = [:ApacheSite]
# Plugins get joined under a common name. So you can spread a plugin out across multiple files.
# plugins/kernel/windows.rb
Ohai.plugin(:Kernel) do
require 'mixlib/shellout'
provides "kernel"
collect_data(:windows) do
kernel Mash.new
...
end
end
# plugins/kernel/linux.rb
Ohai.plugin(:Kernel) do
provides "kernel"
collect_data(:linux) do
kernel Mash.new
...
end
end
# But you can't define more than one collect_data block per platform.
# In Ohai6 if you said
# require_plugin 'apache/sites'
# then it would look for the file "#{Ohai[:plugin_path]}/apache/sites.rb"
# "apache/sites" means
# apache => {"sites" => [mysite1, mysite2, ...]}
# means that apache attribute hash has sites subkey
# Loading step:
# Searches through your Ohai[:plugins_path] folders and reads all the .rb (plugin) files there
# When we load a plugin we gather what it provides and depends on
# providers = {"apache" => {"modules" => [:ApacheModules]},
# "langauges" => {"ruby" => [:Ruby]}}
#
# Running step:
# `depends "apache/modules"` ohai looks at the providers and goes to providers["apache"]["modules"]
# For ohai 6:
# v6 can depend on v6 plugins
# v6 can depend on v7 plugins (but we sort of have to guess), to help make upgrading easier
# v7 can depend on v7 plugins
# v7 cannot depend on v6 plugins - we want you to upgrade!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment