Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 111 lines (87 sloc) 2.38 KB
#!/bin/sh
set -o nounset -o errexit
# Author: Niklas Laxström
# License: GPL-2.0-or-later
COMMAND=$1
if [ ! -d workdir ]; then
echo "This is not a valid oregano environment."
echo "Please create a directory named workdir."
COMMAND=""
fi
case $COMMAND in
deploy)
mkdir -p targets tags
# Default to newest tag
TAG=${2:-$(ls -1t tags | head -n1)}
ENV=${3:-production}
OLD=$(readlink "targets/$ENV" || :)
if [ -z "$TAG" ]; then
echo "Unable to find any tags"
exit 1
fi
if [ ! -d "tags/$TAG" ]; then
echo "Tag '$TAG' does not exist"
exit 1
fi
echo "Updating targets/$ENV to '$TAG'"
# Update symlink atomically
ln -s "../tags/$TAG" "targets/$ENV.new"
mv -T "targets/$ENV.new" "targets/$ENV"
# Update symlink to previous version for rollback
if [ -n "$OLD" ]; then
rm -f "targets/${ENV}_previous"
ln -s "$OLD" "targets/${ENV}_previous"
fi
if [ -x "hooks/deploy" ]; then
./hooks/deploy "$TAG" "$ENV"
fi
;;
rollback)
mkdir -p targets
ENV=${2:-production}
TAG=$(readlink "targets/${ENV}_previous" || :)
TAG_NAME=$(basename "$TAG")
if [ -n "$TAG" ]; then
echo "Updating targets/$ENV to previously deployed '$TAG_NAME'"
rm -f "targets/${ENV}_previous"
ln -s "$TAG" "targets/$ENV.new"
mv -T "targets/$ENV.new" "targets/$ENV"
if [ -x "./hooks/deploy" ]; then
./hooks/deploy "$TAG_NAME" "$ENV"
fi
else
echo "Unable to rollback. Either already rollbacked or first deployment"
fi
;;
tag)
mkdir -p tags
MAX=4
TAG=$(date --utc +%F_%T)
# Multple tags created from unchanged workdir would have same date
# and sort in incorrect order
touch workdir
if rsync --version | grep -q 'version 3\.1\.'; then
rsync -a --info=progress2 --cvs-exclude workdir/ --chmod=a-w "tags/$TAG"
else
rsync -a --cvs-exclude workdir/ --chmod=a-w "tags/$TAG"
fi
echo "Created tag $TAG"
cd tags/
ls -1tr | head -n-$MAX | xargs --no-run-if-empty -d"\n" echo "Pruning"
# Need to restore the write permissions we removed
ls -1tr | head -n-$MAX | xargs --no-run-if-empty -d"\n" chmod -R ug+w
ls -1tr | head -n-$MAX | xargs --no-run-if-empty -d"\n" rm -r
cd ..
if [ -x "hooks/tag" ]; then
./hooks/tag "$TAG"
fi
;;
*)
echo "
Usage:
$0 tag [name] - creates new tag
$0 deploy [tag] [target] - deploys tag (default latest) to the target (default production)
$0 rollback [target] - rollbacks to previously deployed tag in the target (default production)
"
;;
esac
You can’t perform that action at this time.