From 45b8e87546b70205c087a4d30b1cecaa33682ec5 Mon Sep 17 00:00:00 2001 From: Lutchy Horace Date: Tue, 9 Nov 2021 09:08:20 -0500 Subject: [PATCH] * Fix spacing so more * Added confpath --- createVhosts.sh | 50 ++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/createVhosts.sh b/createVhosts.sh index 53e462d..88fdfc4 100755 --- a/createVhosts.sh +++ b/createVhosts.sh @@ -38,14 +38,14 @@ function get_cert if [ "$DEBUG" = "1" ]; then _debug_arg="--debug" fi - /root/.acme.sh/acme.sh --issue --domain $_domain --webroot /srv/http-content-combined/ --cert-file /etc/ssl/${_domain}.crt --key-file /etc/ssl/${_domain}.key --fullchain-file /etc/ssl/${_domain}-fullchain.crt $_debug_arg + /root/.acme.sh/acme.sh --issue --domain "$_domain" --webroot /srv/http-content-combined/ --cert-file /etc/ssl/"${_domain}".crt --key-file /etc/ssl/"${_domain}".key --fullchain-file /etc/ssl/"${_domain}"-fullchain.crt $_debug_arg return $? } function reload_nginx { echo -n "Reloading ${_servicename}..." - if systemctl reload ${_servicename} > /dev/null 2>&1; then + if systemctl reload "${_servicename}" > /dev/null 2>&1; then echo "Success" else echo "Failed" @@ -60,10 +60,10 @@ function reload_nginx function clean_up { debug "Removing Nginx configuration and logs..." - rm $_vhost_conf_file - rm /var/log/nginx/$_domain.* > /dev/null 2>&1 + rm "$_vhost_conf_file" + rm /var/log/nginx/"$_domain".* > /dev/null 2>&1 reload_nginx - err $1 + err "$1" } function verify_vhost @@ -80,7 +80,7 @@ function verify_vhost mkdir -p $verify_path touch $verify_full_path - http_code=$(curl -I -H "Host: $_domain" http://$target/.well-known/$verify_file_name 2> /dev/null | grep 'HTTP/1.1' | cut -d " " -f 2) + http_code=$(curl -I -H "Host: $_domain" http://"$target"/.well-known/"$verify_file_name" 2> /dev/null | grep 'HTTP/1.1' | cut -d " " -f 2) if [[ $http_code = '200' ]]; then return 0 @@ -96,8 +96,8 @@ _bb_myname=$(basename "$0") _bb_mypath=$(realpath $BASH_SOURCE) # Init script -if test -f $_bootstrap; then - source $_bootstrap 2> /dev/null +if test -f "$_bootstrap"; then + source "$_bootstrap" 2> /dev/null else echo "Unable to parse BOOTSTRAP: $_bootstrap" exit 1 @@ -111,7 +111,7 @@ fi # gain priviledges become "$@" -OPTS=$(getopt -o h,d -l domain:,root:,backend:,listenip:,desc:,donotredirect,servicename:,debug -n 'createVhosts' -- "$@") +OPTS=$(getopt -o h,d -l domain:,root:,backend:,listenip:,desc:,donotredirect,servicename:,confpath:,debug -n 'createVhosts' -- "$@") if [ "$?" -gt '0' ]; then echo 'Failed to set command line arguments' exit 1; @@ -149,6 +149,9 @@ while true; do --servicename ) _servicename=$2 shift ;; + --confpath ) + _confpath=$2 + shift ;; -d | --debug ) _debug=true shift ;; @@ -173,9 +176,9 @@ fi if test -n "$_root"; then echo -n "Checking if $_root exists?" - if ! test -d $_root; then + if ! test -d "$_root"; then echo " Creating..." - mkdir -p $_root + mkdir -p "$_root" else echo " Yes!" fi @@ -187,7 +190,7 @@ _locationblock_http="" _locationblock_https="" if test -n "$_backend"; then echo "Verifying backend(s)..." - if ! validate_host $_backend; then + if ! validate_host "$_backend"; then _check_host=failed fi @@ -229,7 +232,7 @@ EOF fi if test -n "$_listenip"; then - if ! validate_ip $_listenip; then + if ! validate_ip "$_listenip"; then err "Invalid IP: $_listenip" fi _listenip="$_listenip:" @@ -249,6 +252,15 @@ else echo " No!" fi +echo -n "Checking if conf path '$_confpath' exists? " +if test -d "$_confpath"; then + echo "Yes!" + clean_up +else + echo "No!" + clean_up +fi + ## ## End processing command line arguments ########################################### @@ -265,18 +277,18 @@ else echo " Yes!" fi -_vhost_conf_file=/etc/nginx/conf.d/${_domain}.conf +_vhost_conf_file=$_confpath/conf.d/${_domain}.conf echo -n "Checking if $_vhost_conf_file exists? " -if test -f $_vhost_conf_file; then +if test -f "$_vhost_conf_file"; then echo "Removing!" - rm $_vhost_conf_file + rm "$_vhost_conf_file" else echo "No!" fi echo "Creating Nginx configuration..." -cat << EOF > $_vhost_conf_file +cat << EOF > "$_vhost_conf_file" #### Description ## Type: HTTP ## VHost: $_domain @@ -300,7 +312,7 @@ $_locationblock_http EOF echo "Setting permissions on conf file..." -setfacl -m user:sysadmin:rw $_vhost_conf_file +setfacl -m user:sysadmin:rw "$_vhost_conf_file" if ! reload_nginx; then clean_up "Failed to reload Nginx" @@ -316,7 +328,7 @@ if ! get_cert; then clean_up "Failed to retrieve certificate!" fi -cat << EOF >> $_vhost_conf_file +cat << EOF >> "$_vhost_conf_file" server { listen ${_listenip}443 http2 ssl;