#!/bin/bash -e . /libfile.sh . /liblog.sh . /libos.sh . /libservice.sh . /libvalidations.sh # Echo env vars for redis global configuration. redis_env() { cat <<"EOF" export REDIS_EXTRAS_DIR=/opt/bitnami/extra/redis export REDIS_TEMPLATES_DIR=$REDIS_EXTRAS_DIR/templates export REDIS_BASEDIR=/opt/bitnami/redis export REDIS_VOLUME=/bitnami/redis export REDIS_TMPDIR=$REDIS_BASEDIR/tmp export REDIS_LOGDIR=$REDIS_BASEDIR/logs export PATH=$REDIS_BASEDIR/bin:$PATH export REDIS_DAEMON_USER=redis export REDIS_DAEMON_GROUP=redis EOF } # Validate settings in REDIS_* env vars. redis_validate() { empty_password_enabled_warn() { warn "You set the environment variable ALLOW_EMPTY_PASSWORD=${ALLOW_EMPTY_PASSWORD}. For safety reasons, do not use this flag in a production environment." } empty_password_error() { error "The $1 environment variable is empty or not set. Set the environment variable ALLOW_EMPTY_PASSWORD=yes to allow the container to be started with blank passwords. This is recommended only for development." exit 1 } for var in REDIS_MASTER_PORT_NUMBER; do local value=${!var} if ! err=$(validate_port "$value"); then error "The $var environment variable is invalid: $err" exit 1 fi done if is_boolean_yes "$ALLOW_EMPTY_PASSWORD"; then empty_password_enabled_warn else # Root user if [[ -z "$REDIS_PASSWORD" ]]; then empty_password_error REDIS_PASSWORD fi # Replication user if [[ "$REDIS_REPLICATION_MODE" == "slave" && -z "$REDIS_MASTER_PASSWORD" ]]; then empty_password_error REDIS_MASTER_PASSWORD fi fi } # Ensure the redis volume is initialised. redis_initialize() { if [ -e "$REDIS_BASEDIR/etc/redis.conf" ]; then if [ -e "$REDIS_BASEDIR/etc/redis-default.conf" ]; then rm "$REDIS_BASEDIR/etc/redis-default.conf" fi return fi for dir in "$REDIS_VOLUME/data" "$REDIS_BASEDIR/tmp" "$REDIS_LOGDIR"; do ensure_dir_exists "$dir" if am_i_root; then chown "$REDIS_DAEMON_USER:$REDIS_DAEMON_GROUP" "$dir" fi done mv "$REDIS_BASEDIR/etc/redis-default.conf" "$REDIS_BASEDIR/etc/redis.conf" # Redis config redis_conf_set dir "$REDIS_VOLUME/data" # Log to stdout redis_conf_set logfile "" redis_conf_set pidfile "$REDIS_BASEDIR/tmp/redis.pid" redis_conf_set daemonize yes # Allow remote connections redis_conf_set bind 0.0.0.0 # Enable AOF https://redis.io/topics/persistence#append-only-file # Leave default fsync (every second) redis_conf_set appendonly yes if [ -n "$REDIS_PASSWORD" ]; then redis_conf_set requirepass "$REDIS_PASSWORD" else redis_conf_unset requirepass fi if [ -n "$REDIS_DISABLE_COMMANDS" ]; then # The current syntax gets a comma separated list of commands, we split them # before passing to redis_disable_unsafe_commands redis_disable_unsafe_commands $(tr ',' ' ' <<<"$REDIS_DISABLE_COMMANDS") fi if [ -n "$REDIS_REPLICATION_MODE" ]; then redis_configure_replication "$REDIS_REPLICATION_MODE" fi } redis_disable_unsafe_commands() { info "Disabling commands: $*" for cmd in "$@"; do if egrep -q "^\s*rename-command\s+$cmd\s+\"\"\s*$" "$REDIS_BASEDIR/etc/redis.conf" ; then info "$cmd was already disabled" continue fi cat >> "$REDIS_BASEDIR/etc/redis.conf" <