#!/bin/bash
# Date:2022-09-05
# docker安装后优化及配置

dockerinstall(){
    cd "${event_path}"/soft/k8s"${kversion}"
    sleep 5
    rpm -ivh "${event_path}"/soft/docker-ce-20.10.9-1.ky10.aarch64.rpm --force    
    mkdir -p /etc/docker
    systemctl daemon-reload
    systemctl enable docker.service
    systemctl start docker

    modprobe overlay
    modprobe br_netfilter

    echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
    echo '1' > /proc/sys/net/bridge/bridge-nf-call-ip6tables
    echo '1' > /proc/sys/net/ipv4/ip_forward

    cat <<EOF > /etc/docker/daemon.json
{
  "bip": "${DOCKER0_NETWORK}",
  "default-address-pools": [{"base": "${DOCKER_GWBRIDGE_NETWORK}", "size":24}],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "max-concurrent-downloads": 25,
  "log-driver": "json-file",
  "log-opts": {
  "max-size": "5m",
  "max-file":"10"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
  "overlay2.override_kernel_check=true"
  ],
  "registry-mirrors": ["http://bc437cce.m.daocloud.io"],
  "insecure-registries": [
    "${REGISTRY}",
    "http://easzlab.io.local:5000",
    "192.168.251.78",
    "registry.edoc2.com:5000"
  ],
  "data-root": "${DOCKER_DATA_ROOT}"
}
EOF

    #\cp "${event_path}"/Install_Packages/config.toml /etc/containerd/config.toml
    #\cp "${event_path}"/Install_Packages/config.toml /run/docker/containerd/containerd.toml

    systemctl daemon-reload && systemctl enable --now docker
    systemctl restart docker
    systemctl status docker | grep ""
}

if [ "${TYPE}" == "allinone" ];then
    event_path=$1
    kversion=$2
    systemctl stop docker &
    sleep 15
    rm -rf /etc/systemd/system/docker.service &
    sleep 5
    yum remove docker-ce -y &
    sleep 15
fi
if [ "${TYPE}" == "cluster" ];then
    event_path="/tmp"
    kversion=$KVERSION
    system=$3
    if [ "${system}" == "KyCentos" ];then
        mkdir -p /opt/rpm && \cp -rf /usr/local/docker* /opt/rpm/
        systemctl stop docker
        sleep 15
        yum remove docker-ce -y
        sleep 3
        rpm -ivh "${event_path}"/soft/docker-ce-20.10.9-1.ky10.aarch64.rpm --force
        systemctl start docker
    fi
fi
dockerinstall
