Commit ff793781 authored by sim's avatar sim

Add start/stop/reload/restart script for development servers

parent 04947664
.PHONY: gargantext venv envs migrate conf
POSTGREST_INIT=./tools/init.d/gargantext-postgrest
BACKEND_INIT=./tools/init.d/gargantext-testserver
ifeq ($(TARGET), "prod")
TARG=prod
......@@ -10,8 +11,11 @@ endif
PY_VERSION='import sys; v=sys.version_info; print("{0}.{1}".format(*v))'
gargantext: venv conf migrate
.PHONY: setup
setup: venv conf migrate
.PHONY: venv
venv: envs
@echo "• Setup virtualenv with all dependencies..."
pipenv install $(PIPENV_ARGS)
......@@ -20,18 +24,70 @@ venv: envs
@pwd > $$(pipenv --venv)/lib/python$$($$(pipenv --py) -c $(PY_VERSION))/site-packages/gargantext.pth
@echo
.PHONY: envs
envs:
@echo "• Setup django settings module and configuration path..."
./tools/mkenvs.sh
@echo
.PHONY: migrate
migrate:
@echo "• Migrate database to latest version..."
pipenv run ./manage.py migrate
pipenv run alembic upgrade head
@echo
.PHONY: conf
conf:
@echo "• Setup gargantext configuration..."
./tools/mkconf.sh $(TARG)
@echo
.PHONY: checkdebian
checkdebian:
@./tools/checkdebian.sh
.PHONY: checkpipenv
checkpipenv:
@./tools/checkpipenv.sh
.PHONY: checkstartup
checkstartup: checkdebian checkpipenv
.PHONY: start
start: checkstartup
@echo "• Start gargantext servers..."
@$(BACKEND_INIT) start
@$(POSTGREST_INIT) start
@echo
.PHONY: stop
stop: checkstartup
@echo "• Stop gargantext servers..."
@$(BACKEND_INIT) stop
@$(POSTGREST_INIT) stop
@echo
.PHONY: restart
restart: checkstartup
@echo "• Restart gargantext servers..."
@$(BACKEND_INIT) restart
@$(POSTGREST_INIT) restart
@echo
.PHONY: reload
reload: checkstartup
@echo "• Reload gargantext servers..."
@$(BACKEND_INIT) reload
@$(POSTGREST_INIT) reload
@echo
.PHONY: check
check: checkstartup
@echo "• Check gargantext servers..."
@$(BACKEND_INIT) status || true
@$(POSTGREST_INIT) status || true
@echo
.PHONY: status
status: check
#!/usr/bin/env bash
fail () {
echo "Sorry, startup scripts only work on Debian and derivatives."
exit 1
}
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
. /lib/init/vars.sh
. /lib/lsb/init-functions
[ "$(type -t log_daemon_msg)" = "function" ] || fail
[ "$(type -t log_end_msg)" = "function" ] || fail
[ "$(type -t status_of_proc)" = "function" ] || fail
[ "$(type -t start-stop-daemon)" = "file" ] || fail
#!/usr/bin/env bash
[ -n "$PIPENV_ACTIVE" -o "$_" = "$(type -p pipenv)" ] || \
( echo "Please run this with pipenv run or in pipenv shell." && exit 1 )
......@@ -18,6 +18,8 @@ DB_PASS = {DB_PASS}
LOG_FILE = /var/log/gargantext/backend/django.log
LOG_LEVEL = {LOG_LEVEL}
LOG_FORMATTER = verbose
# Pidfile of django backend test server
TESTSERVER_PIDFILE = /tmp/gargantext_testserver.pid
[uwsgi]
......
......@@ -19,3 +19,8 @@ max-rows = 100
## stored proc to exec immediately after auth
# pre-request = "stored_proc_name"
# pidfile and logfile are not taken into account by postgrest itself, they are
# only used by our startup script
pidfile = "/tmp/postgrest.pid"
logfile = "/var/log/gargantext/postgrest/postgrest.log"
#!/bin/sh
### BEGIN INIT INFO
# Provides: gargantext-postgrest
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts postgrest web server for gargantext
# Description: starts postgrest for gargantext using start-stop-daemon
### END INIT INFO
PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=${POSTGREST:-$(which postgrest)}
NAME=gargantext-postgrest
DESC=gargantext-postgrest
STOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/5/KILL/5}"
BASH=/bin/bash
POSTGREST_CONF=$(realpath ${POSTGREST_CONF:-postgrest.conf})
# Try to extract postgrest logfile
POSTGREST_LOG=$(printf "$(grep -oP '^\s*logfile\s*=\s*"\K(\\"|[^"])+' $POSTGREST_CONF | tail -1)")
POSTGREST_LOG=$(realpath ${POSTGREST_LOG:-postgrest.log})
# Try to extract postgrest pidfile
PID=$(printf "$(grep -oP '^\s*pidfile\s*=\s*"\K(\\"|[^"])+' $POSTGREST_CONF | tail -1)")
PID=${PID:-/tmp/postgrest.pid}
DAEMON_OPTS=$POSTGREST_CONF
test -x "$DAEMON" || exit 0
. /lib/init/vars.sh
. /lib/lsb/init-functions
start() {
# Start the daemon/service
#
# Returns:
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
CMD="exec $DAEMON $DAEMON_OPTS >> $POSTGREST_LOG 2>&1"
start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --pidfile $PID --make-pidfile --background --startas $BASH -- -c "$CMD" \
|| return 2
sleep 1
start-stop-daemon --status --pidfile $PID --exec $DAEMON > /dev/null \
|| return 2
}
test_config() {
# Test the postgrest configuration
[ -f "$POSTGREST_CONF" ]
}
stop() {
# Stops the daemon/service
#
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry $STOP_SCHEDULE --remove-pidfile --pidfile $PID
# Let start-stop-daemon take time to remove pidfile
RETVAL="$?"
sleep 1
return "$RETVAL"
}
reload() {
# Function that sends a SIGHUP to the daemon/service
start-stop-daemon --stop --signal HUP --quiet --pidfile $PID
return 0
}
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
restart)
log_daemon_msg "Restarting $DESC" "$NAME"
# Check configuration before stopping nginx
if ! test_config; then
log_end_msg 1 # Configuration error
exit $?
fi
stop
case "$?" in
0|1)
start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
reload|force-reload)
log_daemon_msg "Reloading $DESC configuration" "$NAME"
# Check configuration before stopping nginx
#
# This is not entirely correct since the on-disk nginx binary
# may differ from the in-memory one, but that's not common.
# We prefer to check the configuration and return an error
# to the administrator.
if ! test_config; then
log_end_msg 1 # Configuration error
exit $?
fi
reload
log_end_msg $?
;;
configtest|testconfig)
log_daemon_msg "Testing $DESC configuration"
test_config
log_end_msg $?
;;
status)
status_of_proc -p $PID "$DAEMON" "$NAME" && exit 0 || exit $?
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
exit 3
;;
esac
#!/bin/sh
### BEGIN INIT INFO
# Provides: gargantext-testserver
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts gargantext backend test server
# Description: starts gargantext backend test server using start-stop-daemon
### END INIT INFO
PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=$(pipenv --py)
NAME=gargantext-testserver
DESC=gargantext-testserver
# Have to send SIGINT to django testserver main process in order to kill its sub-processes
STOP_SCHEDULE="${STOP_SCHEDULE:-INT/5/KILL/5}"
BASH=/bin/bash
GARGANTEXT_CONF=$(realpath ${GARGANTEXT_CONF:-gargantext.ini})
DAEMON_OPTS="$PWD/manage.py runserver"
test -x "$DAEMON" || exit 0
. /lib/init/vars.sh
. /lib/lsb/init-functions
# Try to extract gargantext testserver pidfile
PID=$(grep -oP '^\s*TESTSERVER_PIDFILE\s*=\s*\K.+' $GARGANTEXT_CONF | tail -1)
PID=${PID:-/tmp/gargantext_testserver.pid}
start() {
# Start the daemon/service
#
# Returns:
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
ERROR_LOG=$(mktemp)
CMD="exec $DAEMON $DAEMON_OPTS > $ERROR_LOG 2>&1"
start-stop-daemon --start --pidfile $PID --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --pidfile $PID --make-pidfile --background --startas $BASH --chdir "$PWD" -- -c "$CMD" \
|| return 2
# Sleep 1 second and get error message if any
sleep 1
ERROR=$(tail -1 $ERROR_LOG)
start-stop-daemon --status --pidfile $PID --exec $DAEMON > /dev/null \
|| ( log_progress_msg "[$ERROR]" && return 2 )
}
test_config() {
# Test the gargantext configuration
[ -f "$GARGANTEXT_CONF" ]
}
stop() {
# Stops the daemon/service
#
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry $STOP_SCHEDULE --remove-pidfile --pidfile $PID
# Let start-stop-daemon take time to remove pidfile
RETVAL="$?"
sleep 1
return "$RETVAL"
}
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
cat $ERROR_LOG && rm $ERROR_LOG
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
restart|reload|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
# Check configuration before stopping nginx
if ! test_config; then
log_end_msg 1 # Configuration error
exit $?
fi
stop
case "$?" in
0|1)
start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
cat $ERROR_LOG && rm $ERROR_LOG
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
configtest|testconfig)
log_daemon_msg "Testing $DESC configuration"
test_config
log_end_msg $?
;;
status)
status_of_proc -p $PID "$DAEMON" "$NAME" && exit 0 || exit $?
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
exit 3
;;
esac
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment