Sophie

Sophie

distrib > Fedora > 18 > i386 > by-pkgid > 41cf7f31e8cc53ea053b7410f9b93228 > files > 1

nordugrid-arc-acix-index-4.0.0-1.fc18.noarch.rpm

#!/bin/sh
# ARC cache index server
#
# chkconfig: - 75 25
# description:  The ARC cache index server collects cache information from \
#               cache servers and can be queried for the locations of cached \
#               files.

### BEGIN INIT INFO
# Provides:             acix-index
# Required-Start:       $network
# Required-Stop:        $network
# Should-Start:         $time
# Should-Stop:          $time
# Default-Stop:0 1 2 3 4 5 6
# Short-Description:    ARC cacheindex, index server
# Description:          The ARC cache index server collects cache information
#                       from cache servers and can be queried for the locations
#                       of cached files.
### END INIT INFO

# source function library
if [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
    log_success_msg() {
        echo -n "$@"
        success "$@"
        echo
    }
    log_warning_msg() {
        echo -n "$@"
        warning "$@"
        echo
    }
    log_failure_msg() {
        echo -n "$@"
        failure "$@"
        echo
    }
elif [ -f /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
else
    echo "Error: Cannot source neither init.d nor lsb functions"
    exit 1
fi


PIDFILE=/var/run/acix-index.pid
LOGFILE=/var/log/arc/acix-index.log

if [ ! -d `dirname $LOGFILE` ]; then
    mkdir -p `dirname $LOGFILE`
fi

prog=twistd

RUN=yes

# sysconfig files
if [ -r /etc/sysconfig/nordugrid ]; then
    . /etc/sysconfig/nordugrid
elif [ -r /etc/default/nordugrid ]; then
    . /etc/default/nordugrid
fi
if [ -r /etc/sysconfig/acix-index ]; then
    . /etc/sysconfig/acix-index
elif [ -r /etc/default/acix-index ]; then
    . /etc/default/acix-index
fi

if [ `id -u` = 0 ] ; then
    # Debian does not have /var/lock/subsys
    if [ -d /var/lock/subsys ]; then
        LOCKFILE=/var/lock/subsys/acix-index
    else
        LOCKFILE=/var/lock/acix-index
    fi
else
    LOCKFILE=$HOME/acix-index.lock
fi

APPSTART="from acix import indexserver ; application = indexserver.createApplication()"

do_start() {

    if [ "$RUN" != "yes" ] ; then
        echo "acix-index service is disabled, please adjust the configuration to your"
        echo "needs and then set RUN to 'yes' in /etc/default/acix-index to enable it."
        return 0
    fi

    # ARC_CONFIG
    if [ "x$ARC_CONFIG" = "x" ] && [ -r /etc/arc.conf ]; then
        ARC_CONFIG=/etc/arc.conf
    fi
    if [ ! -r "$ARC_CONFIG" ]; then
        log_failure_msg "ARC configuration not found (usually /etc/arc.conf)"
        exit 1
    fi

    echo -n "Starting ARC cache index..."

    # Check if we are already running
    if [ -f $PIDFILE ]; then
        read pid < $PIDFILE
        if [ "x$pid" != "x" ]; then
            ps -p "$pid" -o comm 2>/dev/null | grep "^$prog$" 1>/dev/null 2>/dev/null
            if [ $? -eq 0 ] ; then
                log_success_msg "already running (pid $pid)"
                return 0
            fi
        fi
        rm -f "$PIDFILE" "$LOCKFILE"
    fi
    
    TACFILE=`mktemp` || exit 1
    echo $APPSTART > $TACFILE
        
    $prog --pidfile $PIDFILE -l $LOGFILE -y $TACFILE
    RETVAL=$?
    rm -f $TACFILE
    
    if [ $RETVAL -eq 0 ]; then
        touch $LOCKFILE
        log_success_msg
    else
        log_failure_msg
    fi
    return $RETVAL    
}

do_stop() {

    echo -n "Stopping ARC cache index..."

    if [ -f "$PIDFILE" ]; then
        read pid < "$PIDFILE"
        if [ ! -z "$pid" ] ; then
            kill "$pid"
            RETVAL=$?
            if [ $RETVAL -eq 0 ]; then
                log_success_msg
            else
                log_failure_msg
            fi
      
            timeout=2; # for stopping nicely
            
            while ( ps -p "$pid" -o comm 2>/dev/null | grep "^$prog$" 1>/dev/null 2>/dev/null ) && [ $timeout -ge 1 ] ; do
                sleep 1
                timeout=$(($timeout - 1))
            done

            [ $timeout -lt 1 ] && kill -9 "$pid" 1>/dev/null 2>&1
            rm -f "$PIDFILE" "$LOCKFILE"
        else
            RETVAL=1
            log_failure_msg "$prog shutdown - pidfile is empty"
        fi
    else
        RETVAL=0
        log_success_msg "$prog shutdown - already stopped"
    fi
    return $RETVAL
}

do_status() {
    if [ -f "$PIDFILE" ]; then
        read pid < "$PIDFILE"
        if [ "$pid" != "" ]; then
            if ps -p "$pid" > /dev/null; then
                echo "$1 (pid $pid) is running..."
                return 0
            fi
            echo "$1 stopped but pid file exists"
            return 1
        fi
    fi
    if [ -f $LOCKFILE ]; then
        echo "$1 stopped but lockfile exists"
        return 2
    fi
    echo "$1 is stopped"
    return 3
}

do_restart() {
    do_stop
    do_start
}

case "$1" in
    start)
        do_start
    ;;
    stop)
        do_stop
    ;;
    restart|reload|force-reload)
        do_restart
    ;;
    condrestart|try-restart)
        [ -f $LOCKFILE ] && do_restart || :
    ;;
    status)
        do_status $prog
    ;;
    *)
        echo "Usage: $0 {start|stop|restart|status|reload|condrestart|try-restart}"
        exit 1
    ;;
esac

exit 0