Skip to content

Instantly share code, notes, and snippets.

@zokeber
Forked from mariuz/emperor.sh
Created January 10, 2014 19:13
Show Gist options
  • Select an option

  • Save zokeber/8360614 to your computer and use it in GitHub Desktop.

Select an option

Save zokeber/8360614 to your computer and use it in GitHub Desktop.

Revisions

  1. @mariuz mariuz revised this gist Nov 25, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion emperor.sh
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ do_start()
    local START_OPTS=" \
    --emperor $ENABLED_CONFIGS_DIR \
    --pidfile $PIDFILE \
    --daemonize /var/log/uwsgi/uwsgi-emperor.log"
    --daemonize /var/log/$NAME/uwsgi-emperor.log"
    if do_pid_check $PIDFILE; then
    $NAME $DAEMON_OPTS $START_OPTS
    else
  2. @mariuz mariuz revised this gist Nov 25, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion emperor.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/usr/bin/env bash

    ### BEGIN INIT INFO
    # Provides: uwsgi
    # Provides: emperor
    # Required-Start: $all
    # Required-Stop: $all
    # Default-Start: 2 3 4 5
  3. @mariuz mariuz revised this gist Nov 25, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions emperor.sh
    Original file line number Diff line number Diff line change
    @@ -6,8 +6,8 @@
    # Required-Stop: $all
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: starts the uwsgi app server
    # Description: starts uwsgi app server using start-stop-daemon
    # Short-Description: starts the uwsgi emperor app server
    # Description: starts uwsgi emperor app server using start-stop-daemon
    ### END INIT INFO
    set -e

  4. @mariuz mariuz revised this gist Nov 25, 2013. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions emperor.sh
    Original file line number Diff line number Diff line change
    @@ -20,10 +20,11 @@ AVAILABLE_CONFIGS_DIR=/etc/uwsgi/apps-available
    NAME=uwsgi
    DESC=emperor
    OWNER=www-data
    GROUP=www-data
    OP=$1

    [[ -x $DAEMON ]] || exit 0
    [[ -d $RUN ]] || mkdir $RUN && chown $OWNER $RUN
    [[ -d $RUN ]] || mkdir $RUN && chown $OWNER.$GROUP $RUN

    DAEMON_OPTS=""

    @@ -46,7 +47,7 @@ do_pid_check()

    do_start()
    {
    local PIDFILE=$RUN/$uwsgi.pid
    local PIDFILE=$RUN/$NAME.pid
    local START_OPTS=" \
    --emperor $ENABLED_CONFIGS_DIR \
    --pidfile $PIDFILE \
    @@ -60,7 +61,7 @@ do_start()

    send_sig()
    {
    local PIDFILE=$RUN/uwsgi.pid
    local PIDFILE=$RUN/$NAME.pid
    set +e
    [[ -f $PIDFILE ]] && kill $1 $(cat $PIDFILE) > /dev/null 2>&1
    set -e
  5. @mariuz mariuz revised this gist Nov 25, 2013. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions emperor.sh
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ do_pid_check()
    local PIDFILE=$1
    [[ -f $PIDFILE ]] || return 0
    local PID=$(cat $PIDFILE)
    for p in $(pgrep $VERSION); do
    for p in $(pgrep $NAME); do
    [[ $p == $PID ]] && return 1
    done
    return 0
    @@ -178,8 +178,7 @@ case "$OP" in
    ;;
    *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|reload|force-reload|status"
    "|enable|disable}">&2
    echo "Usage: $N {start|stop|restart|reload|force-reload|status|enable|disable}">&2
    exit 1
    ;;
    esac
  6. @mariuz mariuz revised this gist Nov 25, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion emperor.sh
    Original file line number Diff line number Diff line change
    @@ -19,10 +19,11 @@ ENABLED_CONFIGS_DIR=/etc/uwsgi/apps-enabled
    AVAILABLE_CONFIGS_DIR=/etc/uwsgi/apps-available
    NAME=uwsgi
    DESC=emperor
    OWNER=www-data
    OP=$1

    [[ -x $DAEMON ]] || exit 0
    [[ -d $RUN ]] || mkdir $RUN
    [[ -d $RUN ]] || mkdir $RUN && chown $OWNER $RUN

    DAEMON_OPTS=""

  7. @mariuz mariuz revised this gist Nov 25, 2013. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions emperor.sh
    Original file line number Diff line number Diff line change
    @@ -11,20 +11,18 @@
    ### END INIT INFO
    set -e

    VERSION=$(basename $0)

    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    DAEMON=/usr/bin/uwsgi
    RUN=/var/run/uwsgi
    ENABLED_CONFIGS_DIR=/etc/uwsgi/apps-enabled
    AVAILABLE_CONFIGS_DIR=/etc/uwsgi/apps-available
    OWNER=www-data
    NAME=uwsgi
    DESC=emperor
    OP=$1

    [[ -x $DAEMON ]] || exit 0
    [[ -d $RUN ]] || mkdir $RUN && chown www-data $RUN
    [[ -d $RUN ]] || mkdir $RUN

    DAEMON_OPTS=""

  8. @mariuz mariuz revised this gist Nov 25, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions emperor.sh
    Original file line number Diff line number Diff line change
    @@ -16,8 +16,8 @@ VERSION=$(basename $0)
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    DAEMON=/usr/bin/uwsgi
    RUN=/var/run/uwsgi
    ENABLED_CONFIGS_DIR=/etc/uwsgi/sites-enabled
    AVAILABLE_CONFIGS_DIR=/etc/uwsgi/sites-available
    ENABLED_CONFIGS_DIR=/etc/uwsgi/apps-enabled
    AVAILABLE_CONFIGS_DIR=/etc/uwsgi/apps-available
    OWNER=www-data
    NAME=uwsgi
    DESC=emperor
  9. @mariuz mariuz revised this gist Nov 25, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions emperor.sh
    Original file line number Diff line number Diff line change
    @@ -51,9 +51,9 @@ do_start()
    local START_OPTS=" \
    --emperor $ENABLED_CONFIGS_DIR \
    --pidfile $PIDFILE \
    --daemon /var/log/uwsgi/uwsgi-emperor.log"
    --daemonize /var/log/uwsgi/uwsgi-emperor.log"
    if do_pid_check $PIDFILE; then
    uwsgi $DAEMON_OPTS $START_OPTS
    $NAME $DAEMON_OPTS $START_OPTS
    else
    echo "Already running!"
    fi
  10. @mariuz mariuz revised this gist Nov 25, 2013. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions emperor.sh
    Original file line number Diff line number Diff line change
    @@ -51,10 +51,9 @@ do_start()
    local START_OPTS=" \
    --emperor $ENABLED_CONFIGS_DIR \
    --pidfile $PIDFILE \
    --daemon /var/log/uwsgi/uwsgi-emperor.log \
    "
    --daemon /var/log/uwsgi/uwsgi-emperor.log"
    if do_pid_check $PIDFILE; then
    sudo -u $OWNER -i uwsgi $DAEMON_OPTS $START_OPTS
    uwsgi $DAEMON_OPTS $START_OPTS
    else
    echo "Already running!"
    fi
  11. @mariuz mariuz revised this gist Nov 25, 2013. 1 changed file with 14 additions and 14 deletions.
    28 changes: 14 additions & 14 deletions emperor.sh
    Original file line number Diff line number Diff line change
    @@ -13,14 +13,14 @@ set -e

    VERSION=$(basename $0)

    PATH=/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    DAEMON=/usr/local/bin/$VERSION
    RUN=/var/run/$VERSION
    ENABLED_CONFIGS_DIR=/etc/uwsgi/$VERSION/sites-enabled
    AVAILABLE_CONFIGS_DIR=/etc/uwsgi/$VERSION/sites-available
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    DAEMON=/usr/bin/uwsgi
    RUN=/var/run/uwsgi
    ENABLED_CONFIGS_DIR=/etc/uwsgi/sites-enabled
    AVAILABLE_CONFIGS_DIR=/etc/uwsgi/sites-available
    OWNER=www-data
    NAME=$VERSION
    DESC=$VERSION
    NAME=uwsgi
    DESC=emperor
    OP=$1

    [[ -x $DAEMON ]] || exit 0
    @@ -29,8 +29,8 @@ OP=$1
    DAEMON_OPTS=""

    # Include uwsgi defaults if available
    if [[ -f /etc/default/$VERSION ]]; then
    . /etc/default/$VERSION
    if [[ -f /etc/default/uwsgi ]]; then
    . /etc/default/uwsgi
    fi

    do_pid_check()
    @@ -47,30 +47,30 @@ do_pid_check()

    do_start()
    {
    local PIDFILE=$RUN/$VERSION.pid
    local PIDFILE=$RUN/$uwsgi.pid
    local START_OPTS=" \
    --emperor $ENABLED_CONFIGS_DIR \
    --pidfile $PIDFILE \
    --daemon /var/log/uwsgi/$VERSION-emperor.log \
    --daemon /var/log/uwsgi/uwsgi-emperor.log \
    "
    if do_pid_check $PIDFILE; then
    sudo -u $OWNER -i $VERSION $DAEMON_OPTS $START_OPTS
    sudo -u $OWNER -i uwsgi $DAEMON_OPTS $START_OPTS
    else
    echo "Already running!"
    fi
    }

    send_sig()
    {
    local PIDFILE=$RUN/$VERSION.pid
    local PIDFILE=$RUN/uwsgi.pid
    set +e
    [[ -f $PIDFILE ]] && kill $1 $(cat $PIDFILE) > /dev/null 2>&1
    set -e
    }

    wait_and_clean_pidfile()
    {
    local PIDFILE=$RUN/$VERSION.pid
    local PIDFILE=$RUN/uwsgi.pid
    until do_pid_check $PIDFILE; do
    echo -n "";
    done
  12. @mariuz mariuz created this gist Nov 25, 2013.
    188 changes: 188 additions & 0 deletions emperor.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,188 @@
    #!/usr/bin/env bash

    ### BEGIN INIT INFO
    # Provides: uwsgi
    # Required-Start: $all
    # Required-Stop: $all
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: starts the uwsgi app server
    # Description: starts uwsgi app server using start-stop-daemon
    ### END INIT INFO
    set -e

    VERSION=$(basename $0)

    PATH=/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    DAEMON=/usr/local/bin/$VERSION
    RUN=/var/run/$VERSION
    ENABLED_CONFIGS_DIR=/etc/uwsgi/$VERSION/sites-enabled
    AVAILABLE_CONFIGS_DIR=/etc/uwsgi/$VERSION/sites-available
    OWNER=www-data
    NAME=$VERSION
    DESC=$VERSION
    OP=$1

    [[ -x $DAEMON ]] || exit 0
    [[ -d $RUN ]] || mkdir $RUN && chown www-data $RUN

    DAEMON_OPTS=""

    # Include uwsgi defaults if available
    if [[ -f /etc/default/$VERSION ]]; then
    . /etc/default/$VERSION
    fi

    do_pid_check()
    {
    local PIDFILE=$1
    [[ -f $PIDFILE ]] || return 0
    local PID=$(cat $PIDFILE)
    for p in $(pgrep $VERSION); do
    [[ $p == $PID ]] && return 1
    done
    return 0
    }


    do_start()
    {
    local PIDFILE=$RUN/$VERSION.pid
    local START_OPTS=" \
    --emperor $ENABLED_CONFIGS_DIR \
    --pidfile $PIDFILE \
    --daemon /var/log/uwsgi/$VERSION-emperor.log \
    "
    if do_pid_check $PIDFILE; then
    sudo -u $OWNER -i $VERSION $DAEMON_OPTS $START_OPTS
    else
    echo "Already running!"
    fi
    }

    send_sig()
    {
    local PIDFILE=$RUN/$VERSION.pid
    set +e
    [[ -f $PIDFILE ]] && kill $1 $(cat $PIDFILE) > /dev/null 2>&1
    set -e
    }

    wait_and_clean_pidfile()
    {
    local PIDFILE=$RUN/$VERSION.pid
    until do_pid_check $PIDFILE; do
    echo -n "";
    done
    rm -f $PIDFILE
    }

    do_stop()
    {
    send_sig -3
    wait_and_clean_pidfile
    }

    do_reload()
    {
    send_sig -1
    }

    do_force_reload()
    {
    send_sig -15
    }

    get_status()
    {
    send_sig -10
    }

    enable_configs()
    {
    local configs

    if [[ $# -eq 0 || ${1,,} = 'all' ]]; then
    configs=$(diff $AVAILABLE_CONFIGS_DIR $ENABLED_CONFIGS_DIR \
    | grep $AVAILABLE_CONFIGS_DIR \
    | sed -re 's#.+: (.+)$#\1#')
    else
    configs=$@
    fi

    for c in $configs; do
    echo -n "Enabling $c..."
    [[ -f $ENABLED_CONFIGS_DIR/$c ]] && echo "Skipped" && continue
    [[ -f $AVAILABLE_CONFIGS_DIR/$c ]] && \
    ln -s $AVAILABLE_CONFIGS_DIR/$c $ENABLED_CONFIGS_DIR && \
    echo "Done" && \
    continue
    echo "Error"
    done
    }

    disable_configs()
    {
    local configs
    if [[ $# -eq 0 || ${1,,} = 'all' ]]; then
    configs=$(find $ENABLED_CONFIGS_DIR -type l -exec basename {} \;)
    else
    configs=$@
    fi

    for c in $configs; do
    local config_path="$ENABLED_CONFIGS_DIR/$c"
    echo -n "Disabling $c..."
    [[ ! -L $config_path ]] && echo "Skipped" && continue
    [[ -f $config_path ]] && rm $config_path && echo "Done" && continue
    echo "Error"
    done
    }

    case "$OP" in
    start)
    echo "Starting $DESC: "
    do_start
    echo "$NAME."
    ;;
    stop)
    echo -n "Stopping $DESC: "
    do_stop
    echo "$NAME."
    ;;
    reload)
    echo -n "Reloading $DESC: "
    do_reload
    echo "$NAME."
    ;;
    force-reload)
    echo -n "Force-reloading $DESC: "
    do_force_reload
    echo "$NAME."
    ;;
    restart)
    echo "Restarting $DESC: "
    do_stop
    sleep 1
    do_start
    echo "$NAME."
    ;;
    status)
    get_status
    ;;
    enable)
    shift
    enable_configs $@
    ;;
    disable)
    shift
    disable_configs $@
    ;;
    *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|reload|force-reload|status"
    "|enable|disable}">&2
    exit 1
    ;;
    esac
    exit 0