2020-12-14 15:48:36 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# -e Exit immediately if a command exits with a non-zero status.
|
|
|
|
# -u Treat unset variables as an error when substituting.
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
START=`date +%s`
|
|
|
|
BACKUP_DATE=`date +%Y%m%d`
|
|
|
|
BACKUP_LOGS_FILENAME=${BACKUP_DATE}_${URL}_logs.tar
|
2021-04-29 13:57:02 +02:00
|
|
|
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" # https://stackoverflow.com/a/246128/6429616
|
2020-12-14 15:48:36 +01:00
|
|
|
|
|
|
|
logs_backup() {
|
|
|
|
echo "📥 Sauvegarde les logs"
|
|
|
|
mkdir -p backups
|
|
|
|
ssh $DOCKER_CONTEXT mkdir -p backups/${URL}
|
2021-02-07 01:17:25 +01:00
|
|
|
docker --context $DOCKER_CONTEXT run -t --rm -v $HOME/backups/${URL}:/backup -v ${NAME}-log:/logs alpine:3.12.3 ash -c "cd /logs && tar cvf /backup/${BACKUP_LOGS_FILENAME} ."
|
2020-12-14 15:48:36 +01:00
|
|
|
ssh $DOCKER_CONTEXT "sudo chown kosssi:docker $HOME/backups/${URL}/${BACKUP_LOGS_FILENAME}"
|
|
|
|
scp $DOCKER_CONTEXT:$HOME/backups/${URL}/${BACKUP_LOGS_FILENAME} backups
|
|
|
|
}
|
|
|
|
|
|
|
|
logs_restore() {
|
|
|
|
echo "📤 Restaure les logs"
|
|
|
|
ssh $DOCKER_CONTEXT mkdir -p backups/${URL}
|
|
|
|
scp backups/${BACKUP_LOGS_FILENAME} $DOCKER_CONTEXT:$HOME/backups/${URL}/
|
2021-02-07 01:17:25 +01:00
|
|
|
docker --context $DOCKER_CONTEXT run -t --rm -v $HOME/backups/${URL}:/backup -v ${NAME}-log:/logs alpine:3.12.3 ash -c "cd /logs && tar xvf /backup/${BACKUP_LOGS_FILENAME} --strip 1"
|
2020-12-14 15:48:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stats() {
|
|
|
|
echo "🌠 Mise à jour de GoAccess"
|
|
|
|
docker --context $DOCKER_CONTEXT exec -it ${STATS_CONTAINER} ash /bin/stats.sh generate
|
|
|
|
}
|
|
|
|
|
|
|
|
install() {
|
|
|
|
echo "📦 Installation des dépendances 📦"
|
|
|
|
echo "----------------------------------"
|
|
|
|
echo
|
|
|
|
echo "- Récupération du theme"
|
|
|
|
git submodule update --init
|
|
|
|
echo "- Installation des dépendances node du theme"
|
|
|
|
cd themes/hugo-theme-lowtech && npm i
|
|
|
|
}
|
|
|
|
|
|
|
|
dev() {
|
|
|
|
echo "🚧 Lancement du serveur pour le développement"
|
2021-04-27 14:49:35 +02:00
|
|
|
hugo server -DF
|
2020-12-14 15:48:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dev_prod() {
|
|
|
|
echo "🚧 Lancement du serveur pour le développement sans les brouillons"
|
|
|
|
hugo server
|
|
|
|
}
|
|
|
|
|
|
|
|
prod() {
|
|
|
|
echo
|
|
|
|
echo "🚀 Déploiement du site en mode production 🚀"
|
|
|
|
echo
|
|
|
|
rm -rf public
|
|
|
|
hugo --minify --environment production
|
|
|
|
node themes/hugo-theme-lowtech/scripts/typo
|
|
|
|
#DATE=`date +\"%Y0101\"` && find public -exec touch -d $DATE {} +
|
|
|
|
docker-compose --context $DOCKER_CONTEXT -f docker-compose.prod.yml up -d --build --force-recreate
|
|
|
|
}
|
|
|
|
|
|
|
|
staging() {
|
|
|
|
echo
|
|
|
|
echo "🚀 Déploiement du site en mode staging 🚀"
|
|
|
|
echo
|
|
|
|
rm -rf public
|
2021-04-27 14:49:35 +02:00
|
|
|
hugo --minify -DF --environment staging
|
2020-12-14 15:48:36 +01:00
|
|
|
node themes/hugo-theme-lowtech/scripts/typo
|
|
|
|
#DATE=`date +\"%Y0101\"` && find public -exec touch -d $DATE {} +
|
|
|
|
docker-compose --context $DOCKER_CONTEXT -f docker-compose.staging.yml up -d --build --force-recreate
|
|
|
|
}
|
|
|
|
|
2021-02-07 01:18:16 +01:00
|
|
|
favicons() {
|
|
|
|
echo
|
|
|
|
echo "🔍 Génération des favicons à partir d'une image 512px"
|
|
|
|
echo
|
|
|
|
convert static/logo.png -resize 32x32 -colors 16 \( -clone 0 -resize 16x16 -extent 16x16 \) \( -clone 0 -resize 32x32 -extent 32x32 \) -delete 0 static/favicon.ico
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:57:02 +02:00
|
|
|
optimizer() {
|
|
|
|
echo
|
|
|
|
echo "🖼️ Optimisation des images"
|
|
|
|
echo
|
|
|
|
# $SCRIPT_DIR/../node_modules/.bin/imagemin $PWD/static -o $PWD/static
|
2021-05-12 08:42:53 +02:00
|
|
|
find ./static ./content -type f \( -iname \*.jpg \) | xargs -I % bash -c '$0/../node_modules/.bin/imagemin % --plugin.webp.quality=95 > $(dirname %)/$(basename % | sed "s/\(.*\)\..*/\1/").webp' $SCRIPT_DIR
|
2021-04-29 13:57:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
optimizer_git_image() {
|
2021-04-29 21:55:43 +02:00
|
|
|
first_commit_hash=$(git rev-list --max-parents=0 HEAD)
|
|
|
|
|
|
|
|
# https://stackoverflow.com/a/48999882/6429616
|
|
|
|
# install git alias amend-to with auto stash
|
|
|
|
check_git_command=`git config --global --list | grep amend-to | wc -c`
|
|
|
|
if [ "$check_git_command" -ne 187 ]; then
|
|
|
|
git config --global alias.amend-to '!f() { SHA=`git rev-parse "$1"`; git stash -k && git commit --fixup "$SHA" && GIT_SEQUENCE_EDITOR=true git rebase --interactive --autosquash "$SHA^" && git stash pop; }; f'
|
|
|
|
fi
|
|
|
|
|
2021-04-29 13:57:02 +02:00
|
|
|
for file in $(git diff --name-only --diff-filter=M --ignore-submodules); do
|
2021-04-29 21:55:43 +02:00
|
|
|
last_file_update_commit_hash=`git log -n 1 --pretty=format:%H -- $file`
|
2021-04-29 13:57:02 +02:00
|
|
|
current_size=`cat $file | wc -c`
|
2021-04-29 21:55:43 +02:00
|
|
|
old_size=`git cat-file -p $last_file_update_commit_hash:$file | wc -c`
|
2021-04-29 13:57:02 +02:00
|
|
|
|
2021-04-29 21:55:43 +02:00
|
|
|
if [ "$first_commit_hash" == "$last_file_update_commit_hash" ]; then
|
|
|
|
echo "💩 file '$file' is on first commit..."
|
|
|
|
elif [ "$current_size" -lt "$old_size" ]; then
|
2021-04-29 13:57:02 +02:00
|
|
|
git add $file
|
2021-04-29 21:55:43 +02:00
|
|
|
git amend-to $last_file_update_commit_hash
|
2021-04-29 22:02:56 +02:00
|
|
|
else
|
|
|
|
echo "😳 file '$file' is bigger than original file..."
|
2021-04-29 13:57:02 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-12-14 15:48:36 +01:00
|
|
|
help_generic() {
|
2020-12-21 11:58:17 +01:00
|
|
|
echo
|
|
|
|
echo "💡 Aide 💡"
|
|
|
|
echo "----------"
|
|
|
|
echo
|
|
|
|
echo "Commandes :"
|
2020-12-14 15:48:36 +01:00
|
|
|
echo "- ./run install 📦 Installation des dépendances"
|
|
|
|
echo "- ./run dev 🚧 Lancement du serveur pour le développement"
|
|
|
|
echo "- ./run dev_prod 🚧 Lancement du serveur pour le développement sans les brouillons"
|
|
|
|
echo "- ./run prod 🚀 Déploiement du site en mode production"
|
|
|
|
echo "- ./run staging 🚀 Déploiement du site en mode staging"
|
|
|
|
echo "- ./run logs_backup 📥 Sauvegarde les logs"
|
|
|
|
echo "- ./run logs_restore 📤 Restaure les logs"
|
|
|
|
echo "- ./run stats 🌠 Mise à jour de GoAccess"
|
2021-02-07 01:18:16 +01:00
|
|
|
echo "- ./run favicons 🔍 Génération des favicons à partir d'une image 512px"
|
2021-04-29 13:57:02 +02:00
|
|
|
echo "- ./run optimizer 🖼️ Optimisation des images"
|
2020-12-14 15:48:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
end() {
|
|
|
|
END=`date +%s`
|
|
|
|
echo
|
|
|
|
echo "✨ Done in $((END-START))s"
|
|
|
|
echo
|
|
|
|
}
|