Skip to content

Instantly share code, notes, and snippets.

@blasterpal
Created January 26, 2011 16:52
Show Gist options
  • Select an option

  • Save blasterpal/796991 to your computer and use it in GitHub Desktop.

Select an option

Save blasterpal/796991 to your computer and use it in GitHub Desktop.
Chef Data Bag Helper Module to pop into recipes and easily get overrides from Data Bags.
module DataBagHelper
#wrapper methods to insulate from change :)
def node_run_list
node.run_list.role_names.reverse
end
def current_data_bag_items
data_bag current_cookbook_name
end
def current_cookbook_name
self.cookbook_name
end
#finds databags that match certain criteria for a node based on roles and possible arguments to method.
# Recipe Usage Example:
#
# mongo_data_bag_config = find_matched_bag_items({:bagname => 'mongodb'}).first[:mongodb].first
# node[:mongodb].current_override.merge!(mongo_data_bag_config)
#
# Example Data Bag for a Mongo recipe:
#{
#"id" : "db-multiapp-mongo-master-qa",
#"enabled" : "true",
#"environments" : ["qa"],
#"required_roles" : ["db-mongo-master"],
#"active_groups" : [{
#"app" : {"enabled" : true},
#"storage" : {"enabled" : true}
#}],
#"active_sudo_groups" : [{
#"app" : {"enabled" : true}
#}],
#"mongodb" :[{
#"replication" : {"master" : false},
#"version" : "1.6.3",
#"perform_backups" : true
#}]
#}
def find_matched_bag_items(arguments = {})
matched_items = []
Chef::Log.debug "In use_data_bag method with arguments: #{arguments.inspect} ..."
# setup some useful defaults
bagname = arguments["bagname"] || arguments[:bagname] || current_cookbook_name
run_list = arguments["run_list"] || arguments[:run_list] || node_run_list
filter = arguments["filter"] || arguments[:filter] || "enabled:true"
raise "find_matched_bag_items required arguments not found" unless bagname && run_list
# find items that meet filter, usually enabled:true
search(bagname.to_sym, filter) do |bagitem|
Chef::Log.debug "Iterating over bagitem: #{bagitem.id}"
# find any required roles
required_roles = bagitem["required_roles"] || []
# find environments required
environments = bagitem["environments"] || []
# skip this bagitem IF hnvironment is specified AND environment is NOT found in run_list
unless environments.size > 0 && node_run_list.select {|ea| environments.include? ea}.empty?
# the current data_bag bagitem matches a run_list bagitem OR all required_roles are found in run_list
if (required_roles.size > 0 && required_roles.select{|ea| run_list.include? ea }.size == required_roles.size) || run_list.include?(bagitem.id)
Chef::Log.debug "All criteria met,storing databag item: #{bagitem.inspect}"
matched_items << bagitem #since we have reversed the order of run list the last, or highest priority item to match will return its bag
else
Chef::Log.debug "One or more of the required_roles: #{required_roles.join(' | ')} was NOT in run_list: #{run_list.join(' | ')}"
end
else
Chef::Log.debug "None of the nodes run_list environment bagitems #{run_list.join(' | ')}, match the data_bag environments: #{environments.join(' | ')}"
end
end
return matched_items
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment