source ../environment.sh

if [ -z "$NETWORK" ]; then
echo "错误: 未找到 NETWORK 环境变量"
exit 1
fi
if [ -z "$SKYWALKING_OAP_NAME" ]; then
echo "错误: 未找到 SKYWALKING_OAP_NAME 环境变量"
exit 1
fi
if [ -z "$SKYWALKING_UI_NAME" ]; then
echo "错误: 未找到 SKYWALKING_UI_NAME 环境变量"
exit 1
fi
if [ -z "$SKYWALKING_OAP_IMAGE" ]; then
echo "错误: 未找到 SKYWALKING_OAP_IMAGE 环境变量"
exit 1
fi
if [ -z "$SKYWALKING_UI_IMAGE" ]; then
echo "错误: 未找到 SKYWALKING_UI_IMAGE 环境变量"
exit 1
fi
if [ -z "$SKYWALKING_OAP_MEMORY" ]; then
echo "错误: 未找到 SKYWALKING_OAP_MEMORY 环境变量"
exit 1
fi
if [ -z "$SKYWALKING_UI_MEMORY" ]; then
echo "错误: 未找到 SKYWALKING_UI_MEMORY 环境变量"
exit 1
fi
if [ -z "$SKYWALKING_UI_PORT" ]; then
echo "错误: 未找到 SKYWALKING_UI_PORT 环境变量"
exit 1
fi

# 停止并删除已存在的容器
if docker ps -a | grep -q ${SKYWALKING_OAP_NAME}; then
    echo "停止并删除已存在的 SkyWalking OAP 容器..."
    docker stop ${SKYWALKING_OAP_NAME} >/dev/null 2>&1
    docker rm ${SKYWALKING_OAP_NAME} >/dev/null 2>&1
fi

 


# 启动SkyWalking OAP容器
echo "启动 SkyWalking OAP..."
docker run -d \
    --name ${SKYWALKING_OAP_NAME} \
    --network $NETWORK \
    -p 12800:11800 \
    --memory $SKYWALKING_OAP_MEMORY \
    --restart unless-stopped \
    -e TZ=Asia/Shanghai \
    -e SW_STORAGE=elasticsearch7 \
    -e SW_STORAGE_ES_CLUSTER_NODES=${ES_NAME}:9200 \
    -e SW_CORE_RECORD_DATA_TTL=1 \
    -e SW_CORE_METRICS_DATA_TTL=1 \
    --mount type=bind,source=/etc/localtime,target=/etc/localtime,readonly \
    $SKYWALKING_OAP_IMAGE

# 等待OAP启动
   # 确认 OAP 完全启动后再启动 UI
