Make script 'change_ssl_directives.sh' silent if not running in an terminal.

This commit is contained in:
Christoph 2022-07-28 11:12:29 +02:00
parent 4d59074cbe
commit a6274a436e

View File

@ -3305,103 +3305,6 @@ _DH_BASE_DIR=$DH_BASE_DIR
_APACHE_VHOST_DIR=$APACHE_VHOST_DIR
_NGINX_VHOST_DIR=$NGINX_VHOST_DIR
_apache_debian_install=false
# -------------
# --- Some functions
# -------------
echononl(){
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\$\$
}
fatal(){
echo ""
echo -e "fataler Fehler: \$*"
echo ""
echo -e "\t\033[31m\033[1mInstalllation wird abgebrochen\033[m\033[m"
echo ""
exit 1
}
error(){
echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: \$*"
echo ""
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: \$*"
echo ""
}
info (){
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: \$*"
echo ""
}
echo_done() {
echo -e "\033[75G[ \033[32mdone\033[m ]"
}
echo_ok() {
echo -e "\033[75G[ \033[32mok\033[m ]"
}
echo_warning() {
echo -e "\033[75G[ \033[33m\033[1mwarn\033[m ]"
}
echo_failed(){
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[75G[ \033[37m\033[1mskipped\033[m ]"
}
## - Check if a given array (parameter 2) contains a given string (parameter 1)
## -
containsElement () {
local e
for e in "\${@:2}"; do [[ "\$e" == "\$1" ]] && return 0; done
return 1
}
clear
echo ""
echo -e "\033[21G\033[32mChanges SSLCertificate directives at apache vhost configurations\033[m"
echo -e "\033[21G\033[32mto their appropriate path to Let's Encrypt Certificate/Key.\033[m"
echo ""
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert DH Base directory"
echo ""
echo ""
DH_BASE_DIR=
while [[ "X\$DH_BASE_DIR" = "X" ]]; do
echononl "DH Configuration Directory [\$_DH_BASE_DIR]: "
read DH_BASE_DIR
if [[ "X\$DH_BASE_DIR" = "X" ]]; then
DH_BASE_DIR=\$_DH_BASE_DIR
fi
done
SCRIPT_INSTALL_DIR=\${DH_BASE_DIR}/tools
DOMAINS_TXT_FILE=\${DH_BASE_DIR}/domains.txt
if [[ ! -f "\$DOMAINS_TXT_FILE" ]] ; then
fatal "Domain file \"\`basename \$DOMAINS_TXT_FILE\`\" not found!"
fi
if [[ ! -d "\$_APACHE_VHOST_DIR" ]]; then
if [[ -d "/etc/apache2/sites-available" ]]; then
_VHOST_DIR=/etc/apache2/conf-available
@ -3424,52 +3327,199 @@ else
_VHOST_DIR=\$_NGINX_VHOST_DIR
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert VHost Directory"
echo ""
echo ""
VHOST_DIR=
while [[ "X\$VHOST_DIR" = "X" ]]; do
echononl "VHost Directory [\$_VHOST_DIR]: "
read VHOST_DIR
if [ "X\$VHOST_DIR" = "Xnone" -o "X\$VHOST_DIR" = "XNone" ]; then
warn "Webserver Configuration will be ommited"
VHOST_DIR=
break
elif [[ "X\$VHOST_DIR" = "X" ]]; then
VHOST_DIR=\$_VHOST_DIR
fi
done
_apache_debian_install=false
if [[ ! -d "\$VHOST_DIR" ]]; then
fatal "No Webserver VHost Configuration directory found!"
# -------------
# --- Some functions
# -------------
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\$\$
fi
}
fatal(){
echo ""
echo -e "fataler Fehler: \$*"
echo ""
echo -e "\t\033[31m\033[1mInstalllation wird abgebrochen\033[m\033[m"
echo ""
exit 1
}
error(){
echo ""
if \$terminal ; then
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: \$*"
else
echo "[ Error ]: \$*"
fi
echo ""
}
warn (){
if \$terminal ; then
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: \$*"
echo ""
fi
}
info (){
if \$terminal ; then
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: \$*"
echo ""
fi
}
echo_done() {
if \$terminal ; then
echo -e "\033[75G[ \033[32mdone\033[m ]"
fi
}
echo_ok() {
if \$terminal ; then
echo -e "\033[75G[ \033[32mok\033[m ]"
fi
}
echo_warning() {
if \$terminal ; then
echo -e "\033[75G[ \033[33m\033[1mwarn\033[m ]"
fi
}
echo_failed(){
if \$terminal ; then
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
fi
}
echo_skipped() {
if \$terminal ; then
echo -e "\033[75G[ \033[37m\033[1mskipped\033[m ]"
fi
}
blank_line() {
if \$terminal ; then
echo ""
fi
}
## - Check if a given array (parameter 2) contains a given string (parameter 1)
## -
containsElement () {
local e
for e in "\${@:2}"; do [[ "\$e" == "\$1" ]] && return 0; done
return 1
}
# -------------
# --- Check some prerequisites
# -------------
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
echo ""
echo ""
if \$terminal ; then
clear
echo ""
echo -e "\033[21G\033[32mChanges SSLCertificate directives at apache vhost configurations\033[m"
echo -e "\033[21G\033[32mto their appropriate path to Let's Encrypt Certificate/Key.\033[m"
echo ""
fi
echo -e "#\033[32m --------------------\033[m"
echo -e "#\033[32m --- Starts Script the following Parameters \033[m"
echo -e "#\033[32m --------------------\033[m"
echo ""
echo "DH Base Directory.............: \$DH_BASE_DIR"
echo "DH domains.txt file...........: \$DOMAINS_TXT_FILE"
echo ""
echo "Script Installation Directory.: \${DH_BASE_DIR}/tools"
echo ""
echo "Vhosts Directory..............: \$VHOST_DIR"
echo ""
echononl "Start with that configuration? [yes/no]: "
read OK
while [ "X\$OK" != "Xyes" -a "X\$OK" != "XYes" -a "X\$OK" != "XNo" -a "X\$OK" != "Xno" ]
do
echononl "wrong entry! [yes/no] :"
if \$terminal ; then
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert DH Base directory"
echo ""
echo ""
DH_BASE_DIR=
while [[ "X\$DH_BASE_DIR" = "X" ]]; do
echononl "DH Configuration Directory [\$_DH_BASE_DIR]: "
read DH_BASE_DIR
if [[ "X\$DH_BASE_DIR" = "X" ]]; then
DH_BASE_DIR=\$_DH_BASE_DIR
fi
done
SCRIPT_INSTALL_DIR=\${DH_BASE_DIR}/tools
DOMAINS_TXT_FILE=\${DH_BASE_DIR}/domains.txt
if [[ ! -f "\$DOMAINS_TXT_FILE" ]] ; then
fatal "Domain file \"\`basename \$DOMAINS_TXT_FILE\`\" not found!"
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert VHost Directory"
echo ""
echo ""
VHOST_DIR=
while [[ "X\$VHOST_DIR" = "X" ]]; do
echononl "VHost Directory [\$_VHOST_DIR]: "
read VHOST_DIR
if [ "X\$VHOST_DIR" = "Xnone" -o "X\$VHOST_DIR" = "XNone" ]; then
warn "Webserver Configuration will be ommited"
VHOST_DIR=
break
elif [[ "X\$VHOST_DIR" = "X" ]]; then
VHOST_DIR=\$_VHOST_DIR
fi
done
if [[ ! -d "\$VHOST_DIR" ]]; then
fatal "No Webserver VHost Configuration directory found!"
fi
echo ""
echo ""
echo -e "#\033[32m --------------------\033[m"
echo -e "#\033[32m --- Starts Script the following Parameters \033[m"
echo -e "#\033[32m --------------------\033[m"
echo ""
echo "DH Base Directory.............: \$DH_BASE_DIR"
echo "DH domains.txt file...........: \$DOMAINS_TXT_FILE"
echo ""
echo "Script Installation Directory.: \${DH_BASE_DIR}/tools"
echo ""
echo "Vhosts Directory..............: \$VHOST_DIR"
echo ""
echononl "Start with that configuration? [yes/no]: "
read OK
done
[ \$OK = "Yes" -o \$OK = "yes" ] || fatal "Change parameters and restart script: \`basename \$0\`"
while [ "X\$OK" != "Xyes" -a "X\$OK" != "XYes" -a "X\$OK" != "XNo" -a "X\$OK" != "Xno" ]
do
echononl "wrong entry! [yes/no] :"
read OK
done
[ \$OK = "Yes" -o \$OK = "yes" ] || fatal "Change parameters and restart script: \`basename \$0\`"
else
DH_BASE_DIR=\$_DH_BASE_DIR
DOMAINS_TXT_FILE=\${DH_BASE_DIR}/domains.txt
if [[ ! -f "\$DOMAINS_TXT_FILE" ]] ; then
fatal "Domain file \"\`basename \$DOMAINS_TXT_FILE\`\" not found!"
fi
VHOST_DIR=\$_VHOST_DIR
fi
_date=\`date +%Y-%m-%d-%H%M\`
@ -3483,7 +3533,7 @@ if [[ "\$VHOST_DIR" = "/etc/nginx/sites-enabled" ]] ; then
VHOST_DIR=\$NGINX_VHOST_DIR
fi
echo ""
blank_line
echononl " Backup existing VHost Directory .."
if [[ "\$(dirname \$VHOST_DIR)" =~ vhosts?/?\$ ]] ; then
BACKUP_SRC_DIR="\$(dirname \$VHOST_DIR)"
@ -3497,7 +3547,7 @@ else
echo_failed
fi
echo ""
blank_line
declare -a vhost_file_arr
while IFS=' ' read -r site_server_name rest ; do
unset vhost_file_arr
@ -3602,20 +3652,16 @@ if [[ -d "\${BACKUP_SRC_DIR}.\$_date" ]]; then
if [[ -x "$apache_control_script" ]]; then
$apache_control_script graceful > /dev/null 2>&1
if [[ \$? -gt 0 ]]; then
if \$verbose ; then
if \$terminal ; then
error "Restarting Apache Webserver failed!"
else
echo -e "\\n [ Error ]: Restarting Apache Webserver failed! \\n"
fi
else
if \$verbose ; then
if \$terminal ; then
info "Apache Webserver restarted."
else
echo -e "\\n [ Info ]: Apache Webserver restarted.\\n"
fi
fi
else
if \$verbose ; then
if \$terminal ; then
warn "Apache Control Script (apachectl) not found. Take care to restart webservice manually"
else
echo -e "\\n [ Warn ]: Apache Control Script (apachectl) not found. Take care to restart webservice manually"
@ -3626,20 +3672,16 @@ if [[ -d "\${BACKUP_SRC_DIR}.\$_date" ]]; then
if [[ -x "$nginx_init_script" ]]; then
$nginx_init_script restart > /dev/null 2>&1
if [[ \$? -gt 0 ]]; then
if \$verbose ; then
if \$terminal ; then
error "Restarting Nginx Webserver failed!"
else
echo -e "\\n [ Error ]: Restarting Nginx Webserver failed! \\n"
fi
else
if \$verbose ; then
if \$terminal ; then
info "Nginx Webserver restarted."
else
echo -e "\\n [ Info ]: Nginx Webserver restarted.\\n"
fi
fi
else
if \$verbose ; then
if \$terminal ; then
warn "Nginx Init Script ($nginx_init_script) not found. Take care to restart webservice manually"
else
echo -e "\\n [ Warn ]: Nginx INIT Script ($nginx_init_script) not found. Take care to restart webservice manually"
@ -3650,7 +3692,7 @@ if [[ -d "\${BACKUP_SRC_DIR}.\$_date" ]]; then
fi
fi
echo
blank_line
exit
EOF
if [[ $? -eq 0 ]]; then