#/bin/bash
cid=0
httpcode=000
check_docker(){
	echo "-----------------------------"
   	echo "检查Docker安装环境......"
	echo "-----------------------------"
  	 docker -v
	if [ $? -eq  0 ]; then
		echo "检查到Docker已安装!"
		echo "-----------------------------"
		service docker restart
		sleep  5
	else
		echo "安装docker环境..."
		echo "-----------------------------"
		yum install  docker     -y
		echo "安装docker环境...安装完成!"
		echo "-----------------------------"
                service docker start
		sleep  5
	fi
}
check_up(){
	echo "-----------------------------"
	echo "正在确认服务是否完成启动！"
	while  [ $httpcode != 200 ]
	do
		httpcode=`curl -I -m 10 -o /dev/null -s -w %{http_code}  http://localhost:8182/longvideoapi/test`
       		echo  -en  ". "
       		sleep 2
   	 done
	echo  " "
	echo   “请求返回值：  $httpcode  服务器启动完成”
	echo "-----------------------------"  



}



stop(){
	echo "-----------------------------"
	echo "检查video-measure-queue-test容器是否存在"
	echo "-----------------------------"
        echo "停止并删除容器"
        echo "-----------------------------"
        docker  stop  $(docker ps -a|grep  "video-measure-queue-test:latest"|grep -v grep|awk  '{print $1}')
        docker  rm  -f $(docker ps -a|grep  "video-measure-queue-test:latest"|grep -v grep|awk  '{print $1}')
        docker  rmi  registry-internal.cn-hangzhou.aliyuncs.com/stuuudy/video-measure-queue-test:latest

      
}

start(){
	check_docker
	cid=`docker ps |grep  "video-measure-queue-test:latest"|grep -v grep|awk  '{print $1}'`
	if [ $cid  ];then
		echo "video-measure-queue-test already started ！"
		echo "-----------------------------"
	else

		docker login --username=stuuudys --password=Qingqu@2019   registry-internal.cn-hangzhou.aliyuncs.com
		docker run --cap-add=SYS_PTRACE  -d -it  --network host  --restart=always  -v /datalog/weblog/java/stuuudy.com/measurevideoqueue/:/datalog/weblog/java/stuuudy.com/measurevideoqueue/ registry-internal.cn-hangzhou.aliyuncs.com/stuuudy/video-measure-queue-test:latest
	
	fi

}



  case "$1" in
   'start')
      start
      ;;
   'stop')
     stop
     ;;
   'restart')
     stop
     start
     ;;
   'rollback')
     rollback
     ;;
   'deploy')
     deploy
     ;;
   'status')
     check_up
     ;;
   'info')
     info
     ;;
  *)
     echo "Usage: $0 {start|stop|restart|rollback|deploy|status|info}"
     exit 1
;;
esac
