#安装maven
if ! command -v mvn >/dev/null 2>&1; then
    echo "Installing Maven"
    # 下载Maven
    wget https://archive.apache.org/dist/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz
    # 解压Maven
    tar -xzf apache-maven-3.8.1-bin.tar.gz -C /opt
    # 设置Maven环境变量
    echo 'export PATH=/opt/apache-maven-3.8.1/bin:$PATH' >> ~/.bashrc
    # 更改Maven镜像
    mkdir -p ~/.m2
    echo -e "<settings>\n  <mirrors>\n    <mirror>\n      <id>aliyun-maven</id>\n      <mirrorOf>*</mirrorOf>\n      <name>Aliyun Maven</name>\n      <url>https://maven.aliyun.com/repository/public</url>\n    </mirror>\n  </mirrors>\n</settings>" > ~/.m2/settings.xml
    source ~/.bashrc
else
    echo "Maven is already installed"
fi

#安装git
sudo yum install -y git
  #安装依赖
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
  #设置源
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum makecache fast

# 检查Docker是否已安装
if ! command -v docker &> /dev/null
then
    # 如果Docker未安装，执行安装步骤
    sudo yum install -y docker-ce
    sudo systemctl start docker
fi

# 检查Docker Compose是否已安装
if ! command -v docker-compose &> /dev/null
then
    # 如果Docker Compose未安装，执行安装步骤
    sudo curl -L "https://github.com/docker/compose/releases/download/v2.17.1/docker-compose-linux-x86_64" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
fi

# 显示版本号
docker-compose -v



#拉取代码打包
APP_NAME="promotesai"
mkdir /data/app/${APP_NAME}/${APP_NAME}
mkdir /data/app/${APP_NAME}/config
cd /data/app/${APP_NAME}

git init
# 从私有仓库拉取代码
git clone http://124.222.247.184:3000/admin_lsl/patent.git
git pull

cd ${APP_NAME}
# Maven打包
echo 开始打包
mvn clean package


# 定义应用组名
group_name="$1"
# 定义应用名称
app_name="$1"
# 定义应用版本
app_version='latest'
# 定义应用环境
profile_active='prod'
echo '----copy jar----'
docker stop ${app_name}
echo '----stop container----'
docker rm ${app_name}
echo '----rm container----'
docker rmi ${group_name}/${app_name}:${app_version}
echo '----rm image----'
cd ${app_name}
echo "当前目录是：$PWD"
docker build -t ${group_name}/${app_name}:${app_version} --build-arg JAR_FILE=./target/client-api-0.0.1-SNAPSHOT.jar .
echo '----build image----'
docker run -p $2:8881 --name ${app_name} \
  -e TZ="Asia/Shanghai" \
  -v /etc/localtime:/etc/localtime \
  -v /data/app/patent/config:/config \
  -d ${group_name}/${app_name}:${app_version}
echo '----start container----'

echo "拉取nginx镜像"
docker pull nginx:latest
echo "运行nginx容器"
docker run --name nginx -p 80:80 -d nginx
if [ ! -f /opt/docker/nginx/conf/nginx.conf ]; then
    echo "创建文件目录"
    mkdir -p /opt/docker/nginx/conf.d
    mkdir -p /opt/docker/nginx/html
    mkdir -p /opt/docker/nginx/logs
    mkdir -p /opt/docker/nginx/conf/nginx.conf
    echo "拷贝nginx容器对应的文件默认配置"
    docker cp nginx:/etc/nginx/nginx.conf /opt/docker/nginx/conf/nginx.conf
    docker cp nginx:/etc/nginx/conf.d /opt/docker/nginx/conf.d
    docker cp nginx:/usr/share/nginx/html /opt/docker/nginx
fi
echo "停止并移除容器"
docker stop nginx
docker rm nginx


# 设置变量
NGINX_CONF_DIR="/etc/nginx/conf.d"
NGINX_CONF_FILE="example.conf"
SERVER_NAME="example.com"
UPSTREAM_NAME="app_server"
UPSTREAM_SERVER="127.0.0.1:8000"

# 创建配置文件
echo "upstream ${UPSTREAM_NAME} {" > ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
echo "    server ${UPSTREAM_SERVER};" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
echo "}" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
echo "server {" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
echo "    listen 80;" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
echo "    server_name ${SERVER_NAME};" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
echo "    location / {" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
echo "        proxy_pass http://${UPSTREAM_NAME};" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
echo "        proxy_set_header Host \$host;" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
echo "        proxy_set_header X-Real-IP \$remote_addr;" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
echo "    }" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
echo "}" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}


echo "重新启动nginx镜像重新新容器"
docker run  -p 80:80 --name nginx --restart=always
-v /opt/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/docker/nginx/conf.d:/etc/nginx/conf.d \
-v /opt/docker/nginx/html:/usr/share/nginx/html \
-v /opt/docker/nginx/logs:/var/log/nginx \
-d  nginx






