Files
proxy-scheduler/scripts/scheduler.sh

75 lines
2.2 KiB
Bash
Executable File

#!/bin/sh
# Initial parameters
DATE=`date +%F-%H-%M-%S`
# Set env variables
PROXY_TYPE=$PROXY_TYPE
DOMAIN_DIR=$DOMAIN_DIR
CERT_DIR=$CERT_DIR
PROXY_CONFIG_DIR=$PROXY_CONFIG_DIR
# If not exits CERT_DIR, create it
mkdir -p $CERT_DIR
# Triggers by certificate or domain config changes
unset IFS
inotifywait --exclude "\.(swp|tmp)" -m -e CREATE,CLOSE_WRITE,DELETE,MOVED_TO -r $DOMAIN_DIR $CERT_DIR $PROXY_CONFIG_DIR | \
while read dir op file
do
echo "DEBUG: $dir $file $op";
parent="/"$(echo $dir|cut -d / -f2)
if [[ "${parent}" == "${CERT_DIR}" && "${op}" == "CLOSE_WRITE,CLOSE" ]] ; then
DOMAIN=$(echo $dir|cut -d / -f3);
if [[ -f $CERT_DIR/$DOMAIN/new_certificate ]]; then
rm $CERT_DIR/$DOMAIN/new_certificate;
echo "New cert created: '$DOMAIN'";
echo "newcert check proxy";
/scripts/check_proxy_state.sh $DOMAIN;
fi
elif [ "${parent}" == "${DOMAIN_DIR}" ] && [[ "${op}" == "CLOSE_WRITE,CLOSE" || "${op}" == "MOVED_TO" ]]; then
if [[ "${PROXY_TYPE}" == "haproxy" ]]; then
DOMAIN=$(cat $DOMAIN_DIR"/"$file | jq -r .DOMAIN);
if [ "$DOMAIN" == "$file" ]; then
echo "haproxy config created, changed";
/scripts/config_haproxy_create.sh $file;
fi;
else
echo "domain config created, changed";
/scripts/nginx_config_create.sh "$file";
fi
elif [[ "${parent}" == "${PROXY_CONFIG_DIR}" && "${op}" == "CLOSE_WRITE,CLOSE" ]]; then
if [[ $file != "new_config" && $file != "config" ]]; then
DOMAIN=$(echo "${file%.*}");
if [ -f "$PROXY_CONFIG_DIR/new_config" ] ; then
/scripts/check_proxy_state.sh "$DOMAIN";
fi
fi;
elif [[ "${parent}" == "${DOMAIN_DIR}" && "${op}" == "DELETE" ]] ; then
echo "domain file: $file deleted";
if [[ "${PROXY_TYPE}" == "haproxy" ]]; then
echo "haproxy config deleted";
/scripts/config_haproxy_create.sh;
elif [ ! -f "$DOMAIN_DIR/$file" ]; then
/scripts/nginx_config_create.sh "$file" "DEL";
/scripts/check_proxy_state.sh "$file" "DEL";
fi
fi
done