Skip to content
This repository has been archived by the owner on Dec 2, 2019. It is now read-only.

Commit

Permalink
Don't let the script just die, fail it explicitly
Browse files Browse the repository at this point in the history
No more parallel references, we're all bash now!

When a remote command fails, It will display the failing command.
Would be nice to display the host on which it failed. Ought to be done
part of the printf task.
Nice IFS trick to preserve whitespace formatting.
  • Loading branch information
gerhard committed Jun 5, 2012
1 parent ffa1497 commit d02da5b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
4 changes: 3 additions & 1 deletion bin/deliver
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env bash

set -e
# If a specific part fails (I'm thinking mostly remote commands),
# I will fail the script explicitly, don't want it to just die
# set -e

BASE_PATH="$(dirname $0)/.."

Expand Down
2 changes: 1 addition & 1 deletion libexec/app_config
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ fi

# From the time when deliver was single-server
#
SERVERS=$(__parallel_friendly "$SERVER $SERVERS")
SERVERS=$(__remote_friendly "$SERVER $SERVERS")
53 changes: 33 additions & 20 deletions libexec/core
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ __complain_if_interrupted() {
trap "exit 2" 1 2 3 13 15
}

# Ensures args (in this case hosts) are in a format that parallel understands
# e.g. " ruby-1 ruby-2" -> "ruby-1,ruby-2"
# Ensures args (in this case hosts) are space separated so that we can easily loop over them
# e.g. " ruby-1,ruby-2 " -> "ruby-1 ruby-2"
#
__parallel_friendly() {
echo $@ | tr ' ' ',' | sed 's/^,//' | sed 's/,$//'
__remote_friendly() {
echo $@ | tr ',' ' ' | sed 's/^ //' | sed 's/ $//'
}

# If these values are specified at the runtime, hang onto them
# until we're ready to overwrite everything set from other sources
#
__capture_runtime_configs() {
RUNTIME_STRATEGY=$STRATEGY
RUNTIME_SERVERS=$(__parallel_friendly "$SERVER $SERVERS")
RUNTIME_SERVERS=$(__remote_friendly "$SERVER $SERVERS")
}

# If STRATEGY or SERVERS are specified at the time of invoking, overwrite
Expand All @@ -32,9 +32,7 @@ __apply_runtime_configs() {
STRATEGY="$RUNTIME_STRATEGY"
fi

# The tr when joining $SERVER with $SERVERS
# Review this
if [ -n "${RUNTIME_SERVERS/ /}" ]
if [ -n "$RUNTIME_SERVERS" ]
then
SERVERS="$RUNTIME_SERVERS"
fi
Expand Down Expand Up @@ -109,8 +107,8 @@ __remote_hosts() {
do
SERVERS_APP_USER="$SERVERS_APP_USER,$APP_USER@$server"
done
SERVERS=$(__parallel_friendly $SERVERS)
SERVERS_APP_USER=$(__parallel_friendly $SERVERS_APP_USER)
SERVERS=$(__remote_friendly $SERVERS)
SERVERS_APP_USER=$(__remote_friendly $SERVERS_APP_USER)
}

__check_config() {
Expand Down Expand Up @@ -138,12 +136,6 @@ __check_config() {
printf "%-20s\t%s\n" "${config_text}$config" "$config_value${txtrst}"
done

if [ $(which parallel | grep -c "/") = 0 ]
then
local check_failed=true
echo -e "\n${txtred}Can't find GNU parallel in your \$PATH. Have you installed it?${txtrst}"
fi

if [ ! -z "$check_failed" ]
then
echo -e "\n${bldred}CAN'T DELIVER, WE'RE MISSING A FEW THINGS${txtrst}\n"
Expand Down Expand Up @@ -220,14 +212,35 @@ __exec() {
eval "$1 $SILENCE"
}

# Checks if a remote command has exited with a non-zero status
#
__remote_failed() {
local _status="$1"
if [ ! "$_status" = 0 ]
then
error "FAILED!\n$remote_command"
fi
}

# Multi-host & mode aware
#
__remote() {
if [ ! $MODE = "test" ]
if [ ! "$MODE" = "test" ]
then
local _command=$1
local _hosts=${2:-"$SERVERS_APP_USER"}
remote_command="$1"
local _hosts="${2:-"$SERVERS_APP_USER"}"
local _remote_pids=()

for _host in $_hosts
do
ssh -o ConnectTimeout=2 "$_host" "$remote_command $SILENCE" &
_remote_pids+=("$!")
done

parallel --tag --nonall -S $_hosts $_command
for _pid in "${_remote_pids[@]}"
do
wait "$_pid"
__remote_failed "$?"
done
fi
}
2 changes: 2 additions & 0 deletions libexec/states
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# Specific failure, fail the whole script
#
error() {
IFS='%'
error_message $1
unset IFS
exit 1
}

Expand Down

0 comments on commit d02da5b

Please sign in to comment.