#!/bin/bash
# Copyright (C) 2023 Rusbitech-Astra <support@rusbitech.ru>

if [[ $# -lt 2 ]]; then
    echo "Usage:
    brest-kub-installer <commands without spaces> <cluster>

    Description:
    This script handles Brest installation process

    Commands:
        configure - pre-configure all hosts in parallel
        1 - install DC
        2 - configure IPA clients
        3 - install FE
        4 - install KVM
        5 - configure RAFT
        install - all steps"
    exit 0
fi

TERM_FLAG=false
handle_term() {
    echo "Received signal to terminate installation"
    TERM_FLAG=true
}

trap handle_term SIGTERM

exit_if_terminated() {
    if [[ $TERM_FLAG == "true" ]]; then
        echo "Terminating installation"
        exit 2
    fi
}

# MAIN #########################################################################
# shellcheck disable=SC2034
export BR_CLUSTER=$1 # used in source script
BR_CMD=$2
shift 2

sleep 5
source /var/lib/brest-kub/scripts/init-user-env.sh

exec > >( tee "${BR_LOG}" | systemd-cat -t brest-kub-installer -p info) 2>&1

# COMMANDS PROCESSING ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if [[ "$BR_CMD" == "check" ]]; then
    for BR_HOSTNAME in "${!BR_CONFIG[@]}"; do
        echo "Checking host ${BR_HOSTNAME}"
        send_command_to_server "host" "check" "${BR_HOSTNAME}"
        sleep 1
    done
fi

if [[ "$BR_CMD" == "configure" ]] || [[ "$BR_CMD" == "install" ]] ; then
    echo "=== CONFIGURE ALL HOSTS ==="
    brest-kub-ctl cluster step PREPARE
    for BR_HOSTNAME in "${!BR_CONFIG[@]}"; do
        echo "Configuring host ${BR_HOSTNAME}"
        send_command_to_server "host" "configure" "${BR_HOSTNAME}"
        sleep 1
    done
fi

if [[ "$BR_CMD" == "install" ]] ; then
    while true; do
        exit_if_terminated
        sleep 30
        OUTPUT=$( send_command_to_server "state" "hosts" | tail -n 1 )
        if [[ $OUTPUT == "CONFIGURE_PROCESSES ONGOING" ]]; then
            echo "Configure processes are ongoing, waiting..."
            continue;
        elif [[ $OUTPUT == "CONFIGURE_PROCESSES SUCCESS" ]]; then
            echo "Configure processes completed succesfully"
            break;
        else
            echo "ERROR: invalid configure processes state"
            exit 1
        fi
    done
fi

#IMPORTANT TO RUN FROM THIS FOLDER!!!
cd "${BR_ANSIBLE_DIR}" || exit 1
#So ansible can load setting file, that cannot be specified on argument line

exit_if_terminated
if [[ "$BR_CMD" == *1* ]] || [[ "$BR_CMD" == "install" ]] ; then
    if [[ "$BR_SKIP_DC_CONFIG" == "true" ]]; then
        echo "=== CONFIGURING DOMAIN CONTROLLER SKIPPED AS EXTERNAL DC WAS SPECIFIED ==="
    else
        echo "=== CONFIGURING DOMAIN CONTROLLER STARTED ==="
        brest-kub-ctl cluster step FREEIPA
        ansible-playbook "astra_freeipa_server.yml" -i "${INVENTORY_FILE}" || exit-err "Failed to install FeeIPA server"
    fi
fi

exit_if_terminated
if [[ "$BR_CMD" == *2* ]] || [[ "$BR_CMD" == "install" ]]; then
    echo "=== CONFIGURING IPA CLIENT STARTED ==="
    brest-kub-ctl cluster step IPACLIENTS
    ansible-playbook "astra_freeipa_client.yml" -i "${INVENTORY_FILE}" || exit-err "Failed to install FeeIPA client"
fi

for BR_HOSTNAME in "${!BR_CONFIG[@]}"; do
    echo "Run hosts check for ${BR_HOSTNAME}"
    brest-kub-host "${BR_CLUSTER}" "hosts" "${BR_HOSTNAME}" # External DC will be skipped inside brest-kub-host
done

exit_if_terminated
if [[ "$BR_CMD" == *3* ]] || [[ "$BR_CMD" == "install" ]]; then
    echo "=== CONFIGURING FRONT MACHINE STARTED ==="
    brest-kub-ctl cluster step BRESTCLOUD
    ansible-playbook "brestcloud_ipa_configure.yml" -i "${INVENTORY_FILE}" || exit-err "Failed to install frontends"
fi

exit_if_terminated
if [[ "$BR_CMD" == *4* ]] || [[ "$BR_CMD" == "install" ]]; then
    echo "=== CONFIGURING KVM NODE STARTED ==="
    brest-kub-ctl cluster step KVM
    export TERM="xterm" # required to avoid error in ipa-libvirt-qemu postinst caused by tput
    ansible-playbook "brestcloud_ipa_kvm_nodes.yml" -i "${INVENTORY_FILE}" || exit-err "Failed to install KVM nodes"
fi


exit_if_terminated
if [[ "$BR_CMD" == *5* ]] || [[ "$BR_CMD" == "install" ]]; then
    if [[ "$BR_CONFIGURE_RAFT" == "true" ]]; then
        echo "=== CONFIGURING RAFT STARTED ==="
        brest-kub-ctl cluster step RAFT
        ansible-playbook "brestcloud_raft_configure.yml" -i "${INVENTORY_FILE}" || exit-err "Failed to configure RAFT"
    else
        echo "=== CONFIGURING RAFT SKIPPED ==="
    fi
fi

brest-kub-ctl cluster step COMPLETED
echo "Completed"
