diff --git a/resources/libraries/helper.rb b/resources/libraries/helper.rb index d10a806..18708a8 100644 --- a/resources/libraries/helper.rb +++ b/resources/libraries/helper.rb @@ -1,28 +1,4 @@ module Keepalived module Helper - def get_master(virtual_ip, iface, managers) - sorted_managers = managers.map.with_index do |manager, index| - priority = 50 + managers.size - index - { - name: manager.name, - priority: priority, - } - end - - top_nodes = sorted_managers.first(2).sort_by { |node| -node[:priority] } - - if iface_has_virtual_ip?(iface, virtual_ip) - top_nodes[1][:name] - else - top_nodes[0][:name] - end - end - - private - - def iface_has_virtual_ip?(iface, virtual_ip) - iface_data = `ip addr show #{iface}` - iface_data.include?(virtual_ip) - end end end diff --git a/resources/providers/config.rb b/resources/providers/config.rb index 78af146..ef66ff7 100644 --- a/resources/providers/config.rb +++ b/resources/providers/config.rb @@ -4,8 +4,6 @@ # Provider:: config # -include Keepalived::Helper - action :add do begin user = new_resource.user @@ -75,6 +73,7 @@ group 'root' mode '0755' retries 2 + variables(virtual_ip: postgresql_vrrp, iface: postgresql_iface) end template '/usr/lib/redborder/bin/rb_keepalived_backup_notify_postgresql.sh' do @@ -84,7 +83,7 @@ group 'root' mode '0755' retries 2 - variables(master_node: get_master(postgresql_vrrp, postgresql_iface, managers)) + variables(virtual_ip: postgresql_vrrp, iface: postgresql_iface) end execute 'set_keepalived_permissive' do diff --git a/resources/templates/default/keepalived.conf.erb b/resources/templates/default/keepalived.conf.erb index 75c87dc..69088ef 100644 --- a/resources/templates/default/keepalived.conf.erb +++ b/resources/templates/default/keepalived.conf.erb @@ -44,7 +44,11 @@ vrrp_instance vi_<%= ip %> { state BACKUP interface <%= iface %> virtual_router_id <%= ((@start_id.nil? ? 111 : @start_id.to_i) + index).to_s %> + <% if services.detect { |x| x["service"] == "postgresql" } %> + priority 100 + <% else %> priority <%= 50+@managers.size-@managers.index { |m| m.name == node.name } %> + <% end %> advert_int 1 nopreempt <% if !node["redborder"].nil? and !node["redborder"]["dmidecode"].nil? and !node["redborder"]["dmidecode"]["manufacturer"].nil? and node["redborder"]["dmidecode"]["manufacturer"].to_s.downcase == "xen" %> diff --git a/resources/templates/default/notify_backup_postgresql.erb b/resources/templates/default/notify_backup_postgresql.erb index b5fa59c..50871a8 100644 --- a/resources/templates/default/notify_backup_postgresql.erb +++ b/resources/templates/default/notify_backup_postgresql.erb @@ -1,6 +1,8 @@ #!/bin/bash -MASTER_NODE=<%= @master_node %> +VIRTUAL_IP=<%= @virtual_ip %> +IFACE=<%= @iface %> +NAME=postgresql LOG_FILE=/tmp/rb_notify_postgresql_backup.log # Cleaning tmp files @@ -10,5 +12,30 @@ rm -f /tmp/rb_notify_postgresql* date >> "$LOG_FILE" echo "Executing rb_notify_backup_postgresql" >> "$LOG_FILE" -echo "Executing rb_sync_from_master.sh $MASTER_NODE" >> "$LOG_FILE" -/usr/lib/redborder/bin/rb_sync_from_master.sh $MASTER_NODE >> "$LOG_FILE" \ No newline at end of file +grep vrrp_sync_group /etc/keepalived/keepalived.conf | grep -q $NAME +if [ $? -ne 0 ]; then + echo "$NAME has no virtual ip on this node" >> "$LOG_FILE" + exit 0 +fi + +# If the VIP is still up, then something is wrong +found=0 +sleep 5 # wait 5 seconds to converge keepalived successfully + +for i in $(seq 0 2); do + ip addr show dev $IFACE | grep "inet " | awk '{print $2}' | sed 's/\/.*//' | grep -q "^$VIRTUAL_IP$" + if [ $? -eq 0 ]; then + # found virtual ip address + found=1 + break + fi + sleep 1 +done + +if [ $found -eq 1 ]; then + echo "found virtual ip $VIRTUAL_IP, doing nothing" >> "$LOG_FILE" + exit 0 +fi + +echo "Executing rb_sync_from_master.sh $VIRTUAL_IP" >> "$LOG_FILE" +/usr/lib/redborder/bin/rb_sync_from_master.sh $VIRTUAL_IP >> "$LOG_FILE" \ No newline at end of file diff --git a/resources/templates/default/notify_master_postgresql.erb b/resources/templates/default/notify_master_postgresql.erb index 3a9835b..1ad715d 100644 --- a/resources/templates/default/notify_master_postgresql.erb +++ b/resources/templates/default/notify_master_postgresql.erb @@ -1,6 +1,8 @@ #!/bin/bash -MASTER_NODE=<%= @master_node %> +VIRTUAL_IP=<%= @virtual_ip %> +IFACE=<%= @iface %> +NAME=postgresql LOG_FILE=/tmp/rb_notify_postgresql_master.log # Cleaning tmp files @@ -10,6 +12,31 @@ rm -f /tmp/rb_notify_postgresql* date >> "$LOG_FILE" echo "Executing rb_notify_master_postgresql" >> "$LOG_FILE" +grep vrrp_sync_group /etc/keepalived/keepalived.conf | grep -q $NAME +if [ $? -ne 0 ]; then + echo "$NAME has no virtual ip on this node" >> "$LOG_FILE" + exit 0 +fi + +# If the VIP is not up, then something is wrong +found=0 +sleep 5 # wait 5 seconds to converge keepalived successfully + +for i in $(seq 0 2); do + ip addr show dev $IFACE | grep "inet " | awk '{print $2}' | sed 's/\/.*//' | grep -q "^$VIRTUAL_IP$" + if [ $? -eq 0 ]; then + # found virtual ip address + found=1 + break + fi + sleep 1 +done + +if [ $found -eq 0 ]; then + echo "error: virtual ip $VIRTUAL_IP not found, exiting" >> "$LOG_FILE" + exit 0 +fi + echo "Promoting to master" >> "$LOG_FILE" touch /tmp/postgresql.trigger sed -i '/^primary_conninfo/d' /var/lib/pgsql/data/postgresql.conf