Skip to content

Instantly share code, notes, and snippets.

@fchaussin
Last active May 17, 2023 13:21
Show Gist options
  • Save fchaussin/2a927d23936c426f9379bd4943d411ee to your computer and use it in GitHub Desktop.
Save fchaussin/2a927d23936c426f9379bd4943d411ee to your computer and use it in GitHub Desktop.
Manually deploy files, one by one with diff check
#!/bin/bash
# must be sudoer
[ `whoami` = root ] || { sudo "$0" "$@"; exit $?; }
# STD output colors // do not use for logs
RESTORE=$(echo -en '\033[0m')
GRAY=$(echo -en '\033[00;30m')
RED=$(echo -en '\033[00;91m')
GREEN=$(echo -en '\033[00;32m')
YELLOW=$(echo -en '\033[01;33m')
ORANGE=$(echo -en '\033[00;33m')
BLUE=$(echo -en '\033[01;34m')
CYAN=$(echo -en '\033[00;36m')
# BG colors
RED_BG=$(echo -en '\033[00;41m')
GREEN_BG=$(echo -en '\033[00;42m')
YELLOW_BG=$(echo -en '\033[00;43m')
CYAN_BG=$(echo -en '\033[00;46m')
sourcepath="/var/www/preprod.tld"
destinationpath="/var/www/production.tld"
sourcealias="PREPROD"
destinationalias="PROD"
logfile="/var/log/deploy/$(date +%Y%m)/deploy_$(date +%Y%m%d).log"
mkdir -p /var/log/deploy/$(date +%Y%m) >/dev/null 2>&1
# exit properly
trap 'echo -e "\n${ORANGE}EXIT${RESTORE} : Find logs in ${CYAN_BG}${logfile}${RESTORE} ${CYAN}Goodbye!${RESTORE}πŸ‘‹"; exit 0;' 2 3
if [ "$PWD" != "$sourcepath" ]; then
echo "${GRAY}You must deploy from ${BLUE}${sourcepath}${RESTORE}";
pwd;
exit
fi
argfile=false
if [[ -n $1 ]]; then
argfile=true
fi
echo "${GRAY}/\______ . . |\_ . ../|";
echo "\__ \ /|__ ____|\ | | /|__ ___ _/|| |";
echo " | |\ \_/ \ _ \| | / _ \ | || |";
echo " _| |_) \ Β°__/| |_) | |_( (_) \___ ||_\\";
echo "/_______ /\___ \| __/|____/\_____/ ____|(_)";
echo " \/ \/|__| \/${RESTORE}";
# loop file deploy
while true; do
# loop files exist each sides
while true; do
foundsource=false
founddest=false
# use arg or prompt filepath
if [ $argfile == false ]; then
echo -e ">>> Enter the relative path name of the file you want to deploy. πŸ™ ${GRAY}[Ctrl+C] to exit${RESTORE} :"
read -e -p "${GRAY}$sourcepath${RESTORE}/${BLUE}" filerelativepath
echo "${RESTORE}"
else
filerelativepath=$1
argfile=false
fi
# is it directory?
if [ -d $filerelativepath ]; then
echo "This script doesn't deploy full directories, here the diffs :"
echo "diff -qr ${YELLOW}$destinationpath/$filerelativepath${RESTORE} ${BLUE}$filerelativepath${RESTORE}"
diff -qr "$destinationpath/$filerelativepath" "$filerelativepath"
if [ $? == 0 ]; then
echo "${GREEN}No diff${RESTORE}"
fi
continue
fi
# file exists each sides?
if [ -f "$sourcepath/$filerelativepath" ]; then
foundsource=true
else
echo "${RED}$sourcepath/${RED_BG}$filerelativepath${RESTORE} not found or not a file in $sourcealias"
fi
if [ -f "$destinationpath/$filerelativepath" ]; then
founddest=true
else
echo "${RED}$destinationpath/${RED_BG}$filerelativepath${RESTORE} not found or not a file in $destinationalias"
fi
if [ $foundsource == false ] && [ $founddest == true ]; then
echo "File doesn't exists in $sourcealias impossible to deploy to $destinationalias."
echo "You should remove the file in $destinationalias manually :"
echo "${CYAN}sudo rm $destinationpath/$filerelativepath${RESTORE}"
fi
if [ $foundsource == true ]; then
break
fi
done
# show diff
echo "diff ${YELLOW}$destinationpath/$filerelativepath${RESTORE} ${BLUE}$filerelativepath${RESTORE}"
diff --color "$destinationpath/$filerelativepath" "$filerelativepath"
if [ $? == 0 ]; then
echo "${GREEN}No diff${RESTORE}"
continue
fi
if [ $foundsource == true ] && [ $founddest == false ]; then
echo "Do yo want to create a new file in $destinationalias ?"
fi
# confirm deploy
read -p "> Deploy ${BLUE}$filerelativepath${RESTORE} to ${YELLOW}$destinationpath/$filerelativepath${RESTORE} ${GRAY}[Y/n]${RESTORE} " readytodeploy
if [ "$readytodeploy" == "Y" ]; then
sudo cp "$filerelativepath" "$destinationpath/$filerelativepath"
if [ $? != 0 ]; then
echo "The copy failed!"
echo "${YELLOW_BG}$filerelativepath${RESTORE} ${RED_BG}can not be deployed ⚠️ ${RESTORE} "
else
echo "${YELLOW_BG}$filerelativepath${RESTORE} ${GREEN_BG}has been deployed βœ… ${RESTORE}πŸš€"
echo -e "$(date +'%Y-%m-%d %H:%M:%S') : $filerelativepath" >> ${logfile}
fi
else
echo "Aborted! "
echo "${YELLOW_BG}$filerelativepath${RESTORE} ${RED_BG}has not been deployed 🚫 ${RESTORE}"
fi
ls -lah "$destinationpath/$filerelativepath"
continue
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment