2022-06-03 15:24:14 +00:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
2022-07-20 13:15:16 +00:00
|
|
|
|
. /opt/bunkerweb/helpers/utils.sh
|
|
|
|
|
|
|
2022-06-03 15:24:14 +00:00
|
|
|
|
#############################################################
|
|
|
|
|
|
# Help #
|
|
|
|
|
|
#############################################################
|
2022-08-12 08:06:16 +00:00
|
|
|
|
function display_help()
|
2022-06-03 15:24:14 +00:00
|
|
|
|
{
|
2022-08-12 08:06:16 +00:00
|
|
|
|
# Display Help
|
2022-06-03 15:24:14 +00:00
|
|
|
|
echo "Usage of this script"
|
|
|
|
|
|
echo
|
|
|
|
|
|
echo "Syntax: start [start|stop|reload]"
|
|
|
|
|
|
echo "options:"
|
|
|
|
|
|
echo "start Create the configurations file and run all the jobs needed throught the bunkerweb service."
|
|
|
|
|
|
echo "stop Stop the bunkerweb service."
|
|
|
|
|
|
echo "reload Reload the bunkerweb service"
|
|
|
|
|
|
echo
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export PYTHONPATH=/opt/bunkerweb/deps/python/
|
|
|
|
|
|
|
|
|
|
|
|
# Add nginx to sudoers
|
2022-08-12 08:06:16 +00:00
|
|
|
|
if [ ! -f /etc/sudoers.d/bunkerweb ]; then
|
|
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Adding nginx user to sudoers ..."
|
|
|
|
|
|
echo "nginx ALL=(ALL) NOPASSWD: /bin/systemctl restart bunkerweb" > /etc/sudoers.d/bunkerweb
|
2022-06-03 15:24:14 +00:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
#############################################################
|
|
|
|
|
|
# Start #
|
|
|
|
|
|
#############################################################
|
|
|
|
|
|
|
|
|
|
|
|
function start() {
|
|
|
|
|
|
#############################################
|
|
|
|
|
|
# STEP1 #
|
|
|
|
|
|
# Generate variables.env files to /tmp/ #
|
|
|
|
|
|
#############################################
|
2022-08-16 07:35:27 +00:00
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Generate variables.env files to /tmp ..."
|
|
|
|
|
|
cp /opt/bunkerweb/variables.env /tmp/variables.env
|
|
|
|
|
|
result=$?
|
2022-08-12 08:06:16 +00:00
|
|
|
|
if [ $result -ne 0 ];
|
2022-06-03 15:24:14 +00:00
|
|
|
|
then
|
2022-08-16 07:35:27 +00:00
|
|
|
|
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
|
|
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
|
|
|
|
|
printf "\nTEMP_NGINX=yes" >> /tmp/variables.env
|
2022-08-12 08:06:16 +00:00
|
|
|
|
|
2022-06-03 15:24:14 +00:00
|
|
|
|
#############################################
|
|
|
|
|
|
# STEP2 #
|
|
|
|
|
|
# Generate first temporary config #
|
|
|
|
|
|
#############################################
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Generate temporary config ..."
|
2022-06-03 15:24:14 +00:00
|
|
|
|
/opt/bunkerweb/gen/main.py --settings /opt/bunkerweb/settings.json --templates /opt/bunkerweb/confs --output /etc/nginx --variables /tmp/variables.env
|
2022-08-12 08:06:16 +00:00
|
|
|
|
result=$?
|
2022-06-03 15:24:14 +00:00
|
|
|
|
if [ $result -ne 0 ];
|
|
|
|
|
|
then
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
|
2022-06-03 15:24:14 +00:00
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
2022-08-12 08:06:16 +00:00
|
|
|
|
|
2022-06-03 15:24:14 +00:00
|
|
|
|
#############################################
|
|
|
|
|
|
# STEP3 #
|
|
|
|
|
|
# Execute nginx #
|
|
|
|
|
|
#############################################
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Execute temporary BunkerWeb ..."
|
|
|
|
|
|
nginx
|
|
|
|
|
|
result=$?
|
2022-06-03 15:24:14 +00:00
|
|
|
|
if [ $result -ne 0 ];
|
|
|
|
|
|
then
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
|
2022-06-03 15:24:14 +00:00
|
|
|
|
exit 1
|
2022-08-12 08:06:16 +00:00
|
|
|
|
fi
|
|
|
|
|
|
|
2022-06-03 15:24:14 +00:00
|
|
|
|
#############################################
|
|
|
|
|
|
# STEP4 #
|
|
|
|
|
|
# Run jobs script #
|
|
|
|
|
|
#############################################
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Run jobs once ..."
|
2022-07-19 19:23:18 +00:00
|
|
|
|
/opt/bunkerweb/job/main.py --variables /opt/bunkerweb/variables.env --run
|
2022-08-12 08:06:16 +00:00
|
|
|
|
result=$?
|
2022-06-03 15:24:14 +00:00
|
|
|
|
if [ $result -ne 0 ];
|
|
|
|
|
|
then
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
|
2022-06-03 15:24:14 +00:00
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
2022-08-12 08:06:16 +00:00
|
|
|
|
|
2022-06-03 15:24:14 +00:00
|
|
|
|
#############################################
|
|
|
|
|
|
# STEP5 #
|
|
|
|
|
|
# Quit nginx #
|
|
|
|
|
|
#############################################
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Stop temporary BunkerWeb ..."
|
|
|
|
|
|
nginx -s quit
|
|
|
|
|
|
result=$?
|
2022-06-03 15:24:14 +00:00
|
|
|
|
if [ $result -ne 0 ];
|
|
|
|
|
|
then
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
|
2022-06-03 15:24:14 +00:00
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
2022-08-12 08:06:16 +00:00
|
|
|
|
|
2022-06-03 15:24:14 +00:00
|
|
|
|
#############################################
|
|
|
|
|
|
# STEP6 #
|
|
|
|
|
|
# Generate final confs #
|
2022-08-12 08:06:16 +00:00
|
|
|
|
#############################################
|
|
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Generate final config ..."
|
2022-06-03 15:24:14 +00:00
|
|
|
|
/opt/bunkerweb/gen/main.py --settings /opt/bunkerweb/settings.json --templates /opt/bunkerweb/confs --output /etc/nginx --variables /opt/bunkerweb/variables.env
|
2022-08-12 08:06:16 +00:00
|
|
|
|
result=$?
|
2022-06-03 15:24:14 +00:00
|
|
|
|
if [ $result -ne 0 ];
|
|
|
|
|
|
then
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
|
2022-06-03 15:24:14 +00:00
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
2022-08-12 08:06:16 +00:00
|
|
|
|
|
2022-06-03 15:24:14 +00:00
|
|
|
|
#############################################
|
|
|
|
|
|
# STEP7 #
|
|
|
|
|
|
# Run jobs infinite #
|
2022-08-12 08:06:16 +00:00
|
|
|
|
#############################################
|
|
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Run jobs scheduler ..."
|
2022-06-03 15:24:14 +00:00
|
|
|
|
/opt/bunkerweb/job/main.py --variables /etc/nginx/variables.env &
|
2022-08-12 08:06:16 +00:00
|
|
|
|
result=$?
|
2022-06-03 15:24:14 +00:00
|
|
|
|
if [ $result -ne 0 ];
|
|
|
|
|
|
then
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
|
2022-06-03 15:24:14 +00:00
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
2022-08-12 08:06:16 +00:00
|
|
|
|
|
2022-06-03 15:24:14 +00:00
|
|
|
|
#############################################
|
|
|
|
|
|
# STEP8 #
|
|
|
|
|
|
# Start nginx #
|
2022-08-12 08:06:16 +00:00
|
|
|
|
#############################################
|
|
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Start BunkerWeb ..."
|
2022-06-03 15:24:14 +00:00
|
|
|
|
nginx -g "daemon off; user nginx;"
|
2022-08-12 08:06:16 +00:00
|
|
|
|
result=$?
|
2022-06-03 15:24:14 +00:00
|
|
|
|
if [ $result -ne 0 ];
|
|
|
|
|
|
then
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
|
2022-06-03 15:24:14 +00:00
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function stop()
|
|
|
|
|
|
{
|
2022-08-12 08:06:16 +00:00
|
|
|
|
ret=0
|
|
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Stopping BunkerWeb service ..."
|
|
|
|
|
|
|
2022-06-03 15:24:14 +00:00
|
|
|
|
# Check if pid file exist and remove it if so
|
|
|
|
|
|
PID_FILE_PATH="/opt/bunkerweb/tmp/scheduler.pid"
|
|
|
|
|
|
if [ -f "$PID_FILE_PATH" ];
|
|
|
|
|
|
then
|
2022-08-12 08:06:16 +00:00
|
|
|
|
var=$(cat "$PID_FILE_PATH")
|
|
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Sending stop signal to scheduler ..."
|
2022-06-03 15:24:14 +00:00
|
|
|
|
kill -SIGINT $var
|
2022-08-12 08:06:16 +00:00
|
|
|
|
result=$?
|
|
|
|
|
|
if [ $result -ne 0 ] ; then
|
|
|
|
|
|
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
|
|
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
2022-06-03 15:24:14 +00:00
|
|
|
|
else
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "❌" "Scheduler is not running"
|
|
|
|
|
|
ret=1
|
2022-06-03 15:24:14 +00:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Check if nginx running and if so, stop it
|
|
|
|
|
|
SERVICE="nginx"
|
2022-08-12 08:06:16 +00:00
|
|
|
|
if pgrep -x "$SERVICE" > /dev/null
|
2022-06-03 15:24:14 +00:00
|
|
|
|
then
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Sending stop signal to BunkerWeb ..."
|
|
|
|
|
|
nginx -s quit
|
|
|
|
|
|
result=$?
|
|
|
|
|
|
if [ $result -ne 0 ] ; then
|
|
|
|
|
|
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
|
|
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
2022-06-03 15:24:14 +00:00
|
|
|
|
else
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "❌" "BunkerWeb is not running"
|
|
|
|
|
|
exit 1
|
2022-06-03 15:24:14 +00:00
|
|
|
|
fi
|
2022-08-12 08:06:16 +00:00
|
|
|
|
exit $ret
|
2022-06-03 15:24:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function reload()
|
|
|
|
|
|
{
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Reloading BunkerWeb service ..."
|
|
|
|
|
|
|
2022-06-03 15:24:14 +00:00
|
|
|
|
# Check if pid file exist and remove it if so
|
|
|
|
|
|
PID_FILE_PATH="/opt/bunkerweb/tmp/scheduler.pid"
|
|
|
|
|
|
if [ -f "$PID_FILE_PATH" ];
|
|
|
|
|
|
then
|
2022-08-12 08:06:16 +00:00
|
|
|
|
var=$(cat "$PID_FILE_PATH")
|
|
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Sending reload signal to scheduler ..."
|
2022-06-03 15:24:14 +00:00
|
|
|
|
kill -SIGHUP $var
|
2022-08-12 08:06:16 +00:00
|
|
|
|
result=$?
|
|
|
|
|
|
if [ $result -ne 0 ] ; then
|
|
|
|
|
|
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
|
|
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
2022-06-03 15:24:14 +00:00
|
|
|
|
else
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "❌" "Scheduler is not running"
|
|
|
|
|
|
exit 1
|
2022-06-03 15:24:14 +00:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Check if nginx running and if so, reload it
|
|
|
|
|
|
SERVICE="nginx"
|
2022-08-12 08:06:16 +00:00
|
|
|
|
if pgrep -x "$SERVICE" > /dev/null
|
2022-06-03 15:24:14 +00:00
|
|
|
|
then
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "ℹ️" "Sending reload signal to BunkerWeb ..."
|
2022-06-03 15:24:14 +00:00
|
|
|
|
nginx -s reload
|
2022-08-12 08:06:16 +00:00
|
|
|
|
result=$?
|
|
|
|
|
|
if [ $result -ne 0 ] ; then
|
|
|
|
|
|
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
|
|
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
2022-06-03 15:24:14 +00:00
|
|
|
|
else
|
2022-08-12 08:06:16 +00:00
|
|
|
|
log "ENTRYPOINT" "❌" "BunkerWeb is not running"
|
|
|
|
|
|
exit 1
|
2022-06-03 15:24:14 +00:00
|
|
|
|
fi
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# List of differents args
|
|
|
|
|
|
case $1 in
|
|
|
|
|
|
"start")
|
|
|
|
|
|
start
|
|
|
|
|
|
;;
|
|
|
|
|
|
"stop")
|
|
|
|
|
|
stop
|
|
|
|
|
|
;;
|
|
|
|
|
|
"reload")
|
|
|
|
|
|
reload
|
|
|
|
|
|
;;
|
|
|
|
|
|
*)
|
|
|
|
|
|
echo "Invalid option!"
|
|
|
|
|
|
echo "List of option available:"
|
2022-08-12 08:06:16 +00:00
|
|
|
|
display_help
|
2022-06-03 15:24:14 +00:00
|
|
|
|
esac
|