#!/bin/bash# Define working directoriesTMP_DIR="/tmp/privacera-dataserver-custom-build"WORKDIR="/workdir/privacera-dataserver"BACKUP_DIR="/tmp/backup"PRIVACERA_DATASERVER_TAR="privacera-dataserver.tar.gz"DATASERVER_ENV_CUSTOM=dataserver-env-custom.sh
# Define log file pathLOG_FILE="$WORKDIR/logs/dataserver/custom_build_script.log"# Function for logging messages with timestampslog(){localmsg="$1"localcurrentTime
currentTime=$(date"+%Y-%m-%d %H:%M:%S")echo"${currentTime} : ${msg}"|tee-a"$LOG_FILE"}log"======================custom_build.sh execution started!!!======================"# Check if the URL argument is providedif[-z"$1"];thenlog"PRIVACERA_DOWNLOAD_URL is null so exiting...!!!"exit1fi# Assign the provided URL to a variablePRIVACERA_DOWNLOAD_URL=$1# Create necessary directoriesmkdir-p"$BACKUP_DIR"mkdir-p"$(dirname"$LOG_FILE")"OLD_JAR_FILE=$(ls"$WORKDIR/dist/privacera-dataserver-"*2>/dev/null)OLD_MD5_SUM=$(md5sum"$OLD_JAR_FILE"2>/dev/null|awk'{print $1}')log"MD5 checksum of the existing $OLD_JAR_FILE: ${OLD_MD5_SUM}"# Backup dist and lib directoriesif[-d"$WORKDIR/dist"]&&[-d"$WORKDIR/dist"];thenmv"$WORKDIR/dist""$WORKDIR/lib""$BACKUP_DIR/"log"Moved dist and lib directory to $BACKUP_DIR"elselog"Directory not found, skipping backup"fi# Create and navigate to the temporary build directorymkdir-p"$TMP_DIR"cd"$TMP_DIR"||exitlog"Downloading $PRIVACERA_DATASERVER_TAR from: $PRIVACERA_DOWNLOAD_URL"wget"$PRIVACERA_DOWNLOAD_URL"-O$PRIVACERA_DATASERVER_TARif[[$?-ne0]];thenlog"Error: Failed to download the package."exit1filog"Extracting $PRIVACERA_DATASERVER_TAR"tarxfz$PRIVACERA_DATASERVER_TARif[[$?-ne0]];thenlog"Error: Failed to extract $PRIVACERA_DATASERVER_TAR."exit1fi# Copy new dist and lib directorieslog"Copying new files to $WORKDIR..."cp-rprivacera-dataserver/dist/"$WORKDIR/dist/"cp-rprivacera-dataserver/lib/"$WORKDIR/lib/"# Navigate to the configuration directorycd"$WORKDIR/conf/"||exitNEW_JAR_FILE=$(ls"$WORKDIR/dist/privacera-dataserver-"*2>/dev/null)OLD_MD5_SUM=$(md5sum"$NEW_JAR_FILE"2>/dev/null|awk'{print $1}')log"MD5 checksum of the new $NEW_JAR_FILE: ${OLD_MD5_SUM}"log"Updating $DATASERVER_ENV_CUSTOM..."sed-E-i's/DATASERVER_RUN_FOREGROUND=1/DATASERVER_RUN_FOREGROUND=0/'"$(readlink-f$WORKDIR/conf/$DATASERVER_ENV_CUSTOM)"grep"DATASERVER_RUN_FOREGROUND""$WORKDIR/conf/$DATASERVER_ENV_CUSTOM"|tee-a"$LOG_FILE"# Create dont_exit filetouch"$WORKDIR/conf/dont_exit"ls-l"$WORKDIR/conf/dont_exit"|tee-a"$LOG_FILE"log"Running Java processes:"jps|tee-a"$LOG_FILE"# Find and kill ProxyServer processPROXY_PID=$(jps|grepProxyServer|awk'{print $1}')if[-n"$PROXY_PID"];thenlog"Killing ProxyServer process with PID: $PROXY_PID"kill-9"$PROXY_PID"elselog"No ProxyServer process found"fi# Start the S3 proxycd"$WORKDIR/scripts"||exitlog"Starting Privacera S3 Proxy..."./start_s3_proxy.sh2>&1|tee-a"$LOG_FILE"log"====================== custom_build_script execution completed!!! ======================"