puppet-mastodon/files/update_mastodon.sh

161 lines
5.3 KiB
Bash
Raw Normal View History

2024-02-16 13:01:55 +01:00
#!/bin/bash
set -euo pipefail
tag=latest
dummy=false
no_backup=false
reinstall=false
2024-02-16 13:01:55 +01:00
token=""
function usage() {
echo "Usage:"
echo "$(basename "${0}") [-t|--tag <TAG>] [-T|--token <GITHUB_TOKEN>] [-d|--debug]"
echo ""
echo " -d|--debug Show more debug information."
echo " -t|--tag <TAG> Release tag or 'latest' (default)."
echo " -T|--token <TOKEN> Github token with read access to public repositories."
echo " -D|--dummy|--dry-run Run in Dry-mode without actually updating but showing the commands to execute."
echo " -r|--reinstall Allow to reinstall the same tag."
echo " -n|--no-backup Don't do a backup of the database before the upgrade."
2024-02-16 13:01:55 +01:00
}
while [ $# -gt 0 ]
do
case "${1}" in
"-h"|"-?"|"--help")
shift
usage
exit 0
;;
"-d"|"--debug")
shift
export DEBUG=true
;;
"-t"|"--tag")
shift
tag="${1}"
shift
;;
"-T"|"--token")
shift
token="${1}"
shift
;;
'-D'|'--dummy'|'--dry-run')
shift
dummy=true
;;
'-n'|'--no-backup')
shift
no_backup=true
;;
'-r'|'--reinstall')
shift
reinstall=true
;;
2024-02-16 13:01:55 +01:00
*)
message "Unknown parameter '$1'" p
shift
;;
esac
done
if [ -z "${token}" ]; then
echo "You must provide a Github token"
exit 4
fi
2024-02-16 13:01:55 +01:00
if [ "${tag}" == "latest" ]; then
2024-02-16 17:01:23 +01:00
tag=$(curl -L -s -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${token}" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/mastodon/mastodon/releases/latest | jq '.tag_name' | sed 's/"//g')
2024-02-16 14:19:29 +01:00
if [ "${DEBUG}" == "true" ]; then
echo "Latest tag is '${tag}'"
fi
2024-02-16 13:01:55 +01:00
else
if [ "${DEBUG}" == "true" ]; then
echo "Looking for tag '${tag}'..."
fi
2024-02-16 13:16:28 +01:00
next_page=https://api.github.com/repos/mastodon/mastodon/releases
releases_file=$(mktemp /tmp/tmp.XXXX)
while [ -z "${next_page}" ]
do
tmpfile=$(mktemp /tmp/tmp.XXXXX)
2024-02-16 13:19:45 +01:00
releases=$(curl -L -v -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${token}" -H "X-GitHub-Api-Version: 2022-11-28" "${next_page}" | jq '.[]|.tag_name' 2> "${tmpfile}")
2024-02-16 13:16:28 +01:00
echo "${releases}" >> "${releases_file}"
next_page=$(grep '< link: ' "${tmpfile}" | sed 's/< link: //g' | sed 's/, /\n/g' | grep 'rel="next"' | grep -o 'https://[^>]*')
done
if ! pgrep "${tag}" "${releases_file}"; then
2024-02-16 14:19:29 +01:00
printf "The tag '%s' is not part of the releases:\n%s" "${tag}" "${releases}"
2024-02-16 13:01:55 +01:00
exit 1
fi
fi
2024-02-16 17:26:54 +01:00
mastodon_home="$(grep "^mastodon:" /etc/passwd | awk 'BEGIN {FS=":"} {print($6)}')"
2024-02-16 13:01:55 +01:00
if [ ! -d "${mastodon_home}" ]; then
printf "The home for the user mastodon '%s', doesn't exist." "${mastodon_home}"
exit 2
fi
2024-02-16 14:19:29 +01:00
if [ "${DEBUG}" == "true" ]; then
echo "Changing to Mastodon's home directory '${mastodon_home}'..."
fi
2024-02-16 17:26:54 +01:00
cd "${mastodon_home}/live" || exit 3
2024-02-16 17:34:11 +01:00
# shellcheck disable=SC1091
source "${mastodon_home}/.profile"
2024-02-16 16:59:39 +01:00
current_tag=$(git name-rev --name-only HEAD | awk 'BEGIN {FS="/"} {print($2)}')
if [ "${current_tag}" != "${tag}" ] || [ "${reinstall}" ]; then
2024-02-16 14:19:29 +01:00
if [ "${DEBUG}" == "true" ]; then
2024-02-16 16:59:39 +01:00
echo "Fetching from repository..."
2024-02-16 14:19:29 +01:00
fi
2024-02-16 17:06:29 +01:00
if [ "${dummy}" == "true" ]; then
echo sudo -u mastodon git fetch
else
sudo -u mastodon git fetch
fi
2024-02-16 16:59:39 +01:00
if [ "${DEBUG}" == "true" ]; then
echo "Changing to reference '${tag}'..."
fi
2024-02-16 17:06:29 +01:00
if [ "${dummy}" == "true" ]; then
echo sudo -u mastodon git checkout "${tag}"
else
sudo -u mastodon git checkout "${tag}"
fi
if [ "${no_backup}" == "false" ]; then
current_date=$(date +%Y-%m-%d-%H-%M-%S)
mkdir -p /var/backups/postgres
backup_file="/var/backups/postgres/pgdump_pre_update_mastodon_to_${tag}_${current_date}.sql.gz"
if [ "${dummy}" == "true" ]; then
echo "sudo -u postgres pg_dump mastodon | gzip -c > \"${backup_file}\""
else
if [ "${DEBUG}" == "true" ]; then
echo "Creating a backup of the database in '${backup_file}'..."
fi
sudo -u postgres pg_dump mastodon | gzip -c > "${backup_file}"
2024-02-16 16:59:39 +01:00
fi
fi
if [ "${DEBUG}" == "true" ]; then
echo "Running bundle install..."
fi
2024-02-16 17:06:29 +01:00
if [ "${dummy}" == "true" ]; then
2024-02-16 17:34:11 +01:00
echo sudo -u mastodon "${mastodon_home}/.rbenv/shims/bundle" install
2024-02-16 17:06:29 +01:00
else
2024-02-16 17:34:11 +01:00
sudo -u mastodon "${mastodon_home}/.rbenv/shims/bundle" install
2024-02-16 17:06:29 +01:00
fi
2024-02-16 16:59:39 +01:00
if [ "${DEBUG}" == "true" ]; then
echo "Running yarm install..."
fi
2024-02-16 17:06:29 +01:00
if [ "${dummy}" == "true" ]; then
2024-02-16 17:34:11 +01:00
echo sudo -u mastodon "${mastodon_home}/live/bin/yarn" install --frozen-lockfile
2024-02-16 17:06:29 +01:00
else
2024-02-16 17:34:11 +01:00
sudo -u mastodon "${mastodon_home}/live/bin/yarn" install --frozen-lockfile
2024-02-16 17:06:29 +01:00
fi
2024-02-16 16:59:39 +01:00
if [ "${DEBUG}" == "true" ]; then
echo "Restarting Mastodon services..."
fi
2024-02-16 17:06:29 +01:00
if [ "${dummy}" == "true" ]; then
echo sudo systemctl restart mastodon-sidekiq.service mastodon-web.service mastodon-streaming mastodon-streaming@4000.service
else
sudo systemctl restart mastodon-sidekiq.service mastodon-web.service mastodon-streaming mastodon-streaming@4000.service
fi
2024-02-16 16:59:39 +01:00
else
echo "Current branch is already '${tag}'"
exit 0
fi