pages/deploy.sh

56 lines
1.4 KiB
Bash

#! /bin/bash
#
# This script reads a gzipped tar archive from stdin and deploys it to a
# specified location. Contents of the archive are assumed to be relative to
# some root, and will be deployed relative to the specified path in the first argument.
#
# Example usage:
# tar -C <deploy_root> -cf - <list of files> | ./deploy.sh <path>
# Abort deploy on any mistake
set -e
WEB_ROOT=/var/www/pages.abstractnonsen.se/
STORAGE_ROOT=/var/lib/pages/
PAGES_USER=pages
if [ "$#" -ne 1 ]; then
echo "Usage: $0 DEPLOY_SUBDIR"
exit 1
fi
if [ "$(whoami)" != "${PAGES_USER}" ]; then
echo "Script must be run as user '${PAGES_USER}'"
exit 1
fi
LINK="${WEB_ROOT}/$1"
LINK_DIRECTORY=$(dirname "${LINK}")
STORAGE_NEW="${STORAGE_ROOT}/$1/$(date +%s)"
# ensure root paths exist
mkdir -p "${LINK_DIRECTORY}"
mkdir -p "${STORAGE_NEW}"
# extract to new storage location (this reads from stdin)
echo "Extracting tar files..."
tar -C "${STORAGE_NEW}" -xvzf -
echo "...extracting done"
if [ ! -e "${LINK}" ]; then
# link does not exist, create it
ln -s "${STORAGE_NEW}" "${LINK}"
else
# read old link
STORAGE_OLD=$(readlink "${LINK}")
# link already exists, repoint it
rm -f "${LINK_DIRECTORY}/tmp"
ln -s "${STORAGE_NEW}" "${LINK_DIRECTORY}/tmp"
mv -T "${LINK_DIRECTORY}/tmp" "${LINK}"
# remove old deployment
rm -rf "${STORAGE_OLD}"
fi
echo "Successfully deployed to https://pages.abstractnonsen.se/$1"