#!/usr/bin/env bash
# This script is used to reset and re-provision a zimbot imaged with
# imagingv2
#
# IMPORTANT: Bamboo normally creates a new agent when installing. To
# re-use an existing agent, the bamboo-agent.cfg.xml file must be preserved
# To work around this issue, copy the cfg file to the customization overlay
#
# sudo mount -o remount,rw /overlay/customization
# mkdir -p /overlay/customization/overlay/home/zoox/bamboo-agent-home
# cp /home/zoox/bamboo-agent-home/bamboo-agent.cfg.xml /overlay/customization/overlay/home/zoox/bamboo-agent-home
# sudo mount -o remount,ro /overlay/customization


set -e

ZIMBOT_DIR=$(pwd)
INVENTORY=${ZIMBOT_DIR}/inventory.yaml
BAMBOOPLAYBOOK=${ZIMBOT_DIR}/bamboo-agent.yaml

TAGS=""
LIMIT=""
while [ -n "$1" ]
do
  case $1 in
    --tags)TAGS="$1 $2"; shift;;
    --limit)LIMIT="$1 $2"; shift;;
  esac
  shift
done

if [ -z "$LIMIT" ]
then
  echo "--limit arg required, such as 'kittsim24.zooxlabs.com' or 'all'"
  exit 1
fi

# Reset deploy layer and reboot
ansible-playbook reset-zimbot.yaml -i ${INVENTORY} ${LIMIT} ${TAGS}
