diff --git a/Dockerfile b/Dockerfile index 2f5b7a9..8a201c4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ ARG ALPINE_VERSION=latest -FROM alpine:${ALPINE_VERSION} as build +FROM alpine:${ALPINE_VERSION} AS build ENV CXXFLAGS="" WORKDIR /usr/src/telegram-bot-api diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh old mode 100644 new mode 100755 index 9817963..cb430ad --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -1,87 +1,113 @@ -#!/bin/sh +#!/usr/bin/env sh set -e +USERNAME="telegram-bot-api" +GROUPNAME="telegram-bot-api" + +COMMAND="telegram-bot-api" + +# Appends an argument to the COMMAND variable. +append_args() { + COMMAND="$COMMAND $1" +} + +# Sets $env_var from $file_env_var content or directly from $env_var. +# Usage: file_env +# - If both or neither variables are set, exits with an error. +# - If only $file_env_var is set, reads content from the file path and sets $env_var. +# - Exits with an error if the file does not exist. file_env() { - local var_name="$1" - local file_var_name="$2" + env_var="$1" + file_env_var="$2" + env_value=$(printenv "$env_var") || env_value="" + file_path=$(printenv "$file_env_var") || file_path="" - var_value=$(printenv "$var_name") || var_value="" - file_path=$(printenv "$file_var_name") || file_path="" - - if [ -z "$var_value" ] && [ -z "$file_path" ]; then - echo "error: expected $var_name or $file_var_name env vars to be set" - exit 1 - - elif [ -n "$var_value" ] && [ -n "$file_path" ]; then - echo "both and $var_name $file_var_name env vars are set, expected only one of them" - exit 1 - - else - if [ -n "$file_path" ] && [ "$file_path" != "" ]; then - if [ -f "$file_path" ]; then - file_content=$(cat "$file_path") - export "$var_name=$file_content" - else - echo "error: $var_name=$file_path: file '$file_path' does not exist" + if [ -z "$env_value" ] && [ -z "$file_path" ]; then + echo "error: expected $env_var or $file_env_var env vars to be set" exit 1 - fi + elif [ -n "$env_value" ] && [ -n "$file_path" ]; then + echo "both $env_var and $file_env_var env vars are set, expected only one of them" + exit 1 + elif [ -n "$file_path" ]; then + if [ -f "$file_path" ]; then + export "$env_var=$(cat "$file_path")" + else + echo "error: $env_var=$file_path: file '$file_path' does not exist" + exit 1 + fi fi +} + +# Checks if an environment variable is set. +# Usage: check_required_env +# - Exits with an error if the variable is not set. +check_required_env() { + var_name="$1" + + if [ -z "$(printenv "$var_name")" ]; then + echo "error: environment variable $var_name is required" + exit 1 fi } -USERNAME=telegram-bot-api -GROUPNAME=telegram-bot-api +# Appends an argument to CUSTOM_ARGS based on the environment variable value. +# Usage: append_arg_from_env +# - If is set, uses its value; otherwise, uses . +# - Appends "=" to CUSTOM_ARGS if a value is found. +append_arg_from_env() { + var_name="$1" + arg_name="$2" + default_value="$3" + env_value=$(printenv "$var_name") || env_value="" -chown ${USERNAME}:${GROUPNAME} "${TELEGRAM_WORK_DIR}" + [ -n "$env_value" ] || env_value="$default_value" + if [ -n "$env_value" ]; then + append_args "${arg_name}=$env_value" + fi +} -if [ -n "${1}" ]; then - exec "${*}" -fi +# Appends a flag to CUSTOM_ARGS if the environment variable is set (non-empty). +# Usage: append_flag_from_env +# - If is set, appends to CUSTOM_ARGS. +append_flag_from_env() { + var_name="$1" + flag_name="$2" + if [ -n "$(printenv "$var_name")" ]; then + append_args "$flag_name" + fi +} + +check_required_env "TELEGRAM_WORK_DIR" +chown "${USERNAME}:${GROUPNAME}" "${TELEGRAM_WORK_DIR}" + +# Telegram Bot API Server knows how to read the API ID and API Hash from a environment variable. +# Is not needed to pass it as arguments. file_env "TELEGRAM_API_ID" "TELEGRAM_API_ID_FILE" file_env "TELEGRAM_API_HASH" "TELEGRAM_API_HASH_FILE" -DEFAULT_ARGS="--dir=${TELEGRAM_WORK_DIR} --temp-dir=${TELEGRAM_TEMP_DIR} --username=${USERNAME} --groupname=${GROUPNAME}" -CUSTOM_ARGS="" +# Default arguments, passed from the Dockerfile. +# Potentially can be overwritten by environment variables, if needed, but is not recommended. +append_arg_from_env "TELEGRAM_WORK_DIR" "--dir" +check_required_env "TELEGRAM_TEMP_DIR" +append_arg_from_env "TELEGRAM_TEMP_DIR" "--temp-dir" +append_args "--username=${USERNAME}" +append_args "--groupname=${GROUPNAME}" -if [ -n "$TELEGRAM_LOG_FILE" ]; then - CUSTOM_ARGS=" --log=${TELEGRAM_LOG_FILE}" -fi -if [ -n "$TELEGRAM_STAT" ]; then - CUSTOM_ARGS="${CUSTOM_ARGS} --http-stat-port=8082" -fi -if [ -n "$TELEGRAM_FILTER" ]; then - CUSTOM_ARGS="${CUSTOM_ARGS} --filter=$TELEGRAM_FILTER" -fi -if [ -n "$TELEGRAM_MAX_WEBHOOK_CONNECTIONS" ]; then - CUSTOM_ARGS="${CUSTOM_ARGS} --max-webhook-connections=$TELEGRAM_MAX_WEBHOOK_CONNECTIONS" -fi -if [ -n "$TELEGRAM_VERBOSITY" ]; then - CUSTOM_ARGS="${CUSTOM_ARGS} --verbosity=$TELEGRAM_VERBOSITY" -fi -if [ -n "$TELEGRAM_MAX_CONNECTIONS" ]; then - CUSTOM_ARGS="${CUSTOM_ARGS} --max-connections=$TELEGRAM_MAX_CONNECTIONS" -fi -if [ -n "$TELEGRAM_PROXY" ]; then - CUSTOM_ARGS="${CUSTOM_ARGS} --proxy=$TELEGRAM_PROXY" -fi -if [ -n "$TELEGRAM_LOCAL" ]; then - CUSTOM_ARGS="${CUSTOM_ARGS} --local" -fi -if [ -n "$TELEGRAM_HTTP_IP_ADDRESS" ]; then - CUSTOM_ARGS="${CUSTOM_ARGS} --http-ip-address=$TELEGRAM_HTTP_IP_ADDRESS" -fi +check_required_env "TELEGRAM_API_ID" +check_required_env "TELEGRAM_API_HASH" -# Set http-port arg -if [ -n "$TELEGRAM_HTTP_PORT" ]; then - CUSTOM_ARGS="${CUSTOM_ARGS} --http-port=$TELEGRAM_HTTP_PORT" -else - CUSTOM_ARGS="${CUSTOM_ARGS} --http-port=8081" -fi - -COMMAND="telegram-bot-api ${DEFAULT_ARGS}${CUSTOM_ARGS}" +# Environment variables that can be passed as arguments from environment by administrator. +append_arg_from_env "TELEGRAM_HTTP_PORT" "--http-port" "8081" +append_flag_from_env "TELEGRAM_LOCAL" "--local" +append_flag_from_env "TELEGRAM_STAT" "--http-stat-port=8082" # maybe change it to dynamic variable in the future +append_arg_from_env "TELEGRAM_LOG_FILE" "--log" +append_arg_from_env "TELEGRAM_FILTER" "--filter" +append_arg_from_env "TELEGRAM_MAX_WEBHOOK_CONNECTIONS" "--max-webhook-connections" +append_arg_from_env "TELEGRAM_VERBOSITY" "--verbosity" +append_arg_from_env "TELEGRAM_MAX_CONNECTIONS" "--max-connections" +append_arg_from_env "TELEGRAM_PROXY" "--proxy" +append_arg_from_env "TELEGRAM_HTTP_IP_ADDRESS" "--http-ip-address" echo "$COMMAND" - -# shellcheck disable=SC2086 exec $COMMAND