refactor linux/start.sh and fix tests/cors www copy

This commit is contained in:
bunkerity 2022-08-12 10:06:16 +02:00
parent eb6f0d6737
commit 5c99a4b0e2
No known key found for this signature in database
GPG Key ID: 3D80806F12602A7C
2 changed files with 99 additions and 95 deletions

View File

@ -5,7 +5,15 @@ if [ $(id -u) -ne 0 ] ; then
exit 1
fi
chown -R www-data:nginx ./bw-data/www
find ./bw-data/www -type f -exec chmod 0640 {} \;
find ./bw-data/www -type d -exec chmod 0750 {} \;
cp -rp ./bw-data/www/* /opt/bunkerweb/www
if id www-data > /dev/null 2>&1 ; then
user="www-data"
elif id apache > /dev/null 2>&1 ; then
user="apache"
else
echo "❌ No PHP user found"
exit 1
fi
cp -r ./bw-data/www/* /opt/bunkerweb/www
chown -R $user:nginx /opt/bunkerweb/www
find /opt/bunkerweb/www -type f -exec chmod 0640 {} \;
find /opt/bunkerweb/www -type d -exec chmod 0750 {} \;

View File

@ -5,9 +5,9 @@
#############################################################
# Help #
#############################################################
Help()
function display_help()
{
# Dispaly Help
# Display Help
echo "Usage of this script"
echo
echo "Syntax: start [start|stop|reload]"
@ -21,24 +21,11 @@ Help()
export PYTHONPATH=/opt/bunkerweb/deps/python/
# Add nginx to sudoers
if [ ! -f /etc/sudoers.d/nginx ]; then
log "" "Adding nginx user to sudoers file ..."
log "" "nginx ALL=(ALL) NOPASSWD: /bin/systemctl restart bunkerweb" >> /etc/sudoers.d/nginx
#log "$1" ""
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
#############################################################
# Checker #
#############################################################
function check_ok() {
if [ $? -eq 0 ]; then
result="$?"
else
result="$?"
fi
}
#############################################################
# Start #
#############################################################
@ -48,174 +35,183 @@ function start() {
# STEP1 #
# Generate variables.env files to /tmp/ #
#############################################
log "ENTRYPOINT" "" "Generate variables.env files to /tmp/ ..."
printf "HTTP_PORT=80\nSERVER_NAME=example.com\nTEMP_NGINX=yes" > "/tmp/variables.env"
# Test if command worked
check_ok
# Exit if failed
if [ $result -ne 0 ];
result=$?
if [ $result -ne 0 ];
then
echo "Your command exited with non-zero status $result"
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
log "" "Generate variables.env files to /tmp/"
#echo "OK !"
#############################################
# 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
# Test if command worked
check_ok
# Exit if failed
result=$?
if [ $result -ne 0 ];
then
log "❌" "Your command exited with non-zero status $result"
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
log "" "Generate first temporary config"
#echo "OK !"
#############################################
# STEP3 #
# Execute nginx #
#############################################
nginx
# Test if command worked
check_ok
# Exit if failed
log "ENTRYPOINT" "" "Execute temporary BunkerWeb ..."
nginx
result=$?
if [ $result -ne 0 ];
then
log "❌" "Your command exited with non-zero status $result"
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
log "" "Execute nginx"
#echo "OK !"
fi
#############################################
# STEP4 #
# Run jobs script #
#############################################
log "ENTRYPOINT" "" "Run jobs once ..."
/opt/bunkerweb/job/main.py --variables /opt/bunkerweb/variables.env --run
# Test if command worked
check_ok
# Exit if failed
result=$?
if [ $result -ne 0 ];
then
log "❌" "Your command exited with non-zero status $result"
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
log "" "Run jobs script"
#echo "OK !"
#############################################
# STEP5 #
# Quit nginx #
#############################################
nginx -s quit
# Test if command worked
check_ok
# Exit if failed
log "ENTRYPOINT" "" "Stop temporary BunkerWeb ..."
nginx -s quit
result=$?
if [ $result -ne 0 ];
then
log "❌" "Your command exited with non-zero status $result"
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
log "" "Quit nginx"
#echo "OK !"
#############################################
# 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
# Test if command worked
check_ok
# Exit if failed
result=$?
if [ $result -ne 0 ];
then
log "❌" "Your command exited with non-zero status $result"
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
log "" "Generate final confs"
#echo "OK !"
#############################################
# STEP7 #
# Run jobs infinite #
#############################################
#############################################
log "ENTRYPOINT" "" "Run jobs scheduler ..."
/opt/bunkerweb/job/main.py --variables /etc/nginx/variables.env &
# Test if command worked
check_ok
# Exit if failed
result=$?
if [ $result -ne 0 ];
then
log "❌" "Your command exited with non-zero status $result"
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
log "" "Run jobs infinite"
#echo "OK !"
#############################################
# STEP8 #
# Start nginx #
#############################################
#############################################
log "ENTRYPOINT" "" "Start BunkerWeb ..."
nginx -g "daemon off; user nginx;"
# Test if command worked
check_ok
# Exit if failed
result=$?
if [ $result -ne 0 ];
then
log "❌" "Your command exited with non-zero status $result"
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
log "" "Start nginx"
#echo "OK !"
}
function stop()
{
echo "Stoping Bunkerweb service ..."
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 )
var=$(cat "$PID_FILE_PATH")
log "ENTRYPOINT" "" "Sending stop signal to scheduler ..."
kill -SIGINT $var
log "" echo "Killing : $var"
result=$?
if [ $result -ne 0 ] ; then
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
else
log "❌" "File doesn't exist"
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
if pgrep -x "$SERVICE" > /dev/null
then
log "" "Stopping $SERVICE service"
nginx -s stop
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 "" "$SERVICE already stopped"
log "ENTRYPOINT" "❌" "BunkerWeb is not running"
exit 1
fi
#echo "Done !"
exit $ret
}
function reload()
{
log "" "Reloading Bunkerweb service ..."
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 )
var=$(cat "$PID_FILE_PATH")
log "ENTRYPOINT" "" "Sending reload signal to scheduler ..."
kill -SIGHUP $var
log "" "Reloading : $var"
result=$?
if [ $result -ne 0 ] ; then
log "ENTRYPOINT" "❌" "Your command exited with non-zero status $result"
exit 1
fi
else
log "❌" "File doesn't exist"
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
if pgrep -x "$SERVICE" > /dev/null
then
log "" "Reloading $SERVICE service"
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 "" "$SERVICE already stopped"
log "ENTRYPOINT" "❌" "BunkerWeb is not running"
exit 1
fi
#echo "Done !"
}
# List of differents args
@ -232,5 +228,5 @@ case $1 in
*)
echo "Invalid option!"
echo "List of option available:"
Help
display_help
esac