schleuder3/migrate_so36_schleuder2_lists.sh

505 lines
12 KiB
Bash
Executable File

#!/usr/bin/env bash
script_name="$(basename $(realpath $0))"
working_dir="$(dirname "$(realpath $0)")"
conf_file="${working_dir}/conf/${script_name%%.*}.conf"
_date="$(date +%Y-%m-%d-%H%M)"
log_dir="${working_dir}/log"
log_file="${log_dir}/${script_name%%.*}.${_date}.log"
LOCK_DIR="$(mktemp -d)"
err_msg="${LOCK_DIR}/err_msg.log"
#-----------------------------
# Some variables definitions
#-----------------------------
declare -a schleuder_domains_arr
declare -a schleuder_lists_arr
declare -a lists_done
declare -a lists_failed
declare -a lists_skipped
#-----------------------------
# Base Function(s)
#-----------------------------
clean_up() {
# Perform program exit housekeeping
rm -rf $LOCK_DIR
exit $1
}
echo_log() {
echo "$*" >> "$log_file"
}
echo_log_done() {
echo "[ done ] $*" >> "$log_file"
}
echo_log_failed() {
echo "[ Error ] $*" >> "$log_file"
}
echo_log_skipped() {
echo "[ skipped ] $*" >> "$log_file"
}
echononl(){
if $terminal ; then
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
else
echo -n "$*"
fi
}
fatal(){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
echo ""
echo -e " \033[1mScript terminated\033[m.."
else
echo " [ Fatal ]: $*"
echo ""
echo " Script terminated.."
fi
echo ""
clean_up 1
}
error (){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
else
echo " [ Error ]: $*"
fi
echo ""
}
warn (){
echo ""
if $terminal ; then
echo -e " [ \033[33m\033[1mWarn\033[m ]: $*"
else
echo " [ Warn ]: $*"
fi
echo ""
}
info (){
echo ""
if $terminal ; then
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
else
echo " [ Info ]: $*"
fi
echo ""
}
echo_done() {
if $terminal ; then
echo -e "\033[75G[ \033[32mdone\033[m ]"
else
echo " [ done ]"
fi
}
echo_failed(){
if $terminal ; then
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
else
echo " [ failed ]"
fi
}
echo_skipped() {
if $terminal ; then
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
else
echo " [ skipped ]"
fi
}
# - See https://gist.github.com/epiloque/8cf512c6d64641bde388
parse_yaml() {
local prefix=$2
local s
local w
local fs
s='[[:space:]]*'
w='[a-zA-Z0-9_]*'
#fs="$(echo @|tr @ '\034')"
fs="$(echo @|tr @ '\034'|tr -d '\015')"
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
awk -F"$fs" '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);
}
}' | sed 's/_=/+=/g'
}
# ----------
# - Jobhandling
# ----------
# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM
# -
trap clean_up SIGHUP SIGINT SIGTERM
# - Create lock directory '$LOCK_DIR"
#
mkdir "$LOCK_DIR"
# ----------
# - Some checks ..
# ----------
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
# ==========
# - Begin Main Script
# ==========
# ----------
# - Headline
# ----------
if $terminal ; then
clear
echo ""
echo -e "\033[1m----------\033[m"
echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m"
echo -e "\033[1m----------\033[m"
fi
# ----------
# Read Configurations from $conf_file
# ----------
# - Give your default values here
# -
DEFAULT_GLOBAL_SCHLEUDER_CONF_FILE="/etc/schleuder/schleuder.yml"
DEFAULT_SCHLEUDER_2_BASE_DIR="/data/schleuder2"
if [[ -f "$conf_file" ]]; then
source "$conf_file"
else
warn "No configuration file '$conf_file' present.\n
Loading default values.."
fi
[[ -z "$GLOBAL_SCHLEUDER_CONF_FILE" ]] && GLOBAL_SCHLEUDER_CONF_FILE="$DEFAULT_GLOBAL_SCHLEUDER_CONF_FILE"
[[ -z "$SCHLEUDER_2_BASE_DIR" ]] && SCHLEUDER_2_BASE_DIR="$DEFAULT_SCHLEUDER_2_BASE_DIR"
# - Read in global schleuder configuration
# -
if [[ ! -f "$GLOBAL_SCHLEUDER_CONF_FILE" ]]; then
fatal "Configuration file '$(basename ${GLOBAL_SCHLEUDER_CONF_FILE})' not found!"
else
eval $(parse_yaml "$GLOBAL_SCHLEUDER_CONF_FILE" "schleuder_" )
fi
schleuder_user="$(stat -c '%U' "${schleuder_lists_dir}")"
schleuder_group="$(stat -c '%G' "${schleuder_lists_dir}")"
if ! id -u "$schleuder_user" > "$err_msg" 2>&1; then
error "$(cat "$err_msg")"
fatal "(Schleuder) User '$schleuder_user' does not exists"
fi
if $terminal ; then
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo ""
echo -e " \033[1;32mSettings for script \033[1m$(basename "$0")\033[m"
else
echo ""
echo ""
echo " Settings for script $(basename "$0"):"
fi
echo ""
echo " Global Schleuder3 Configuration..: $GLOBAL_SCHLEUDER_CONF_FILE"
echo " Schleuder User...................: $schleuder_user"
echo ""
echo " Schleuder 2 Base Directory.......: $SCHLEUDER_2_BASE_DIR"
echo ""
if $terminal ; then
echo ""
echononl "Type upper case \033[1;37mYES\033[m to continue executing this script: "
read OK
if [[ "$OK" != "YES" ]] ; then
fatal "Abort by user request - Answer as not 'YES'"
else
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo -e " \033[32m----------\033[m"
echo -e " \033[1mStarting Script to inform all Listadmins\033[m"
echo -e " \033[32m----------\033[m"
fi
else
echo ""
echo " ----------"
echo " Starting Script to inform all Listadmins"
echo " ----------"
fi
echo ""
echononl " Create log directory"
if [[ ! -d "$log_dir" ]]; then
mkdir "$log_dir" > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo_failed
else
echo_done
fi
else
echo_skipped
fi
echo_log "# ----------------------------------------------------------------"
echo_log "# - Log output of script '$(basename $0)' at $_date"
echo_log "# ----------------------------------------------------------------"
echo_log ""
echo_log "----------"
echo_log "Settings for script $(basename "$0"):"
echo_log "----------"
echo_log ""
echo_log ""
echo_log "Global Schleuder3 Configuration..: $GLOBAL_SCHLEUDER_CONF_FILE"
echo_log "Schleuder User...................: $schleuder_user"
echo_log "Schleuder 2 Base Directory.......: $SCHLEUDER_2_BASE_DIR"
echo_log ""
if $terminal ; then
echo ""
echo ""
echo -e " \033[32m----------\033[m"
echo -e " \033[1mSummary Schleuder2 Lists\033[m"
echo -e " \033[32m----------\033[m"
echo ""
else
echo ""
echo ""
echo " ----------"
echo " Summary Schleuder2 Lists"
echo " ----------"
echo ""
fi
echo_log ""
echo_log ""
echo_log "----------"
echo_log "Summary Schleuder2 Lists"
echo_log "----------"
echo_log
# - Get Schleuder2 Domains
# -
while IFS= read -r -d '' _dir ; do
[[ "$(basename "$_dir")" = "DELETED" ]] && continue
[[ "$(basename "$_dir")" = "logs" ]] && continue
schleuder_domains_arr+=("$(basename "$_dir")")
done < <(find "$SCHLEUDER_2_BASE_DIR" -mindepth 1 -maxdepth 1 -type d -print0)
echo_log "Schleuder2 Domains:"
if $terminal ; then
echo -e " \033[32m\033[1mSchleuder Domains\033[m:"
else
echo " Schleuder2 Domains:"
fi
for _domain in ${schleuder_domains_arr[@]} ; do
echo_log " $_domain"
echo " $_domain"
done
# - Get Schleuder2 Lists
# -
for _domain in ${schleuder_domains_arr[@]} ; do
while IFS= read -r -d '' _list ; do
schleuder_lists_arr+=("$_domain:$(basename "$_list")")
done < <(find "${SCHLEUDER_2_BASE_DIR}/${_domain}" -mindepth 1 -maxdepth 1 -type d -print0 | sort -z )
done
echo_log ""
echo_log "Schleuder2 Lists"
if $terminal ; then
echo -e "\n \033[32m\033[1mSchleuder2 Lists\033[m:"
else
echo ""
echo " Schleuder2 Lists"
fi
for _val in "${schleuder_lists_arr[@]}" ; do
IFS=':' read -a _val_arr <<< "${_val}"
_domain="${_val_arr[0]}"
_list="${_val_arr[1]}"
if [[ -n "$_last_domain" ]] && [[ "$_last_domain" != "$_domain" ]] ; then
echo_log ""
echo ""
fi
echo_log " ${_domain}: ${_list}"
echo " ${_domain}: ${_list}"
_last_domain="$_domain"
done
echo_log
echo_log "Total number of schleuder2 lists: ${#schleuder_lists_arr[@]}"
if $terminal ; then
echo -e "\n \033[32m\033[1mTotal number of schleuder2 lists:\033[m: ${#schleuder_lists_arr[@]}"
else
echo ""
echo " Total number of schleuder2 lists: ${#schleuder_lists_arr[@]}"
fi
if $terminal ; then
echo ""
echo ""
echo -e " \033[32m----------\033[m"
echo -e " \033[1mMain part of script: migrate schleuder2 lists to schleuder3\033[m"
echo -e " \033[32m----------\033[m"
else
echo ""
echo ""
echo " ----------"
echo " Main part of script: migrate schleuder2 lists to schleuder3"
echo " ----------"
fi
echo ""
echo_log ""
echo_log ""
echo_log "----------"
echo_log "Main part of script: migrate schleuder2 lists to schleuder3"
echo_log "----------"
echo_log ""
for _val in ${schleuder_lists_arr[@]} ; do
IFS=':' read -a _val_arr <<< "${_val}"
_domain="${_val_arr[0]}"
_list="${_val_arr[1]}"
echononl " Migrate list '${_list}@${_domain}' .."
if [[ -d "$schleuder_lists_dir/${_domain}/${_list}" ]] ; then
echo_skipped
echo_log_skipped "Migrate list '${_list}@${_domain}' .."
lists_skipped+=("${_list}@$_domain")
else
su $schleuder_user -c "schleuder migrate-v2-list ${SCHLEUDER_2_BASE_DIR}/${_domain}/$_list" -s /bin/bash \
> "$err_msg" 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat "$err_msg")"
echo_log_failed "Migrate list '${_list}@${_domain}' .."
echo_log "su $schleuder_user -c \"schleuder migrate-v2-list ${SCHLEUDER_2_BASE_DIR}/${_domain}/$_list\" -s /bin/bash"
echo_log "$(cat "$err_msg")"
lists_failed+=("${_list}@$_domain")
else
echo_done
echo_log_done "Migrate list '${_list}@${_domain}' .."
lists_done+=("${_list}@$_domain")
fi
fi
done
if $terminal ; then
echo ""
echo ""
echo -e " \033[32m----------\033[m"
echo -e " \033[1mStatistics \033[m"
echo -e " \033[32m----------\033[m"
echo ""
echo -e " Total number of lists: ${#schleuder_lists_arr[@]}"
echo ""
echo -e " ${#lists_done[@]} lists successfully migrated."
echo -e " ${#lists_failed[@]} lists failed"
echo -e " ${#lists_skipped[@]} lists skipped - already migrated."
else
echo ""
echo ""
echo " ----------"
echo " Statistics"
echo " ----------"
echo ""
echo " Total number of lists: ${#schleuder_lists_arr[@]}"
echo ""
echo " ${#lists_done[@]} lists successfully migrated."
echo " ${#lists_failed[@]} lists failed."
echo " ${#lists_skipped[@]} lists skipped - already migrated."
fi
echo_log ""
echo_log ""
echo_log "----------"
echo_log "Statistics"
echo_log "----------"
echo_log
echo_log "Total number of lists: ${#schleuder_lists_arr[@]}"
echo_log
echo_log "${#lists_done[@]} lists successfully migrated."
echo_log "${#lists_failed[@]} lists failed"
echo_log "${#lists_skipped[@]} lists skipped."
echo_log
if [[ ${#lists_failed[@]} -gt 0 ]]; then
echo_log "Lists NOT successfully migrated:"
if $terminal ; then
echo -e "\n \033[1mList NOT successfully migrated:\033[m"
else
echo ""
echo " List NOT successfully migrated:"
fi
for _list in "${lists_failed[@]}" ; do
echo -e " $_list"
echo_log " $_list"
done
echo_log
info "See also log file: $log_file"
else
info "See also script output at log file: $log_file"
fi
echo ""
clean_up 0