#!/bin/bash

set -e

function parentDir()
{
    local this_dir=`pwd`
    local child_dir="$1"
    dirname "$child_dir"
    cd $this_dir
}

CURRENT_PATH=$(cd `dirname $0`; pwd)

DOCKER_IMAGE_NAME='syria/java-api:latest'

DOCKER_CONTAINER_NAME='syria-java-api'

DOCKER_CONTAINER_PORT=40010

workSpace=`parentDir "$CURRENT_PATH"`

echo '更新代码'
git pull

echo '工作空间'$workSpace
cd $workSpace

echo '开始打包'
mvn clean install -Dmaven.test.skip=true -Pdev

echo '开始构建docker镜像'
docker build -t $DOCKER_IMAGE_NAME .

count=`docker ps -a|grep $DOCKER_CONTAINER_NAME|wc -l`
if [ $count != 0 ];then
    echo "准备移除容器 $DOCKER_CONTAINER_NAME ..."
    docker rm -f $DOCKER_CONTAINER_NAME
else
    echo "准备启动容器 $DOCKER_CONTAINER_NAME ..."
fi

docker run -d --restart always --net=host --add-host redis:127.0.0.1 --name $DOCKER_CONTAINER_NAME -v /var/log:/var/log -v /data/medical-follow:/data -v /etc/syria.conf:/etc/java/application.conf -it $DOCKER_IMAGE_NAME

docker images|grep -v REPOSITORY|grep none|awk '{print $3}'|xargs docker rmi

exit 0
