#!/usr/bin/env bash

set -e

TEMP_PATH=$(mktemp -d)
NAME=containerd
VERSION="1.7.28"
IMG_HUB=${1:-hub.gitee.local}
DATA_ROOT=${2:-/data/containerd}

txtbld=$(tput bold)             # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldgre=${txtbld}$(tput setaf 2) # green
bldylw=${txtbld}$(tput setaf 3) # yellow
txtrst=$(tput sgr0)             # Reset
err=${bldred}ERROR${txtrst}
info=${bldgre}INFO${txtrst}
warn=${bldylw}WARNING${txtrst}

# 判断环境平台
if [ -z "${ARCH}" ]; then
  case $(uname -m) in
    x86_64) ARCH=amd64 ;;
    aarch64) ARCH=arm64 ;;
    *) printf "$err: 不支持该平台 $(uname -m)"; exit 1 ;;
  esac
fi

# 判断服务是否正常运行
if [[ "$(systemctl is-enabled ${NAME}.service)" != "enabled" ]]
then
    sed -n -e '1,/^exit 0$/!p' $0 > ${TEMP_PATH}/${NAME}-${VERSION}.tar.gz 2>/dev/null

    printf "$info: [ ${bldgre}${NAME}-${VERSION}${txtrst} ] 安装中...\n"

    cd ${TEMP_PATH}
    tar axvf ${NAME}-${VERSION}.tar.gz >/dev/null
    sleep 2 >/dev/null 2>/dev/null
    tar axvf ${NAME}-${VERSION}-${ARCH}.tar.gz -C /usr/local/bin >/dev/null

    cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
    modprobe overlay >/dev/null 2>/dev/null
    modprobe br_netfilter >/dev/null 2>/dev/null

    cat >/etc/systemd/system/containerd.service<<EOF
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target

[Service]
ExecStartPre=-/sbin/modprobe overlay bridge br_netfilter 
ExecStart=/usr/local/bin/containerd
Restart=always
RestartSec=5
Delegate=yes
KillMode=process
OOMScoreAdjust=-999
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity

[Install]
WantedBy=multi-user.target

EOF
    cat >/etc/sysctl.d/99-containerd.conf <<EOF
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
    sysctl --system >/dev/null 2>/dev/null

    mkdir -p /etc/containerd
    /usr/local/bin/containerd config default | sed -e "s@sandbox_image = .*@sandbox_image = \"${IMG_HUB}/base-platform/pause:3.9\"@" \
                                                   -e "s@config_path =.*@config_path = \"/etc/containerd/certs.d\"@" \
                                                   -e "s@SystemdCgroup = .*@SystemdCgroup = true@" \
                                                   -e "s@^root = .*@root = \"${DATA_ROOT}\"@" > /etc/containerd/config.toml

    mkdir -p /etc/containerd/certs.d/${IMG_HUB##%/}
    cat >/etc/containerd/certs.d/${IMG_HUB}/hosts.toml <<EOF
server = "http://${IMG_HUB}"

[host."http://${IMG_HUB}"]
  capabilities = ["pull", "resolve", "push"]
EOF

    cat > /etc/crictl.yaml <<EOF
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
EOF
    systemctl daemon-reload >/dev/null 2>/dev/null
    systemctl enable ${NAME}.service --now >/dev/null 2>/dev/null
else
    printf "$warn: [ ${bldgre}${NAME}-${VERSION}${txtrst} ] 已安装，正常退出~\n"
fi

rm -rf ${TEMP_PATH}
exit 0
