Skip to content
This repository has been archived by the owner on Aug 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from goncalopp/master
Browse files Browse the repository at this point in the history
I'll merge it as is for now. Thanks for your contribution.
  • Loading branch information
Jimmy Thrasibule committed Apr 24, 2015
2 parents 39b00c6 + 5bd1179 commit 8aa5bee
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
25 changes: 14 additions & 11 deletions README
Expand Up @@ -12,39 +12,42 @@ files.
Basic Usage
-----------

You wil find in the tree directory an example of how to use this script.
You will find in the tree directory an example of how to use this script.

For instance each file in the `CONF_DIR` directory, actually `/etc/gunicorn`,
For instance, each file in the `CONF_DIR` directory (`/etc/gunicorn` by default)
represents a web application. For each configuration file, the PID of the master
Gunicorn process is stored in `/var/run/gunicorn/<conf>.pid` and, logs are
Gunicorn process is stored in `/var/run/gunicorn/<conf>.pid`, and logs are
printed to `/var/log/gunicorn/<conf>.log`.


### Script configuration

The script configuration file is `/etc/default/gunicorn`. You have tree
variables to set.
The script configuration file is `/etc/default/gunicorn`. You have the following
variables to set:

* `RUN <yes|no>`, is the script should run on boot ?
* `RUN <yes|no>`, should the script run on system boot ?
* `CONF_DIR`, where are the web application configuration files ?
* `CONFIGS`, which configuration file(s) to load ?
* `USE_VIRTUALENVS`, which configuration file(s) to load ?
* `RUN_USER`, the user to run the gunicorn process

### Web application configuration

For the web application configuration file, you have for pssible variables.
For the web application configuration file, you have these possible variables:

* `WORKING_DIR`, the script will change to this directory before launching
Gunicorn.
* `APP_MODULE`, Python module and callable to run.
* `CONFIG_FILE`, Python Gunicorn configuration file path.
* `LOG_LEVEL`, logging level.
* `VIRTUALENV`, path to the app virtualenv.

### Init script

Theses are the different actions allowed by the init script.
These are the different actions allowed by the init script:

* `start`, starts all defined configuration files. The `RUN` variable must be
setted to `yes`.
set to `yes`.
* `stop`, stops all defined configuration files.
* `restart`, restarts all defined configuration files.
* `reload`, reloads (`kill -HUP`) all defined configuration files.
Expand All @@ -53,8 +56,8 @@ setted to `yes`.
* `stop_one <conf>`, stops the given configuration file.
* `reload_one <conf>`, reloads the given configuration file.

* `inc <conf>`, increase worker for the given configuration file.
* `dec <conf>`, decrease worker for the given configuration file.
* `inc <conf>`, increase workers for the given configuration file.
* `dec <conf>`, decrease workers for the given configuration file.



Expand Down
6 changes: 6 additions & 0 deletions tree/etc/default/gunicorn
Expand Up @@ -6,3 +6,9 @@ CONF_DIR=/etc/gunicorn

# Which configuration file should I run?
CONFIGS="example"

# Run as user
RUN_USER=gunicorn

# Use virtualenvs to run gunicorns
#USE_VIRTUALENVS=yes
2 changes: 2 additions & 0 deletions tree/etc/gunicorn/example.conf
Expand Up @@ -17,3 +17,5 @@ CONFIG_FILE=/etc/gunicorn/py/example.py
# You can change the logging level.
LOG_LEVEL="info"

# If you're using a virtualenv, provide it here
#VIRTUALENV=/path/to/virtualenv
28 changes: 23 additions & 5 deletions tree/etc/init.d/gunicorn
Expand Up @@ -15,7 +15,7 @@ DESC="Python WSGI HTTP Server"

SCRIPTNAME="/etc/init.d/$NAME"

CONFIG_VARS="APP_MODULE CONFIG_FILE LOG_LEVEL WORKING_DIR"
CONFIG_VARS="APP_MODULE CONFIG_FILE LOG_LEVEL WORKING_DIR VIRTUALENV"

PID_DIR="/var/run/$NAME"
LOG_DIR="/var/log/$NAME"
Expand All @@ -30,8 +30,16 @@ if [ -f /etc/default/$NAME ]; then
. /etc/default/$NAME
fi

[ -x $GUNICORN ] || log_failure_msg "Can't find $GUNICORN"
USER=`whoami`
[ "$RUN_USER" ] && USER=$RUN_USER


if [ ! -d $PID_DIR ]; then
mkdir $PID_DIR
chown $USER $PID_DIR
fi

[ -x $GUNICORN ] || [ "$USE_VIRTUALENVS" = "yes" ] || log_failure_msg "Can't find $GUNICORN"

start_one()
{
Expand Down Expand Up @@ -69,9 +77,19 @@ start_one()
fi
[ "$WORKING_DIR" ] && cd $WORKING_DIR
[ "$LOG_LEVEL" ] && args="$args --log-level=$LOG_LEVEL"

$GUNICORN $args --config=$CONFIG_FILE -- $APP_MODULE


if [ "$VIRTUALENV" ]; then
. $VIRTUALENV/bin/activate
GUNICORN=$VIRTUALENV/bin/gunicorn
fi

if [ ! -x $GUNICORN ]; then
log_action_end_msg 1 "Can't find $GUNICORN"
return 1
fi

sudo -E -u $USER $GUNICORN $args --config=$CONFIG_FILE -- $APP_MODULE

for var in $CONFIG_VARS; do
unset $var
done
Expand Down

0 comments on commit 8aa5bee

Please sign in to comment.