#!/bin/sh

# 配置文件根目录，固定是zd
DOCKERHOME=/zd

# 镜像名称前缀、标签
BASE_IMAGE_NAME=zd.harbor.com/zd
BASE_IMAGE_TAG=latest

# 各服务的镜像名称
ZD-AUTH=$BASE_IMAGE_NAME/zd-auth:$BASE_IMAGE_TAG
ZD-SYSTEM=$BASE_IMAGE_NAME/zd-modules-system:$BASE_IMAGE_TAG
ZD-GATEWAY=$BASE_IMAGE_NAME/zd-gateway:$BASE_IMAGE_TAG
ZD-FORWARD=$BASE_IMAGE_NAME/zd-forward:$BASE_IMAGE_TAG
ZD-AIRBOTTLE=$BASE_IMAGE_NAME/zd-airbottle:$BASE_IMAGE_TAG
ZD-ALARM=$BASE_IMAGE_NAME/zd-alarm:$BASE_IMAGE_TAG
ZD-CAMERA=$BASE_IMAGE_NAME/zd-camera:$BASE_IMAGE_TAG
ZD-CHEMICAL=$BASE_IMAGE_NAME/zd-chemical:$BASE_IMAGE_TAG
ZD-FACE=$BASE_IMAGE_NAME/zd-common-face:$BASE_IMAGE_TAG
ZD-EXAM=$BASE_IMAGE_NAME/zd-exam:$BASE_IMAGE_TAG
ZD-MESSAGE=$BASE_IMAGE_NAME/zd-message:$BASE_IMAGE_TAG
ZD-APP=$BASE_IMAGE_NAME/zd-modules-app:$BASE_IMAGE_TAG
ZD-FILE=$BASE_IMAGE_NAME/zd-modules-file:$BASE_IMAGE_TAG
ZD-GEN=$BASE_IMAGE_NAME/zd-modules-gen:$BASE_IMAGE_TAG
ZD-JOB=$BASE_IMAGE_NAME/zd-modules-job:$BASE_IMAGE_TAG
ZD-LABORATORY=$BASE_IMAGE_NAME/zd-modules-laboratory:$BASE_IMAGE_TAG
ZD-NETTY=$BASE_IMAGE_NAME/zd-netty:$BASE_IMAGE_TAG
ZD-SPEAKER=$BASE_IMAGE_NAME/zd-speaker:$BASE_IMAGE_TAG
ZD-MONITOR=$BASE_IMAGE_NAME/zd-monitor:$BASE_IMAGE_TAG


case "$1" in

    # 删除容器
    removeAll)
        echo "* 正在删除容器..."
        # shellcheck disable=SC2046
        time docker rm $(docker ps -aq) -f
        echo "* 删除容器成功..."
        ;;
    # 拉取镜像
    pull)
        echo "* 正在拉取后端镜像..."
        time docker pull "$ZD-AUTH"
        time docker pull "$ZD-SYSTEM"
        time docker pull "$ZD-GATEWAY"
        time docker pull "$ZD-FORWARD"
        time docker pull "$ZD-AIRBOTTLE"
        time docker pull "$ZD-ALARM"
        time docker pull "$ZD-CAMERA"
        time docker pull "$ZD-CHEMICAL"
        time docker pull "$ZD-FACE"
        time docker pull "$ZD-EXAM"
        time docker pull "$ZD-MESSAGE"
        time docker pull "$ZD-APP"
        time docker pull "$ZD-FILE"
        time docker pull "$ZD-GEN"
        time docker pull "$ZD-JOB"
        time docker pull "$ZD-LABORATORY"
        time docker pull "$ZD-NETTY"
        time docker pull "$ZD-SPEAKER"
        time docker pull "$ZD-MONITOR"
        echo "* 开始拉取前端镜像..."
        time docker pull "$WEB_SERVICE"
        echo "* 拉取镜像成功..."
        ;;
    # 运行镜像
    run)
        echo "* 开始运行后端服务镜像..."
        time docker-compose -f $DOCKERHOME/docker-compose-services.yml up -d
        echo "* 等待10s..."
        sleep 10
        echo "* 开始运行前端服务镜像..."
#        time docker-compose -f $DOCKERHOME/docker-compose-nginx.yml up -d
        echo "* 运行成功..."
        ;;
    # 拉取镜像并运行
    pullrun)
        echo "* 正在拉取后端镜像..."
        time docker pull "$ZD-AUTH"
        time docker pull "$ZD-SYSTEM"
        time docker pull "$ZD-GATEWAY"
        time docker pull "$ZD-FORWARD"
        time docker pull "$ZD-AIRBOTTLE"
        time docker pull "$ZD-ALARM"
        time docker pull "$ZD-CAMERA"
        time docker pull "$ZD-CHEMICAL"
        time docker pull "$ZD-FACE"
        time docker pull "$ZD-EXAM"
        time docker pull "$ZD-MESSAGE"
        time docker pull "$ZD-APP"
        time docker pull "$ZD-FILE"
        time docker pull "$ZD-GEN"
        time docker pull "$ZD-JOB"
        time docker pull "$ZD-LABORATORY"
        time docker pull "$ZD-NETTY"
        time docker pull "$ZD-SPEAKER"
        time docker pull "$ZD-MONITOR"
        echo "* 开始拉取前端镜像..."
        time docker pull "$WEB_SERVICE"
        echo "* 拉取镜像成功..."

        echo "* 开始运行后端服务镜像..."
        time docker-compose -f docker-compose-services.yml up -d
        echo "* 等待10s..."
        sleep 10
        echo "* 开始运行前端服务镜像..."
#        time docker-compose -f $DOCKERHOME/docker-compose-nginx.yml up -d
        echo "* 运行成功..."
        ;;
    # 停止容器
    stop)
        echo "* 正在停止容器..."
#        time docker-compose -f $DOCKERHOME/docker-compose-nginx.yml stop
        time docker-compose -f $DOCKERHOME/docker-compose-services.yml stop
        echo "* 停止容器成功..."
        ;;
    # 重启容器
    restart)
        echo "* 正在停止镜像..."
#        time docker-compose -f $DOCKERHOME/docker-compose-nginx.yml restart
        time docker-compose -f $DOCKERHOME/docker-compose-services.yml restart
        ;;
    # 其它
    *)
        echo "* ..."
        ;;
esac
exit 0