#!/usr/bin/env bash
# COPYRIGHT:   (c) Awasu Pty. Ltd. 2024 (all rights reserved).
#              Unauthorized use of this code is prohibited.
#
# LICENSE:     This software is provided 'as-is', without any express
#              or implied warranty.
#
#              In no event will the author be held liable for any damages
#              arising from the use of this software.
#
#              Permission is granted to anyone to use this software
#              for any purpose, and to alter it and redistribute it freely,
#              subject to the following restrictions:
#
#              - The origin of this software must not be misrepresented;
#                you must not claim that you wrote the original software.
#                If you use this software, an acknowledgement is requested
#                but not required.
#
#              - Altered source versions must be plainly marked as such,
#                and must not be misrepresented as being the original software.
#                Altered source is encouraged to be submitted back to
#                the original author so it can be shared with the community.
#                Please share your changes.
#
#              - This notice may not be removed or altered from any
#                source distribution.

# initialize
set -euo pipefail
PROVISIONING=/var/mnt/data/provisioning/k3s-v1.29.2+k3s1/
DOCKER_DATA=/mnt/data/docker-data/
K3S_DATA=/mnt/data/k3s-data/

# install SELinux support
#   -C = --cacheonly
rpm-ostree --apply-live -C -y install "$PROVISIONING/k3s-selinux-1.5-1.coreos.noarch.rpm"

# prepare the K3s install
cp "$PROVISIONING/k3s" /usr/local/bin/
chmod +x /usr/local/bin/k3s
mkdir -p /var/lib/rancher/k3s/agent/images/
cp "$PROVISIONING/k3s-airgap-images-amd64.tar.gz" /var/lib/rancher/k3s/agent/images/

# run the K3s installer
export K3S_KUBECONFIG_MODE="644"
export INSTALL_K3S_EXEC="--docker"
export INSTALL_K3S_SKIP_DOWNLOAD=true
"$PROVISIONING/install.sh"

# stop K3s
systemctl stop k3s
/usr/local/bin/k3s-killall.sh

# move the K3s data files
function move_k3s_data {
    local src=$1
    dest=$( echo "$src" | tr '/' '-' )
    dest="$K3S_DATA/${dest:1}"
    if [ -d "$dest" ]; then
        echo "Already moved: $dest"
        rm -rf "$src"
        # nb: so that we can create the symlink
        mkdir -p "$(dirname "$src")"
    elif [ -d "$src" ]; then
        echo "Moving k3s data: $src -> $dest"
        mv "$src" "$dest"
    else
        echo "k3s data not present: $src"
        mkdir "$dest"
        # nb: so that we can create the symlink
        mkdir -p "$(dirname "$src")"
    fi
    ln -s "$dest" "$src"
}
mkdir -p "$K3S_DATA"
move_k3s_data /var/lib/rancher
move_k3s_data /var/lib/kubelet/pods
move_k3s_data /run/k3s

# update the SELinux security contexts
dname="$DOCKER_DATA/overlay2"
mkdir -p "$dname"
chcon --type container_share_t --recursive "$dname"
dname="$K3S_DATA/var-lib-rancher/k3s/storage"
mkdir -p "$dname"
chcon --type container_file_t --recursive "$dname"

# FUDGE! Try to work-around Docker not working with its new data directory the first time it's run.
systemctl restart docker

# start K3s
systemctl start k3s

# everything worked - we can now disable the install service
systemctl disable install-k3s
