From 0a4a9f670fbe662e739555a3ccdf48177753ed8f Mon Sep 17 00:00:00 2001 From: Tanmay Garg <68961153+tanmaygarg-oracle@users.noreply.github.com> Date: Tue, 27 Jun 2023 17:53:17 +0530 Subject: [PATCH] Fixed shellcheck issues in the script (#2650) * Changed for handling already upgrade in progress, changed frequency of cron and added command to upgrade without providing agent package Signed-off-by: Tanmay Garg * fixed shellcheck Signed-off-by: Tanmay Garg * Added scripts for EBS HRMS * fixed linter * fixed lintr checks * added copyright header * added scripts for EBSUM * added missing header * added readme * fixed linter * fixed linter issues * fixed linter issues * added custom jar * fixed custom jar upgrade --------- Signed-off-by: Tanmay Garg --- .../samples/scripts/agentManagement.sh | 343 ++++++++++-------- 1 file changed, 191 insertions(+), 152 deletions(-) diff --git a/OracleIdentityGovernance/samples/scripts/agentManagement.sh b/OracleIdentityGovernance/samples/scripts/agentManagement.sh index d98be65745..a11765fa36 100755 --- a/OracleIdentityGovernance/samples/scripts/agentManagement.sh +++ b/OracleIdentityGovernance/samples/scripts/agentManagement.sh @@ -19,25 +19,25 @@ newContainer="" agentVersion="" validateEmpty(){ - if [ "$1" == "" ]; + if [ "$1" = "" ]; then errorFlag=true - echo "ERROR: "$2" is mandatory." - echo " Specify Using "$3"." + echo "ERROR: $2 is mandatory." + echo " Specify Using $3." else - echo "INFO: Using "$1" for "$2"." + echo "INFO: Using $1 for $2." fi } createDir(){ - if [[ ! -d "$1" ]] + if [ ! -d "$1" ] then - echo "ERROR: Volume directory "$1" does not exist" + echo "ERROR: Volume directory $1 does not exist" exit 1; fi - absoluteVolumePath=$(cd "$(dirname "$1")"; pwd -P)/$(basename "$1") + absoluteVolumePath=$(cd "$(dirname "$1")" || exit 1; pwd -P)/$(basename "$1") CONFDIR="$absoluteVolumePath"/data/conf; LOGSDIR="$absoluteVolumePath"/data/logs; @@ -45,6 +45,7 @@ createDir(){ AGENTDIR="$absoluteVolumePath"/data/agent; METRICSDIR="$absoluteVolumePath"/data/metrics; BUNDLEDIR="$absoluteVolumePath"/data/bundle-home; + CUSTOMJARSDIR="$absoluteVolumePath"/data/customJars; if [ ! -d "$CONFDIR" ] then @@ -82,9 +83,15 @@ createDir(){ mkdir -p "$BUNDLEDIR"; fi + if [ ! -d "$CUSTOMJARSDIR" ] + then + echo "INFO: Creating custom driver jars directory" + mkdir -p "$CUSTOMJARSDIR"; + fi + if [ -d "$absoluteVolumePath"/data ] then - if [[ $(podman --version 2>/dev/null) ]] + if [ "$(podman --version 2>/dev/null)" ] then chmod -R 777 "$absoluteVolumePath"/data >/dev/null 2>&1 else @@ -103,7 +110,7 @@ createDir(){ isDockerAvailable() { - if [[ $(docker --version 2>/dev/null) ]] + if [ "$(docker --version 2>/dev/null)" ] then echo "INFO: Docker is available" echo "--------------------------------------------------" @@ -115,7 +122,7 @@ isDockerAvailable() isPodmanAvailable() { - if [[ $(podman --version 2>/dev/null) ]] + if [ "$(podman --version 2>/dev/null)" ] then echo "INFO: Podman is available" echo "--------------------------------------------------" @@ -128,14 +135,14 @@ isPodmanAvailable() detectJDKversion() { javac -version - if [[ "$?" != "0" ]] + if [ ! "$?" ] then echo "ERROR: JDK is not installed. Please install JDK 11" errorFlag=true return fi javaVersion=$(javac -version 2>&1 | awk '{ print $2 }' | cut -d'.' -f1) - if [[ "$javaVersion" != "11" ]] + if [ "$javaVersion" != "11" ] then echo "ERROR: JDK 11 is required" errorFlag=true @@ -146,12 +153,12 @@ detectContainerRuntime() { echo "INFO: Detecting Container Runtime" isPodmanAvailable - if [ -z "$containerRuntime" -a "$containerRuntime" == "" ] + if [ -z "$containerRuntime" ] && [ "$containerRuntime" = "" ] then isDockerAvailable fi - if [ -z "$containerRuntime" -a "$containerRuntime" == "" ] + if [ -z "$containerRuntime" ] && [ "$containerRuntime" = "" ] then echo "ERROR: No container runtime available. Please install Docker/Podman before proceeding" errorFlag=true @@ -161,10 +168,11 @@ detectContainerRuntime() echo containerRuntime=$containerRuntime >> "$ENV_FILE_TEMP" } +# shellcheck source=/dev/null copyAndUnzipAgentPackage() { - source "$ENV_FILE_TEMP" - rm -rf "$AGENTDIR"/* + . "$ENV_FILE_TEMP" + rm -rf "$AGENTDIR:?"/* unzip "$AP" -d "$AGENTDIR" } @@ -175,9 +183,10 @@ copyConfigOverride(){ fi } +# shellcheck source=/dev/null copyConfig(){ echo "INFO: Copying wallet and config.json" - source "$ENV_FILE_TEMP" + . "$ENV_FILE_TEMP" CONFFILE="$CONFDIR"/config.json WALLETFILE="$WALLETDIR"/cwallet.sso WALLETLOCKFILE="$WALLETDIR"/cwallet.sso.lck @@ -203,22 +212,24 @@ copyConfig(){ } +# shellcheck source=/dev/null setupConfig(){ -source "$ENV_FILE" +. "$ENV_FILE" echo "INFO: Setting up Configuration" -if [ "${AI}" == "" ]; +if [ "${AI}" = "" ]; then AI=agent_"$(hostname -f)"_"$(date +%s)" - echo AI=agent_"$(hostname -f)"_"$(date +%s)" >> $ENV_FILE_TEMP + echo AI=agent_"$(hostname -f)"_"$(date +%s)" >> "$ENV_FILE_TEMP" fi -if [ "${AU}" == "" ]; +if [ "${AU}" = "" ]; then AU=true - echo AU=true >> $ENV_FILE_TEMP + echo AU=true >> "$ENV_FILE_TEMP" fi +# shellcheck disable=SC3010,SC3028 if [[ "$OSTYPE" == "darwin"* ]]; then sed -i "" -e "s/__AGENT_ID__/${AI}/g" "$CONFDIR"/config.json else @@ -230,27 +241,28 @@ fi loadImage(){ echo "INFO: Loading container image. It may take some time." imageName="" - if [ "$containerRuntime" == "docker" ] + if [ "$containerRuntime" = "docker" ] then imageName=$(docker load < "$AGENTDIR"/agent-lcm/idm-agcs-agent-framework.dockerize_agent.tar.gz | grep "Loaded image" | awk '{ print $3 }') - elif [ "$containerRuntime" == "podman" ] + elif [ "$containerRuntime" = "podman" ] then imageName=$(podman load < "$AGENTDIR"/agent-lcm/idm-agcs-agent-framework.dockerize_agent.tar.gz | grep "Loaded image" | awk '{ print $3 }') fi - if [ "$imageName" == "" ] + if [ "$imageName" = "" ] then echo "ABORTED: Unable to load the image." exit 1; fi - echo imageName=$imageName >> $ENV_FILE_TEMP + echo imageName="$imageName" >> "$ENV_FILE_TEMP" } +# shellcheck source=/dev/null runAgent(){ groupId=$(id -g) - source "$ENV_FILE" - if [ "$containerRuntime" == "docker" ] + . "$ENV_FILE" + if [ "$containerRuntime" = "docker" ] then - if [[ ! $(docker ps -a -f "name=$AI" --format '{{.Names}}') ]] + if [ ! "$(docker ps -a -f "name=$AI" --format '{{.Names}}')" ] then echo "INFO: Starting new container" if [ -f "$CONFDIR"/config.properties ]; then @@ -260,38 +272,38 @@ runAgent(){ fi docker exec "$AI" /bin/bash -c 'agent ido validate --config /app/data/conf/config.json; if [[ "$?" != "0" ]] ; then echo VALIDATE_FAILED > /app/data/conf/status.txt; else echo VALIDATE_SUCCESS > /app/data/conf/status.txt; fi ;' validateStatus=$(cat "$CONFDIR"/status.txt) - if [[ "$validateStatus" == "VALIDATE_FAILED" ]] + if [ "$validateStatus" = "VALIDATE_FAILED" ] then echo "ERROR: Agent Validation Failed. Exiting" docker rm -f "$AI" exit 1; fi - if [[ ! "$operation" == "upgrade" ]] + if [ ! "$operation" = "upgrade" ] then docker exec "$AI" /bin/bash -c "agent ido start --config /app/data/conf/config.json &" fi - elif [[ ! $(docker ps -f "name=$AI" --format '{{.Names}}') ]] + elif [ ! "$(docker ps -f "name=$AI" --format '{{.Names}}')" ] then - echo "INFO: Starting existing container "$AI" " + echo "INFO: Starting existing container $AI " docker start "$AI" docker exec "$AI" /bin/bash -c 'agent ido validate --config /app/data/conf/config.json; if [[ "$?" != "0" ]] ; then echo VALIDATE_FAILED > /app/data/conf/status.txt; else echo VALIDATE_SUCCESS > /app/data/conf/status.txt; fi ;' validateStatus=$(cat "$CONFDIR"/status.txt) - if [[ "$validateStatus" == "VALIDATE_FAILED" ]] + if [ "$validateStatus" = "VALIDATE_FAILED" ] then echo "ERROR: Agent Validation Failed. Exiting" docker rm -f "$AI" exit 1; fi - if [[ ! "$operation" == "upgrade" ]] + if [ ! "$operation" = "upgrade" ] then docker exec "$AI" /bin/bash -c 'agent ido start --config /app/data/conf/config.json &' fi else echo "WARN: Agent is already running" fi - elif [ "$containerRuntime" == "podman" ] + elif [ "$containerRuntime" = "podman" ] then - if [[ ! $(podman ps -a -f "name=$AI" --format '{{.Names}}') ]] + if [ ! "$(podman ps -a -f "name=$AI" --format '{{.Names}}')" ] then echo "INFO: Starting new container" if [ -f "$CONFDIR"/config.properties ]; then @@ -302,30 +314,30 @@ runAgent(){ podman exec "$AI" /bin/bash -c 'agent ido validate --config /app/data/conf/config.json; if [[ "$?" != "0" ]] ; then echo VALIDATE_FAILED > /app/data/conf/status.txt; else echo VALIDATE_SUCCESS > /app/data/conf/status.txt; fi ;' validateStatus=$(cat "$CONFDIR"/status.txt) - if [[ "$validateStatus" == "VALIDATE_FAILED" ]] + if [ "$validateStatus" = "VALIDATE_FAILED" ] then echo "ERROR: Agent Validation Failed. Exiting" podman rm -f "$AI" exit 1; fi - if [[ ! "$operation" == "upgrade" ]] + if [ ! "$operation" = "upgrade" ] then podman exec "$AI" /bin/bash -c "agent ido start --config /app/data/conf/config.json &" fi - elif [[ ! $(podman ps -f "name=$AI" --format '{{.Names}}') ]] + elif [ ! "$(podman ps -f "name=$AI" --format '{{.Names}}')" ] then - echo "INFO: Starting existing container "$AI" " + echo "INFO: Starting existing container $AI " podman start "$AI" podman exec "$AI" /bin/bash -c 'agent ido validate --config /app/data/conf/config.json; if [[ "$?" != "0" ]] ; then echo VALIDATE_FAILED > /app/data/conf/status.txt; else echo VALIDATE_SUCCESS > /app/data/conf/status.txt; fi ;' validateStatus=$(cat "$CONFDIR"/status.txt) - if [[ "$validateStatus" == "VALIDATE_FAILED" ]] + if [ "$validateStatus" = "VALIDATE_FAILED" ] then echo "ERROR: Agent Validation Failed. Exiting" podman rm -f "$AI" exit 1; fi - if [[ ! "$operation" == "upgrade" ]] + if [ ! "$operation" = "upgrade" ] then podman exec "$AI" /bin/bash -c "agent ido start --config /app/data/conf/config.json &" fi @@ -343,6 +355,7 @@ hasDockerPermissions() isWriteAccessOnVolume() { + # shellcheck disable=SC2012 permissions=$(ls -ld "$PV" | awk '{print $1}') if [ "$permissions" != "drwxrwxr-x" ] && [ "$permissions" != "drwxrwxrwx" ] && [ "$permissions" != "drwxrwxr-x." ] && [ "$permissions" != "drwxrwxrwx." ]; then echo "ERROR: Volume does not have required permissions. Make sure to have 775" @@ -359,58 +372,59 @@ info(){ agentImageVersion=$(echo "$imageName" | cut -d':' -f2) echo "Agent Id : $AI" - if [ "$containerRuntime" == "docker" ] + if [ "$containerRuntime" = "docker" ] then echo "Container Runtime : $(docker --version)" - elif [ "$containerRuntime" == "podman" ] + elif [ "$containerRuntime" = "podman" ] then echo "Container Runtime : $(podman --version)" fi echo "Install Location : $PV" echo "Agent Package used : $AP" echo "Agent Version : $agentImageVersion" - echo "Logs directory : "${PV}"/data/logs" + echo "Logs directory : ${PV}/data/logs" } agentDaemonStatus(){ validateStatus=$(cat "$CONFDIR"/status.txt) - if [[ "$validateStatus" == "AGENT_RUNNING_NORMALLY" ]] + if [ "$validateStatus" = "AGENT_RUNNING_NORMALLY" ] then echo "Agent Status : Running normally" - elif [ "$validateStatus" == "AGENT_SHUTDOWN_IN_PROGRESS" ]; then + elif [ "$validateStatus" = "AGENT_SHUTDOWN_IN_PROGRESS" ]; then echo "Agent Status : Shutdown is in Progress" else echo "Agent Status : Stopped" fi } +# shellcheck source=/dev/null status(){ errorFlag=false if [ -f "$ENV_FILE" ] then - source "$ENV_FILE" + . "$ENV_FILE" fi validateEmpty "${PV}" "Volume" "--volume" isAgentAvailable - if [ $errorFlag == "true" ]; then + if [ $errorFlag = "true" ]; then echo "Agent is not installed." exit 1 fi agentVersion=$(grep agentVersion "$CONFDIR"/config.json | awk '{ print $2 }' | sed 's/,//g') info - if [ "$containerRuntime" == "docker" ] + if [ "$containerRuntime" = "docker" ] then - if [[ $(docker ps -f "name=$AI" --format '{{.Names}}') ]] + if [ "$(docker ps -f "name=$AI" --format '{{.Names}}')" ] then docker exec "$AI" /bin/bash -c 'agent --config /app/data/conf/config.json ido lcm -i status_check; if [[ "$?" == "0" ]] ; then echo AGENT_RUNNING_NORMALLY > /app/data/conf/status.txt; elif [[ "$?" == "1" ]] ; then echo AGENT_SHUTDOWN_IN_PROGRESS > /app/data/conf/status.txt; else echo AGENT_SHUTDOWN > /app/data/conf/status.txt; fi ;' >/dev/null agentDaemonStatus else echo "Agent Status : Container not running" fi - elif [ "$containerRuntime" == "podman" ] + elif [ "$containerRuntime" = "podman" ] then - if [[ $(podman ps -a -f "name=$AI" --format '{{.Names}}') ]] + if [ "$(podman ps -a -f "name=$AI" --format '{{.Names}}')" ] then podman exec "$AI" /bin/bash -c 'agent --config /app/data/conf/config.json ido lcm -i status_check; if [[ "$?" == "0" ]] ; then echo AGENT_RUNNING_NORMALLY > /app/data/conf/status.txt; elif [[ "$?" == "1" ]] ; then echo AGENT_SHUTDOWN_IN_PROGRESS > /app/data/conf/status.txt; else echo AGENT_SHUTDOWN > /app/data/conf/status.txt; fi ;' >/dev/null agentDaemonStatus @@ -420,10 +434,11 @@ status(){ fi } +# shellcheck source=/dev/null setproxy(){ # new/user provided configuration is stored in ENV_FILE_TEMP so sourcing it first - source "ENV_FILE" - source "ENV_FILE_TEMP" + . "$ENV_FILE" + . "$ENV_FILE_TEMP" echo "INFO: Setting proxy" # Set proxy params in config.json @@ -432,17 +447,18 @@ setproxy(){ } isAlreadyInstalled(){ - if [ "$isInstallSuccess" == "true" ] && [[ ! "$operation" == "upgrade" ]] +# shellcheck disable=SC2154 + if [ "$isInstallSuccess" = "true" ] && [ ! "$operation" = "upgrade" ] then - echo "INFO: Agent is already installed with agent id "${AI}" " + echo "INFO: Agent is already installed with agent id ${AI} " errorFlag=true fi } isAgentAvailable(){ - if [ "$isInstallSuccess" == "true" ] + if [ "$isInstallSuccess" = "true" ] then - echo "INFO: Agent with agent id "${AI}" is available." + echo "INFO: Agent with agent id ${AI} is available." else errorFlag=true fi @@ -450,6 +466,7 @@ isAgentAvailable(){ getProperty() { PROP_KEY=$1 +# shellcheck disable=SC2002 PROP_VALUE=$(cat "$CONFDIR"/config.properties | grep "$PROP_KEY" | cut -d'=' -f2) echo "$PROP_VALUE" } @@ -481,33 +498,34 @@ isValidChecksum() { echo "INFO: Verifying Integrity Check" java -jar "$AGENTDIR"/agent-lcm/idm-agcs-agent-lcm.jar validateIntegrity -w "$WALLETDIR" -p "$AGENTDIR"/agent-lcm/idm-agcs-agent-framework.dockerize_agent.tar.gz - if [[ "$?" != "0" ]] + if [ ! "$?" ] then echo "ERROR: Integrity Check Verification failed." exit 1 fi } +# shellcheck source=/dev/null install() { if [ -f "$ENV_FILE_TEMP" ] then - source "$ENV_FILE_TEMP" + . "$ENV_FILE_TEMP" fi if [ -f "$ENV_FILE" ] then - source "$ENV_FILE" + . "$ENV_FILE" fi validateEmpty "${AP}" "Agent Package" "--agentpackage" validateEmpty "${PV}" "Volume" "--volume" - if [[ ! -f "${AP}" ]] + if [ ! -f "${AP}" ] then echo "ERROR: Agent Package does not exist" exit 1; fi - if [[ ! -d "${PV}" ]] + if [ ! -d "${PV}" ] then echo "ERROR: Volume directory does not exist" exit 1; @@ -517,7 +535,7 @@ install() detectJDKversion isWriteAccessOnVolume detectContainerRuntime - if [ $errorFlag == "true" ]; then + if [ $errorFlag = "true" ]; then echo "ABORTED: Please rectify the errors. Use -h/--help option for help" exit 1 fi @@ -528,7 +546,7 @@ install() fetchAgentContainerImage isValidChecksum loadImage - if [ "$AU" == "true" ] + if [ "$AU" = "true" ] then enableAutoUpgrade fi @@ -537,116 +555,121 @@ install() cp "$ENV_FILE_TEMP" "$ENV_FILE" } +# shellcheck source=/dev/null enableAutoUpgrade(){ if [ -f "$ENV_FILE_TEMP" ] then - source "$ENV_FILE_TEMP" + . "$ENV_FILE_TEMP" fi if [ -f "$ENV_FILE" ] then - source "$ENV_FILE" + . "$ENV_FILE" fi validateEmpty "${PV}" "Volume" "--volume" detectJDKversion - if [ $errorFlag == "true" ]; then + if [ $errorFlag = "true" ]; then echo "ABORTED: Please rectify the errors. Use -h/--help option for help" exit 1 fi crontab -l > autoupdatercron alreadyExists=$(grep -rnw autoupdatercron -e "${AI}") - if [[ "" != "${alreadyExists}" ]] + if [ "" != "${alreadyExists}" ] then - echo "INFO: Auto Upgrade for the agent with id "${AI}" already exists. " + echo "INFO: Auto Upgrade for the agent with id ${AI} already exists. " else - echo "INFO: Setting Up Auto Upgrade of the agent with id "${AI}". " + echo "INFO: Setting Up Auto Upgrade of the agent with id ${AI}. " javaPath=$(which java | rev | cut -c6- | rev) - proxyUri=$(cat "$PV"/data/conf/config.properties | grep "idoConfig.httpClientConfiguration.proxyUri" | cut -d'=' -f2) - echo "INFO: Proxy URL is "${proxyUri}"" - if [[ "${proxyUri}" != "" ]] + proxyUri=$("$PV"/data/conf/config.properties | grep "idoConfig.httpClientConfiguration.proxyUri" | cut -d'=' -f2) + echo "INFO: Proxy URL is ${proxyUri}" + if [ "${proxyUri}" != "" ] then - echo "*/30 * * * * export HTTPS_PROXY="${proxyUri}";https_proxy="${proxyUri}";export PATH="${javaPath}":$PATH;curl https://raw.githubusercontent.com/oracle/docker-images/main/OracleIdentityGovernance/samples/scripts/agentAutoUpdate.sh -o "${PV}"/agentAutoUpdate.sh;sh "${PV}"/agentAutoUpdate.sh "${PV}" "${AI}" " >> autoupdatercron + echo "*/30 * * * * export HTTPS_PROXY=${proxyUri};https_proxy=${proxyUri};export PATH=${javaPath}:$PATH;curl https://raw.githubusercontent.com/oracle/docker-images/main/OracleIdentityGovernance/samples/scripts/agentAutoUpdate.sh -o ${PV}/agentAutoUpdate.sh;sh ${PV}/agentAutoUpdate.sh ${PV} ${AI} " >> autoupdatercron else - echo "*/30 * * * * export PATH="${javaPath}":$PATH;curl https://raw.githubusercontent.com/oracle/docker-images/main/OracleIdentityGovernance/samples/scripts/agentAutoUpdate.sh -o "${PV}"/agentAutoUpdate.sh;sh "${PV}"/agentAutoUpdate.sh "${PV}" "${AI}" " >> autoupdatercron + echo "*/30 * * * * export PATH=${javaPath}:$PATH;curl https://raw.githubusercontent.com/oracle/docker-images/main/OracleIdentityGovernance/samples/scripts/agentAutoUpdate.sh -o ${PV}/agentAutoUpdate.sh;sh ${PV}/agentAutoUpdate.sh ${PV} ${AI} " >> autoupdatercron fi crontab autoupdatercron rm autoupdatercron currentCron=$(crontab -l) - if [ "" == "${currentCron}" ] + if [ "" = "${currentCron}" ] then echo "INFO: No cron exists. Please try again." else echo "INFO: List of the current cron tabs" echo "${currentCron}" - echo "INFO: Successfully Set Up Auto Upgrade of the agent with id "${AI}"." + echo "INFO: Successfully Set Up Auto Upgrade of the agent with id ${AI}." fi fi } +# shellcheck source=/dev/null autoUpgrade(){ if [ -f "$ENV_FILE_TEMP" ] then - source "$ENV_FILE_TEMP" + . "$ENV_FILE_TEMP" fi if [ -f "$ENV_FILE" ] then - source "$ENV_FILE" + . "$ENV_FILE" fi validateEmpty "${PV}" "Volume" "--volume" detectJDKversion - if [ $errorFlag == "true" ]; then + if [ $errorFlag = "true" ]; then echo "ABORTED: Please rectify the errors. Use -h/--help option for help" exit 1 fi curl https://raw.githubusercontent.com/oracle/docker-images/main/OracleIdentityGovernance/samples/scripts/agentAutoUpdate.sh -o "${PV}"/agentAutoUpdate.sh;sh "${PV}"/agentAutoUpdate.sh "${PV}" "${AI}" } +# shellcheck source=/dev/null disableAutoUpgrade(){ if [ -f "$ENV_FILE_TEMP" ] then - source "$ENV_FILE_TEMP" + . "$ENV_FILE_TEMP" fi if [ -f "$ENV_FILE" ] then - source "$ENV_FILE" + . "$ENV_FILE" fi validateEmpty "${PV}" "Volume" "--volume" - if [ $errorFlag == "true" ]; then + if [ $errorFlag = "true" ]; then echo "ABORTED: Please rectify the errors. Use -h/--help option for help" exit 1 fi crontab -l > autoupdatercron ifExists=$(grep -rnw autoupdatercron -e "${AI}") - if [[ "" == "${ifExists}" ]] + if [ "" = "${ifExists}" ] then - echo "INFO: Auto Upgrade for the agent with id "${AI}" does not exist. " + echo "INFO: Auto Upgrade for the agent with id ${AI} does not exist. " else - echo "INFO: Removing Auto Upgrade of the agent with id "${AI}". " + echo "INFO: Removing Auto Upgrade of the agent with id ${AI}. " crontab -l | grep -v "${AI}" | crontab - rm autoupdatercron currentCron=$(crontab -l) - if [ "" == "${currentCron}" ] + if [ "" = "${currentCron}" ] then echo "INFO: No cron exists now." else echo "${currentCron}" fi - echo "INFO: Successfully Removed Auto Upgrade of the agent with id "${AI}"." + echo "INFO: Successfully Removed Auto Upgrade of the agent with id ${AI}." fi } + +# shellcheck source=/dev/null start() { if [ -f "$ENV_FILE" ] then - source "$ENV_FILE" + . "$ENV_FILE" fi validateEmpty "${PV}" "Volume" "--volume" - if [ $errorFlag == "true" ]; then + if [ $errorFlag = "true" ]; then echo "ABORTED: Please rectify the errors. Use -h/--help option for help" exit 1 fi @@ -660,29 +683,30 @@ start() agentVersion=$(grep agentVersion "$CONFDIR"/config.json | awk '{ print $2 }' | sed 's/,//g') info echo "" - echo "INFO: Logs directory: "${PV}"/data/logs" - echo "INFO: You can monitor the agent "${AI}" from the Access Governance Console." + echo "INFO: Logs directory: ${PV}/data/logs" + echo "INFO: You can monitor the agent ${AI} from the Access Governance Console." } +# shellcheck source=/dev/null stop() { echo "INFO: Gracefully Stopping Agent" if [ -f "$ENV_FILE" ] then - source "$ENV_FILE" + . "$ENV_FILE" fi validateEmpty "${PV}" "Volume" "--volume" - if [ $errorFlag == "true" ]; then + if [ $errorFlag = "true" ]; then echo "ABORTED: Please rectify the errors. Use -h/--help option for help" exit 1 fi - if [ "$containerRuntime" == "docker" ] + if [ "$containerRuntime" = "docker" ] then docker exec "$AI" /bin/bash -c "agent --config /app/data/conf/config.json ido lcm -i graceful_shutdown;" echo "INFO: Waiting for running operations to complete. It may take some time" docker exec "$AI" /bin/bash -c 'agent --config /app/data/conf/config.json ido lcm -i status_check; while [[ "$?" != "2" && "$?" != "255" ]]; do sleep 5s;agent --config /app/data/conf/config.json ido lcm -i status_check; done' >/dev/null docker stop "$AI" - elif [ "$containerRuntime" == "podman" ] + elif [ "$containerRuntime" = "podman" ] then podman exec "$AI" /bin/bash -c "agent --config /app/data/conf/config.json ido lcm -i graceful_shutdown;" echo "INFO: Waiting for running operations to complete. It may take some time" @@ -692,30 +716,31 @@ stop() echo "INFO: Agent Stopped" } +# shellcheck source=/dev/null kill() { if [ -f "$ENV_FILE" ] then - source "$ENV_FILE" + . "$ENV_FILE" fi validateEmpty "${PV}" "Volume" "--volume" - if [ $errorFlag == "true" ]; then + if [ $errorFlag = "true" ]; then echo "ABORTED: Please rectify the errors. Use -h/--help option for help" exit 1 fi - if [[ "$containerRuntime" == "docker" && $(docker ps -a -f "name=$AI" --format '{{.Names}}') ]] + if [ "$containerRuntime" = "docker" ] && [ "$(docker ps -a -f "name=$AI" --format '{{.Names}}')" ] then - if [[ ! "$operation" == "upgrade" ]] + if [ ! "$operation" = "upgrade" ] then docker exec "$AI" /bin/bash -c "agent --config /app/data/conf/config.json ido lcm -i graceful_shutdown;" echo "INFO: Waiting for running operations to complete. It may take some time" docker exec "$AI" /bin/bash -c 'agent --config /app/data/conf/config.json ido lcm -i status_check; while [[ "$?" != "2" && "$?" != "255" ]]; do sleep 5s;agent --config /app/data/conf/config.json ido lcm -i status_check; done' >/dev/null fi docker rm -f "$AI" - elif [[ "$containerRuntime" == "podman" && $(podman ps -a -f "name=$AI" --format '{{.Names}}') ]] + elif [ "$containerRuntime" = "podman" ] && [ "$(podman ps -a -f "name=$AI" --format '{{.Names}}')" ] then - if [[ ! "$operation" == "upgrade" ]] + if [ ! "$operation" = "upgrade" ] then podman exec "$AI" /bin/bash -c "agent --config /app/data/conf/config.json ido lcm -i graceful_shutdown;" echo "INFO: Waiting for running operations to complete. It may take some time" @@ -738,31 +763,32 @@ restoreBackup(){ cp -rf "${PV}"/backup "${PV}" } +# shellcheck source=/dev/null upgrade() { if [ -f "$ENV_FILE_TEMP" ] then - source "$ENV_FILE_TEMP" + . "$ENV_FILE_TEMP" fi #validate mandatory fields validateEmpty "${AP}" "New Agent Package" "--agentpackage" validateEmpty "${PV}" "Volume" "--volume" - if [ $errorFlag == "true" ]; then + if [ $errorFlag = "true" ]; then echo "ABORTED: Please rectify the errors. Use -h/--help option for help" exit 1 fi - if [[ ! "${AI}" == "" ]] + if [ ! "${AI}" = "" ] then - echo "WARN: Ignoring Agent Id "${AI}" " + echo "WARN: Ignoring Agent Id ${AI} " fi #store the new agent package into a variable newAgentPackage="${AP}" - source "$ENV_FILE" #older config file + . "$ENV_FILE" #older config file isAgentAvailable - if [ $errorFlag == "true" ]; then + if [ $errorFlag = "true" ]; then echo "ABORTED: Agent is not installed." rm -rf "$PV/data" rm -rf "$PV/upgrade" @@ -770,17 +796,16 @@ upgrade() exit 1 fi - echo "INFO: Upgrading Agent with id "${AI}" " + echo "INFO: Upgrading Agent with id ${AI} " installedPV="${PV}" installedAgentId="${AI}" - installedImage="${imageName}" #generate a new agent id for upgrade using old agent id AI="${installedAgentId}"_upgrade newAgentId=${AI} #createDir changes the current working directory mkdir -p "${PV}/upgrade" - if [[ $(podman --version 2>/dev/null) ]] + if [ "$(podman --version 2>/dev/null)" ] then chmod -R 777 "${PV}/upgrade" >/dev/null 2>&1 else @@ -788,12 +813,22 @@ upgrade() fi createDir "${PV}/upgrade" +# shellcheck disable=SC2129 echo AP="${newAgentPackage}" >> "$ENV_FILE_TEMP" echo PV="${PV}"/upgrade >> "$ENV_FILE_TEMP" echo AI="${AI}" >> "$ENV_FILE_TEMP" #install the upgrade operation=upgrade + # Following copies the customJars from the install location to the upgrade directory + if [ -d "${installedPV}/data/customJars" ] + then + if [ "$(ls -A "${installedPV}/data/customJars")" ] + then + echo "INFO: Copying custom jars" + cp -rf "${installedPV}/data/customJars" "${PV}/upgrade/data" + fi + fi install #install also loads the image, so we can get the new image here newimage="${imageName}" @@ -808,7 +843,7 @@ upgrade() createDir "${installedPV}" # sourcing installed config to kill the older container operation=postUpgrade - source "$ENV_FILE" + . "$ENV_FILE" createBackup echo "INFO: Removing the old agent" kill @@ -842,30 +877,30 @@ upgrade() start crontab -l > autoupdatercron alreadyExists=$(grep -rnw autoupdatercron -e "${AI}") - if [[ "" != "${alreadyExists}" ]] + if [ "" != "${alreadyExists}" ] then - echo "INFO: Auto Upgrade for the agent with id "${AI}" is already setup. " + echo "INFO: Auto Upgrade for the agent with id ${AI} is already setup. " else - echo "INFO: Setting Up Auto Upgrade of the agent with id "${AI}". " + echo "INFO: Setting Up Auto Upgrade of the agent with id ${AI}. " javaPath=$(which java | rev | cut -c6- | rev) - proxyUri=$(cat "$PV"/data/conf/config.properties | grep "idoConfig.httpClientConfiguration.proxyUri" | cut -d'=' -f2) - echo "INFO: Proxy URL is "${proxyUri}"" - if [[ "${proxyUri}" != "" ]] + proxyUri=$("$PV"/data/conf/config.properties | grep "idoConfig.httpClientConfiguration.proxyUri" | cut -d'=' -f2) + echo "INFO: Proxy URL is ${proxyUri}" + if [ "${proxyUri}" != "" ] then - echo "*/30 * * * * export HTTPS_PROXY="${proxyUri}";https_proxy="${proxyUri}";export PATH="${javaPath}":$PATH;curl https://raw.githubusercontent.com/oracle/docker-images/main/OracleIdentityGovernance/samples/scripts/agentAutoUpdate.sh -o "${PV}"/agentAutoUpdate.sh;sh "${PV}"/agentAutoUpdate.sh "${PV}" "${AI}" " >> autoupdatercron + echo "*/30 * * * * export HTTPS_PROXY=${proxyUri};https_proxy=${proxyUri};export PATH=${javaPath}:$PATH;curl https://raw.githubusercontent.com/oracle/docker-images/main/OracleIdentityGovernance/samples/scripts/agentAutoUpdate.sh -o ${PV}/agentAutoUpdate.sh;sh ${PV}/agentAutoUpdate.sh ${PV} ${AI} " >> autoupdatercron else - echo "*/30 * * * * export PATH="${javaPath}":$PATH;curl https://raw.githubusercontent.com/oracle/docker-images/main/OracleIdentityGovernance/samples/scripts/agentAutoUpdate.sh -o "${PV}"/agentAutoUpdate.sh;sh "${PV}"/agentAutoUpdate.sh "${PV}" "${AI}" " >> autoupdatercron + echo "*/30 * * * * export PATH=${javaPath}:$PATH;curl https://raw.githubusercontent.com/oracle/docker-images/main/OracleIdentityGovernance/samples/scripts/agentAutoUpdate.sh -o ${PV}/agentAutoUpdate.sh;sh ${PV}/agentAutoUpdate.sh ${PV} ${AI} " >> autoupdatercron fi crontab autoupdatercron rm autoupdatercron currentCron=$(crontab -l) - if [ "" == "${currentCron}" ] + if [ "" = "${currentCron}" ] then echo "INFO: No cron exists. Please try again." else echo "INFO: List of the current cron tabs" echo "${currentCron}" - echo "INFO: Successfully Set Up Auto Upgrade of the agent with id "${AI}"." + echo "INFO: Successfully Set Up Auto Upgrade of the agent with id ${AI}." fi fi rm -rf "${PV}/upgrade" @@ -874,12 +909,12 @@ upgrade() restart() { echo "INFO: Restarting Agent" - if [ "$newContainer" == "true" ] + if [ "$newContainer" = "true" ] then echo "WARN: This will remove the existing agent container and start a new one." echo "Are you sure to continue? [y/N]" - read input - if [[ $input == "y" || $input == "Y" ]] + read -r input + if [ "$input" = "y" ] || [ "$input" = "Y" ] then kill else @@ -892,21 +927,22 @@ restart() start } +# shellcheck source=/dev/null uninstall(){ echo "WARN: This will remove the existing agent and clean up the install directory." echo "Are you sure to continue? [y/N]" - read input - if [[ ! $input == "y" && ! $input == "Y" ]] + read -r input + if [ ! "$input" = "y" ] && [ ! "$input" = "Y" ] then exit 1; fi if [ -f "$ENV_FILE" ] then - source "$ENV_FILE" + . "$ENV_FILE" fi isAgentAvailable - if [ $errorFlag == "true" ]; then + if [ $errorFlag = "true" ]; then echo "ABORTED: Agent is not installed." exit 1 fi @@ -915,7 +951,7 @@ uninstall(){ disableAutoUpgrade if [ -d "${PV}" ] then - echo "INFO: Removing agent data from "${PV}" " + echo "INFO: Removing agent data from ${PV} " rm -rf "${PV}/data" rm -rf "${PV}/upgrade" rm -rf "${PV}/backup" @@ -923,35 +959,36 @@ uninstall(){ fi } +# shellcheck source=/dev/null rename(){ - source "$ENV_FILE_TEMP" + . "$ENV_FILE_TEMP" validateEmpty "${AI}" "Agent Id" "--agentid" - if [ $errorFlag == "true" ]; then + if [ $errorFlag = "true" ]; then echo "ABORTED: Please rectify the errors. Use -h/--help option for help" exit 1 fi newAgentId="${AI}" - source "$ENV_FILE" + . "$ENV_FILE" - if [ "$containerRuntime" == "docker" ] + if [ "$containerRuntime" = "docker" ] then - if [[ $(docker ps -a -f "name=$AI" --format '{{.Names}}') ]] + if [ "$(docker ps -a -f "name=$AI" --format '{{.Names}}')" ] then echo "INFO: Renaming Agent" docker rename "${AI}" "${newAgentId}" else - echo "INFO: No Container with the name "${AI}" is available to rename" + echo "INFO: No Container with the name ${AI} is available to rename" exit 1 fi - elif [ "$containerRuntime" == "podman" ] + elif [ "$containerRuntime" = "podman" ] then - if [[ $(podman ps -a -f "name=$AI" --format '{{.Names}}') ]] + if [ "$(podman ps -a -f "name=$AI" --format '{{.Names}}')" ] then echo "INFO: Renaming Agent" podman rename "${AI}" "${newAgentId}" else - echo "INFO: No Container with the name "${AI}" is available to rename" + echo "INFO: No Container with the name ${AI} is available to rename" exit 1 fi fi @@ -1027,21 +1064,23 @@ if [ $# -eq 0 ]; then Help; exit 1 fi -while [[ $# -gt 0 ]]; do +while [ $# -gt 0 ]; do opt="$1" shift; current_arg="$1" - if [[ "$current_arg" =~ ^-{1,2}.* ]]; then - echo "WARNING: You may have left an argument blank. Double check your command." - fi + case $current_arg in + -[!-]* | --*) + echo "WARNING: You may have left an argument blank. Double check your command." + ;; + esac case "$opt" in - "-pv"|"--volume" ) createDir "$1"; echo PV=$(cd "$(dirname "$1")"; pwd -P)/$(basename "$1") >> $ENV_FILE_TEMP; shift;; + "-pv"|"--volume" ) createDir "$1"; echo PV="$(cd "$(dirname "$1")" || exit 1; pwd -P)"/"$(basename "$1")" >> "$ENV_FILE_TEMP"; shift;; "-h"|"--help" ) Help; exit 1;; - "-ai"|"--agentid" ) echo AI="$1" >> $ENV_FILE_TEMP; shift;; - "-au"|"--autoupgrade" ) echo AU="$1" >> $ENV_FILE_TEMP; shift;; - "-ap"|"--agentpackage" ) echo AP=$(cd "$(dirname "$1")"; pwd -P)/$(basename "$1") >> $ENV_FILE_TEMP; shift;; - "-c"|"--config" ) configOverride=$(cd "$(dirname "$1")"; pwd -P)/$(basename "$1"); shift;; + "-ai"|"--agentid" ) echo AI="$1" >> "$ENV_FILE_TEMP"; shift;; + "-au"|"--autoupgrade" ) echo AU="$1" >> "$ENV_FILE_TEMP"; shift;; + "-ap"|"--agentpackage" ) echo AP="$(cd "$(dirname "$1")" || exit 1; pwd -P)"/"$(basename "$1")" >> "$ENV_FILE_TEMP"; shift;; + "-c"|"--config" ) configOverride=$(cd "$(dirname "$1")" || exit 1; pwd -P)/$(basename "$1"); shift;; "-nc"|"--newcontainer" ) newContainer=true;; "-i"|"--install" ) install; exit 1;; "-up"|"--upgrade" ) autoUpgrade; exit 1;; @@ -1053,7 +1092,7 @@ while [[ $# -gt 0 ]]; do "-sa"|"--status" ) status; exit 1;; "-eau"|"--enableautoupgrade" ) enableAutoUpgrade; exit 1;; "-dau"|"--disableautoupgrade" ) disableAutoUpgrade; exit 1;; - * ) echo "ERROR: agentManagement: Invalid option: \""$opt"\"" >&2 + * ) echo "ERROR: agentManagement: Invalid option: \"$opt\"" >&2 exit 1;; esac done