Skip to content

Instantly share code, notes, and snippets.

@roktas
Created July 25, 2012 20:25

Revisions

  1. roktas revised this gist Jul 25, 2012. 2 changed files with 174 additions and 0 deletions.
    68 changes: 68 additions & 0 deletions unicorn-multi
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    #!/bin/sh
    ### BEGIN INIT INFO
    # Provides: unicorn
    # Required-Start: $local_fs $remote_fs
    # Required-Stop: $local_fs $remote_fs
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: unicorn initscript
    # Description: unicorn
    ### END INIT INFO

    set -e

    NAME=unicorn-multi
    DESC="Unicorn web server for multiple applications"

    . /lib/lsb/init-functions

    if [ -f /etc/default/unicorn-multi ]; then
    . /etc/default/unicorn-multi
    fi

    RUNNER=/etc/unicorn/unicorn-multi.sh
    CONFDIR=${CONFDIR-/etc/unicorn}

    [ -f $RUNNER ] || exit_with_message "Unicorn initializer $RUNNER is not exist."
    [ -d $CONFDIR ] || exit_with_message "Configuration directory $CONFDIR is not exist."

    list() {
    local f
    for f in ${CONFDIR}/${1}; do
    case "$f" in
    "${CONFDIR}/"'*' | *.* | *[A-Z]*)
    continue
    ;;
    *)
    [ -f "$f" ] || continue
    echo "$f"
    ;;
    esac
    done
    }

    run() {
    sh $RUNNER "$1" "$2" || {
    log_warning_msg "Action $1 failed for configuration $2" || true
    ERR=1
    }
    }

    ERR=

    if [ $# -ge 1 ]; then
    conffiles=$(list ${2-'*'})
    if [ -n "$conffiles" ]; then
    for conf in $conffiles; do
    run "$1" "$conf"
    done
    else
    log_warning_msg "No configuration file found." || true
    ERR=1
    fi
    else
    log_action_msg "Usage: $0 <start|stop|restart|force-reload|reload|force-stop|reopen-logs|status> [<conffile>]" || true
    ERR=1
    fi

    exit $ERR
    106 changes: 106 additions & 0 deletions unicorn-multi.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    #!/bin/sh

    set -e

    . /lib/lsb/init-functions

    usage() {
    log_action_msg "Usage: $0 <start|stop|restart|force-reload|reload|force-stop|reopen-logs|status> <conffile>" || true
    exit 1
    }

    [ $# -eq 2 ] || usage
    [ -f "$2" ] || exit_with_message "Configuration file '$2' not found."

    ACTION=$1
    CONFFILE=$2

    APP_NAME=${CONFFILE##*/}

    NAME=unicorn
    DESC="Unicorn web server for $APP_NAME"

    . $CONFFILE

    DAEMON=/usr/bin/unicorn
    PID=${PID-/run/unicorn-${APP_NAME}.pid}

    run_by_init() {
    ([ "${previous-}" ] && [ "${runlevel-}" ]) || [ "${runlevel-}" = S ]
    }

    exit_with_message() {
    if ! run_by_init; then
    log_action_msg "$1 Not starting."
    fi
    exit 0
    }

    check_app_root() {
    if ! [ -d $APP_ROOT ]; then
    exit_with_message "Application directory $APP_ROOT is not exist."
    fi
    }

    set -u

    case "$ACTION" in
    start)
    check_app_root

    log_daemon_msg "Starting $DESC" $NAME || true
    if start-stop-daemon --start --quiet --oknodo --pidfile $PID --exec $DAEMON -- $UNICORN_OPTS; then
    log_end_msg 0 || true
    else
    log_end_msg 1 || true
    fi
    ;;
    stop)
    log_daemon_msg "Stopping $DESC" $NAME || true
    if start-stop-daemon --stop --signal QUIT --quiet --oknodo --pidfile $PID; then
    log_end_msg 0 || true
    else
    log_end_msg 1 || true
    fi
    ;;
    force-stop)
    log_daemon_msg "Forcing stop of $DESC" $NAME || true
    if start-stop-daemon --stop --quiet --oknodo --pidfile $PID; then
    log_end_msg 0 || true
    else
    log_end_msg 1 || true
    fi
    ;;
    restart|force-reload)
    log_daemon_msg "Restarting $DESC" $NAME || true
    start-stop-daemon --stop --quiet --oknodo --pidfile $PID
    sleep 1
    if start-stop-daemon --start --quiet --oknodo --pidfile $PID --exec $DAEMON -- $UNICORN_OPTS; then
    log_end_msg 0 || true
    else
    log_end_msg 1 || true
    fi
    ;;
    reload)
    log_daemon_msg "Reloading $DESC" $NAME || true
    if start-stop-daemon --stop --signal HUP --quiet --oknodo --pidfile $PID; then
    log_end_msg 0 || true
    else
    log_end_msg 1 || true
    fi
    ;;
    reopen-logs)
    log_daemon_msg "Relopening log files of $DESC" $NAME || true
    if start-stop-daemon --stop --signal USR1 --quiet --oknodo --pidfile $PID; then
    log_end_msg 0 || true
    else
    log_end_msg 1 || true
    fi
    ;;
    status)
    status_of_proc -p $PID $DAEMON $NAME && exit 0 || exit $?
    ;;
    *)
    usage
    ;;
    esac
  2. roktas created this gist Jul 25, 2012.
    6 changes: 6 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    - Yapılandırmalar eski yerinde `/etc/unicorn`
    - `unicorn-multi.sh` (şimdilik) `/etc/unicorn` altında (executable olması
    gerekmiyor
    - `unicorn-multi` betiği `/etc/init.d` altında
    - Bu sistemin çalışması için mevcut `unicorn` betiğini kaldırmak gerekmiyor ama
    kaldırılırsa daha güvenli olabilir.