1
0
Fork 0
mirror of https://github.com/overleaf/toolkit.git synced 2025-04-19 07:18:06 +02:00

Use local for bash vars, as much as possible

This commit is contained in:
Shane Kilkelly 2020-07-30 09:52:43 +01:00
parent d007d7f1f6
commit 15d2c36d86
4 changed files with 39 additions and 37 deletions

View file

@ -18,13 +18,13 @@ fi
RC_FILE="$TOOLKIT_ROOT/config/overleaf.rc"
function __main__() {
SHARELATEX_IMAGE_VERSION="$(head -n 1 "$TOOLKIT_ROOT/config/version")"
MONGO_IMAGE="mongo:3.6"
REDIS_IMAGE="redis:5.0"
local SHARELATEX_IMAGE_VERSION="$(head -n 1 "$TOOLKIT_ROOT/config/version")"
local MONGO_IMAGE="mongo:3.6"
local REDIS_IMAGE="redis:5.0"
MONGO_URL="mongodb://mongo/sharelatex"
REDIS_HOST="redis"
REDIS_PORT="6379"
local MONGO_URL="mongodb://mongo/sharelatex"
local REDIS_HOST="redis"
local REDIS_PORT="6379"
if [[ ! "$SHARELATEX_IMAGE_VERSION" \
=~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
@ -37,7 +37,7 @@ function __main__() {
source "$RC_FILE"
# Select which docker-compose files to load
compose_file_flags=("-f $TOOLKIT_ROOT/lib/docker-compose.base.yml")
local compose_file_flags=("-f $TOOLKIT_ROOT/lib/docker-compose.base.yml")
if [[ "$REDIS_ENABLED" == "true" ]]; then
compose_file_flags+=("-f $TOOLKIT_ROOT/lib/docker-compose.redis.yml")
fi
@ -54,14 +54,14 @@ function __main__() {
fi
# Build up the flags to pass to docker-compose
project_name="${PROJECT_NAME:-overleaf}"
local project_name="${PROJECT_NAME:-overleaf}"
image_name="sharelatex/sharelatex"
local image_name="sharelatex/sharelatex"
if [[ "${SERVER_PRO:-null}" == "true" ]]; then
image_name="quay.io/sharelatex/sharelatex-pro"
fi
full_image_spec="$image_name:$SHARELATEX_IMAGE_VERSION"
local full_image_spec="$image_name:$SHARELATEX_IMAGE_VERSION"
# Canonicalize data paths
SHARELATEX_DATA_PATH=$(cd "$TOOLKIT_ROOT"; realpath "$SHARELATEX_DATA_PATH")

View file

@ -24,9 +24,9 @@ function add_warning() {
}
function indent_to_level() {
levels="$1"
number_of_spaces=$(( levels * SPACES_PER_INDENT ))
spaces="$(printf %${number_of_spaces}s)"
local levels="$1"
local number_of_spaces=$(( levels * SPACES_PER_INDENT ))
local spaces="$(printf %${number_of_spaces}s)"
echo -n "${spaces}"
}
@ -35,7 +35,7 @@ function print_section_separator() {
}
function print_point() {
indent_level="0"
local indent_level="0"
if [[ "${1:-null}" =~ [0-9]{1} ]]; then
indent_level="$1"
shift
@ -70,7 +70,7 @@ function check_host_information() {
function check_dependencies() {
function get_version() {
binary_name="$1"
local binary_name="$1"
if [[ "bash" == "$binary_name" ]]; then
bash -c 'echo $BASH_VERSION'
elif [[ "perl" == "$binary_name" ]]; then
@ -81,15 +81,15 @@ function check_dependencies() {
}
function check_for_binary() {
binary_name="$1"
local binary_name="$1"
print_point 1 "$binary_name"
if [[ -n $(command -v "$binary_name") ]]; then
print_point 2 "status: present"
version=$(get_version "$binary_name")
local version=$(get_version "$binary_name")
print_point 2 "version info: $version"
if [[ "$binary_name" == "realpath" ]] \
&& [[ "$(command -V "$binary_name")" =~ .*function.* ]]; then
message="Could not find 'realpath' binary, falling back to custom function"
local message="Could not find 'realpath' binary, falling back to custom function"
print_point 2 "WARNING: $message"
add_warning "$message"
fi
@ -138,7 +138,7 @@ function print_warnings() {
function check_config_files() {
print_section_separator "Configuration"
config_files=(
local config_files=(
"config/version"
"config/overleaf.rc"
"config/variables.env"

View file

@ -42,8 +42,8 @@ function usage() {
}
function __main__() {
LINES="$DEFAULT_LINES"
FOLLOW_FLAG=""
local LINES="$DEFAULT_LINES"
local FOLLOW_FLAG=""
while getopts "fn:" opt
do
@ -61,22 +61,22 @@ function __main__() {
usage && exit
fi
log_base_path="/var/log/sharelatex"
local log_base_path="/var/log/sharelatex"
declare -a services
IFS=" " read -r -a services <<< "$*"
path_spec="$log_base_path/*.log"
local path_spec="$log_base_path/*.log"
if [[ ! "0" == "${#services[@]}" ]]; then
log_paths=()
local log_paths=()
for service in "${services[@]}"; do
log_paths+=("$log_base_path/${service}.log")
done
path_spec="${log_paths[*]}"
fi
bash_exec_command_string="tail $FOLLOW_FLAG -n $LINES $path_spec"
local bash_exec_command_string="tail $FOLLOW_FLAG -n $LINES $path_spec"
exec "$TOOLKIT_ROOT/bin/docker-compose" exec sharelatex bash -c "$bash_exec_command_string"
}

View file

@ -28,7 +28,7 @@ function usage() {
}
function services_up() {
top_output="$("$TOOLKIT_ROOT/bin/docker-compose" top)"
local top_output="$("$TOOLKIT_ROOT/bin/docker-compose" top)"
if [[ -z "$top_output" ]]; then
return 1
else
@ -37,9 +37,9 @@ function services_up() {
}
function git_pull_available() {
branch="$1"
fetch_output="$(git -C "$TOOLKIT_ROOT" fetch --dry-run origin "$branch" 2>&1)"
filtered_fetch_output="$(echo "$fetch_output" | grep '\-> origin/'"$branch")"
local branch="$1"
local fetch_output="$(git -C "$TOOLKIT_ROOT" fetch --dry-run origin "$branch" 2>&1)"
local filtered_fetch_output="$(echo "$fetch_output" | grep '\-> origin/'"$branch")"
if [[ -z "$filtered_fetch_output" ]]; then
return 1
else
@ -48,8 +48,8 @@ function git_pull_available() {
}
function handle_image_upgrade() {
user_image_version="$(head -n 1 "$TOOLKIT_ROOT/config/version")"
seed_image_version="$(head -n 1 "$TOOLKIT_ROOT/lib/config-seed/version")"
local user_image_version="$(head -n 1 "$TOOLKIT_ROOT/config/version")"
local seed_image_version="$(head -n 1 "$TOOLKIT_ROOT/lib/config-seed/version")"
if [[ ! "$seed_image_version" > "$user_image_version" ]]; then
echo "No change to docker image version"
@ -59,7 +59,7 @@ function handle_image_upgrade() {
echo "New docker image version available ($seed_image_version)"
echo "Current image version is '$user_image_version' (from config/version)"
should_upgrade="n"
local should_upgrade="n"
read -r -p "Upgrade image? [y/n] " should_upgrade
if [[ ! "$should_upgrade" =~ [Yy] ]]; then
@ -70,10 +70,10 @@ function handle_image_upgrade() {
echo "Upgrading config/version from $user_image_version to $seed_image_version"
## Offer to stop docker services
services_stopped="false"
local services_stopped="false"
if services_up; then
echo "docker services are up, stop them first?"
should_stop="n"
local should_stop="n"
read -r -p "Stop docker services? [y/n] " should_stop
if [[ ! "$should_stop" =~ [Yy] ]]; then
echo "exiting without stopping services"
@ -89,7 +89,7 @@ function handle_image_upgrade() {
## sudo privileges. We leave it to the user to sort out for now)
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"
local should_proceed="n"
read -r -p "Proceed? [y/n] " should_proceed
if [[ ! "$should_proceed" =~ [Yy] ]]; then
echo "Not proceeding with upgrade"
@ -104,7 +104,7 @@ function handle_image_upgrade() {
## Maybe offer to start services again
if [[ "${services_stopped:-null}" == "true" ]]; then
should_start="n"
local should_start="n"
read -r -p "Start docker services again? [y/n] " should_start
if [[ "$should_start" =~ [Yy] ]]; then
echo "Starting docker services"
@ -114,7 +114,9 @@ function handle_image_upgrade() {
}
function handle_git_update() {
local current_branch
current_branch="$(git -C "$TOOLKIT_ROOT" rev-parse --abbrev-ref HEAD)"
local current_commit
current_commit="$(git -C "$TOOLKIT_ROOT" rev-parse --short HEAD)"
if [[ ! "$current_branch" == "master" ]]; then
@ -128,7 +130,7 @@ function handle_git_update() {
else
echo "Code update available! (current commit is $current_commit)"
should_pull="n"
local should_pull="n"
read -r -p "Proceed to pull update? [y/n] " should_pull
if [[ ! "$should_pull" =~ [Yy] ]]; then