56 lines
1.6 KiB
Bash
56 lines
1.6 KiB
Bash
#!/bin/bash
|
|
set -euo pipefail
|
|
umask 022
|
|
|
|
releases_dir="/var/www/html/randomdrift/.EACompanion-releases"
|
|
publish_dir="/var/www/html/randomdrift/.EACompanion-publish"
|
|
public_link="${publish_dir}/current"
|
|
original_command="${SSH_ORIGINAL_COMMAND:-}"
|
|
|
|
if [[ ! "${original_command}" =~ ^deploy\ ([0-9a-f]{40})$ ]]; then
|
|
echo "Invalid deployment command" >&2
|
|
exit 1
|
|
fi
|
|
|
|
revision="${BASH_REMATCH[1]}"
|
|
release_dir="${releases_dir}/${revision}"
|
|
|
|
exec 9>/var/lib/eacompanion/deploy.lock
|
|
flock -n 9 || exit 1
|
|
|
|
if [[ ! -f "${release_dir}/index.html" ]]; then
|
|
archive_path="$(mktemp /var/lib/eacompanion/artifact.XXXXXX.tar.gz)"
|
|
staging_dir="$(mktemp -d "${releases_dir}/.incoming.XXXXXX")"
|
|
|
|
cleanup() {
|
|
case "${archive_path}" in
|
|
/var/lib/eacompanion/artifact.*.tar.gz) rm -f -- "${archive_path}" ;;
|
|
esac
|
|
case "${staging_dir}" in
|
|
"${releases_dir}"/.incoming.*) rm -rf -- "${staging_dir}" ;;
|
|
esac
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
cat > "${archive_path}"
|
|
tar --extract --gzip --file "${archive_path}" --directory "${staging_dir}" \
|
|
--no-same-owner --no-same-permissions
|
|
|
|
if [[ ! -f "${staging_dir}/index.html" ]]; then
|
|
echo "Artifact does not contain index.html" >&2
|
|
exit 1
|
|
fi
|
|
if find "${staging_dir}" -type l -print -quit | grep -q .; then
|
|
echo "Artifact contains a symbolic link" >&2
|
|
exit 1
|
|
fi
|
|
|
|
chmod -R a+rX,u+w "${staging_dir}"
|
|
mv "${staging_dir}" "${release_dir}"
|
|
rm -f -- "${archive_path}"
|
|
trap - EXIT
|
|
fi
|
|
|
|
temporary_link="${public_link}.new"
|
|
ln -sfn "${release_dir}" "${temporary_link}"
|
|
mv -Tf "${temporary_link}" "${public_link}"
|