#!/bin/bash
#---------------------
# Testing nova-daemons
#---------------------
set -e
DAEMONS=('nova-conductor' 'nova-scheduler' \
    'nova-console' 'nova-consoleauth' 'nova-api')

if grep -qE "^(sql_)?connection.*sqlite.*" /etc/nova/nova.conf
then
    su -s /bin/sh -c 'nova-manage api_db sync' nova
    su -s /bin/sh -c 'nova-manage db sync' nova
fi

ret=0

for daemon in "${DAEMONS[@]}"; do
    TIMEOUT=50
    systemctl restart $daemon
    while [ "$TIMEOUT" -gt 0 ]; do
        if pidof -x $daemon > /dev/null; then
            echo "OK"
            break
        fi
        TIMEOUT=$((TIMEOUT - 1))
        sleep 0.1
    done

    if [ "$TIMEOUT" -le 0 ]; then
        echo "ERROR: ${daemon} IS NOT RUNNING"
        ret=1
    fi
done

exit $ret
