hugo-theme-lowtech/scripts/run

133 lines
4.7 KiB
Plaintext
Raw Normal View History

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"
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
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
find ./static ./content -type f | xargs -I % bash -c '$0/../node_modules/.bin/imagemin % -o $(dirname %)' $SCRIPT_DIR
}
optimizer_git_image() {
for file in $(git diff --name-only --diff-filter=M --ignore-submodules); do
last_commit=`git log -n 1 --pretty=format:%H -- $file`
current_size=`cat $file | wc -c`
old_size=`git cat-file -p $last_commit:$file | wc -c`
# https://stackoverflow.com/a/48999882/6429616
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
if [ "$current_size" -lt "$old_size" ]; then
git add $file
git amend-to $last_commit
fi
done
}
2020-12-14 15:48:36 +01:00
help_generic() {
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
}