#安装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 -p /data/app/${APP_NAME}
mkdir -p /data/app/${APP_NAME}/config
cd /data/app/${APP_NAME}

# 设置私有仓库信息
repo_url="http://admin_lsl:Lsl18718383742@124.222.247.184:3000/wteamYD/promotesai-background.git"
repo_branch="develop"
# 克隆私有仓库代码

# 删除目标路径（如果存在）
if [ -d "promotesai-background" ]; then
    #  更新代码
    echo "更新代码"
    cd promotesai-background
    git pull origin ${repo_branch}

else
    echo "拉取代码"
    git clone ${repo_url}
    cd promotesai-background
    git checkout -b ${repo_branch}
    git pull origin ${repo_branch}
    echo "Code successfully pulled from the private repository."
fi



# Maven打包
echo 开始打包
mvn clean package

#echo 拷贝文件
#cp -r ./config /data/app/${APP_NAME}

# 定义应用组名：项目名
group_name="promotesai"
# 定义应用名称：客户端
app_client_name="client-api"
# 定义应用名称：管理端
app_manager_name="manager-api"
# 定义应用版本：最新版
app_version='latest'
# 定义应用环境
profile_active='prod'
echo '----stop container----'
docker stop ${app_client_name}
docker stop ${app_manager_name}
echo '----rm container----'
docker rm ${app_client_name}
docker rm ${app_manager_name}
echo '----rm image----'
docker rmi ${group_name}/${app_client_name}:${app_version}
docker rmi ${group_name}/${app_manager_name}:${app_version}

# 构建客户端容器运行
cd ${app_client_name}
docker build -t ${group_name}/${app_client_name}:${app_version} --build-arg JAR_FILE=./target/client-api-0.0.1-SNAPSHOT.jar .
echo '----build image----'
docker run -p 8889:8889 --name ${app_client_name} \
  -e TZ="Asia/Shanghai" \
  -v /etc/localtime:/etc/localtime \
  -d ${group_name}/${app_client_name}:${app_version}
echo '----start container----'

# 构建管理端容器运行
cd ../${app_manager_name}
docker build -t ${group_name}/${app_manager_name}:${app_version} --build-arg JAR_FILE=./target/manager-api-0.0.1-SNAPSHOT.jar .
echo '----build image----'
docker run -p 8887:8887 --name ${app_manager_name} \
  -e TZ="Asia/Shanghai" \
  -v /etc/localtime:/etc/localtime \
  -d ${group_name}/${app_manager_name}:${app_version}
echo '----start container----'












