# This file managed by Chef.
# Note: most of the template is pulled directly from the chef-cookbooks keepalived template. 
# https://github.com/chef-cookbooks/keepalived
# RHEL-based distros were not supported at the time this was written, hence the redo.  

global_defs {

  notification_email {
   <% Array(node['keepalived']['global']['notification_emails']).each do |email| %>
   <%= email %>
   <% end %>
  }
  notification_email_from <%= node['keepalived']['global']['notification_email_from'] %>
  smtp_server <%= node['keepalived']['global']['smtp_server'] %>
  smtp_connect_timeout <%= node['keepalived']['global']['smtp_connect_timeout'] %>
  router_id <%= node['hostname'] %>
}

<% node['keepalived']['check_scripts'].each_pair do |name, script| %>
vrrp_script <%= name %> {
  script "<%= script['script'] %>"
  interval <%= script['interval'] %>
  weight <%= script['weight'] %>
  <% if script['fall'] %>
  fall <%= script['fall'] %>
  <% end %>
  <% if script['rise'] %>
  rise <%= script['rise'] %>
  <% end -%>
}
<% end %>

<% node['keepalived']['instances'].each_pair do |name, instance| -%>
<%
  states = instance['states'] || {}
  priorities = instance['priorities'] || {}
  virtual_router_ids = instance['virtual_router_ids'] || {}
-%>
vrrp_instance <%= name.upcase %> {
  interface <%= instance['interface'] || node['network']['default_interface'] %>
  virtual_router_id <%= instance['virtual_router_id'] || node['keepalived']['instance_defaults']['virtual_router_id'] %>
  <% if instance['garp_master_delay'] %>
  garp_master_delay <%= instance['garp_master_delay'] %>
  <% end %>
  <% if instance['garp_master_repeat'] %>
  garp_master_repeat <%= instance['garp_master_repeat'] %>
  <% end %>
  <% if instance['garp_master_refresh'] %>
  garp_master_refresh <%= instance['garp_master_refresh'] %>
  <% end %>
  <% if instance['garp_master_refresh_repeat'] %>
  garp_master_refresh_repeat <%= instance['garp_master_refresh_repeat'] %>
  <% end %>
  <% if instance['nopreempt'] -%>
  nopreempt
  <% end -%>
  state <%= instance['state'][node['hostname']] || node['keepalived']['instance_defaults']['state'] %>
  priority  <%= instance['priority'][node['hostname']] || node['keepalived']['instance_defaults']['priority'] %>
  <% if instance['advert_int'] -%>
  advert_int <%= instance['advert_int'] %>
  <% end -%>
  <% if instance['unicast_peer'] && !instance['unicast_peer'].empty? -%>
  unicast_peer {
    <% Array(instance['unicast_peer']).each do |address| %>
    <%= address %>
    <% end %>
  }
  <% end -%>
  <% if instance['auth_type'] -%>
  authentication {
    auth_type <%= instance['auth_type'].to_s.upcase %>
    auth_pass <%= instance['auth_pass'] %>
  }
  <% end -%>
  virtual_ipaddress {
    <% Array(instance['ip_addresses']).each do |address| %>
    <%= address %>
    <% end %>
  }
  <% if instance['track_script'] %>
  track_script {
    <%= instance['track_script'] %>
  }
  <% if instance['smtp_alert'] %>
  smtp_alert
  <% end %>
  <% if instance['notify_master'] %>
  notify_master "<%= instance['notify_master'] %>"
  <% end %>
  <% if instance['notify_backup'] %>
  notify_backup "<%= instance['notify_backup'] %>"
  <% end %>
  <% if instance['notify_fault'] %>
  notify_fault "<%= instance['notify_fault'] %>"
  <% end %>
  <% end %>
}
<% end -%>
