1
0
Fork 0
mirror of https://github.com/overleaf/toolkit.git synced 2025-04-19 07:18:06 +02:00
overleaf-toolkit/bin/logs
Miguel Serrano e2b99f150f
Scripts to update config/overleaf.rc and config-seed rebrand (#217)
* Add scripts to rebrand variables.env and overleaf.rc

* Update bin/upgrade to prompt for config file rebrand

* Update bin/up to check for correct variable prefix

Ensures SHARELATEX_ is in place for version <= 4.x, and
OVERLEAF_ for version >= 5.x

* Rebrand variables for bin/docker-compose

Updates docker-compose.base.yml and sibling containers
base file with the changes in the script

* Update bin/doctor to support OVERLEAF_ prefix

* Update documentation with the OVERLEAF_ prefix

* Rebrand variables.env and overleaf.rc in config-seed

* Prepare config/version and CHANGELOG for release (WIP)

* Fix script documentation

Co-authored-by: Jakob Ackermann <jakob.ackermann@overleaf.com>

* Fix doctor logs

Co-authored-by: Jakob Ackermann <jakob.ackermann@overleaf.com>

* Remove unnecessary fallbacks to SHARELATEX_ vars

* SEt OVERLEAF_DATA_PATH to data/overleaf

* Remove duplicated environment entries

* Moved prefix brand checs from bin/up to bin/docker-compose

* Move set +o pipefail into subshell commands

* Use separate legacy compose files for required SHARELATEX_ vars

* Handle overleaf.rc rebranding before version upgrade

* Group output from rebranding process

* Move prompt for rebranding into helper function

* Refuse to start with mismatching ShareLaTeX vs Overleaf branded configs

* Print expected prefix when checking variables.env

* Print number of mismatching variables in overleaf.rc

* Check on variable rebranding from bin/doctor

* Cleanup bin/doctor lookup for ShareLaTeX branded overleaf.rc

* Update filesystem paths in bin/logs and docs

* Flag old TEXMFVAR entry in config/variables.env

REF: 1829e7ee2a

* Update config-seed version to 5.0.1 and changelog

---------

Co-authored-by: Jakob Ackermann <jakob.ackermann@overleaf.com>
2024-04-02 16:43:08 +02:00

163 lines
4.2 KiB
Bash
Executable file

#! /usr/bin/env bash
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"
DEFAULT_TAIL_LINES=20
ALL_SERVICES=(chat clsi contacts docstore document-updater filestore git-bridge \
mongo notifications real-time redis spelling tags track-changes web \
history-v1 project-history)
LOGS_PID_FILE="/tmp/toolkit-logs.$$.pid"
function usage() {
echo "Usage: bin/logs [OPTIONS] [SERVICES...]
Services: chat, clsi, contacts, docstore, document-updater, filestore,
git-bridge, mongo, notifications, real-time, redis, spelling,
tags, track-changes, web, history-v1, project-history
Options:
-f follow log output
-n {number} number of lines to print (default $DEFAULT_TAIL_LINES)
use '-n all' to show all log lines
Examples:
bin/logs -n 50 web clsi
bin/logs -n all web > web.log
bin/logs -f web
bin/logs -f web chat docstore
bin/logs -n 100 -f filestore
bin/logs -f"
}
function parse_args() {
TAIL_LINES=$DEFAULT_TAIL_LINES
FOLLOW=false
if [[ $# -gt 0 && $1 =~ ^help|-h|--help$ ]]; then
usage && exit
fi
while getopts "fn:" opt
do
case $opt in
f) FOLLOW=true ;;
n) TAIL_LINES="$OPTARG" ;;
\?) usage && exit 1;;
esac
done
shift $(( OPTIND - 1 ))
if [[ $# -eq 0 ]]; then
SERVICES=("${ALL_SERVICES[@]}")
else
SERVICES=("$@")
fi
}
function docker_compose() {
# Ignore stderr and errors when calling docker compose
"$TOOLKIT_ROOT/bin/docker-compose" "$@" 2>/dev/null || true
}
function kill_background_jobs() {
if [[ ${#COMPOSE_LOGS_PIDS[@]} -gt 0 ]]; then
# Kill "docker compose logs" processes
kill "${COMPOSE_LOGS_PIDS[@]}" 2>/dev/null || true
fi
# Kill "tail -f" processes started inside the sharelatex container
docker_compose exec -T sharelatex bash -c "
[[ -f $LOGS_PID_FILE ]] && kill \$(cat $LOGS_PID_FILE) 2>/dev/null
rm -f $LOGS_PID_FILE"
}
function show_logs() {
COMPOSE_LOGS_PIDS=()
trap kill_background_jobs EXIT
for service in "${SERVICES[@]}"; do
if [[ $service =~ ^(git-bridge|mongo|redis)$ ]]; then
show_compose_logs "$service" &
COMPOSE_LOGS_PIDS+=($!)
else
show_sharelatex_logs "$service" &
fi
done
wait
}
function show_compose_logs() {
local service="$1"
local flags=(--no-color)
if [[ $FOLLOW == "true" ]]; then
flags+=(-f)
fi
if [[ $TAIL_LINES != "all" ]]; then
flags+=(--tail="$TAIL_LINES")
fi
if [[ ${#SERVICES[@]} -eq 1 ]]; then
# Do not add a service prefix when outputting logs from a single
# service
flags+=(--no-log-prefix)
fi
docker_compose logs "${flags[@]}" "$service"
}
function show_sharelatex_logs() {
local base_path=/var/log/overleaf
if [[ "$IMAGE_VERSION_MAJOR" -lt 5 ]]; then
base_path=/var/log/sharelatex
fi
local service="$1"
local log_path="${base_path}/$service.log"
local flags=()
if [[ $FOLLOW == "true" ]]; then
flags+=(-f)
fi
if [[ $TAIL_LINES = "all" ]]; then
flags+=(-n "+1")
else
flags+=(-n "$TAIL_LINES")
fi
local logs_cmd="[[ -f $log_path ]] && echo \$\$ >> $LOGS_PID_FILE && tail --pid=\$\$ ${flags[*]} $log_path"
if [[ ${#SERVICES[@]} -gt 1 ]]; then
# Roughly reproduce the service prefix format from docker compose
local padded_service
padded_service=$(printf "%-13s" "$service")
logs_cmd="$logs_cmd | sed -u -e 's/^/$padded_service | /'"
fi
docker_compose exec -T sharelatex bash -c "$logs_cmd"
}
parse_args "$@"
read_image_version
show_logs