#!/usr/bin/env bash

set -Eeuo pipefail

print_state() {
    local unit
    for unit in nginx haproxy orchestrator sentinel5 sentinel7 keepalived; do
        printf 'UNIT=%s ENABLED=' "$unit"
        systemctl is-enabled "$unit" || true
        printf 'UNIT=%s ACTIVE=' "$unit"
        systemctl is-active "$unit" || true
    done
    ss -lntp || true
    ip -o -4 addr show || true
}

on_error() {
    local rc=$?
    trap - ERR
    echo "Q101_ORCHESTRATOR_REPAIR_FAILED_RC=$rc"
    print_state
    journalctl -u orchestrator.service --no-pager -n 80 || true
    echo 'Q101_ORCHESTRATOR_REPAIR_FAILED_STOP_NO_RETRY'
    exit "$rc"
}
trap on_error ERR

echo 'Q101_ORCHESTRATOR_REPAIR_BEGIN'
date -Is

[[ "$(hostname -s)" == 'sqjcnginx01' ]]
ip -o -4 addr show scope global | grep -F '10.2.203.102/'
[[ -z "$(systemctl show orchestrator.service -p User --value)" ]]
[[ -z "$(systemctl show orchestrator.service -p Group --value)" ]]

systemctl is-enabled --quiet nginx
systemctl is-active --quiet nginx
systemctl is-enabled --quiet haproxy
systemctl is-active --quiet haproxy
systemctl is-enabled --quiet orchestrator

for unit in sentinel5 sentinel7 keepalived; do
    [[ "$(systemctl is-enabled "$unit" 2>/dev/null || true)" == 'disabled' ]]
    [[ "$(systemctl is-active "$unit" 2>/dev/null || true)" == 'inactive' ]]
done

[[ ! -e /var/lib/orchestrator ]]
[[ ! -e /var/log/orchestrator ]]

install -d -m 0750 -o root -g root /var/lib/orchestrator
install -d -m 0750 -o root -g root /var/log/orchestrator

stat -c 'PATH=%n MODE=%a OWNER=%U:%G' \
    /var/lib/orchestrator \
    /var/log/orchestrator

systemctl reset-failed orchestrator.service
systemctl start orchestrator.service
sleep 3

systemctl is-enabled --quiet orchestrator.service
systemctl is-active --quiet orchestrator.service

stat -c 'PATH=%n MODE=%a OWNER=%U:%G SIZE=%s' \
    /var/lib/orchestrator \
    /var/log/orchestrator
find /var/lib/orchestrator -maxdepth 1 -type f \
    -printf 'ORCHESTRATOR_DATA=%f mode=%m owner=%u:%g size=%s\n' | sort

for unit in sentinel5 sentinel7 keepalived; do
    [[ "$(systemctl is-enabled "$unit" 2>/dev/null || true)" == 'disabled' ]]
    [[ "$(systemctl is-active "$unit" 2>/dev/null || true)" == 'inactive' ]]
done

if ip -o -4 addr show | grep -Eq '10\.2\.203\.(115|116|117)/'; then
    echo 'Q101_UNEXPECTED_VIP_PRESENT'
    exit 1
else
    echo 'Q101_VIP_ABSENT=PASS'
fi

curl -fsS --max-time 5 http://127.0.0.1:8081/__edge_health
echo
print_state
echo 'Q101_ORCHESTRATOR_REPAIR_PASS'
