From: Frank Brehm Date: Wed, 22 Nov 2017 10:06:09 +0000 (+0100) Subject: Moved some scripts -> bin/ X-Git-Tag: 0.1.2~68 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=f4e98a5cda0c669d521f16710059ce24c166cc02;p=pixelpark%2Fadmin-tools.git Moved some scripts -> bin/ --- diff --git a/archive-old-homes.sh b/archive-old-homes.sh deleted file mode 100755 index b17fbc6..0000000 --- a/archive-old-homes.sh +++ /dev/null @@ -1,451 +0,0 @@ -#!/bin/bash -################################################################################ -## -## Header details -## -## Host..............: achilles.pixelpark.com -## Zone..............: N/A -## -## Copyright ........: Pixelpark AG -## File ........ ....: archive-old-homes.sh -## Maintained by ....: Frank Brehm -## Description ......: -## -## -## History ..........: -## -## Vers. Date By Reason -## ----- ----------- ---------------- --------------------------------------- -## 1.0 09.01.2009 thomas.kotschok initial Version -## 2.0 18.04.2017 frank.brehm modifying for running on Linux -## -################################################################################ - -set -e -set -u - -VERBOSE="n" -DEBUG="n" - -VERSION="2.1" - -# console colors: -RED="" -YELLOW="" -GREEN="" -BLUE="" -NORMAL="" - -HAS_TTY='y' - -DO_ASK="n" -SIMULATE="n" -NO_REMOVE="n" - -BASENAME="$(basename ${0})" -BASE_DIR="$(dirname ${0})" - -INPUT_TIMEOUT=5 - -DATE=$( date '+%G%m%d' ) - -HOMES_DIR="/mnt/nfs/home" -SAVEPATH="${HOMES_DIR}/_old_homes" -NODENAME=$( uname -n ) -LOGFILE="${SAVEPATH}/pp_archive_old_homes.log" -FILELIST="" - -declare -a DIRS2ARCH=() -declare -A BACKUP_FILES=() - -#------------------------------------------------------------------- -detect_color() { - - local safe_term="${TERM//[^[:alnum:]]/?}" - local match_lhs="" - local use_color="false" - [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)" - [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(/dev/null \ - && match_lhs=$(dircolors --print-database) - [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color="true" - - # console colors: - if [ "${use_color}" = "true" ] ; then - RED="\033[38;5;196m" - YELLOW="\033[38;5;226m" - GREEN="\033[38;5;46m" - BLUE="\033[38;5;27m" - NORMAL="\033[39m" - else - RED="" - YELLOW="" - GREEN="" - BLUE="" - NORMAL="" - fi - - local my_tty=$(tty) - if [[ "${my_tty}" =~ 'not a tty' ]] ; then - my_tty='-' - fi - - if [[ "${my_tty}" = '-' || "${safe_term}" = "dump" ]] ; then - HAS_TTY='n' - fi - -} -detect_color - -#------------------------------------------------------------------------------ -description() { - echo -e $( cat <<-EOF - Archiving and removing all given directories beneath '${HOMES_DIR}'. - - EOF - ) -} - -#------------------------------------------------------------------------------ -usage() { - cat <<-EOF - Usage: ${BASENAME} [OPTIONS] [ [&2 - usage >&2 - exit 1 - fi - set -e - - # Note the quotes around `$TEMP': they are essential! - eval set -- "${tmp}" - - local p= - - while true ; do - case "$1" in - -a|--ask) - DO_ASK="y" - shift - ;; - -s|--simulate) - SIMULATE="y" - shift - ;; - -N|--no-remove) - NO_REMOVE="y" - shift - ;; - -d|--debug) - DEBUG="y" - shift - ;; - -v|--verbose) - VERBOSE="y" - shift - ;; - --nocolor) - RED="" - YELLOW="" - GREEN="" - BLUE="" - NORMAL="" - shift - ;; - -h|--help) - description - usage - exit 0 - ;; - -V|--version) - echo "${BASENAME} version: ${VERSION}" - exit 0 - ;; - --) shift - break - ;; - *) echo "Internal error!" - exit 1 - ;; - esac - done - - if [[ "${DEBUG}" = "y" ]] ; then - set -x - fi - - if [[ "$#" -lt 1 ]] ; then - DO_ASK="y" - else - local item - for item in "$@" ; do - base_dir=$( basename "${item}" ) - add_dir "${base_dir}" - done - if [[ "${#DIRS2ARCH[*]}" == "0" ]] ; then - DO_ASK="y" - fi - fi - -} - -######################################### -# Some often used funktions - -#------------------------------------------------------------------------------ -my_date() { - date +'%F %T.%N %:::z' -} - -#------------------------------------------------------------------------------ -debug() { - if [[ "${VERBOSE}" != "y" ]] ; then - return 0 - fi - echo -e " * [$(my_date)] [${BASENAME}:DEBUG]: $@" >&2 -} - -#------------------------------------------------------------------------------ -info() { - echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2 -} - -#------------------------------------------------------------------------------ -warn() { - echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2 -} - -#------------------------------------------------------------------------------ -error() { - echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2 -} - -if [[ ! -d "${HOMES_DIR}" ]] ; then - echo "Directory '${HOMES_DIR}' does not exists." >&2 - exit 4 -fi -HOMES_DIR=$( readlink -f "${HOMES_DIR}" ) - -if [[ ! -d "${SAVEPATH}" ]] ; then - echo "Directory '${SAVEPATH}' does not exists." >&2 - exit 5 -fi -SAVEPATH=$( readlink -f "${SAVEPATH}" ) - -#------------------------------------------------------------------------------ -add_dir() { - - local base_dir="$1" - - local i= - local real_path="${HOMES_DIR}/${base_dir}" - local backup_file= - - if [[ ! -e "${real_path}" ]] ; then - error "Directory '${real_path}' does not exists." - return 0 - fi - - real_path=$( readlink -f "${real_path}" ) - if [[ "${real_path}" == "${SAVEPATH}" ]] ; then - error "Directory '${real_path}' is the backup directory." - return 0 - fi - - i=0 - backup_file="${SAVEPATH}/${base_dir}.${i}.tar.gz" - while [[ -e "${backup_file}" ]] ; do - i=$(( $i + 1 )) - backup_file="${SAVEPATH}/${base_dir}.${i}.tar.gz" - done - - info "ADD ${base_dir} -> ${backup_file}" - DIRS2ARCH+=("${base_dir}") - BACKUP_FILES[${base_dir}]="${backup_file}" - -} - -#------------------------------------------------------------------------------ -create_file_list() { - - local cont="1" - local input= - local base_dir= - local ret= - - info "Create a file list ..." - echo - - # constructing comand - echo - - while [[ "${cont}" -eq 1 ]] ; do - - echo "######################################" - echo " q = Done " - echo " e = Exit " - echo "weiteren hinzufügen oder mit \"q\" beenden?" - echo "######################################" - echo "" - - set +e - read -p "Directory [|q|e]: " -t "${INPUT_TIMEOUT}" input - ret="$?" - set -e - if [[ -z "${input}" ]] ; then - echo - fi - debug "Got input: \"${input}\", return value: ${ret}." - if [[ -z "${input}" ]] ; then - if [[ ${ret} -ne 0 ]] ; then - error "Input ${RED}timeout after ${INPUT_TIMEOUT} seconds${NORMAL}." - exit 1 - fi - continue - fi - case "${input}" in - q|Q) - cont=0 - ;; - e|E) - info "Exit" - exit 0 - ;; - *) - base_dir=$( basename "${input}" ) - add_dir "${base_dir}" - ;; - esac - done - -} - -#------------------------------------------------------------------------------ -backup_archive() { - - local base_dir= - local backup_file= - local real_path= - local cmd= - local annotation="Old home archive from ${NODENAME} -> ${SAVEPATH} - Date ${DATE}" - - if [[ "${SIMULATE}" != "y" ]] ; then - echo | tee -a "${LOGFILE}" - echo "${annotation}" | tee -a "${LOGFILE}" - else - echo - echo "${annotation}" - fi - cd "${HOMES_DIR}" - - for base_dir in "${DIRS2ARCH[@]}" ; do - - echo - debug "Performing '${base_dir}' ..." - - backup_file="${BACKUP_FILES[${base_dir}]}" - real_path="${HOMES_DIR}/${base_dir}" - - info "Creating '${GREEN}${backup_file}${NORMAL}' ..." - - cmd="tar --create --gzip --file=\"${backup_file}\" \"${base_dir}\"" - if [[ "${VERBOSE}" == "y" ]] ; then - echo "${cmd}" - fi - eval ${cmd} - - debug "Setting timestamps of '${backup_file}' ..." - cmd="touch -r \"${real_path}\" \"${backup_file}\"" - if [[ "${VERBOSE}" == "y" ]] ; then - echo "${cmd}" - fi - eval ${cmd} - - if [[ "${NO_REMOVE}" != "y" ]] ; then - info "Removing '${GREEN}${real_path}${NORMAL}' ..." - cmd="rm -rf \"${base_dir}\"" - if [[ "${VERBOSE}" == "y" ]] ; then - echo "${cmd}" - fi - if [[ "${SIMULATE}" != "y" ]] ; then - eval ${cmd} - fi - fi - - if [[ "${SIMULATE}" != "y" ]] ; then - echo " - ${base_dir} ${backup_file}" >> "${LOGFILE}" - fi - printf " " - ls -l "${backup_file}" || true - - if [[ "${SIMULATE}" == "y" ]] ; then - debug "Removing '${backup_file}' ..." - rm -f "${backup_file}" - fi - - done - -} - -################################################################################ -## -## Main -## -################################################################################ - -#------------------------------------------------------------------------------ -main() { - - get_options "$@" - - if [[ "${SIMULATE}" == "y" ]] ; then - info "Simulation mode, nothing is really done." - fi - - if [[ "${DO_ASK}" == "y" ]] ; then - create_file_list - fi - - if [[ "${#DIRS2ARCH[*]}" == "0" ]] ; then - warn "No directories to archive given." - exit 0 - fi - - backup_archive - - echo - info "Finished." - -} - -main "$@" - -exit 0 - -# vim: ts=4 et list diff --git a/barracuda-sync b/barracuda-sync deleted file mode 100755 index 2d5fe69..0000000 --- a/barracuda-sync +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env python3 - -# Standard modules -import sys -import os -import logging -import locale - -# own modules: -cur_dir = os.getcwd() -base_dir = cur_dir - -if sys.argv[0] != '' and sys.argv[0] != '-c': - cur_dir = os.path.dirname(sys.argv[0]) -if os.path.exists(os.path.join(cur_dir, 'pp_lib')): - sys.path.insert(0, os.path.abspath(cur_dir)) - -from pp_lib.barracuda_sync_app import PpBarracudaSyncApp - -log = logging.getLogger(__name__) - -__author__ = 'Frank Brehm ' -__copyright__ = '(C) 2017 by Frank Brehm, Pixelpark GmbH, Berlin' - -appname = os.path.basename(sys.argv[0]) - -locale.setlocale(locale.LC_ALL, '') - -app = PpBarracudaSyncApp(appname=appname) -app.initialized = True - -if app.verbose > 2: - print("{c}-Object:\n{a}".format(c=app.__class__.__name__, a=app)) - -app() - -sys.exit(0) - -# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 diff --git a/bin/archive-old-homes.sh b/bin/archive-old-homes.sh new file mode 100755 index 0000000..b17fbc6 --- /dev/null +++ b/bin/archive-old-homes.sh @@ -0,0 +1,451 @@ +#!/bin/bash +################################################################################ +## +## Header details +## +## Host..............: achilles.pixelpark.com +## Zone..............: N/A +## +## Copyright ........: Pixelpark AG +## File ........ ....: archive-old-homes.sh +## Maintained by ....: Frank Brehm +## Description ......: +## +## +## History ..........: +## +## Vers. Date By Reason +## ----- ----------- ---------------- --------------------------------------- +## 1.0 09.01.2009 thomas.kotschok initial Version +## 2.0 18.04.2017 frank.brehm modifying for running on Linux +## +################################################################################ + +set -e +set -u + +VERBOSE="n" +DEBUG="n" + +VERSION="2.1" + +# console colors: +RED="" +YELLOW="" +GREEN="" +BLUE="" +NORMAL="" + +HAS_TTY='y' + +DO_ASK="n" +SIMULATE="n" +NO_REMOVE="n" + +BASENAME="$(basename ${0})" +BASE_DIR="$(dirname ${0})" + +INPUT_TIMEOUT=5 + +DATE=$( date '+%G%m%d' ) + +HOMES_DIR="/mnt/nfs/home" +SAVEPATH="${HOMES_DIR}/_old_homes" +NODENAME=$( uname -n ) +LOGFILE="${SAVEPATH}/pp_archive_old_homes.log" +FILELIST="" + +declare -a DIRS2ARCH=() +declare -A BACKUP_FILES=() + +#------------------------------------------------------------------- +detect_color() { + + local safe_term="${TERM//[^[:alnum:]]/?}" + local match_lhs="" + local use_color="false" + [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)" + [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(/dev/null \ + && match_lhs=$(dircolors --print-database) + [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color="true" + + # console colors: + if [ "${use_color}" = "true" ] ; then + RED="\033[38;5;196m" + YELLOW="\033[38;5;226m" + GREEN="\033[38;5;46m" + BLUE="\033[38;5;27m" + NORMAL="\033[39m" + else + RED="" + YELLOW="" + GREEN="" + BLUE="" + NORMAL="" + fi + + local my_tty=$(tty) + if [[ "${my_tty}" =~ 'not a tty' ]] ; then + my_tty='-' + fi + + if [[ "${my_tty}" = '-' || "${safe_term}" = "dump" ]] ; then + HAS_TTY='n' + fi + +} +detect_color + +#------------------------------------------------------------------------------ +description() { + echo -e $( cat <<-EOF + Archiving and removing all given directories beneath '${HOMES_DIR}'. + + EOF + ) +} + +#------------------------------------------------------------------------------ +usage() { + cat <<-EOF + Usage: ${BASENAME} [OPTIONS] [ [&2 + usage >&2 + exit 1 + fi + set -e + + # Note the quotes around `$TEMP': they are essential! + eval set -- "${tmp}" + + local p= + + while true ; do + case "$1" in + -a|--ask) + DO_ASK="y" + shift + ;; + -s|--simulate) + SIMULATE="y" + shift + ;; + -N|--no-remove) + NO_REMOVE="y" + shift + ;; + -d|--debug) + DEBUG="y" + shift + ;; + -v|--verbose) + VERBOSE="y" + shift + ;; + --nocolor) + RED="" + YELLOW="" + GREEN="" + BLUE="" + NORMAL="" + shift + ;; + -h|--help) + description + usage + exit 0 + ;; + -V|--version) + echo "${BASENAME} version: ${VERSION}" + exit 0 + ;; + --) shift + break + ;; + *) echo "Internal error!" + exit 1 + ;; + esac + done + + if [[ "${DEBUG}" = "y" ]] ; then + set -x + fi + + if [[ "$#" -lt 1 ]] ; then + DO_ASK="y" + else + local item + for item in "$@" ; do + base_dir=$( basename "${item}" ) + add_dir "${base_dir}" + done + if [[ "${#DIRS2ARCH[*]}" == "0" ]] ; then + DO_ASK="y" + fi + fi + +} + +######################################### +# Some often used funktions + +#------------------------------------------------------------------------------ +my_date() { + date +'%F %T.%N %:::z' +} + +#------------------------------------------------------------------------------ +debug() { + if [[ "${VERBOSE}" != "y" ]] ; then + return 0 + fi + echo -e " * [$(my_date)] [${BASENAME}:DEBUG]: $@" >&2 +} + +#------------------------------------------------------------------------------ +info() { + echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2 +} + +#------------------------------------------------------------------------------ +warn() { + echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2 +} + +#------------------------------------------------------------------------------ +error() { + echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2 +} + +if [[ ! -d "${HOMES_DIR}" ]] ; then + echo "Directory '${HOMES_DIR}' does not exists." >&2 + exit 4 +fi +HOMES_DIR=$( readlink -f "${HOMES_DIR}" ) + +if [[ ! -d "${SAVEPATH}" ]] ; then + echo "Directory '${SAVEPATH}' does not exists." >&2 + exit 5 +fi +SAVEPATH=$( readlink -f "${SAVEPATH}" ) + +#------------------------------------------------------------------------------ +add_dir() { + + local base_dir="$1" + + local i= + local real_path="${HOMES_DIR}/${base_dir}" + local backup_file= + + if [[ ! -e "${real_path}" ]] ; then + error "Directory '${real_path}' does not exists." + return 0 + fi + + real_path=$( readlink -f "${real_path}" ) + if [[ "${real_path}" == "${SAVEPATH}" ]] ; then + error "Directory '${real_path}' is the backup directory." + return 0 + fi + + i=0 + backup_file="${SAVEPATH}/${base_dir}.${i}.tar.gz" + while [[ -e "${backup_file}" ]] ; do + i=$(( $i + 1 )) + backup_file="${SAVEPATH}/${base_dir}.${i}.tar.gz" + done + + info "ADD ${base_dir} -> ${backup_file}" + DIRS2ARCH+=("${base_dir}") + BACKUP_FILES[${base_dir}]="${backup_file}" + +} + +#------------------------------------------------------------------------------ +create_file_list() { + + local cont="1" + local input= + local base_dir= + local ret= + + info "Create a file list ..." + echo + + # constructing comand + echo + + while [[ "${cont}" -eq 1 ]] ; do + + echo "######################################" + echo " q = Done " + echo " e = Exit " + echo "weiteren hinzufügen oder mit \"q\" beenden?" + echo "######################################" + echo "" + + set +e + read -p "Directory [|q|e]: " -t "${INPUT_TIMEOUT}" input + ret="$?" + set -e + if [[ -z "${input}" ]] ; then + echo + fi + debug "Got input: \"${input}\", return value: ${ret}." + if [[ -z "${input}" ]] ; then + if [[ ${ret} -ne 0 ]] ; then + error "Input ${RED}timeout after ${INPUT_TIMEOUT} seconds${NORMAL}." + exit 1 + fi + continue + fi + case "${input}" in + q|Q) + cont=0 + ;; + e|E) + info "Exit" + exit 0 + ;; + *) + base_dir=$( basename "${input}" ) + add_dir "${base_dir}" + ;; + esac + done + +} + +#------------------------------------------------------------------------------ +backup_archive() { + + local base_dir= + local backup_file= + local real_path= + local cmd= + local annotation="Old home archive from ${NODENAME} -> ${SAVEPATH} - Date ${DATE}" + + if [[ "${SIMULATE}" != "y" ]] ; then + echo | tee -a "${LOGFILE}" + echo "${annotation}" | tee -a "${LOGFILE}" + else + echo + echo "${annotation}" + fi + cd "${HOMES_DIR}" + + for base_dir in "${DIRS2ARCH[@]}" ; do + + echo + debug "Performing '${base_dir}' ..." + + backup_file="${BACKUP_FILES[${base_dir}]}" + real_path="${HOMES_DIR}/${base_dir}" + + info "Creating '${GREEN}${backup_file}${NORMAL}' ..." + + cmd="tar --create --gzip --file=\"${backup_file}\" \"${base_dir}\"" + if [[ "${VERBOSE}" == "y" ]] ; then + echo "${cmd}" + fi + eval ${cmd} + + debug "Setting timestamps of '${backup_file}' ..." + cmd="touch -r \"${real_path}\" \"${backup_file}\"" + if [[ "${VERBOSE}" == "y" ]] ; then + echo "${cmd}" + fi + eval ${cmd} + + if [[ "${NO_REMOVE}" != "y" ]] ; then + info "Removing '${GREEN}${real_path}${NORMAL}' ..." + cmd="rm -rf \"${base_dir}\"" + if [[ "${VERBOSE}" == "y" ]] ; then + echo "${cmd}" + fi + if [[ "${SIMULATE}" != "y" ]] ; then + eval ${cmd} + fi + fi + + if [[ "${SIMULATE}" != "y" ]] ; then + echo " - ${base_dir} ${backup_file}" >> "${LOGFILE}" + fi + printf " " + ls -l "${backup_file}" || true + + if [[ "${SIMULATE}" == "y" ]] ; then + debug "Removing '${backup_file}' ..." + rm -f "${backup_file}" + fi + + done + +} + +################################################################################ +## +## Main +## +################################################################################ + +#------------------------------------------------------------------------------ +main() { + + get_options "$@" + + if [[ "${SIMULATE}" == "y" ]] ; then + info "Simulation mode, nothing is really done." + fi + + if [[ "${DO_ASK}" == "y" ]] ; then + create_file_list + fi + + if [[ "${#DIRS2ARCH[*]}" == "0" ]] ; then + warn "No directories to archive given." + exit 0 + fi + + backup_archive + + echo + info "Finished." + +} + +main "$@" + +exit 0 + +# vim: ts=4 et list diff --git a/bin/barracuda-sync b/bin/barracuda-sync new file mode 100755 index 0000000..8607c19 --- /dev/null +++ b/bin/barracuda-sync @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +# Standard modules +import sys +import os +import logging +import locale + +# own modules: +cur_dir = os.getcwd() +base_dir = cur_dir + +if sys.argv[0] != '' and sys.argv[0] != '-c': + bin_dir = os.path.dirname(sys.argv[0]) +base_dir = os.path.abspath(os.path.join(bin_dir, '..')) +module_dir = os.path.join(base_dir, 'pp_lib') +if os.path.exists(module_dir): + sys.path.insert(0, base_dir) + +from pp_lib.barracuda_sync_app import PpBarracudaSyncApp + +log = logging.getLogger(__name__) + +__author__ = 'Frank Brehm ' +__copyright__ = '(C) 2017 by Frank Brehm, Pixelpark GmbH, Berlin' + +appname = os.path.basename(sys.argv[0]) + +locale.setlocale(locale.LC_ALL, '') + +app = PpBarracudaSyncApp(appname=appname) +app.initialized = True + +if app.verbose > 2: + print("{c}-Object:\n{a}".format(c=app.__class__.__name__, a=app)) + +app() + +sys.exit(0) + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 diff --git a/bin/format-du b/bin/format-du new file mode 100755 index 0000000..f26a00a --- /dev/null +++ b/bin/format-du @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +# Standard modules +import sys +import os +import logging +import locale + +# own modules: +cur_dir = os.getcwd() +base_dir = cur_dir + +if sys.argv[0] != '' and sys.argv[0] != '-c': + bin_dir = os.path.dirname(sys.argv[0]) +base_dir = os.path.abspath(os.path.join(bin_dir, '..')) +module_dir = os.path.join(base_dir, 'pp_lib') +if os.path.exists(module_dir): + sys.path.insert(0, base_dir) + +from pp_lib.format_du import FormatDuApp + +log = logging.getLogger(__name__) + +__author__ = 'Frank Brehm ' +__copyright__ = '(C) 2017 by Frank Brehm, Pixelpark GmbH, Berlin' + +appname = os.path.basename(sys.argv[0]) + +locale.setlocale(locale.LC_ALL, '') + +app = FormatDuApp(appname=appname) + +if app.verbose > 2: + print("{c}-Object:\n{a}".format(c=app.__class__.__name__, a=app)) + +app() + +sys.exit(0) + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 diff --git a/bin/get-mounted-park-fs.sh b/bin/get-mounted-park-fs.sh new file mode 100755 index 0000000..7bb1ab8 --- /dev/null +++ b/bin/get-mounted-park-fs.sh @@ -0,0 +1,121 @@ +#!/bin/bash + +set -e +set -u + +SERVER_LIST=$( cat <<-EOF + aurora.pixelpark.net test-www01-bibliomed-de + beaver.pixelpark.com + cell.pixelpark.com + consus.pixelpark.net test-www02-bibliomed-de + erebus.pixelpark.net + gna.pixelpark.com iwc01 caldav01 + hektor.pixelpark.com + horen.pixelpark.com + intra-collective01.pixelpark.com + libs.pixelpark.com + megaira.pixelpark.net www02-bibliomed-de + merlin.pixelpark.net www-mb-kampagnen-de + nb-58-01.pixelpark.com + nb-58-02.pixelpark.com + nechtan.pixelpark.com + nox.pixelpark.com + nut.pixelpark.com mysql-pp06 + nyx.pixelpark.com + pan.pixelpark.com + phoenix-dom01.pixelpark.com + phoenix-dom02.pixelpark.com + pluton01.pixelpark.com global + spes.pixelpark.com + tantalos.pixelpark.com global + tyr.pixelpark.com + EOF +) + +PURE_ROOT_SERVERS=" +gna.pixelpark.com +nut.pixelpark.com +pluton01.pixelpark.com +" + +THIS_SCRIPT=$( readlink -f "${0}" ) +BIN_DIR=$( dirname "${THIS_SCRIPT}" ) +USER="frank.brehm" + +if [[ ! -r "${BIN_DIR}/solaris.rc" ]] ; then + echo "Resource file '${BIN_DIR}/solaris.rc' not found." >&2 + exit 66 +fi +source "${BIN_DIR}/solaris.rc" + +check_for_park() { + local server="${1}" + local user="${2}" + local zone="${3}" + local fs= + + local pf= + if [[ "${zone}" == "global" ]] ; then + pf="/usr/bin/pfexec /usr/sbin/zlogin ${zone} " + fi + + echo " checking for mounted /park filesystem:" + cmd="${pf}df -k | grep ' /park' | awk '{print \$6}'" + + local mounted_parks= + if echo "${PURE_ROOT_SERVERS}" | grep -q -w "${server}" ; then + mounted_parks=$( exec_remote "${server}" "${user}" "${cmd}" "${PW_FILE}" || true ) + else + mounted_parks=$( exec_remote "${server}" "${user}" "${cmd}" || true ) + fi + for fs in ${mounted_parks}; do + echo " - ${fs}" + done + + if [[ -n "${mounted_parks}" ]] ; then + + cmd= + + fi + +} + +main() { + + local -a servers=() + + local line= + local server= + local zone= + local o_ifs="${IFS}" + IFS=" +" + for line in ${SERVER_LIST} ; do + server=$( echo "${line}" | awk '{print $1}' ) + servers+=( "${server}" ) + done + IFS="${o_ifs}" + + for server in "${servers[@]}" ; do + echo + line=$( echo "${SERVER_LIST}" | \ + grep -w "${server}" | \ + sed -e 's/^[^[:space:]][^[:space:]]*[[:space:]]*//' ) + echo "${server}:" + if [[ -z "${line}" ]] ; then + line=$( get_solaris_zones "${server}" "${USER}" ) + fi + + for zone in ${line}; do + echo " - Zone ${zone}" + check_for_park "${server}" "${USER}" "${zone}" + done + + done + +} + +main + + +# vim: et ts=4 shiftwidth=4 softtabstop=4 list diff --git a/bin/get-solaris-zones.sh b/bin/get-solaris-zones.sh new file mode 100755 index 0000000..32447b5 --- /dev/null +++ b/bin/get-solaris-zones.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +set -e +set -u + + +PW_FILE="/home/fbrehm/.private/bla.txt" + +exec_remote() { + + local host="${1}" + local cmd="${2}" + local pw_file= + if [[ "$#" -ge 3 ]] ; then + pw_file="${3}" + fi + + if [[ -z "${pw_file}" ]] ; then + ssh -X root@${host} "${cmd}" + else + sshpass -f "${pw_file}" ssh -X root@${host} "${cmd}" + fi + +} + + +get_solaris_zones() { + + local host="${1}" + local pw_file= + if [[ "$#" -ge 2 ]] ; then + pw_file="${2}" + fi + + local zone= + local -a zones=() + + if exec_remote "${host}" "true" "${pw_file}" ; then + : + else + return 0 + fi + + cmd=$( cat <<-EOF + if [ ! -x /usr/sbin/zoneadm ] ; then + exit 0 + fi + /usr/sbin/zoneadm list + EOF + ) + + for zone in $( exec_remote "${host}" "${cmd}" "${pw_file}" ) ; do + zones+=( "${zone}" ) + done + + echo "${zones[@]}" + +} + +get_solaris_zones "intra-collective01.pixelpark.com" "${PW_FILE}" + + +# vim: et ts=4 shiftwidth=4 softtabstop=4 list diff --git a/bin/get_sieve b/bin/get_sieve new file mode 100755 index 0000000..56642a6 --- /dev/null +++ b/bin/get_sieve @@ -0,0 +1,38 @@ +#!/bin/bash + +set -e +set -u + +LDAP_SERVER='ldap://ldap.pixelpark.com' +LDAP_BIND_DN='cn=admin' +LDAP_BIND_PW='3o.o0dlt' +LDAP_BASE_DN='o=isp' + +USER="${1:-frank.brehm}" + +echo "Suche DN für User '${USER}' ..." >&2 + +USER_DN=$( ldapsearch -LLL \ + -H "${LDAP_SERVER}" \ + -b "${LDAP_BASE_DN}" \ + -x -D "${LDAP_BIND_DN}" -w "${LDAP_BIND_PW}" \ + "uid=${USER}" \ + dn | sed -e 's/^dn:[ ][ ]*//i' ) +echo "Fand DN '${USER_DN}'." >&2 +echo >&2 + +SIEVE64=$( ldapsearch -LLL \ + -H "${LDAP_SERVER}" \ + -b "${USER_DN}" \ + -x -D "${LDAP_BIND_DN}" -w "${LDAP_BIND_PW}" \ + "objectclass=*" mailSieveRuleSource | \ + grep -v '^dn:' | \ + sed -e 's/^mailSieveRuleSource::*[ ]*//i' \ + -e 's/[ ]//g' \ + -e 's/=//g' | \ + tr -d '[:cntrl:]' ) + +printf "${SIEVE64}==" | base64 --decode --ignore-garbage + + +# vim: ts=4 list diff --git a/bin/get_used_park.sh b/bin/get_used_park.sh new file mode 100644 index 0000000..d79e338 --- /dev/null +++ b/bin/get_used_park.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +for pid in $( ls -d /proc/* | xargs -l basename | sort -n ); do printf "${pid}: "; pwdx "${pid}" && pfiles "${pid}" | grep '^[ ]*/' | grep '/park'; done + +# vim: ts=4 diff --git a/bin/put_sieve b/bin/put_sieve new file mode 100755 index 0000000..2eea6a5 --- /dev/null +++ b/bin/put_sieve @@ -0,0 +1,325 @@ +#!/bin/bash + +set -e +set -u + +VERBOSE="n" +DEBUG="n" +SIMULATE="n" + +VERSION="2.1" + +# console colors: +RED="" +YELLOW="" +GREEN="" +BLUE="" +NORMAL="" + +HAS_TTY='y' + +BASENAME="$(basename ${0})" +BASE_DIR="$(dirname ${0})" + +LDAP_SERVER='ldap://ldap.pixelpark.com' +LDAP_BIND_DN='cn=admin' +LDAP_BIND_PW='3o.o0dlt' +LDAP_BASE_DN='o=isp' + +USER="frank.brehm" +SIEVE_FILE="" + +#------------------------------------------------------------------- +detect_color() { + + local safe_term="${TERM//[^[:alnum:]]/?}" + local match_lhs="" + local use_color="false" + [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)" + [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(/dev/null \ + && match_lhs=$(dircolors --print-database) + [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color="true" + + # console colors: + if [ "${use_color}" = "true" ] ; then + RED="\033[38;5;196m" + YELLOW="\033[38;5;226m" + GREEN="\033[38;5;46m" + BLUE="\033[38;5;27m" + NORMAL="\033[39m" + else + RED="" + YELLOW="" + GREEN="" + BLUE="" + NORMAL="" + fi + + local my_tty=$(tty) + if [[ "${my_tty}" =~ 'not a tty' ]] ; then + my_tty='-' + fi + + if [[ "${my_tty}" = '-' || "${safe_term}" = "dump" ]] ; then + HAS_TTY='n' + fi + +} +detect_color + +#------------------------------------------------------------------------------ +description() { + echo -e $( cat <<-EOF + Aktualisiert die Sieve-Regeln im LDAP. + + EOF + ) +} + +#------------------------------------------------------------------------------ +usage() { + cat <<-EOF + Usage: ${BASENAME} [-s|--simulate] [-d|--debug] [-v|--verbose] [-U|--user USER] SIEVE_FILE + ${BASENAME} [-h|--help] + + EOF +} + +#------------------------------------------------------------------------------ +get_options() { + + local tmp= + local base_dir= + + set +e + tmp=$( getopt -o sdvU: \ + --long simulate,debug,verbose,nocolor,user:,help \ + -n "${BASENAME}" -- "$@" ) + if [[ $? != 0 ]] ; then + echo "" >&2 + usage >&2 + exit 1 + fi + set -e + + # Note the quotes around `$TEMP': they are essential! + eval set -- "${tmp}" + + local p= + + while true ; do + case "$1" in + -U|--user) + USER="$2" + shift + shift + ;; + -s|--simulate) + SIMULATE="y" + shift + ;; + -d|--debug) + DEBUG="y" + shift + ;; + -v|--verbose) + VERBOSE="y" + shift + ;; + --nocolor) + RED="" + YELLOW="" + GREEN="" + BLUE="" + NORMAL="" + shift + ;; + -h|--help) + description + usage + exit 0 + ;; + -V|--version) + echo "${BASENAME} version: ${VERSION}" + exit 0 + ;; + --) shift + break + ;; + *) echo "Internal error!" + exit 1 + ;; + esac + done + + if [[ "${DEBUG}" = "y" ]] ; then + set -x + fi + + if [[ "$#" -ne 1 ]] ; then + usage >&2 + exit 1 + fi + + SIEVE_FILE="${1}" + +} + +######################################### +# Some often used funktions + +#------------------------------------------------------------------------------ +my_date() { + date +'%F %T.%N %:::z' +} + +#------------------------------------------------------------------------------ +debug() { + if [[ "${VERBOSE}" != "y" ]] ; then + return 0 + fi + echo -e " * [$(my_date)] [${BASENAME}:DEBUG]: $@" >&2 +} + +#------------------------------------------------------------------------------ +info() { + echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2 +} + +#------------------------------------------------------------------------------ +warn() { + echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2 +} + +#------------------------------------------------------------------------------ +error() { + echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2 +} + +#------------------------------------------------------------------------------ +RM() { + if [[ "${VERBOSE}" == "y" ]] ; then + rm --verbose "$@" + else + rm "$@" + fi + +} + +#------------------------------------------------------------------------------ +cleanup_tmp_file() { + + if [[ -n "${LDIF_FILE}" ]] ; then + if [[ -f "${LDIF_FILE}" ]] ; then + RM -f "${LDIF_FILE}" + fi + fi + +} + +################################################################################ +## +## Main +## +################################################################################ +main() { + + get_options "$@" + + if [[ "${SIMULATE}" == "y" ]] ; then + info "Simulation mode, nothing is really done." + fi + + if [[ ! -f "${SIEVE_FILE}" ]] ; then + error "Sieve file '${SIEVE_FILE}' existiert nicht." >&2 + echo >&2 + usage >&2 + exit 1 + fi + SIEVE_FILE=$( readlink -f "${SIEVE_FILE}" ) + + info "Suche DN für User '${USER}' ..." + + USER_DN=$( ldapsearch -LLL \ + -H "${LDAP_SERVER}" \ + -b "${LDAP_BASE_DN}" \ + -x -D "${LDAP_BIND_DN}" -w "${LDAP_BIND_PW}" \ + "uid=${USER}" \ + dn | sed -e 's/^dn:[ ][ ]*//i' ) + + if [[ -z "${USER_DN}" ]] ; then + error "User '${USER}' nicht im LDAP gefunden." >&2 + echo >&2 + exit 1 + fi + + LDIF_FILE=$( mktemp modify-sieve-XXXXXXXX.ldif ) + + trap cleanup_tmp_file INT TERM EXIT ABRT + + cat > "${LDIF_FILE}" <<-EOF + dn: ${USER_DN} + changetype: modify + EOF + + echo + info "Fand DN '${USER_DN}'." + echo + info "Suche existierenden Sieve Eintrag ..." + echo + + CUR_SIEVE64=$( ldapsearch -LLL \ + -H "${LDAP_SERVER}" \ + -b "${USER_DN}" \ + -x -D "${LDAP_BIND_DN}" -w "${LDAP_BIND_PW}" \ + "objectclass=*" mailSieveRuleSource | \ + grep -i '^mailSieveRuleSource:' || true ) + + if [[ -z "${CUR_SIEVE64}" ]] ; then + info "Füge Attribut mailSieveRuleSource hinzu ..." + cat >> "${LDIF_FILE}" <<-EOF + add: mailSieveRuleSource + EOF + else + info "Ändere Attribut mailSieveRuleSource ..." + cat >> "${LDIF_FILE}" <<-EOF + replace: mailSieveRuleSource + EOF + fi + + echo "mailSieveRuleSource:< file://${SIEVE_FILE}" >> "${LDIF_FILE}" + echo "-" >> "${LDIF_FILE}" + echo '' >> "${LDIF_FILE}" + + echo + echo "Resultierendes LDIF:" + echo "--------------------" + echo + cat "${LDIF_FILE}" + + CMD="ldapmodify -H \"${LDAP_SERVER}\"" + CMD+=" -x -D \"${LDAP_BIND_DN}\" -w \"${LDAP_BIND_PW}\"" + CMD+=" -f \"$( readlink -f "${LDIF_FILE}" )\"" + + echo + info "Ausführen:" + echo "${CMD}" + echo + + if [[ "${SIMULATE}" != "y" ]] ; then + eval ${CMD} + echo + fi + + info "Fertig." + +} + +main "$@" + +exit 0 + + +# vim: ts=4 list diff --git a/format-du b/format-du deleted file mode 100755 index ae0becf..0000000 --- a/format-du +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python3 - -# Standard modules -import sys -import os -import logging -import locale - -# own modules: -cur_dir = os.getcwd() -base_dir = cur_dir - -if sys.argv[0] != '' and sys.argv[0] != '-c': - cur_dir = os.path.dirname(sys.argv[0]) -if os.path.exists(os.path.join(cur_dir, 'pp_lib')): - sys.path.insert(0, os.path.abspath(cur_dir)) - -from pp_lib.format_du import FormatDuApp - -log = logging.getLogger(__name__) - -__author__ = 'Frank Brehm ' -__copyright__ = '(C) 2017 by Frank Brehm, Pixelpark GmbH, Berlin' - -appname = os.path.basename(sys.argv[0]) - -locale.setlocale(locale.LC_ALL, '') - -app = FormatDuApp(appname=appname) - -if app.verbose > 2: - print("{c}-Object:\n{a}".format(c=app.__class__.__name__, a=app)) - -app() - -sys.exit(0) - -# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 diff --git a/get-mounted-park-fs.sh b/get-mounted-park-fs.sh deleted file mode 100755 index 7bb1ab8..0000000 --- a/get-mounted-park-fs.sh +++ /dev/null @@ -1,121 +0,0 @@ -#!/bin/bash - -set -e -set -u - -SERVER_LIST=$( cat <<-EOF - aurora.pixelpark.net test-www01-bibliomed-de - beaver.pixelpark.com - cell.pixelpark.com - consus.pixelpark.net test-www02-bibliomed-de - erebus.pixelpark.net - gna.pixelpark.com iwc01 caldav01 - hektor.pixelpark.com - horen.pixelpark.com - intra-collective01.pixelpark.com - libs.pixelpark.com - megaira.pixelpark.net www02-bibliomed-de - merlin.pixelpark.net www-mb-kampagnen-de - nb-58-01.pixelpark.com - nb-58-02.pixelpark.com - nechtan.pixelpark.com - nox.pixelpark.com - nut.pixelpark.com mysql-pp06 - nyx.pixelpark.com - pan.pixelpark.com - phoenix-dom01.pixelpark.com - phoenix-dom02.pixelpark.com - pluton01.pixelpark.com global - spes.pixelpark.com - tantalos.pixelpark.com global - tyr.pixelpark.com - EOF -) - -PURE_ROOT_SERVERS=" -gna.pixelpark.com -nut.pixelpark.com -pluton01.pixelpark.com -" - -THIS_SCRIPT=$( readlink -f "${0}" ) -BIN_DIR=$( dirname "${THIS_SCRIPT}" ) -USER="frank.brehm" - -if [[ ! -r "${BIN_DIR}/solaris.rc" ]] ; then - echo "Resource file '${BIN_DIR}/solaris.rc' not found." >&2 - exit 66 -fi -source "${BIN_DIR}/solaris.rc" - -check_for_park() { - local server="${1}" - local user="${2}" - local zone="${3}" - local fs= - - local pf= - if [[ "${zone}" == "global" ]] ; then - pf="/usr/bin/pfexec /usr/sbin/zlogin ${zone} " - fi - - echo " checking for mounted /park filesystem:" - cmd="${pf}df -k | grep ' /park' | awk '{print \$6}'" - - local mounted_parks= - if echo "${PURE_ROOT_SERVERS}" | grep -q -w "${server}" ; then - mounted_parks=$( exec_remote "${server}" "${user}" "${cmd}" "${PW_FILE}" || true ) - else - mounted_parks=$( exec_remote "${server}" "${user}" "${cmd}" || true ) - fi - for fs in ${mounted_parks}; do - echo " - ${fs}" - done - - if [[ -n "${mounted_parks}" ]] ; then - - cmd= - - fi - -} - -main() { - - local -a servers=() - - local line= - local server= - local zone= - local o_ifs="${IFS}" - IFS=" -" - for line in ${SERVER_LIST} ; do - server=$( echo "${line}" | awk '{print $1}' ) - servers+=( "${server}" ) - done - IFS="${o_ifs}" - - for server in "${servers[@]}" ; do - echo - line=$( echo "${SERVER_LIST}" | \ - grep -w "${server}" | \ - sed -e 's/^[^[:space:]][^[:space:]]*[[:space:]]*//' ) - echo "${server}:" - if [[ -z "${line}" ]] ; then - line=$( get_solaris_zones "${server}" "${USER}" ) - fi - - for zone in ${line}; do - echo " - Zone ${zone}" - check_for_park "${server}" "${USER}" "${zone}" - done - - done - -} - -main - - -# vim: et ts=4 shiftwidth=4 softtabstop=4 list diff --git a/get-solaris-zones.sh b/get-solaris-zones.sh deleted file mode 100755 index 32447b5..0000000 --- a/get-solaris-zones.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/bin/bash - -set -e -set -u - - -PW_FILE="/home/fbrehm/.private/bla.txt" - -exec_remote() { - - local host="${1}" - local cmd="${2}" - local pw_file= - if [[ "$#" -ge 3 ]] ; then - pw_file="${3}" - fi - - if [[ -z "${pw_file}" ]] ; then - ssh -X root@${host} "${cmd}" - else - sshpass -f "${pw_file}" ssh -X root@${host} "${cmd}" - fi - -} - - -get_solaris_zones() { - - local host="${1}" - local pw_file= - if [[ "$#" -ge 2 ]] ; then - pw_file="${2}" - fi - - local zone= - local -a zones=() - - if exec_remote "${host}" "true" "${pw_file}" ; then - : - else - return 0 - fi - - cmd=$( cat <<-EOF - if [ ! -x /usr/sbin/zoneadm ] ; then - exit 0 - fi - /usr/sbin/zoneadm list - EOF - ) - - for zone in $( exec_remote "${host}" "${cmd}" "${pw_file}" ) ; do - zones+=( "${zone}" ) - done - - echo "${zones[@]}" - -} - -get_solaris_zones "intra-collective01.pixelpark.com" "${PW_FILE}" - - -# vim: et ts=4 shiftwidth=4 softtabstop=4 list diff --git a/get_sieve b/get_sieve deleted file mode 100755 index 56642a6..0000000 --- a/get_sieve +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash - -set -e -set -u - -LDAP_SERVER='ldap://ldap.pixelpark.com' -LDAP_BIND_DN='cn=admin' -LDAP_BIND_PW='3o.o0dlt' -LDAP_BASE_DN='o=isp' - -USER="${1:-frank.brehm}" - -echo "Suche DN für User '${USER}' ..." >&2 - -USER_DN=$( ldapsearch -LLL \ - -H "${LDAP_SERVER}" \ - -b "${LDAP_BASE_DN}" \ - -x -D "${LDAP_BIND_DN}" -w "${LDAP_BIND_PW}" \ - "uid=${USER}" \ - dn | sed -e 's/^dn:[ ][ ]*//i' ) -echo "Fand DN '${USER_DN}'." >&2 -echo >&2 - -SIEVE64=$( ldapsearch -LLL \ - -H "${LDAP_SERVER}" \ - -b "${USER_DN}" \ - -x -D "${LDAP_BIND_DN}" -w "${LDAP_BIND_PW}" \ - "objectclass=*" mailSieveRuleSource | \ - grep -v '^dn:' | \ - sed -e 's/^mailSieveRuleSource::*[ ]*//i' \ - -e 's/[ ]//g' \ - -e 's/=//g' | \ - tr -d '[:cntrl:]' ) - -printf "${SIEVE64}==" | base64 --decode --ignore-garbage - - -# vim: ts=4 list diff --git a/get_used_park.sh b/get_used_park.sh deleted file mode 100644 index d79e338..0000000 --- a/get_used_park.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -for pid in $( ls -d /proc/* | xargs -l basename | sort -n ); do printf "${pid}: "; pwdx "${pid}" && pfiles "${pid}" | grep '^[ ]*/' | grep '/park'; done - -# vim: ts=4 diff --git a/put_sieve b/put_sieve deleted file mode 100755 index 2eea6a5..0000000 --- a/put_sieve +++ /dev/null @@ -1,325 +0,0 @@ -#!/bin/bash - -set -e -set -u - -VERBOSE="n" -DEBUG="n" -SIMULATE="n" - -VERSION="2.1" - -# console colors: -RED="" -YELLOW="" -GREEN="" -BLUE="" -NORMAL="" - -HAS_TTY='y' - -BASENAME="$(basename ${0})" -BASE_DIR="$(dirname ${0})" - -LDAP_SERVER='ldap://ldap.pixelpark.com' -LDAP_BIND_DN='cn=admin' -LDAP_BIND_PW='3o.o0dlt' -LDAP_BASE_DN='o=isp' - -USER="frank.brehm" -SIEVE_FILE="" - -#------------------------------------------------------------------- -detect_color() { - - local safe_term="${TERM//[^[:alnum:]]/?}" - local match_lhs="" - local use_color="false" - [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)" - [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(/dev/null \ - && match_lhs=$(dircolors --print-database) - [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color="true" - - # console colors: - if [ "${use_color}" = "true" ] ; then - RED="\033[38;5;196m" - YELLOW="\033[38;5;226m" - GREEN="\033[38;5;46m" - BLUE="\033[38;5;27m" - NORMAL="\033[39m" - else - RED="" - YELLOW="" - GREEN="" - BLUE="" - NORMAL="" - fi - - local my_tty=$(tty) - if [[ "${my_tty}" =~ 'not a tty' ]] ; then - my_tty='-' - fi - - if [[ "${my_tty}" = '-' || "${safe_term}" = "dump" ]] ; then - HAS_TTY='n' - fi - -} -detect_color - -#------------------------------------------------------------------------------ -description() { - echo -e $( cat <<-EOF - Aktualisiert die Sieve-Regeln im LDAP. - - EOF - ) -} - -#------------------------------------------------------------------------------ -usage() { - cat <<-EOF - Usage: ${BASENAME} [-s|--simulate] [-d|--debug] [-v|--verbose] [-U|--user USER] SIEVE_FILE - ${BASENAME} [-h|--help] - - EOF -} - -#------------------------------------------------------------------------------ -get_options() { - - local tmp= - local base_dir= - - set +e - tmp=$( getopt -o sdvU: \ - --long simulate,debug,verbose,nocolor,user:,help \ - -n "${BASENAME}" -- "$@" ) - if [[ $? != 0 ]] ; then - echo "" >&2 - usage >&2 - exit 1 - fi - set -e - - # Note the quotes around `$TEMP': they are essential! - eval set -- "${tmp}" - - local p= - - while true ; do - case "$1" in - -U|--user) - USER="$2" - shift - shift - ;; - -s|--simulate) - SIMULATE="y" - shift - ;; - -d|--debug) - DEBUG="y" - shift - ;; - -v|--verbose) - VERBOSE="y" - shift - ;; - --nocolor) - RED="" - YELLOW="" - GREEN="" - BLUE="" - NORMAL="" - shift - ;; - -h|--help) - description - usage - exit 0 - ;; - -V|--version) - echo "${BASENAME} version: ${VERSION}" - exit 0 - ;; - --) shift - break - ;; - *) echo "Internal error!" - exit 1 - ;; - esac - done - - if [[ "${DEBUG}" = "y" ]] ; then - set -x - fi - - if [[ "$#" -ne 1 ]] ; then - usage >&2 - exit 1 - fi - - SIEVE_FILE="${1}" - -} - -######################################### -# Some often used funktions - -#------------------------------------------------------------------------------ -my_date() { - date +'%F %T.%N %:::z' -} - -#------------------------------------------------------------------------------ -debug() { - if [[ "${VERBOSE}" != "y" ]] ; then - return 0 - fi - echo -e " * [$(my_date)] [${BASENAME}:DEBUG]: $@" >&2 -} - -#------------------------------------------------------------------------------ -info() { - echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2 -} - -#------------------------------------------------------------------------------ -warn() { - echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2 -} - -#------------------------------------------------------------------------------ -error() { - echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2 -} - -#------------------------------------------------------------------------------ -RM() { - if [[ "${VERBOSE}" == "y" ]] ; then - rm --verbose "$@" - else - rm "$@" - fi - -} - -#------------------------------------------------------------------------------ -cleanup_tmp_file() { - - if [[ -n "${LDIF_FILE}" ]] ; then - if [[ -f "${LDIF_FILE}" ]] ; then - RM -f "${LDIF_FILE}" - fi - fi - -} - -################################################################################ -## -## Main -## -################################################################################ -main() { - - get_options "$@" - - if [[ "${SIMULATE}" == "y" ]] ; then - info "Simulation mode, nothing is really done." - fi - - if [[ ! -f "${SIEVE_FILE}" ]] ; then - error "Sieve file '${SIEVE_FILE}' existiert nicht." >&2 - echo >&2 - usage >&2 - exit 1 - fi - SIEVE_FILE=$( readlink -f "${SIEVE_FILE}" ) - - info "Suche DN für User '${USER}' ..." - - USER_DN=$( ldapsearch -LLL \ - -H "${LDAP_SERVER}" \ - -b "${LDAP_BASE_DN}" \ - -x -D "${LDAP_BIND_DN}" -w "${LDAP_BIND_PW}" \ - "uid=${USER}" \ - dn | sed -e 's/^dn:[ ][ ]*//i' ) - - if [[ -z "${USER_DN}" ]] ; then - error "User '${USER}' nicht im LDAP gefunden." >&2 - echo >&2 - exit 1 - fi - - LDIF_FILE=$( mktemp modify-sieve-XXXXXXXX.ldif ) - - trap cleanup_tmp_file INT TERM EXIT ABRT - - cat > "${LDIF_FILE}" <<-EOF - dn: ${USER_DN} - changetype: modify - EOF - - echo - info "Fand DN '${USER_DN}'." - echo - info "Suche existierenden Sieve Eintrag ..." - echo - - CUR_SIEVE64=$( ldapsearch -LLL \ - -H "${LDAP_SERVER}" \ - -b "${USER_DN}" \ - -x -D "${LDAP_BIND_DN}" -w "${LDAP_BIND_PW}" \ - "objectclass=*" mailSieveRuleSource | \ - grep -i '^mailSieveRuleSource:' || true ) - - if [[ -z "${CUR_SIEVE64}" ]] ; then - info "Füge Attribut mailSieveRuleSource hinzu ..." - cat >> "${LDIF_FILE}" <<-EOF - add: mailSieveRuleSource - EOF - else - info "Ändere Attribut mailSieveRuleSource ..." - cat >> "${LDIF_FILE}" <<-EOF - replace: mailSieveRuleSource - EOF - fi - - echo "mailSieveRuleSource:< file://${SIEVE_FILE}" >> "${LDIF_FILE}" - echo "-" >> "${LDIF_FILE}" - echo '' >> "${LDIF_FILE}" - - echo - echo "Resultierendes LDIF:" - echo "--------------------" - echo - cat "${LDIF_FILE}" - - CMD="ldapmodify -H \"${LDAP_SERVER}\"" - CMD+=" -x -D \"${LDAP_BIND_DN}\" -w \"${LDAP_BIND_PW}\"" - CMD+=" -f \"$( readlink -f "${LDIF_FILE}" )\"" - - echo - info "Ausführen:" - echo "${CMD}" - echo - - if [[ "${SIMULATE}" != "y" ]] ; then - eval ${CMD} - echo - fi - - info "Fertig." - -} - -main "$@" - -exit 0 - - -# vim: ts=4 list