description "Trove Guest"
author "Auto-Gen"

start on (filesystem and net-device-up IFACE!=lo)
stop on runlevel [016]
chdir /var/run
pre-start script
    mkdir -p /var/run/trove
    chown GUEST_USERNAME:root /var/run/trove/

    mkdir -p /var/lock/trove
    chown GUEST_USERNAME:root /var/lock/trove/

    mkdir -p GUEST_LOGDIR
    chown GUEST_USERNAME:root GUEST_LOGDIR

    # Copy the trove source from the user's development environment
    if [ ! -d /home/GUEST_USERNAME/trove ]; then
        sudo -u GUEST_USERNAME rsync -e 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' -avz --exclude='.*' HOST_SCP_USERNAME@CONTROLLER_IP:PATH_TROVE/ /home/GUEST_USERNAME/trove
    fi

    # Ensure conf dir exists and is readable
    mkdir -p /etc/trove/conf.d
    chmod -R +r /etc/trove
end script

script
    # For backwards compatibility until https://review.openstack.org/#/c/100381 merges
    TROVE_CONFIG="--config-dir=/etc/trove/conf.d"
    if [ ! -f /etc/trove/conf.d/guest_info ] && [ ! -f /etc/trove/conf.d/trove-guestagent.conf ]; then

        chmod +r /etc/guest_info
        sudo -u GUEST_USERNAME rsync -e 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' -avz --exclude='.*' HOST_SCP_USERNAME@CONTROLLER_IP:/etc/trove/trove-guestagent.conf ~GUEST_USERNAME/
        mv ~GUEST_USERNAME/trove-guestagent.conf /etc/trove/trove-guestagent.conf
        TROVE_CONFIG="--config-file=/etc/guest_info --config-file=/etc/trove/trove-guestagent.conf"

    fi

    exec su -c "/home/GUEST_USERNAME/trove/contrib/trove-guestagent $TROVE_CONFIG" GUEST_USERNAME
end script
