Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save maurice-does-software/88d486754e0be39f09bad526a6d7fc18 to your computer and use it in GitHub Desktop.

Select an option

Save maurice-does-software/88d486754e0be39f09bad526a6d7fc18 to your computer and use it in GitHub Desktop.
This script creates a patch file of the most recent git commit, uses scp and patch to apply the changes to each of 3 app servers, then restarts the rails apps on those servers one by one, after waiting for the user to give the go-ahead (so they can take the servers down from a load balancer, or perform other tasks first). It accepts two optional…
#!/bin/bash
#Allow numeric prefix to be set as an argument.
#Can use `-` to pass through and still pass additional commands.
if [ -n "$1" ] && [ "$1" != "-" ]
then
NUMERIC_PREFIX=$1
else
NUMERIC_PREFIX='0001'
fi
#Allow additional commands like compiling assets to be set as an argument.
if [[ -n "$2" ]]
then
ADDITIONAL_COMMANDS="$2;"
PROMPT_TEXT='run commands and restart'
else
ADDITIONAL_COMMANDS=''
PROMPT_TEXT='restart'
fi
PATCH_FILE_NAME="$(git format-patch HEAD~1)"
CURRENT_BRANCH_NAME="$(git rev-parse --abbrev-ref HEAD)"
NEW_PATCH_FILE_NAME="${NUMERIC_PREFIX}_${CURRENT_BRANCH_NAME/\//_}.patch"
echo "Renaming patch file to \`$NEW_PATCH_FILE_NAME\`."
mv $PATCH_FILE_NAME $NEW_PATCH_FILE_NAME
echo 'Sending patch files to app servers.'
scp -P 5000 $NEW_PATCH_FILE_NAME deploy@yourapp.one:/var/www/yourapp/current/
scp -P 5000 $NEW_PATCH_FILE_NAME deploy@yourapp.two:/var/www/yourapp/current/
scp -P 5000 $NEW_PATCH_FILE_NAME deploy@yourapp.three:/var/www/yourapp/current/
echo 'Applying patch to app server 1.'
ssh deploy@yourapp.one -p 5000 "cd dr; patch -p1 < $NEW_PATCH_FILE_NAME"
echo 'Applying patch to app server 2.'
ssh deploy@yourapp.two -p 5000 "cd dr; patch -p1 < $NEW_PATCH_FILE_NAME"
echo 'Applying patch to app server 3.'
ssh deploy@yourapp.three -p 5000 "cd dr; patch -p1 < $NEW_PATCH_FILE_NAME"
read -p "Press [Enter] to $PROMPT_TEXT app server 1 or Ctrl-C to exit."
ssh deploy@yourapp.one -p 5000 "cd dr; $ADDITIONAL_COMMANDS touch tmp/restart.txt"
read -p "Press [Enter] to $PROMPT_TEXT app server 2 or Ctrl-C to exit."
ssh deploy@yourapp.two -p 5000 "cd dr; $ADDITIONAL_COMMANDS touch tmp/restart.txt"
read -p "Press [Enter] to $PROMPT_TEXT on app server 3 or Ctrl-C to exit."
ssh deploy@yourapp.three -p 5000 "cd dr; $ADDITIONAL_COMMANDS touch tmp/restart.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment