Skip to content

Instantly share code, notes, and snippets.

@spheromak
Created June 28, 2010 22:38
Show Gist options
  • Save spheromak/456477 to your computer and use it in GitHub Desktop.
Save spheromak/456477 to your computer and use it in GitHub Desktop.
ldirectord dynamic chef example
package "ldirectord" do
action :install
end
lb = node[:heartbeat][:cluster]
# find all nodes that think they belong to a service
#
subscribes=Hash.new
search(:node, "milt_services:[* TO *]" ).each do |n|
n["milt"]["services"].each do |svc, val|
subscribes.merge!( { svc => { n["hostname"] => val } } )
end
end
vip_defaults = node[:ldirectord][:defaults].to_hash
vips=Array.new
# pull each services vip data bag
# -> load lb settings for vip
# -> add host to vips its subscribed too
subscribes.keys.each do |svc|
data_bag_item('services', svc)["vips"].each { |j| vips << j }
# load the hosts from the nodes to the vips its subscribed to
# make sure vips are for this lb/cluster only
vips.each_index do |i|
if vips[i]["ha"]["cluster"] == lb
vips[i]["lb"]["config"] = vip_defaults.merge(vips[i]["lb"]["config"].to_hash)
if vips[i]["lb"].has_key?("real_servers")
vips[i]["lb"]["real_servers"].merge!(subscribes[svc])
else
vips[i]["lb"]["real_servers"] = subscribes[svc]
end
end
end
end
# find all node data that optionally subscribes to a vip
# -> add it to vips hash
search(:node, "milt_vips_#{lb}:[* TO *]" ).each do |n|
n[:milt][:vips][lb].keys.each do |node_vip|
vips.each_index do |i|
if vips[i]["name"] == node_vip
vips[i]["lb"]["real_servers"].to_hash.merge!(n["milt"]["vips"][lb][node_vip])
end
end
end
end
# unroll vips
template node[:ldirectord][:config] do
source "lvs.cf.erb"
mode 0640
backup false
variables(:vips => vips)
end
# ~~~ ldirectord configuration ~~~
#
# <%= @node[:ldirectord][:config] %>
#
# config autogenerated via chef
# local edits will be destroyed
#
# ~~~ ldirectord configuration ~~~
quiescent = <%= @node[:ldirectord][:quiescent] %>
autoreload = <%= @node[:ldirectord][:autoreload] %>
checktimeout = <%= @node[:ldirectord][:checktimeout] %>
checkinterval = <%= @node[:ldirectord][:checkinterval] %>
<% @vips.each do |vip| -%>
virtual = <%= vip["name"] -%>:<%= vip["lb"]["config"]["service"] %>
<% vip["lb"]["config"].sort.each do |k, v| -%>
<%= "#{k}\t= #{v}" %>
<% end %>
<% if vip["lb"]["maintenance"] %>
# maintenance mode enabled for this vip
<% next %>
<% end %>
<% vip["lb"]["real_servers"].sort.each do |k, v| -%>
real = <%= "#{k}:#{vip["lb"]["config"]["service"]}\tgate\t#{v}" %>
<% end %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment