#!/bin/bash
#
# 4any Bootstrapper
#
# This script bootstraps and configure 4any prerequisites
# so full deployment can be triggered from 4any/L4U NOC.
#
# It requires the following inputs:
# - the "tenant ID" (3 digits unique code)
# - the Teleport's cluster token (derivated from the modulus of tenant's CA
#   certificate private key)
#
# The provisionning of the 4any tenant itself is triggered from 4any NOC
# by opening a Teleport tunnel from the NOC Ansible controller to the 4any
# tenant and, then the 4any provisionning playbook is executed remotely.
#
# Usage example:
#   curl -s https://installer.noc.4any.io/4any-bootstrap.sh | bash -s -- -i '123' -t bebedeadbebedeadbebedeadbebedead
#
# initial write: Eric Belhomme <ebelhomme@fr.scc.com>
# Copyright by SCC France SA (2021 onwards)

TENANT=
TOKEN=
AIRGAP=

if [ $UID -ne 0 ]; then
    echo -e "\e[1;31mError :\e[22m the 4any bootstrapper requires root privileges to execute\e[0m"
    exit 1
fi

ELRELEASE="$(sed -E 's/^.* ([0-9]+)\.[0-9\.]+ .*/\1/' /etc/redhat-release)"


print_help() {
cat <<EOF

                        -----<  SCC - 4ANY - Online bootstrapper  >-----
                      ---  Run as root user with -i and -t arguments:  ---
                        ------------------------------------------------

bash <(curl -s https://installer.noc.4any.io/4any-bootstrap.sh) [ -i <tenant ID> ] [ -t <Teleport token> ] [ -v <Teleport version> ]

  -i <tenant ID>       : 3 digit 4any Tenant ID
  -t <token>           : Teleport Token for Root cluster trusted cluster enrolment
  -v <Teleport version>: Teleport version to lock (defaults to '$TELEPORT_VERSION')
  -a                   : airgap installation mode (d/l all resources from SCC's NOC artifacts servers)
EOF
    exit 1
}

print_part_message() {
    cat <<EOF
Recommended partitioning scheme and minimum sizing for 4ANY Tenant:
  /                5 G
  /boot            1 G
  /tmp             5 G
  /var             10 G
  /var/log         10 G
  /usr             5G
  /var/lib/rancher 100 G
  /opt/k3s         100 G
  /opt/backup      10 G

EOF
}


sanitize() {
    local STR="$1"
    STR="${STR//[^[:alnum:]]/_}"
    STR="${STR//+(_)/_}"
    STR="${STR/#_}"
    STR="${STR/%_}"
    echo "${STR}"
}

while getopts ahi:t:v: arg; do
    case "$arg" in
        h) print_help;;
        i) TENANT=$OPTARG;;
        t) TOKEN=$OPTARG;;
        a) AIRGAP='yes';;
        v) TELEPORT_VERSION=$OPTARG;;
        *) print_help;;
    esac
done

if [ -z "$TENANT" ] || [ -z "$TOKEN" ]; then print_help; fi
if [[ ! "$TENANT" =~ ^[0-9]{3}$ ]]; then print_help; fi
echo "TENANT_ID='${TENANT}'" > /etc/default/4any-tenant

# install SCC release packages at very first
yum install -y "https://rpms.scc-services.fr/stable/el${ELRELEASE}/x86_64/Packages/scc-release-${ELRELEASE}-0.el${ELRELEASE}.noarch.rpm"
#yum install -y "https://rpms.scc-services.fr/rawhide/el${ELRELEASE}/x86_64/Packages/4any-release-${ELRELEASE}-0.el${ELRELEASE}.noarch.rpm"
yum install -y scc-artifacts-release 4any-release


# install packages
sed -i -E '/^\[scc-teleport\]$/,/^\[/ s/^enabled *= *0/enabled = 1/' /etc/yum.repos.d/scc-artifacts.repo
test -n "$AIRGAP" && sed -i -E '/^\[scc-epel\]$/,/^\[/ s/^enabled *= *0/enabled = 1/' /etc/yum.repos.d/scc-artifacts-epel.repo
test -z "$AIRGAP" && yum install -y epel-release
yum clean all &> /dev/null
if [ "$ELRELEASE" -lt "8" ]; then
  yum install -y yum-plugin-versionlock
else
  yum install -y python3-dnf-plugin-versionlock
fi

yum install -y chrony jq openssl ca-certificates tar unzip

# enable & start chronyd
systemctl enable chronyd
systemctl restart chronyd
# force chrony time synchronization
chronyc makestep

# configure & start Teleport
/usr/libexec/teleport-setup.sh "$TOKEN"
exit $?
