harbor run -t openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-1790harbor download openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-1790Task Description: Implement a Memcached Startup Script
Functionality Requirements: Develop a shell script to initialize and start a Memcached server with configurable parameters based on environment variables. The script should handle user permissions and manage various startup options for the Memcached daemon. Ensure that the script is robust and adheres to best practices for error handling.
Expected Inputs:
MEMCACHED_DAEMON_USER: The user under which Memcached will run.MEMCACHED_PORT_NUMBER: The port number on which Memcached will listen.MEMCACHED_LISTEN_ADDRESS: (Optional) The specific IP address on which Memcached will listen.SASL_DB_FILE: (Optional) Path to the SASL database file.MEMCACHED_CACHE_SIZE: (Optional) Size of the memory to allocate for caching.MEMCACHED_MAX_CONNECTIONS: (Optional) Maximum number of simultaneous connections.MEMCACHED_THREADS: (Optional) Number of threads to use for handling requests.MEMCACHED_MAX_ITEM_SIZE: (Optional) Maximum size of a single item that can be stored.MEMCACHED_EXTRA_FLAGS: (Optional) Any additional flags to pass to Memcached.Expected Outputs:
gosu command to switch to the specified MEMCACHED_DAEMON_USER before starting Memcached.File Location and Naming:
/app/start-memcached.sh.The following test script will be used to verify your solution:
#!/bin/bash
# shellcheck disable=SC1091
set -o errexit
set -o nounset
set -o pipefail
# set -o xtrace # Uncomment this line for debugging purposes
# Load libraries
. /opt/bitnami/scripts/liblog.sh
. /opt/bitnami/scripts/libos.sh
. /opt/bitnami/scripts/libmemcached.sh
# Load Memcached environment variables
. /opt/bitnami/scripts/memcached-env.sh
# Configure arguments with extra flags
args=("-u" "$MEMCACHED_DAEMON_USER" "-p" "$MEMCACHED_PORT_NUMBER" "-v")
[[ -n "$MEMCACHED_LISTEN_ADDRESS" ]] && args+=("-l" "$MEMCACHED_LISTEN_ADDRESS")
# SASL
[[ -f "$SASL_DB_FILE" ]] && args+=("-S")
# Memory configuration
[[ -n "$MEMCACHED_CACHE_SIZE" ]] && args+=("-m" "$MEMCACHED_CACHE_SIZE")
[[ -n "$MEMCACHED_MAX_CONNECTIONS" ]] && args+=("-c" "$MEMCACHED_MAX_CONNECTIONS")
[[ -n "$MEMCACHED_THREADS" ]] && args+=("-t" "$MEMCACHED_THREADS")
[[ -n "$MEMCACHED_MAX_ITEM_SIZE" ]] && args+=("-I" "$MEMCACHED_MAX_ITEM_SIZE")
# Extra flags
read -r -a extra_flags <<< "$MEMCACHED_EXTRA_FLAGS"
[[ "${#extra_flags[@]}" -gt 0 ]] && args+=("${extra_flags[@]}")
args+=("$@")
info "** Starting Memcached **"
if am_i_root; then
exec gosu "$MEMCACHED_DAEMON_USER" memcached "${args[@]}"
else
exec memcached "${args[@]}"
fi