#!/bin/bash
# Info: Script to install/create/delete/update containerd from file settings 
# Author: JesusPerezLorenzo  
# Release: 1.0 
# Date: 12-10-2024

USAGE="install-containerd.sh install | update | remvoe"
[ "$1" == "-h"  ] && echo "$USAGE"  && exit 1

ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')"
OS="$(uname | tr '[:upper:]' '[:lower:]')"

[ -r "env-containerd" ] && . ./env-containerd

CONTAINERD_VERSION="${CONTAINERD_VERSION:-1.7.18}"
CONTAINERD_URL=https://github.com/containerd/containerd/releases/download/v$CONTAINERD_VERSION/containerd-$CONTAINERD_VERSION-$OS-$ARCH.tar.gz

CRICTL_VERSION="${CRICTL_VERSION:-1.28.0}"
CRICTL_URL="https://github.com/kubernetes-sigs/cri-tools/releases/download/"

CONTAINERD_SYSTEMCTL_MODE=enabled

CMD_TSKSRVC=${1:-install}

export LC_CTYPE=C.UTF-8 
export LANG=C.UTF-8

ORG=$(pwd)

_clean_others() {
  [ -d "/etc/cni" ] && sudo rm -r /etc/cni
  [ -d "/var/lib/containers" ] && sudo rm -r /var/lib/containers
  sudo rm -f /etc/systemd/system/podman* 2>/dev/null
}
_init() {
  [ -z "$CONTAINERD_VERSION" ] && exit 1  # || [ -z "$CONTAINERD_ARCH" ] || [ -z "$CONTAINERD_URL" ] || [ -z "$CONTAINERD_FILE" ] && exit 1
  local curr_vers
  local has_containerd
  has_containerd=$(type containerd 2>/dev/null)
  if [ -n "$has_containerd" ]  ; then 
    curr_vers=$(containerd --version | awk '{print $3}' | sed 's/v//g')
  else
    _clean_others
  fi
  if [ "$curr_vers" != "$CONTAINERD_VERSION" ] ; then
    if ! curl -fsSL "$CONTAINERD_URL" -o /tmp/containerd.tar.gz ; then
      echo "error downloading containerd "
      return 1 
    fi
    tar xzf /tmp/containerd.tar.gz 
    if [ -r "bin/containerd" ] ; then
      cd bin || exit 1
      [ -n "$has_containerd" ] && sudo timeout -k 10 20 systemctl stop containerd 
      sudo cp * /usr/local/bin
      cd "$ORG" || exit 1
    else  
      echo "error installing containerd"
      ret=1
    fi
    rm -fr cri-o
    rm -f /tmp/containerd_installer.sh
    [ "$ret" == 1 ] && return 1 
  fi
  curr_vers=$(crictl --version | awk '{print $3}' | sed 's/v//g')
  if [ "$curr_vers" != "$CRICTL_VERSION" ] ; then
    if ! curl -fsSL "${CRICTL_URL}/v${CRICTL_VERSION}/crictl-v${CRICTL_VERSION}-${OS}-${ARCH}.tar.gz" -o /tmp/crictl.tar.gz ; then
      echo "error downloading crictl installer"
      return 1 
    fi
    tar xzf /tmp/crictl.tar.gz 
    if [ -r "crictl" ] ; then
      chmod +x crictl 
      sudo mv crictl /usr/local/bin
    fi
    rm -f /tmp/crictl.tar.gz 
  fi
  return 0 
}

_config_containerd() {
  [ ! -d "/etc/containerd" ] && mkdir -p /etc/containerd 
  if [ -r "config.toml" ] && [ ! -r "/etc/containerd/config.toml" ] ; then
    sudo cp config.toml /etc/containerd/config.toml
  elif [ ! -r "/etc/containerd/config.toml" ] ; then
    sudo containerd config default | sudo tee /etc/containerd/config.toml >/dev/null
  fi
  local youki_path=$(type -p youki 2>/dev/null)
  if [ -n "$youki_path" ] && [ -x "$youki_path" ] ; then
    local has_youki=$(grep youki /etc/containerd/config.toml)
    if [ -z "$has_youki" ] ; then
      echo '[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.youki]' >> /etc/containerd/config.toml
      echo '  runtime_type = "io.containerd.runc.v2"' >> /etc/containerd/config.toml
      echo '   [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.youki.options]' >> /etc/containerd/config.toml
      echo '     BinaryName = "'$youki_path'"' >> /etc/containerd/config.toml
      sed -i 's,SystemdCgroup = true,,' /etc/containerd/config.toml
    fi
  fi
  if [ -r "crictl.yaml" ] && [ ! -r "/etc/containerd-crictl.yaml" ] ; then
    sudo cp crictl.yaml /etc/containerd-crictl.yaml
    sudo cp crictl.yaml /etc/crictl.yaml
  fi
  if [ -r "containerd.service" ] && [ ! -r "/lib/systemd/containerd.service" ] ; then
    sudo cp containerd.service  /lib/systemd/system
    [ ! -L "/etc/systemd/system/containerd.service" ] && sudo ln -s /lib/systemd/system/containerd.service /etc/systemd/system
    sudo timeout -k 10 20 systemctl daemon-reload
  fi
  TARGET=/etc/modules-load.d/containerd.conf
  ITEMS="overlay br_netfilter"
  for it in $ITEMS
  do 
    has_item=$(sudo grep ^"$it" $TARGET 2>/dev/null)
    [ -z "$has_item" ] && echo "$it" | sudo tee -a /etc/modules-load.d/containerd.conf
  done
  _start_containerd
}

_remove_containerd() {
  sudo timeout -k 10 20 systemctl stop containerd 
  sudo timeout -k 10 20 systemctl disable containerd 
}

_start_containerd() {
  if [ "$CONTAINERD_SYSTEMCTL_MODE" == "enabled" ] ; then 
    sudo timeout -k 10 20 systemctl enable containerd 
  else 
    sudo timeout -k 10 20 systemctl disable containerd 
  fi
  sudo timeout -k 10 20 systemctl start containerd
}

_restart_containerd() {
  sudo timeout -k 10 20 systemctl restart containerd
}
[ "$CMD_TSKSRVC" == "remove" ] && _remove_containerd && exit 0
if ! _init ; then
  echo "error containerd install"
  exit 1
fi
[ "$CMD_TSKSRVC"  == "update" ] &&  _restart_containerd && exit 0
if ! _config_containerd ; then 
  echo "error containerd config"
  exit 1
fi
if ! _start_containerd ; then 
  echo "error containerd start"
  exit 1
fi
