#!/bin/bash
readonly PACKAGE_NAME="Ascend-spc_pyACL"
readonly PYACL_PACKAGE_SHORT_NAME="pyACL"
PACKAGE_VERSION=""
spc_path="$(
cd "$(dirname "$0")/../../"
pwd
)"
if [ ! -d "$spc_path/backup/pyACL" ]; then
echo "ERROR" "The backup directory $spc_path/backup/pyACL/ does not exist. Rollback failed"
exit 1
fi
spc_install_path="$(
cd "$(dirname "$0")/../../backup/pyACL/pyACL/"
pwd
)"
install_path="$(
cd "$(dirname "$0")/../../../"
pwd
)"
if [ $(id -u) -ne 0 ]; then
log_dir="${HOME}/var/log/ascend_seclog"
else
log_dir="/var/log/ascend_seclog"
fi
log_file="${log_dir}/ascend_install.log"
function print() {
echo "[pyACL] [$(date +"%Y-%m-%d %H:%M:%S")] [$1]: $2" | tee -a $log_file
}
function log_init() {
if [ ! -f "$log_file" ]; then
touch $log_file
if [ $? -ne 0 ]; then
print "ERROR" "touch $log_file permission denied"
exit 1
fi
fi
chmod 640 $log_file
}
function del_file() {
file_path=$1
if [ -f "${file_path}" ]; then
chmod 750 $1
rm -f "${file_path}"
if [ $? = 0 ]; then
print "INFO" "delete file ${file_path} successfully"
else
print "ERROR" "delete file ${file_path} fail"
exit 1
fi
else
print "WARNING" "the file ${file_path} is not exist"
fi
}
function del_dir() {
local dir_path=$1
if [ -n "${dir_path}" ] && [[ ! "${dir_path}" =~ ^/+$ ]]; then
if [ -d "${dir_path}" ]; then
chmod 750 -R ${dir_path}
rm -rf "${dir_path}"
if [ $? = 0 ]; then
print "INFO" "delete directory ${dir_path} successfully"
else
print "ERROR" "delete directory ${dir_path} fail"
exit 1
fi
else
print "WARNING" "the directory ${dir_path} is not exist"
fi
else
print "WARNING" "the directory ${dir_path} path is NULL"
fi
}
function cp_file() {
if [ -f "$1" ]; then
cp -f $1 $2
if [ $? = 0 ]; then
print "INFO" "copy $3 to directory $2 successfully"
else
print "ERROR" "copy $3 to directory $2 fail"
exit 1
fi
else
print "ERROR" "the file $1 is not exist"
exit 1
fi
}
function get_ver() {
local ver
ver=`grep Version $1 | cut -d '=' -f 2`
if [ $? != 0 ]; then
print "ERROR" "grep the version.info fail"
exit 1
fi
echo $ver
}
function deal_rollback() {
spc_python_path=$spc_path/backup/pyACL/python/site-packages/acl.so
acl_path=$install_path/python/site-packages
if [ -f "${acl_path}/acl.so" ]; then
cp_file ${spc_python_path} ${acl_path} "acl.so"
else
print "WARNING" "the file ${acl_path}/acl.so is not exist"
fi
spc_pyacl_path=$spc_install_path/version.info
cur_ver=`get_ver "$spc_pyacl_path"`
ver_per==`stat -c %a "$spc_pyacl_path"`
if [ -f "${install_path}/pyACL/version.info" ]; then
chmod 750 "${install_path}/pyACL/version.info"
cp_file ${spc_pyacl_path} ${install_path}/pyACL "version.info"
chmod $ver_per "${install_path}/pyACL/version.info"
else
print "WARNING" "the file ${install_path}/pyACL/version.info is not exist"
fi
if [ -d "$spc_path/backup/pyACL" ]; then
chmod 750 -R $spc_path/backup/pyACL
fi
del_file "$spc_pyacl_path"
del_file "$spc_python_path"
del_dir "$spc_path/backup/pyACL/python/site-packages"
del_dir "$spc_path/backup/pyACL"
print "INFO" "${PACKAGE_NAME}-${PACKAGE_VERSION} rolled back successfully, The current version is $cur_ver"
}
log_init
deal_rollback