#!/bin/bash

## docker 版本 19.03.12
set -eu

DOCKERFILE='https://packages.zmops.cn/docker/docker-19.03.12.tgz'
DOCKERCOMPOSE='https://packages.zmops.cn/docker/docker-compose.1.28'
DOCKERDIR=/opt/argus
DOCKERHOME=${DOCKERDIR}/docker

# 初始化配置
[[ ! -d /opt/argus ]] && mkdir /opt/argus

# 下载并解压docker
cd ${DOCKERDIR} && sudo curl ${DOCKERFILE} | tar -xz
mkdir ${DOCKERHOME}/data

# 配置docker启动文件
cat << EOF > /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.io

[Service]
Environment="PATH=/opt/argus/docker:/bin:/sbin:/usr/bin:/usr/sbin"
EnvironmentFile=-/run/flannel/docker
ExecStart=/opt/argus/docker/dockerd \\
         --graph /opt/argus/docker/data \\
         --bip 18.0.0.1/24 \\
         --log-level=error \\
          \$DOCKER_NETWORK_OPTIONS
ExecReload=/bin/kill -s HUP \$MAINPID
Restart=on-failure
RestartSec=5
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Delegate=yes
KillMode=process

[Install]
WantedBy=multi-user.target
EOF

# 启动配置
systemctl daemon-reload
systemctl enable docker
# 添加环境变量
cat << EOF > /etc/profile.d/argus_env.sh
#!/bin/bash
export DOCKER_HOME=/opt/argus/docker
export PATH=\$PATH:\$DOCKER_HOME
EOF

# Reload System Variables
source /etc/profile.d/argus_env.sh

# 安装docker-compose
curl ${DOCKERCOMPOSE} -o /bin/docker-compose
chmod a+x /bin/docker-compose

# Check Java
#java_version=$(java -version 2>&1 |awk 'NR==1{gsub(/"/,"");print $3}')

#[[ ${java_version} == "1.8.0_242" ]] && echo "Java 安装完成，版本为${java_version}" || echo "java安装失败请检查"
