Merge PR #6: Simplify entrypoint script a little bit

pull/11/head
Robert Adam 2022-05-16 15:00:30 +02:00 zatwierdzone przez GitHub
commit 6a9d8f5cdb
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 64 dodań i 94 usunięć

Wyświetl plik

@ -1,136 +1,106 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
DATA_DIR="/data" readonly DATA_DIR="/data"
BARE_BONES_CONFIG_FILE="/etc/mumble/bare_config.ini" readonly BARE_BONES_CONFIG_FILE="/etc/mumble/bare_config.ini"
CONFIG_FILE="${DATA_DIR}/mumble_server_config.ini" readonly CONFIG_FILE="${DATA_DIR}/mumble_server_config.ini"
readonly CONFIG_REGEX="^(\;|\#)?\ *([a-zA-Z_0-9]+)=.*"
# Compile list of configuration options from the bare-bones config
readarray -t existing_config_options < <(sed -En "s/$CONFIG_REGEX/\2/p" "$BARE_BONES_CONFIG_FILE")
# Grab the original command line that is supposed to start the Mumble server # Grab the original command line that is supposed to start the Mumble server
server_invocation=( "${@}" ) declare -a server_invocation=("${@}")
declare -a used_configs
array_contains () { normalize_name() {
local array="$1[@]" local uppercase="${1^^}"
local seeking=$2 echo "${uppercase//_/}"
local contained=false }
for element in "${!array}"; do
if [[ $element == "$seeking" ]]; then # Create an associative array for faster config option lookup
contained=true declare -A option_for
break
fi for config in "${existing_config_options[@]}"; do
option_for["$(normalize_name "$config")"]="$config"
done
array_contains() {
local array_expansion="$1[@]" seeking="$2"
for element in "${!array_expansion}"; do
[[ "$element" = "$seeking" ]] && return 0
done done
return 1
echo "$contained"
} }
set_config() { set_config() {
local config_name="$1" local config_name="$1" config_value="$2" is_default="$3"
local config_value="$2"
local is_default="$3"
local apply_value=true local apply_value=true
# Don't use default value if the user already set one [[ "$is_default" = true ]] && array_contains "used_configs" "$config_name" && \
if [ "$is_default" = "true" ]; then apply_value=false # Don't use default value if the user already set one!
contained=$( array_contains used_configs "$config_name" )
if [[ "$contained" = "true" ]]; then [[ "$apply_value" != true ]] && return 0
apply_value=false
fi echo "Setting config \"$config_name\" to: '$config_value'"
fi used_configs+=("$config_name")
if [ "$apply_value" = "true" ]; then # Append config to our on-the-fly-built config file
echo "Setting config \"$config_name\" to: '$config_value'" echo "${config_name}=${config_value}" >> "$CONFIG_FILE"
used_configs+=("$config_name")
# Append config to our on-the-fly-built config file
echo "${config_name}=${config_value}" >> "$CONFIG_FILE"
fi
} }
# Drop the user into a shell, if they so wish # Drop the user into a shell, if they so wish
if [ "$1" = "bash" ] || [ "$1" = "sh" ]; then if [[ "$1" = "bash" || "$1" = "sh" ]]; then
echo "Dropping into interactive BASH session" echo "Dropping into interactive BASH session"
exec "${@}" exec "${@}"
fi fi
if [ -f "$MUMBLE_CUSTOM_CONFIG_FILE" ]; then if [[ -f "$MUMBLE_CUSTOM_CONFIG_FILE" ]]; then
# Just use the config file specified by the user and don't bother assembling our own
echo "Using manually specified config file at $MUMBLE_CUSTOM_CONFIG_FILE" echo "Using manually specified config file at $MUMBLE_CUSTOM_CONFIG_FILE"
echo "All MUMBLE_CONFIG variables will be ignored" echo "All MUMBLE_CONFIG variables will be ignored"
CONFIG_FILE="$MUMBLE_CUSTOM_CONFIG_FILE" CONFIG_FILE="$MUMBLE_CUSTOM_CONFIG_FILE"
else else
# As a first step, we ensure that the config file is empty, so we can always start from a clean slate # Ensures the config file is empty, starting from a clean slate
echo -e "# Config file automatically generated from the MUMBLE_CONFIG_* environment variables\n" > "${CONFIG_FILE}" echo -e "# Config file automatically generated from the MUMBLE_CONFIG_* environment variables\n" > "${CONFIG_FILE}"
used_configs=() # Process settings through variables of format MUMBLE_CONFIG_*
existing_config_options=()
####
# Check what kind of configurations there exist in the bare bones config file
####
while read -r line; do
if [[ "$line" =~ ^(\;|\#)?\ *([a-zA-Z_0-9]+)=.* ]]; then
existing_config_options+=("${BASH_REMATCH[2]}")
fi
done < "$BARE_BONES_CONFIG_FILE"
####
# Process settings following environments variables starting with "MUMBLE_CONFIG_"
# Iterate over all environment variable key, value pairs and check if they match our naming scheme
####
while IFS='=' read -d '' -r var value; do while IFS='=' read -d '' -r var value; do
if [[ "$var" == "MUMBLE_CONFIG_"* ]]; then config_option="${option_for[$(normalize_name "$var")]}"
uppercase_variable=${var/MUMBLE_CONFIG_/}
# Remove underscores (to ensure that it doesn't matter whether the user if [[ -z "$config_option" ]]; then
# uses e.g. MUMBLE_CONFIG_DB_BLA or MUMBLE_CONFIG_DBBLA) >&2 echo "[ERROR]: Unable to find config corresponding to variable \"$var\""
uppercase_variable_no_underscores="${uppercase_variable//_/}" exit 1
found=false
for current_config in "${existing_config_options[@]}"; do
# convert to uppercase
upper_current_config=${current_config^^}
if [ "$upper_current_config" = "$uppercase_variable" ] || [ "$upper_current_config" = "$uppercase_variable_no_underscores" ]; then
set_config "$current_config" "$value"
found=true
break
fi
done
if [ "$found" = "false" ]; then
>&2 echo "[ERROR]: Unable to find config corresponding to variable \"$var\""
exit 1
fi
fi fi
done < <( printenv --null ) # Feeding it in like this, prevents the creation of a subshell for the while-loop
#### set_config "$config_option" "$value"
# Default settings (will apply only, if user hasn't specified the respective config options themselves) done < <( printenv --null | sed -zn 's/^MUMBLE_CONFIG_//p' )
#### # ^ Feeding it in like this, prevents the creation of a subshell for the while-loop
# Apply default settings if they're missing
set_config "database" "${DATA_DIR}/murmur.sqlite" true set_config "database" "${DATA_DIR}/murmur.sqlite" true
set_config "ice" "\"tcp -h 127.0.0.1 -p 6502\"" true set_config "ice" "\"tcp -h 127.0.0.1 -p 6502\"" true
set_config "welcometext" "\"<br />Welcome to this server, running the official Mumble Docker image.<br />Enjoy your stay!<br />\"" true set_config "welcometext" "\"<br />Welcome to this server, running the official Mumble Docker image.<br />Enjoy your stay!<br />\"" true
set_config "port" 64738 true set_config "port" 64738 true
set_config "users" 100 true set_config "users" 100 true
# Add ICE section { # Add ICE section
echo -e "\n[Ice]" >> "$CONFIG_FILE" echo -e "\n[Ice]"
echo "Ice.Warn.UnknownProperties=1" >> "$CONFIG_FILE" echo "Ice.Warn.UnknownProperties=1"
echo "Ice.MessageSizeMax=65536" >> "$CONFIG_FILE" echo "Ice.MessageSizeMax=65536"
} >> "$CONFIG_FILE"
fi fi
#### # Additional environment variables
# Additionnal environement variables
####
if [ -n "$MUMBLE_VERBOSE" ] && [ "$MUMBLE_VERBOSE" = true ]; then
server_invocation+=( "-v" )
fi
# Make sure the correct config file will be used [[ "$MUMBLE_VERBOSE" = true ]] && server_invocation+=( "-v" )
# Make sure the correct configuration file is used
server_invocation+=( "-ini" "${CONFIG_FILE}") server_invocation+=( "-ini" "${CONFIG_FILE}")
####
# Variable to change the superuser password # Variable to change the superuser password
#### if [[ -n "${MUMBLE_SUPERUSER_PASSWORD}" ]]; then
if [ -n "${MUMBLE_SUPERUSER_PASSWORD}" ]; then
"${server_invocation[@]}" -supw "$MUMBLE_SUPERUSER_PASSWORD" "${server_invocation[@]}" -supw "$MUMBLE_SUPERUSER_PASSWORD"
echo "Successfully configured superuser password" echo "Successfully configured superuser password"
fi fi
@ -140,7 +110,7 @@ echo "Running Mumble server as uid=$(id -u) gid=$(id -g)"
echo "\"${DATA_DIR}\" has the following permissions set:" echo "\"${DATA_DIR}\" has the following permissions set:"
echo " $( stat ${DATA_DIR} --printf='%A, owner: \"%U\" (UID: %u), group: \"%G\" (GID: %g)' )" echo " $( stat ${DATA_DIR} --printf='%A, owner: \"%U\" (UID: %u), group: \"%G\" (GID: %g)' )"
echo "Command run to start the service : ${server_invocation[@]}" echo "Command run to start the service : ${server_invocation[*]}"
echo "Starting..." echo "Starting..."
exec "${server_invocation[@]}" exec "${server_invocation[@]}"