#!/bin/bash
TIMEOUT=<%= @timeout %>
PORT=<%= @app_port %>

# FAILURE TIME TO DIE
function die_loudly {
  echo "**************** $@ ****************"
  exit 1
}

function say_loudly {
  echo "================ $@ ================"
}

# Let the app server finish any open requests
function finish_requests {
 <%= @finished_requests_command %>
}

# Is the app currently running
function app_running {
  <%= @app_running_command %>
}

# Wait until the app is running
function app_up {
  # Give the app *at least* a minute to boot
  # (curl will wait 5 seconds per attempt)
  for i in $(seq 1 $TIMEOUT)
  do
    app_running && return
    status=$?
    sleep 1
  done

  return $status
}


# Remove Instance from the load balancer
<%= "#{@bin_dir}/#{@remove_bin}" %>

# Allow open requests to finish
finish_requests

# Restart the application
say_loudly "ATTEMPTING to RESTART <%= node[:hostname] %>"
<%= @restart_command %>

# Wait for the app to come back up or fail the restart
app_up || die_loudly "RESTART FAILED <%= node[:hostname] %>"
say_loudly "RESTART COMPLETE <%= node[:hostname] %>"

# Add Instance back into the load balancer
<%= "#{@bin_dir}/#{@add_bin}" %>

#!/bin/bash
TIMEOUT=<%= @timeout %>
PORT=<%= @app_port %>

# FAILURE TIME TO DIE
function die_loudly {
  echo "**************** $@ ****************"
  exit 1
}

function say_loudly {
  echo "================ $@ ================"
}

# Let the app server finish any open requests
function finish_requests {
 <%= @finished_requests_command %>
}

# Is the app currently running
function app_running {
  <%= @app_running_command %> && echo
}

# Wait until the app is running
function app_up {
  # Give the app *at least* a minute to boot
  # (curl will wait 5 seconds per attempt)
  for i in $(seq 1 $TIMEOUT)
  do
    app_running && return
    status=$?
    sleep 1
  done

  return $status
}


# Remove Instance from the load balancer
<%= "#{@bin_dir}/#{@remove_bin}" %>

# Allow open requests to finish
finish_requests

# Restart the application
say_loudly "ATTEMPTING to RESTART <%= node[:hostname] %>"
<%= @restart_command %>

# Wait for the app to come back up or fail the restart
app_up || die_loudly "RESTART FAILED <%= node[:hostname] %>"
say_loudly "RESTART COMPLETE <%= node[:hostname] %>"

# Add Instance back into the load balancer
<%= "#{@bin_dir}/#{@add_bin}" %>

Add some newline love because curl
Add some newline love because curl
