#!/bin/bash
CUR_PATH=$(cd "$(dirname "$0")" || { echo "Failed to enter current path" ; exit ; } ; pwd)
info_record_path="${HOME}/log/AgentSDK"
info_record_file="deployment.log"
info_record_file_back="deployment.log.bak"
log_file=$info_record_path/$info_record_file
LOG_SIZE_THRESHOLD=1024000
USER_N="$(whoami)" || exist 1
readonly USER_N
WHO_PATH="$(which who)" || exist 1
readonly WHO_PATH
CUT_PATH="$(which cut)" || exist 1
readonly CUT_PATH
IP_N="$("${WHO_PATH}" -m | "${CUT_PATH}" -d '(' -f 2 | "${CUT_PATH}" -d ')' -f 1)"
if [ "${IP_N}" = "" ]; then
IP_N="localhost"
fi
function rotate_log() {
check_path "$log_file"
mv -f "$log_file" "$info_record_path/$info_record_file_back"
touch "$log_file" 2>/dev/null
check_path "$info_record_path/$info_record_file_back"
chmod 440 "$info_record_path/$info_record_file_back"
check_path "$log_file"
chmod 640 "$log_file"
}
function check_path() {
if [ "$1" != "$(realpath "$1")" ]; then
echo "Log file is not support symlink, exiting!"
exit 1
fi
}
function log_check() {
local log_size
log_size="$(find "$log_file" -exec ls -l {} \; | awk '{ print $5 }')"
if [[ "${log_size}" -ge "${LOG_SIZE_THRESHOLD}" ]];then
rotate_log
fi
}
function log() {
if [ "$log_file" = "" ]; then
echo "[$(date +%Y%m%d-%H:%M:%S)] [$USER_N] [$IP_N]: $1" >&2
fi
if [ -f "$log_file" ]; then
log_check
if ! echo "[$(date +%Y%m%d-%H:%M:%S)] [$USER_N] [$IP_N]: $1" >> "$log_file"; then
echo "Can not write log, exiting!"
exit 1
fi
else
echo "Log file does not exist, exiting!"
exit 1
fi
}
get_run_path() {
run_path=$(pwd)
cd ..
if [[ "$run_path" =~ /agent ]];then
suffix='agent'
else
log "Error: Directory agent does not exist, exiting!"
echo "Directory agent does not exist in path[$run_path], exiting!"
exit 1
fi
del_path=$(pwd)/"$suffix"
}
real_delete() {
cd "${CUR_PATH}/.." || {
echo "Cannot locate Agent SDK directory: ${CUR_PATH}/.."
log "Error: Cannot locate Agent SDK directory!"
exit 255
}
get_run_path
if [[ -f "$del_path"/version.info ]];then
version_info="$del_path"/version.info
if [ ! -d "$info_record_path" ];then
mkdir -p "$info_record_path"
chmod 750 "$info_record_path"
fi
if [[ ! -f "$log_file" ]];then
touch "$log_file"
fi
find "$log_file" -type f -exec chmod 640 {} +
log "$(cat "${version_info}")"
python3 -m pip uninstall aura -y
if test $? -ne 0; then
log "Error: Uninstall wheel package failed."
exit 1
fi
chmod u+w -R "$del_path"
del_real_path=$(realpath "$del_path")
if [ -d "$del_path" ];then
rm -rf "$del_path"
fi
if [ -d "$del_real_path" ];then
rm -rf "$del_real_path"
fi
log "Uninstall Agent SDK package successfully."
echo 'Uninstall Agent SDK package successfully.'
else
log "Uninstall Agent SDK package failed, version info is missed."
echo 'Uninstall Agent SDK package failed, version info is missed.'
fi
}
real_delete