{#
# Copyright 2015 Hewlett-Packard Development Company, L.P.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#
#}
#!/bin/sh

RETVAL=0

prog="octavia-keepalived"

start() {
    echo -n $"Starting $prog"
    ip netns exec {{ amphora_nsname }} {{ keepalived_cmd }} -D -d -f {{ keepalived_cfg }}
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch {{ keepalived_pid }}
}

stop() {
    echo -n $"Stopping $prog"
    kill -9 `pidof keepalived`
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f {{ keepalived_pid }}
}

status() {
    kill -0 `pidof keepalived`
    RETVAL=$?
    [ $RETVAL -eq 0 ] && echo -n $"$prog is running"
    [ $RETVAL -eq 1 ] && echo -n $"$prog is not found"
    echo
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    reload)
        stop
        start
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: $0 {start|stop|reload|status}"
        RETVAL=1
esac

exit $RETVAL
