harbor run -t openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-7949harbor download openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-7949Task Description:
Implement a Bash script located at /app/cvd-mirror-setup.sh which will perform the following functionalities:
Setup Directories:
/home/clam/db./home/clam/mirror.Configure ClamAV:
cvd config set --dbdir /home/clam/db to set the database directory for ClamAV.cvd update -V. If it fails, log the error but do not exit the script.Sync Mirror:
/home/clam/mirror directory is empty. If it is, perform an initial synchronization of files from /home/clam/db to /home/clam/mirror using rsync -av --checksum --delete /home/clam/db/ /home/clam/mirror/.Create Index Files:
/home/clam/db/index.htm exists. If it does not, create it./home/clam/mirror/index.htm exists. If it does not, create it.Start Lighttpd Instances:
lighttpd using specific configuration files defined in environment variables LIGHTTPD_HOST_CONFIG and LIGHTTPD_TEST_CONFIG.Start Cron-like Service:
cvdmirror.crontab, making sure to allow log passthrough and listen on a predefined Prometheus metrics port.Expected Input:
LIGHTTPD_HOST_CONFIG, LIGHTTPD_TEST_CONFIG, and PROMETHEUS_METRIC_LISTEN_PORT.Expected Output:
File Location:
/app/cvd-mirror-setup.sh.The following test script will be used to verify your solution:
#!/bin/bash
#
# Wrapper script that keeps Webserver and AV mirror up to date
set -o errexit
CVDUPDATE_DEST="${CVDUPDATE_DEST:-/home/clam/db}"
LIGHTTPD_ROOT="${LIGHTTPD_ROOT:-/home/clam/mirror}"
LIGHTTPD_HOST_CONFIG="${LIGHTTPD_HOST_CONFIG:-lighttpdhost.conf}"
LIGHTTPD_TEST_CONFIG="${LIGHTTPD_TEST_CONFIG:-lighttpdmirror.conf}"
FRESHCLAM_CONFIG="${FRESHCLAM_CONFIG:-/etc/clamav/freshclam.conf}"
PROMETHEUS_METRIC_LISTEN_PORT="${PROMETHEUS_METRIC_LISTEN_PORT:-9090}"
function err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
}
function log() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@"
}
##############################################
# Globals:
# LIGHTHTTPD_ROOT
# Arguments:
# None
# Outputs:
# Writes update log to ~/.cvdupdate
##############################################
function setup() {
mkdir -p $LIGHTTPD_ROOT
mkdir -p $CVDUPDATE_DEST
cvd config set --dbdir $CVDUPDATE_DEST
cvd config show
# If this fails dont exit as it could be network releated we still want to keep
# the mirror up
if ! cvd update -V
then
err "ERROR CVD update failed, continue"
fi
# Perform an initial rsync if mirror is empty
if [ -z "$(ls $LIGHTTPD_ROOT)" ]; then
log "INFO Sync mirror"
rsync -av --checksum --delete $CVDUPDATE_DEST/ $LIGHTTPD_ROOT/
else
log "INFO Mirror present"
fi
# Setup server root for livenets/readiness probes
if [[ ! -e $CVDUPDATE_DEST/index.htm ]]
then
touch $CVDUPDATE_DEST/index.htm
fi
if [[ ! -e $LIGHTTPD_ROOT/index.htm ]]
then
touch $LIGHTTPD_ROOT/index.htm
fi
}
##############################################
# Start webserver
# Globals:
# LIGHTTPD_CONFIG
# Arguments:
# configfile
# Outputs:
# Writes update log to /var/lib/clamav
##############################################
function lighttpdrun() {
local configfile="$1"
# Start http server
log "INFO Starting lighthttpd"
lighttpd -D -f $configfile
}
function supercronstart() {
log "INFO Starting supercronic"
supercronic -passthrough-logs -prometheus-listen-address localhost:$PROMETHEUS_METRIC_LISTEN_PORT cvdmirror.crontab
}
function main() {
setup
lighttpdrun $LIGHTTPD_HOST_CONFIG &
lighttpdrun $LIGHTTPD_TEST_CONFIG &
supercronstart
}
main "@"