1
0
Fork 0
mirror of https://github.com/overleaf/toolkit.git synced 2025-04-19 23:38:06 +02:00
overleaf-toolkit/bin/upgrade

155 lines
4.7 KiB
Text
Raw Permalink Normal View History

2020-07-10 09:27:50 +01:00
#! /usr/bin/env bash
set -euo pipefail
2020-07-16 10:56:44 +01:00
2020-07-16 10:45:46 +01:00
#### Detect Toolkit Project Root ####
# if realpath is not available, create a semi-equivalent function
command -v realpath >/dev/null 2>&1 || realpath() {
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}
2020-07-16 10:45:46 +01:00
SCRIPT_PATH="$(realpath "${BASH_SOURCE[0]}")"
SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
TOOLKIT_ROOT="$(realpath "$SCRIPT_DIR/..")"
if [[ ! -d "$TOOLKIT_ROOT/bin" ]] || [[ ! -d "$TOOLKIT_ROOT/config" ]]; then
echo "ERROR: could not find root of overleaf-toolkit project (inferred project root as '$TOOLKIT_ROOT')"
exit 1
fi
2020-07-10 09:27:50 +01:00
function usage() {
echo "Usage: bin/upgrade"
2020-07-14 14:02:53 +01:00
echo ""
echo "This script will check for updates to the toolkit code (via git),"
echo "and offer to pull the new changes. It will then check the latest"
echo "available version of the docker image, and offer to update the"
echo "locally configured image (in config/image) if applicable"
echo ""
echo "This script will prompt the user for confirmation at"
echo "each step along the way."
2020-07-10 09:27:50 +01:00
}
2020-07-14 09:40:30 +01:00
function services_up() {
2020-07-16 10:45:46 +01:00
top_output="$("$TOOLKIT_ROOT/bin/docker-compose" top)"
2020-07-14 09:40:30 +01:00
if [[ -z "$top_output" ]]; then
return 1
else
return 0
fi
}
function git_pull_available() {
branch="$1"
2020-07-16 10:45:46 +01:00
fetch_output="$(git -C "$TOOLKIT_ROOT" fetch --dry-run origin "$branch" 2>&1)"
filtered_fetch_output="$(echo "$fetch_output" | grep '\-> origin/'"$branch")"
if [[ -z "$filtered_fetch_output" ]]; then
2020-07-14 09:40:30 +01:00
return 1
else
return 0
fi
}
function handle_image_upgrade() {
2020-07-16 10:45:46 +01:00
user_image_version="$(head -n 1 "$TOOLKIT_ROOT/config/version")"
seed_image_version="$(head -n 1 "$TOOLKIT_ROOT/lib/config-seed/version")"
2020-07-14 09:40:30 +01:00
if [[ ! "$seed_image_version" > "$user_image_version" ]]; then
echo "No change to docker image version"
return 0
fi
2020-07-14 09:40:30 +01:00
echo "New docker image version available ($seed_image_version)"
echo "Current image version is '$user_image_version' (from config/version)"
2020-07-14 09:40:30 +01:00
should_upgrade="n"
read -r -p "Upgrade image? [y/n] " should_upgrade
2020-07-14 09:40:30 +01:00
if [[ ! "$should_upgrade" =~ [Yy] ]]; then
echo "Keeping image version '$user_image_version'"
return 0
fi
2020-07-14 09:40:30 +01:00
echo "Upgrading config/version from $user_image_version to $seed_image_version"
## Offer to stop docker services
services_stopped="false"
if services_up; then
echo "docker services are up, stop them first?"
should_stop="n"
read -r -p "Stop docker services? [y/n] " should_stop
if [[ ! "$should_stop" =~ [Yy] ]]; then
echo "exiting without stopping services"
exit 1
2020-07-14 09:40:30 +01:00
fi
services_stopped="true"
echo "Stopping docker services"
2020-07-16 10:45:46 +01:00
"$TOOLKIT_ROOT/bin/docker-compose" stop
fi
2020-07-14 09:40:30 +01:00
## Advise the user to take a backup
## (NOTE: we can't do this automatically because it will likely require
## sudo privileges. We leave it to the user to sort out for now)
2020-07-14 13:33:38 +01:00
echo "At this point, we recommend backing up your data before proceeding"
echo "!! WARNING: Only do this while the docker services are stopped!!"
should_proceed="n"
read -r -p "Proceed? [y/n] " should_proceed
if [[ ! "$should_proceed" =~ [Yy] ]]; then
echo "Not proceeding with upgrade"
return 1
fi
2020-07-14 09:40:30 +01:00
## Set the new image version
2020-07-14 13:33:38 +01:00
echo "Backing up old version file to config/__old-version"
2020-07-16 10:45:46 +01:00
cp "$TOOLKIT_ROOT/config/version" "$TOOLKIT_ROOT/config/__old-version"
echo "Over-writing config/version with $seed_image_version"
2020-07-16 10:45:46 +01:00
cp "$TOOLKIT_ROOT/lib/config-seed/version" "$TOOLKIT_ROOT/config/version"
## Maybe offer to start services again
if [[ "${services_stopped:-null}" == "true" ]]; then
should_start="n"
read -r -p "Start docker services again? [y/n] " should_start
if [[ "$should_start" =~ [Yy] ]]; then
echo "Starting docker services"
2020-07-16 10:45:46 +01:00
"$TOOLKIT_ROOT/bin/docker-compose" up -d
fi
2020-07-14 09:40:30 +01:00
fi
}
function handle_git_update() {
2020-07-16 10:45:46 +01:00
current_branch="$(git -C "$TOOLKIT_ROOT" rev-parse --abbrev-ref HEAD)"
current_commit="$(git -C "$TOOLKIT_ROOT" rev-parse --short HEAD)"
2020-07-14 09:40:30 +01:00
if [[ ! "$current_branch" == "master" ]]; then
2020-07-14 13:33:38 +01:00
echo "Warning: current branch is not master, '$current_branch' instead"
2020-07-14 09:40:30 +01:00
fi
echo "Checking for code update..."
if ! git_pull_available "$current_branch"; then
echo "No code update available"
else
echo "Code update available! (current commit is $current_commit)"
should_pull="n"
2020-07-21 10:23:06 +01:00
read -r -p "Proceed to pull update? [y/n] " should_pull
2020-07-14 09:40:30 +01:00
if [[ ! "$should_pull" =~ [Yy] ]]; then
2020-07-14 14:00:04 +01:00
echo "Continuing without pulling update"
2020-07-14 09:40:30 +01:00
else
echo "Pulling new code..."
2020-07-16 10:45:46 +01:00
git -C "$TOOLKIT_ROOT" pull origin "$current_branch"
2020-07-14 09:40:30 +01:00
fi
fi
}
2020-07-10 09:27:50 +01:00
function __main__() {
2020-07-14 14:04:24 +01:00
if [[ "${1:-null}" == "help" ]] \
|| [[ "${1:-null}" == "--help" ]] ; then
usage && exit
fi
2020-07-14 09:40:30 +01:00
handle_git_update
handle_image_upgrade
2020-07-14 13:33:38 +01:00
echo "Done"
2020-07-10 09:27:50 +01:00
exit 0
}
__main__ "$@"