#!/usr/bin/env bash
# 文件名: docker-env.sh
# https://yq.aliyun.com/articles/110806

# 2020-12-25 更新 docker-compose 版本为1.21.2

export repos=https://z16b1cyc.mirror.aliyuncs.com
export bip_addr=192.168.254.1/24
export docker_compose_ver='1.28.5'
export docker_compose_url=https://mirrors.aliyun.com/docker-toolbox/linux/compose/${docker_compose_ver}/docker-compose-`uname -s`-`uname -m`


function centos_install(){
    
    yum install -y yum-utils device-mapper-persistent-data lvm2
    #yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
	yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    # curl -skL https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo \
                                           # -o /etc/yum.repos.d/docker-ce.repo
    yum update -y && yum install docker-ce -y
}

function debian_install(){
    
    # Jessie or Stretch
    sudo apt-get update -y
    sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common -y
    curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/debian/gpg | sudo apt-key add -
    sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/debian $(lsb_release -cs) stable"
    
    sudo apt-get update -y
    sudo apt-get install -y docker-ce docker-ce-cli containerd.io
}

function ubuntu_install(){
    
    sudo apt-get update -y
    sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
    curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
    sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
    
    sudo apt-get update -y
    sudo apt-get install docker-ce docker-ce-cli containerd.io
}

function common_config(){
	modprobe br_netfilter
		
	#echo "DefaultLimitNOFILE=1024000"  >> /etc/systemd/system.conf
	
    mkdir -p /etc/docker
    cat >/etc/docker/daemon.json<<EOF
{
  "registry-mirrors": ["${repos}"],
  "bip": "$bip_addr",
  "log-driver": "json-file",
  "max-concurrent-uploads": 5,
  "max-concurrent-downloads": 10,
  "log-opts": {
    "max-size": "100m",
    "max-file": "10",
    "labels": "production_status",
    "env": "os,customer"
  },
  "exec-opts": ["native.cgroupdriver=systemd"],
  "storage-driver": "overlay2",
  "storage-opts": ["overlay2.override_kernel_check=true"]
}
EOF
    systemctl daemon-reload
    systemctl restart docker
    systemctl enable docker

    curl -skL ${docker_compose_url} -o /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
	#curl -L https://raw.githubusercontent.com/docker/compose/1.19.0/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
}

if [ -s /etc/redhat-release ]; then
    . /etc/os-release

    # Includes Fedora, CentOS 7, RHEL 7
    if echo "$ID $VERSION_ID" | egrep -q 'rhel 7|centos 7'; then
        centos_install
        common_config
    fi

elif [ $(lsb_release -is) = "Debian" ]; then
        debian_install
        common_config
elif [ $(lsb_release -is) = "Ubuntu" ]; then
        ubuntu_install
        common_config
else
    echo "Error: unknown system type."
    exit 1
fi

