bunkerweb/src/linux/scripts/start.sh

130 lines
No EOL
3.7 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
# Source the utils helper script
source /usr/share/bunkerweb/helpers/utils.sh
# Set the PYTHONPATH
export PYTHONPATH=/usr/share/bunkerweb/deps/python/
# Display usage information
function display_help() {
echo "Usage: $(basename "$0") [start|stop|reload]"
echo "Options:"
echo " start: Create configurations and run necessary jobs for the bunkerweb service."
echo " stop: Stop the bunkerweb service."
echo " reload: Reload the bunkerweb service."
}
# Start the bunkerweb service
function start() {
# Set the PYTHONPATH
export PYTHONPATH=/usr/share/bunkerweb/deps/python
# Get the pid of nginx and put it in a file
log "ENTRYPOINT" "" "Getting nginx pid ..."
nginx_pid=$(pgrep -x "nginx")
echo $nginx_pid > /var/tmp/bunkerweb/nginx.pid
# Check if scheduler pid file exist and remove it if so
if [ -f /var/tmp/bunkerweb/scheduler.pid ] ; then
rm -f /var/tmp/bunkerweb/scheduler.pid
fi
# Setup and check /data folder
/usr/share/bunkerweb/helpers/data.sh "ENTRYPOINT"
# Init database
echo -e "IS_LOADING=yes\nSERVER_NAME=\nAPI_HTTP_PORT=${API_HTTP_PORT:-7000}\nAPI_SERVER_NAME=${API_SERVER_NAME:-bwapi}\nAPI_WHITELIST_IP=${API_WHITELIST_IP:-127.0.0.0/8}" > /etc/bunkerweb/variables.env
/usr/share/bunkerweb/gen/save_config.py --variables /etc/bunkerweb/variables.env --init
if [ "$?" -ne 0 ] ; then
log "ENTRYPOINT" "❌" "Scheduler generator failed"
exit 1
fi
# Execute jobs
log "ENTRYPOINT" " " "Executing scheduler ..."
/usr/share/bunkerweb/scheduler/main.py --variables /etc/bunkerweb/variables.env
if [ "$?" -ne 0 ] ; then
log "ENTRYPOINT" "❌" "Scheduler failed"
exit 1
fi
log "ENTRYPOINT" " " "Scheduler stopped"
exit 0
}
function stop() {
ret=0
log "ENTRYPOINT" "" "Stopping BunkerWeb service ..."
# Check if pid file exist and remove it if so
pid_file_path="/var/tmp/bunkerweb/scheduler.pid"
if [ -f "$pid_file_path" ]; then
scheduler_pid=$(cat "$pid_file_path")
log "ENTRYPOINT" "" "Sending stop signal to scheduler with pid: $scheduler_pid"
kill -SIGINT $scheduler_pid
if [ "$?" -ne 0 ]; then
log "ENTRYPOINT" "❌" "Failed to stop scheduler process with pid: $scheduler_pid"
exit 1
fi
else
log "ENTRYPOINT" "❌" "Scheduler is not running"
ret=1
fi
# Check if nginx is running and if so, stop it
service="nginx"
if pgrep -x "$service" > /dev/null; then
log "ENTRYPOINT" "" "Stopping $service service"
nginx -s quit
if [ "$?" -ne 0 ]; then
log "ENTRYPOINT" "❌" "Failed to stop $service service"
exit 1
fi
else
log "ENTRYPOINT" "❌" "$service is not running"
ret=1
fi
exit $ret
}
function reload()
{
log "ENTRYPOINT" "" "Reloading BunkerWeb service ..."
# Send signal to scheduler to reload
PID_FILE_PATH="/var/tmp/bunkerweb/scheduler.pid"
if [ -f "$PID_FILE_PATH" ];
then
var=$(cat "$PID_FILE_PATH")
# Send signal to scheduler to reload
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
}
# List of differents args
case $1 in
"start")
start
;;
"stop")
stop
;;
"reload")
reload
;;
*)
echo "Invalid option!"
echo "List of options availables:"
display_help
esac