#!/bin/bash
#
# chkconfig: 345 10 93
# description: EosCloudInit is used in vEOS for running in Cloud (AWS, Azure,..)
# This should run before EosStage2. This gets IP addr using dhcp, gets keys
# for login, downloads initial config before EOS runs. 
# Since there is no console on AWS, we are dumping logs into /mnt/flash
# so we can debug boot failures by mounting the disk on another machine

. /etc/rc.d/init.d/functions

start() {

    # check if it is vEOS
    platform=${EOS_PLATFORM:-$(xargs -n1 < /proc/cmdline |
                                      sed -n '0,/^platform=/{s/^platform=//p}')}
    [ "$platform" != 'veos' ] && [ "$platform" != 'baremetal' ] && exit 0
    echo -n $"Starting EOS Cloud Init: "
    # Redirect stdin to prevent EosInit from dropping into the debugger on failure
    if /usr/bin/EosCloudInit </dev/null >>/mnt/flash/.CloudInitLogs 2>&1; then
        success
        echo
    else
        # If EosInit fails, the system is pretty much hosed
        failure
        echo
        echo $"   (errors in /mnt/flash/.CloudInitLogs)"
        echo $"   (Mount disk on another machine to access logs)"
        exit 1
    fi
}

# Once we figure out what it means to "stop" Eos, add support to do
# that here.
case "$1" in
start)
    start
    ;;
stop)
   ;;
*)
    echo $"Usage: $0 {start}"
    exit 1
esac
