#!/bin/bash

set -eux

TFTP_ROOT=${TFTP_ROOT:-/tftpboot}
IRONIC_DRIVER=${IRONIC_DRIVER:-pxe_ipmitool}

OS_AUTH_URL=${OS_AUTH_URL:-""}
if [ -z "$OS_AUTH_URL" ]; then
    echo "You must source a stackrc file for the Undercloud."
    exit 1
fi

IRONIC_URL=${IRONIC_URL:-$(keystone endpoint-get --service baremetal | awk '/publicURL/ { print $4 } ')}
if [ -z "$IRONIC_URL" ]; then
    echo "IRONIC_URL is not set and cannot get URL from keystone."
    exit 1
fi

IRONIC_URL=${IRONIC_URL%/}
CALLBACK="$IRONIC_URL/v1/drivers/$IRONIC_DRIVER/vendor_passthru/continue_discovery"

mkdir -p "$TFTP_ROOT"

glance image-download --file "$TFTP_ROOT/discovery.kernel" discovery-kernel
glance image-download --file "$TFTP_ROOT/discovery.ramdisk" discovery-ramdisk
