From c635dedc3ad562e815c96dc5921b92e9ed93c916 Mon Sep 17 00:00:00 2001 From: Christoph Date: Fri, 14 Sep 2018 14:06:46 +0200 Subject: [PATCH] handle_domain_on_webserver.sh: add support for cleaning up dehydrated's domains.txt file. --- handle_domain_on_webserver.sh | 104 ++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/handle_domain_on_webserver.sh b/handle_domain_on_webserver.sh index bafdd23..7f3a247 100755 --- a/handle_domain_on_webserver.sh +++ b/handle_domain_on_webserver.sh @@ -508,6 +508,10 @@ declare -a vhost_file_req_for_del_arr declare -a vhost_file_marked_for_del_arr declare -a no_vhost_config_found +declare -a dehydrated_name_marked_for_del_arr +declare -a dehydrated_name_req_for_del_arr +dehydrated_domains_file="/var/lib/dehydrated/domains.txt" + declare -a docroot_req_for_del_arr declare -a docroot_marked_for_del_arr @@ -1303,6 +1307,16 @@ for _vhost_file in ${vhost_file_req_for_del_arr[@]}; do done if ! $_found ; then vhost_file_marked_for_del_arr+=("$_vhost_file") + _dehydrated_name="$(grep ServerName $(realpath ${apache_vhost_dir})/$_vhost_file | awk '{print$2}' | sort -u)" +echo "" +echo "$_vhost_file - $dehydrated_domains_file - $_dehydrated_name" +echo "" + if [[ -n "$_dehydrated_name" ]] \ + && grep -q -E "^\s*$_dehydrated_name" $dehydrated_domains_file 2> /dev/null ; then + if ! containsElement "$_dehydrated_name" "${dehydrated_name_req_for_del_arr[@]}" ; then + dehydrated_name_req_for_del_arr+=("$_dehydrated_name") + fi + fi fi done @@ -1331,6 +1345,19 @@ else echo -e " *** \033[33mNo VHost configuration files are marked for deletion\033[m ***" fi +# - Entier in dehydrated domains file marked for deletion" +# - +echo "" +echo " Entries in dehydrated's domain.txt file requested for deletion" +if [[ ${#dehydrated_name_req_for_del_arr[@]} -gt 0 ]]; then + for _domain_entry in ${dehydrated_name_req_for_del_arr[@]} ; do + echo " $_domain_entry" + done +else + echo -e " *** \033[33mNo domain entry in dehydrated's domain file is requested for deletion\033[m ***" +fi + + # ----- # - Warn about VHost configurtion files not be marked for deletion. # ----- @@ -1697,6 +1724,7 @@ fi apache_needs_restart=false +delete_vhost_file=false if [[ ${#vhost_file_marked_for_del_arr[@]} -gt 0 ]]; then echo "" @@ -1815,9 +1843,15 @@ if [[ ${#vhost_file_marked_for_del_arr[@]} -gt 0 ]]; then echo "# -" >> $main_log echo "mv \"$_vhost_file_to_move\" \"$_delete_dir\"" >> $main_log echononl " Removing VHost configuration \033[1m$(basename $_vhost_file_to_move)\033[m" + _dehydrated_name="$(grep ServerName $_vhost_file_to_move | awk '{print$2}' | sort -u)" mv "$_vhost_file_to_move" "$_delete_dir" > $tmp_log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok + if containsElement "$_dehydrated_name" "${dehydrated_name_req_for_del_arr[@]}" ; then + if ! containsElement "$_dehydrated_name" "${dehydrated_name_marked_for_del_arr[@]}" ; then + dehydrated_name_marked_for_del_arr+=("$_dehydrated_name") + fi + fi else echo_failed error "$(cat $tmp_log_file)" @@ -1836,6 +1870,76 @@ else echo -e "\033[32m------\033[m" fi +if [[ ${#dehydrated_name_marked_for_del_arr[@]} -gt 0 ]]; then + echo "" + echo -e "\033[32m------\033[m" + echo -e "Remove Entries from dehydrated's domains.txt file?" + echo -e "\033[32m------\033[m" + + for _dehydrated_enty in ${dehydrated_name_marked_for_del_arr[@]} ; do + echo " $_dehydrated_enty" + done + + OK="" + echo "" + echo "Type 'y' to continue 'n' or any other key to skip removal for domain entries" + echo + echo -e -n "\033[1mContinue removing concerning entries from dehydrated's domains.txt file [y/n]:\033[m " + read OK + while [[ "X${OK}X" = "XX" ]] ; do + echo "" + echo -e -n "\033[1mContinue removing concerning entries from dehydrated's domains.txt file [y/n]:\033[m " + read OK + done + + if [[ "${OK,,}" = 'yes' ]] || [[ "${OK,,}" = 'y' ]]; then + + echononl " Backup existing domains.txt file" + if [[ -d "$apache_vhost_base_dir" ]]; then + + echo "" >> $main_log + echo "# - Backup existing domains.txt file" >> $main_log + echo "# -" >> $main_log + echo "cp -a \"$dehydrated_domains_file" "${dehydrated_domains_file}.$backup_date\"" >> $main_log + + cp -a "$dehydrated_domains_file" "${dehydrated_domains_file}.$backup_date" > $tmp_log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $tmp_log_file)" + fi + else + echo_skipped + fi + + for _dehydrated_enty in ${dehydrated_name_marked_for_del_arr[@]} ; do + + echo "# - Removing '$_dehydrated_enty' from dehydrated's domains.txt file." >> $main_log + echo "# -" >> $main_log + echo "sed -i \"/$_dehydrated_enty/d\" $dehydrated_domains_file" >> $main_log + + echononl " Removing \033[1m$_dehydrated_enty\033[m from dehydrated's domains.txt file." + sed -i "/$_dehydrated_enty/d" $dehydrated_domains_file > $tmp_log_file 2>&1 + + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $tmp_log_file)" + echo "Error: $(cat $tmp_log_file)" >> $main_log + fi + done + + fi + +else + echo "" + echo -e "\033[32m------\033[m" + echo -e "*** \033[33mNo domain entry in dehydrated's domain file is marked for deletion\033[m ***" + echo -e "\033[32m------\033[m" +fi +