<commit_before>#!/bin/bash
#
# Cluster like it's 1999
#

LOGNAME=/var/log/opscode/keepalived/cluster.log
PROGNAME=$(basename $0)

# must be longer than unicorn 60 second timeout
export SVWAIT=30

# touching the terminate_master_file will abort a stuck master transition
# (if you know it will never be able to grab the drbd primary cluster 'lock')
terminate_master_file=/var/opt/opscode/keepalived/terminate_master
requested_state_file=/var/opt/opscode/keepalived/requested_cluster_status
current_state_file=/var/opt/opscode/keepalived/current_cluster_status
lockfile=/var/opt/opscode/keepalived/cluster-transition.lock

function log_me
{
  echo $1
  echo "$(date -R): ${1}" >> $LOGNAME
}

function error_exit
{
  log_me "$1"
  echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
  exit 1
}

function transition
{
  log_me "Transitioning to $1"
  case "$1" in
    master)
      # 10 * SVWAIT 
      try=300
      while true; do
        log_me "Attempting DRBD primary takeover - attempts left ${try}"
        drbdadm primary pc0 && break
        let "--try" || error_exit "Cannot become drbd primary!" 
        sleep 1
      done

      log_me "DRBD primary takeover successful"

      log_me "Mounting <%= node['private_chef']['drbd']['device'] %> at <%= node['private_chef']['drbd']['data_dir'] %>"
      mount <%= node['private_chef']['drbd']['device'] %> <%= node['private_chef']['drbd']['data_dir'] %> || true

<%- node['private_chef']['keepalived']['service_order'].each do |item| -%>
<%- if node['private_chef'][item['key']]['ha'] -%>
      log_me "Starting service <%= item['service_name'] %>"
      /opt/opscode/bin/private-chef-ctl <%= item['service_name'] %> start || error_exit "Cannot start <%= item['service_name'] %>"
      <%- if node['private_chef']['keepalived']['service_posthooks'][item['service_name']] -%>
        log_me "Doing post-start hook for <%= item['service_name'] %>"
        <%= node['private_chef']['keepalived']['service_posthooks'][item['service_name']] %>
      <%- end -%>
<%- end -%>
<%- end -%>

      ;;
    backup)
<%- node['private_chef']['keepalived']['service_order'].reverse.each do |item| -%>
<%- if node['private_chef'][item['key']]['ha'] -%>
      log_me "Stopping service <%= item['service_name'] %>"
      /opt/opscode/bin/private-chef-ctl <%= item['service_name'] %> graceful-kill || error_exit "Cannot kill <%= item['service_name'] %>"
<%- end -%>
<%- end -%>
      # 10 * SVWAIT 
      try=300
      while true; do
        log_me "Attempting to become DRBD secondary - attempts left ${try}"
        mountpoint <%= node['private_chef']['drbd']['data_dir'] %> && fuser -km <%= node['private_chef']['drbd']['data_dir'] %>
        umount -f <%= node['private_chef']['drbd']['data_dir'] %> || true
        drbdadm secondary pc0 && break
        let "--try" || error_exit "Cannot become DRBD secondary"
        sleep 1
      done
      ;;
    fault)
      # Um... we are on the way elsewhere
      ;;
    *)
  esac
}

function lock
{
  if [ -e $1 ]; then
    read lockpid < $1
    if [ ! -z "$lockpid" -a -d /proc/$lockpid ]; then
      return 1
    fi
  fi
  trap "rm -f $1; exit" INT TERM EXIT
  echo $$ > $1
  return 0
}

function unlock
{
  rm $1
  trap - INT TERM EXIT
}

function transition_loop
{
   transition "$1"
   log_me "Transitioned to $1"
   transitioned_to=$1
   last_requested_state=`echo -n $(cat $requested_state_file)`
   while [ "$transitioned_to" != "$last_requested_state" ]; do
     transition $last_requested_state
     log_me "Transitioned to $1"
     transitioned_to=$last_requested_state
     last_requested_state=`echo -n $(cat $requested_state_file)`
   done
   unlock $lockfile 
   echo $1 > $current_state_file
}

echo $1 > $requested_state_file

try=10
while true; do
  if lock $lockfile; then
     transition_loop "$1"
     break
  else
     log_me "Requested transition to $1"
     if [ "$1" != "backup" ]; then
       error_exit "Requested transition to $1, but state transition already running."
       break
     fi
     log_me "Attempting to interrupt previously running state transition."<commit_msg>misisng code to allow backup to abort a master state transition that is stuck<commit_after>#!/bin/bash
#
# Cluster like it's 1999
#

LOGNAME=/var/log/opscode/keepalived/cluster.log
PROGNAME=$(basename $0)

