diff --git a/bin/rename-env-vars-5-0 b/bin/rename-env-vars-5-0 new file mode 100755 index 0000000..38ecb34 --- /dev/null +++ b/bin/rename-env-vars-5-0 @@ -0,0 +1,44 @@ +#! /usr/bin/env bash +# shellcheck source-path=.. + +set -euo pipefail + +#### 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#./}" +} +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 + +source "$TOOLKIT_ROOT/lib/shared-functions.sh" + +function usage() { + echo "Usage: bin/update-env" + echo "" + echo "Updates config/variables.env, adding new environment variables" + echo "" +} + +function __main__() { + if [[ "${1:-null}" == "help" ]] || [[ "${1:-null}" == "--help" ]]; then + usage + exit + fi + + # read_image_version + echo "This script will update your config/variables.env." + echo "We recommend backing up your config with bin/backup-config." + prompt "Do you want to continue?" + + rebrand_sharelatex_env_variables + + echo "Done." +} + +__main__ "$@" diff --git a/bin/up b/bin/up index 99317b8..35b583d 100755 --- a/bin/up +++ b/bin/up @@ -35,6 +35,22 @@ function check_config() { fi } +function check_sharelatex_env_vars() { + local invalid_prefix="SHARELATEX_" + if [[ "$IMAGE_VERSION_MAJOR" -lt 5 ]]; then + invalid_prefix="OVERLEAF_" + fi + + if grep -q "$invalid_prefix" "$TOOLKIT_ROOT/config/variables.env"; then + echo "WARNING: some environment variables defined in config/variables.env" + echo "contain '$invalid_prefix' in their name" + echo "Starting with Overleaf CE and Server Pro 5.0.0 the environment variables" + echo "use the prefix 'OVERLEAF_' instead of 'SHARELATEX_'." + echo "Please check your config/version and config/variables.env files" + prompt "Do you want to continue?" + fi +} + function initiate_mongo_replica_set() { echo "Initiating Mongo replica set..." "$TOOLKIT_ROOT/bin/docker-compose" up -d mongo @@ -52,9 +68,10 @@ function __main__() { exit fi - check_config - read_config read_image_version + check_config + check_sharelatex_env_vars + read_config if [[ "$MONGO_ENABLED" == "true" && "$IMAGE_VERSION_MAJOR" -ge 4 ]]; then initiate_mongo_replica_set diff --git a/bin/upgrade b/bin/upgrade index 1a7d9c6..5f0a645 100755 --- a/bin/upgrade +++ b/bin/upgrade @@ -15,6 +15,8 @@ if [[ ! -d "$TOOLKIT_ROOT/bin" ]] || [[ ! -d "$TOOLKIT_ROOT/config" ]]; then exit 1 fi +source "$TOOLKIT_ROOT/lib/shared-functions.sh" + function usage() { echo "Usage: bin/upgrade" echo "" @@ -87,22 +89,27 @@ function show_changes() { echo "----------" } -function handle_image_upgrade() { - local user_image_version - user_image_version="$(head -n 1 "$TOOLKIT_ROOT/config/version")" - local seed_image_version - seed_image_version="$(head -n 1 "$TOOLKIT_ROOT/lib/config-seed/version")" +function read_seed_image_version() { + SEED_IMAGE_VERSION="$(head -n 1 "$TOOLKIT_ROOT/lib/config-seed/version")" + if [[ ! "$SEED_IMAGE_VERSION" =~ ^([0-9]+)\.([0-9]+)\.[0-9]+(-RC[0-9]*)?(-with-texlive-full)?$ ]]; then + echo "ERROR: invalid config-seed/version '${SEED_IMAGE_VERSION}'" + exit 1 + fi + SEED_IMAGE_VERSION_MAJOR=${BASH_REMATCH[1]} + SEED_IMAGE_VERSION_MINOR=${BASH_REMATCH[2]} +} - if [[ ! "$seed_image_version" > "$user_image_version" ]]; then +function handle_image_upgrade() { + if [[ ! "$SEED_IMAGE_VERSION" > "$IMAGE_VERSION" ]]; then echo "No change to docker image version" return 0 fi - echo "New docker image version available ($seed_image_version)" - echo "Current image version is '$user_image_version' (from config/version)" + echo "New docker image version available ($SEED_IMAGE_VERSION)" + echo "Current image version is '$IMAGE_VERSION' (from config/version)" - local user_image_major_version="$(echo "$user_image_version" | awk -F. '{print $1}')" - local seed_image_major_version="$(echo "$seed_image_version" | awk -F. '{print $1}')" + local user_image_major_version="$(echo "$IMAGE_VERSION" | awk -F. '{print $1}')" + local seed_image_major_version="$(echo "$SEED_IMAGE_VERSION" | awk -F. '{print $1}')" if [[ "$seed_image_major_version" > "$user_image_major_version" ]]; then echo "WARNING: this is a major version update, please check the Release Notes for breaking changes before proceeding:" echo "* https://github.com/overleaf/overleaf/wiki#release-notes" @@ -112,11 +119,11 @@ function handle_image_upgrade() { read -r -p "Upgrade image? [y/n] " should_upgrade if [[ ! "$should_upgrade" =~ [Yy] ]]; then - echo "Keeping image version '$user_image_version'" + echo "Keeping image version '$IMAGE_VERSION'" return 0 fi - echo "Upgrading config/version from $user_image_version to $seed_image_version" + echo "Upgrading config/version from $IMAGE_VERSION to $SEED_IMAGE_VERSION" ## Offer to stop docker services local services_stopped="false" @@ -148,9 +155,15 @@ function handle_image_upgrade() { ## Set the new image version echo "Backing up old version file to config/__old-version" cp "$TOOLKIT_ROOT/config/version" "$TOOLKIT_ROOT/config/__old-version" - echo "Over-writing config/version with $seed_image_version" + echo "Over-writing config/version with $SEED_IMAGE_VERSION" cp "$TOOLKIT_ROOT/lib/config-seed/version" "$TOOLKIT_ROOT/config/version" + if [[ "$IMAGE_VERSION_MAJOR" -le 4 && "$SEED_IMAGE_VERSION_MAJOR" -ge 5 ]]; then + echo "Renaming environment variables in config/variables.env" + prompt "Proceed with the environment variable renaming?" + rebrand_sharelatex_env_variables + fi + ## Maybe offer to start services again if [[ "${services_stopped:-null}" == "true" ]]; then local should_start="n" @@ -202,8 +215,13 @@ function __main__() { || [[ "${1:-null}" == "--help" ]] ; then usage && exit fi + handle_git_update + + read_seed_image_version + read_image_version handle_image_upgrade + echo "Done" exit 0 } diff --git a/lib/shared-functions.sh b/lib/shared-functions.sh index 825e458..8eb7e0b 100644 --- a/lib/shared-functions.sh +++ b/lib/shared-functions.sh @@ -17,3 +17,28 @@ function read_image_version() { IMAGE_VERSION_MAJOR=${BASH_REMATCH[1]} IMAGE_VERSION_MINOR=${BASH_REMATCH[2]} } + +prompt() { + read -p "$1 (y/n): " choice + if [[ ! "$choice" =~ [Yy] ]]; then + echo "Exiting." + exit 1 + fi +} + +rebrand_sharelatex_env_variables() { + set +o pipefail + sharelatex_occurrences=$(grep -o "SHARELATEX_" "$TOOLKIT_ROOT/config/variables.env" | wc -l | sed 's/ //g') + set -o pipefail + if [ "$sharelatex_occurrences" -gt 0 ]; then + echo "Found $sharelatex_occurrences lines with SHARELATEX_" + local timestamp=$(date "+%Y.%m.%d-%H.%M.%S") + echo "Creating backup file config/__old-variables.env.$timestamp" + cp config/variables.env config/__old-variables.env.$timestamp + echo "Replacing 'SHARELATEX_' with 'OVERLEAF_' in config/variables.env" + sed -i "s/SHARELATEX_/OVERLEAF_/g" "$TOOLKIT_ROOT/config/variables.env" + echo "Updated $sharelatex_occurrences lines in $TOOLKIT_ROOT/config/variables.env" + else + echo "No 'SHARELATEX_' ocurrences found in config/variables.env" + fi +}