Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@matthewhochler
Last active January 4, 2017 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewhochler/9e2ad12135b0f7a0cff6779fb9b8e3a9 to your computer and use it in GitHub Desktop.
Save matthewhochler/9e2ad12135b0f7a0cff6779fb9b8e3a9 to your computer and use it in GitHub Desktop.
Sync RapidSOS repo with dev box
# sync-repo
# Dependencies:
# rsync (https://rsync.samba.org)
############################## BEGIN APP CONFIG ###############################
# You should only have to modify this section
# All paths are relative to your home
# Name of project to sync (used for notifications)
PROJECT_NAME=RapidSOS
# Path of your local repo
# Must not end with a forward slash
LOCAL_DIR=Code/rapidsos
# Path of your remote repo (relative to your remote home)
# Must not end with a forward slash
REMOTE_DIR=git/rapidsos
# SSH host of remote (might need to include username@)
REMOTE_HOST=dev
# Y'know, the log file
LOG_FILE=log/sync-rapidsos
# Commands to be run on remote after file sync
# Edit in-between the "BEGIN" and "END" comments
read -r -d '' REMOTE_CMD <<- EOM
set -e
pushd ~ >/dev/null
##### BEGIN REMOTE COMMANDS
sudo -H pip install -r "$REMOTE_DIR/requirements.txt"
bash -ic raprestart
##### END REMOTE COMMANDS
popd >/dev/null
EOM
############################## END APP CONFIG #################################
# Init logging funcions
logify() {
# Log to stdout and log file
echo -e "$1" | tee -a "$LOG_FILE"
}
notify() {
# Log to stdout, log file, and system notification service
# (Currently only supports macOS)
logify "$1"
if [ "$(uname)" == "Darwin" ]; then
osascript -e \
"display notification \"$1\" with title \"$PROJECT_NAME Sync\""
fi
}
# Run all commands from the home directory
pushd ~ >/dev/null
# Create log file
mkdir -p $(dirname "$LOG_FILE")
logify "$(date)"
logify "Logging to $(readlink -f $LOG_FILE)"
# Sync files to remote
notify "Syncing files..."
touch $HOME/.gitignore
rsync \
-azv \
--delete \
--delete-after \
--delete-excluded \
--force-delete \
--exclude=".git*" \
--exclude-from=$LOCAL_DIR/.gitignore \
--exclude-from=$HOME/.gitignore \
--log-file="$LOG_FILE" \
$LOCAL_DIR/ \
$REMOTE_HOST:$REMOTE_DIR \
>>/dev/null \
2>>"$LOG_FILE"
# If file sync failed, log and exit
sync_code=$?
if [[ $sync_code != 0 ]]; then
notify "File sync failed! :("
logify "Check logs at $LOG_FILE"
exit $sync_code
fi
# If remote commands set, run them
if [ -n "$REMOTE_CMD" ]; then
# Run commands on remote
notify "Running remote commands..."
ssh $REMOTE_HOST "$REMOTE_CMD" >>"$LOG_FILE" 2>>"$LOG_FILE"
# If remote commands failed, log and exit
remotecmd_code=$?
if [[ $remotecmd_code != 0 ]]; then
notify "Remote commands failed! :("
logify "Check logs at $LOG_FILE"
exit $remotecmd_code
fi
fi
# Success! Log it and exit
notify "Project synced successfuly"
echo >>"$LOG_FILE"
popd >/dev/null
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment