#!/usr/bin/env bash
set -euoE pipefail ## -E option will cause functions to inherit trap

# This script is executed by install-to-disk service when installing single node with bootstrap in place

. /usr/local/bin/bootstrap-service-record.sh

record_service_stage_start "wait-for-bootkube"
echo "Waiting for /opt/openshift/.bootkube.done"
until [ -f /opt/openshift/.bootkube.done ]; do
  sleep 5
done
record_service_stage_success

if [ ! -f coreos-installer.done ]; then
  record_service_stage_start "coreos-installer"
  # Write image + ignition to disk
  echo "Executing coreos-installer with the following options: install -i /opt/openshift/master.ign {{.BootstrapInPlace.InstallationDisk}}"
  coreos-installer install -i /opt/openshift/master.ign {{.BootstrapInPlace.InstallationDisk}}

  touch coreos-installer.done
  record_service_stage_success
fi

record_service_stage_start "reboot"
echo "Going to reboot"
shutdown -r +1 "Bootstrap completed, server is going to reboot."
touch /opt/openshift/.install-to-disk.done
echo "Done"
record_service_stage_success