# must be longer than unicorn 60 second timeout
export SVWAIT=30

# touching the terminate_master_file will abort a stuck master transition
# (if you know it will never be able to grab the drbd primary cluster 'lock')
terminate_master_file=/var/opt/opscode/keepalived/terminate_master
requested_state_file=/var/opt/opscode/keepalived/requested_cluster_status
current_state_file=/var/opt/opscode/keepalived/current_cluster_status
lockfile=/var/opt/opscode/keepalived/cluster-transition.lock

function log_me
{
  echo $1
  echo "$(date -R): ${1}" >> $LOGNAME
}

function error_exit
{
  log_me "$1"
  echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
  exit 1
}

function transition
{
  log_me "Transitioning to $1"
  case "$1" in
    master)
      # 10 * SVWAIT 
      try=300
      while true; do
        log_me "Attempting DRBD primary takeover - attempts left ${try}"
        drbdadm primary pc0 && break
        let "--try" || error_exit "Cannot become drbd primary!" 
        [ -e $terminate_master_file ] && error_exit "Aborting master state transition due to request"
        sleep 1
      done

      log_me "DRBD primary takeover successful"

      log_me "Mounting <%= node['private_chef']['drbd']['device'] %> at <%= node['private_chef']['drbd']['data_dir'] %>"
      mount <%= node['private_chef']['drbd']['device'] %> <%= node['private_chef']['drbd']['data_dir'] %> || true

<%- node['private_chef']['keepalived']['service_order'].each do |item| -%>
<%- if node['private_chef'][item['key']]['ha'] -%>
      log_me "Starting service <%= item['service_name'] %>"
      /opt/opscode/bin/private-chef-ctl <%= item['service_name'] %> start || error_exit "Cannot start <%= item['service_name'] %>"
      <%- if node['private_chef']['keepalived']['service_posthooks'][item['service_name']] -%>
        log_me "Doing post-start hook for <%= item['service_name'] %>"
        <%= node['private_chef']['keepalived']['service_posthooks'][item['service_name']] %>
      <%- end -%>
<%- end -%>
<%- end -%>

      ;;
    backup)
<%- node['private_chef']['keepalived']['service_order'].reverse.each do |item| -%>
<%- if node['private_chef'][item['key']]['ha'] -%>
      log_me "Stopping service <%= item['service_name'] %>"
      /opt/opscode/bin/private-chef-ctl <%= item['service_name'] %> graceful-kill || error_exit "Cannot kill <%= item['service_name'] %>"
<%- end -%>
<%- end -%>
      # 10 * SVWAIT 
      try=300
      while true; do
        log_me "Attempting to become DRBD secondary - attempts left ${try}"
        mountpoint <%= node['private_chef']['drbd']['data_dir'] %> && fuser -km <%= node['private_chef']['drbd']['data_dir'] %>
        umount -f <%= node['private_chef']['drbd']['data_dir'] %> || true
        drbdadm secondary pc0 && break
        let "--try" || error_exit "Cannot become DRBD secondary"
        sleep 1
      done
      ;;
    fault)
      # Um... we are on the way elsewhere
      ;;
    *)
  esac
}

function lock
{
  if [ -e $1 ]; then
    read lockpid < $1
    if [ ! -z "$lockpid" -a -d /proc/$lockpid ]; then
      return 1
    fi
  fi
  trap "rm -f $1; exit" INT TERM EXIT
  echo $$ > $1
  return 0
}

function unlock
{
  rm $1
  trap - INT TERM EXIT
}

function transition_loop
{
   transition "$1"
   log_me "Transitioned to $1"
   transitioned_to=$1
   last_requested_state=`echo -n $(cat $requested_state_file)`
   while [ "$transitioned_to" != "$last_requested_state" ]; do
     transition $last_requested_state
     log_me "Transitioned to $1"
     transitioned_to=$last_requested_state
     last_requested_state=`echo -n $(cat $requested_state_file)`
   done
   unlock $lockfile 
   echo $1 > $current_state_file
}

echo $1 > $requested_state_file

try=10
while true; do
  if lock $lockfile; then
     transition_loop "$1"
     break
  else
     let "--try" || error_exit "Cannot become drbd backup!" 
     log_me "Requested transition to $1"
     if [ "$1" != "backup" ]; then
       error_exit "Requested transition to $1, but state transition already running."
       break
     fi
     log_me "Attempting to interrupt previously running state transition."
{'fraction_non_alphanumeric': 0.1574305803837445, 'fraction_numerical': 0.01215588130139435, 'mean_word_length': 3.4168421052631577, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7323094', 'n_tokens_mistral': 2969, 'n_tokens_neox': 2771, 'n_words': 698}