Skip to content

Instantly share code, notes, and snippets.

@oliveiraev
Last active December 18, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oliveiraev/5769332 to your computer and use it in GitHub Desktop.
Save oliveiraev/5769332 to your computer and use it in GitHub Desktop.
Run web2py server whitin a virtualenv/pyenv
#!/usr/bin/env /bin/sh
PIDFILE="web2py.pid"
LOGFILE="web2py.log"
SERVER_NAME="web2py.local"
if [ ! "$1" ]; then
echo 'Usage: {start|stop|restart|status| --web2py params}'
exit 1
fi
SERVICE="$0"
COMMAND="$1"
case $COMMAND in
start|stop|restart|status) shift;;
esac
if [ "$1" -a ! "$1{1}" = "-" ]; then
# replaces current virtual env
if [ "$VIRTUAL_ENV" ]; then
VIRTUAL_ENV="`echo $1 | sed -r 's:[^/]*/?\$:$1:'`"
fi
# uses matching workon virtual env
if [ "$WORKON_HOME" -a -d "$WORKON_HOME" ]; then
VIRTUAL_ENV="$WORKON_HOME/$1"
fi
# if absolute path, use it
if [ "$1{1}" = "/" ]; then
VIRTUAL_ENV="$1"
fi
# got a real virtual env! Append to path and shift arg
if [ -d "$VIRTUAL_ENV/bin" ]; then
PATH=$VIRTUAL_ENV/bin:$PATH
shift
fi
fi
# If pyenv is on, use the shims pointer
if [ "`command -v pyenv`" ]; then
EXECUTABLE="`pyenv which w2p_run 2>/dev/null ||:`"
fi
# If no pyenv or not valid environment, use global bin
if [ ! "$EXECUTABLE" ]; then
EXECUTABLE="`command -v w2p_run`"
fi
# If we're pointing to a shim or to nothing, die
if [ ! "$EXECUTABLE" -o "`head -n1 $EXECUTABLE | grep -v python`" ]; then
echo 'Web2py starter not found. Exiting.' >&2
exit 2
fi
BIN_DIR="`dirname $EXECUTABLE`"
EXECUTABLE="$BIN_DIR/python $BIN_DIR/w2p_run"
W2P_DIR="`dirname $BIN_DIR`/web2py"
DAEMON_ARGS="--exec $EXECUTABLE --pidfile $W2P_DIR/$PIDFILE"
VERSION_FILE="$W2P_DIR/VERSION"
test -d "$W2P_DIR" || mkdir -p "$W2P_DIR"
if [ ! -f "$VERSION_FILE" -o ! "`cat $VERSION_FILE`" ]; then
grep '__requires__' $BIN_DIR/w2p_run | cut -f2 -d"'" | awk -F'=' '{print $NF}' > $VERSION_FILE
fi
case "$COMMAND" in
"start")
DAEMON_ARGS="$DAEMON_ARGS --chdir $W2P_DIR --start --background"
EXECUTABLE_ARGS="-f $W2P_DIR -d $PIDFILE -l $LOGFILE -s $SERVER_NAME -a'<recycle>' $@"
start-stop-daemon $DAEMON_ARGS -- $EXECUTABLE_ARGS
;;
"stop")
start-stop-daemon $DAEMON_ARGS --stop
;;
"restart")
$SERVICE stop
while start-stop-daemon $DAEMON_ARGS --status; do sleep 1; done
$SERVICE start
;;
"status")
prefix=""
start-stop-daemon $DAEMON_ARGS --status || prefix="Not "
if [ ! "`echo "$@" | grep '\-q'`" ]; then
echo $prefix 'Running.'
fi
;;
*)
$EXECUTABLE -f $W2P_DIR "$@"
;;
esac
@oliveiraev
Copy link
Author

Several usages:

  • web2py.sh start myvirtualenv Open up myvirtualenv on $WORKON_HOME or .pyenv/versions
  • web2py.sh start /foo/bar/baz Uses /foo/bar/baz as virtual env home
  • web2py.sh start Will start the following alternatives:
    • pyenv version if a .python-version file exists
    • wide w2p_run if it exists

Append other parameters

web2py.sh start foo -p8001
web2py.sh start bar -p8002

Works with pyenv, virtualenv and virtualenvwrapper.

Make sure to put w2p_run into $PATH

Or just run web2py server normally

web2py.sh myvirtualenv -M -S myapp/default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment