bunkerweb/linux/scripts/start.sh

233 lines
7.1 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
. /opt/bunkerweb/helpers/utils.sh
#############################################################
# Help #
#############################################################
function display_help()
{
# Display Help
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
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
fi
#############################################################
# Start #
#############################################################
function start() {
#############################################
# STEP1 #
# Generate variables.env files to /tmp/ #
#############################################
log "ENTRYPOINT" "" "Generate variables.env files to /tmp ..."
cp /opt/bunkerweb/variables.env /tmp/variables.env
result=$?
if [ $result -ne 0 ];
then
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
printf "\nTEMP_NGINX=yes" >> /tmp/variables.env
#############################################
# STEP2 #
# Generate first temporary config #
#############################################
log "ENTRYPOINT" "" "Generate temporary config ..."
/opt/bunkerweb/gen/main.py --settings /opt/bunkerweb/settings.json --templates /opt/bunkerweb/confs --output /etc/nginx --variables /tmp/variables.env
result=$?
if [ $result -ne 0 ];
then
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
#############################################
# STEP3 #
# Execute nginx #
#############################################
log "ENTRYPOINT" "" "Execute temporary BunkerWeb ..."
nginx
result=$?
if [ $result -ne 0 ];
then
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
#############################################
# STEP4 #
# Run jobs script #
#############################################
log "ENTRYPOINT" "" "Run jobs once ..."
/opt/bunkerweb/job/main.py --variables /opt/bunkerweb/variables.env --run
result=$?
if [ $result -ne 0 ];
then
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
#############################################
# STEP5 #
# Quit nginx #
#############################################
log "ENTRYPOINT" "" "Stop temporary BunkerWeb ..."
nginx -s quit
result=$?
if [ $result -ne 0 ];
then
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
#############################################
# STEP6 #
# Generate final confs #
#############################################
log "ENTRYPOINT" "" "Generate final config ..."
/opt/bunkerweb/gen/main.py --settings /opt/bunkerweb/settings.json --templates /opt/bunkerweb/confs --output /etc/nginx --variables /opt/bunkerweb/variables.env
result=$?
if [ $result -ne 0 ];
then
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
#############################################
# STEP7 #
# Run jobs infinite #
#############################################
log "ENTRYPOINT" "" "Run jobs scheduler ..."
/opt/bunkerweb/job/main.py --variables /etc/nginx/variables.env &
result=$?
if [ $result -ne 0 ];
then
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
#############################################
# STEP8 #
# Start nginx #
#############################################
log "ENTRYPOINT" "" "Start BunkerWeb ..."
nginx -g "daemon off; user nginx;"
result=$?
if [ $result -ne 0 ];
then
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
}
function stop()
{
ret=0
log "ENTRYPOINT" "" "Stopping BunkerWeb service ..."
# Check if pid file exist and remove it if so
PID_FILE_PATH="/opt/bunkerweb/tmp/scheduler.pid"
if [ -f "$PID_FILE_PATH" ];
then
var=$(cat "$PID_FILE_PATH")
log "ENTRYPOINT" "" "Sending stop signal to scheduler ..."
kill -SIGINT $var
result=$?
if [ $result -ne 0 ] ; then
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
else
log "ENTRYPOINT" "❌" "Scheduler is not running"
ret=1
fi
# Check if nginx running and if so, stop it
SERVICE="nginx"
if pgrep -x "$SERVICE" > /dev/null
then
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
else
log "ENTRYPOINT" "❌" "BunkerWeb is not running"
exit 1
fi
exit $ret
}
function reload()
{
log "ENTRYPOINT" "" "Reloading BunkerWeb service ..."
# Check if pid file exist and remove it if so
PID_FILE_PATH="/opt/bunkerweb/tmp/scheduler.pid"
if [ -f "$PID_FILE_PATH" ];
then
var=$(cat "$PID_FILE_PATH")
log "ENTRYPOINT" "" "Sending reload signal to scheduler ..."
kill -SIGHUP $var
result=$?
if [ $result -ne 0 ] ; then
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
else
log "ENTRYPOINT" "❌" "Scheduler is not running"
exit 1
fi
# Check if nginx running and if so, reload it
SERVICE="nginx"
if pgrep -x "$SERVICE" > /dev/null
then
log "ENTRYPOINT" "" "Sending reload signal to BunkerWeb ..."
nginx -s reload
result=$?
if [ $result -ne 0 ] ; then
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
else
log "ENTRYPOINT" "❌" "BunkerWeb is not running"
exit 1
fi
}
# List of differents args
case $1 in
"start")
start
;;
"stop")
stop
;;
"reload")
reload
;;
*)
echo "Invalid option!"
echo "List of option available:"
display_help
esac