source ../environment.sh
if [ -z "$NETWORK" ]; then
echo "错误: 未找到 NETWORK 环境变量"
exit 1
fi

 

if [ -z "$KAFKA_PORT" ]; then
echo "错误: 未找到 KAFKA_PORT 环境变量"
exit 1
fi
if [ -z "$KAFKA_NAME" ]; then
echo "错误: 未找到 KAFKA_NAME 环境变量"
exit 1
fi

if [ -z "$KAFKA_IMAGE" ]; then
echo "错误: 未找到 KAFKA_IMAGE 环境变量"
exit 1
fi

if [ -z "$KAFKA_MEMORY" ]; then
echo "错误: 未找到 KAFKA_MEMORY 环境变量"
exit 1
fi


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

echo "开始创建 KAFKA 容器..."
echo ${KAFKA_DOMAIN_FULL}
echo ${KAFKA_NAME}

docker run -d \
--name ${KAFKA_NAME} \
--network $NETWORK \
-p ${KAFKA_PORT}:${KAFKA_PORT} \
-e KAFKA_CFG_NODE_ID=1 \
-e KAFKA_CFG_PROCESS_ROLES=broker,controller \
-e KAFKA_CFG_LISTENERS=PLAINTEXT://:${KAFKA_PORT},CONTROLLER://:29093 \
-e KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://${KAFKA_DOMAIN_FULL} \
-e KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@${KAFKA_NAME}:29093 \
-e KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER \
-e KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT \
-e KAFKA_CFG_INTER_BROKER_LISTENER_NAME=PLAINTEXT \
-e KAFKA_CFG_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \
-e KAFKA_CFG_TRANSACTION_STATE_LOG_REPLICATION_FACTOR=1 \
-e KAFKA_CFG_TRANSACTION_STATE_LOG_MIN_ISR=1 \
-e KAFKA_CFG_GROUP_INITIAL_REBALANCE_DELAY_MS=0 \
-e KAFKA_CFG_NUM_PARTITIONS=3 \
-e KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=true \
-e KAFKA_CFG_LOG_RETENTION_HOURS=168 \
-e KAFKA_CFG_LOG_RETENTION_BYTES=-1 \
-e KAFKA_CFG_DELETE_TOPIC_ENABLE=true \
--env TZ=Asia/Shanghai \
--mount type=bind,source=/etc/localtime,target=/etc/localtime,readonly \
--mount type=volume,source=${KAFKA_DATA},target=/bitnami/kafka \
--memory ${KAFKA_MEMORY} \
--restart unless-stopped \
${KAFKA_IMAGE}

docker ps

# 等待 Kafka 服务就绪
echo "等待 Kafka 服务就绪..."
sleep 30

# 创建所需的 topics
echo "开始创建 Kafka topics..."

# 创建机场相关 topics
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic airport-push-voltage-imitateFly
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic airport-push-voltage-task
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic airport-push-voltage-test
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic business-update-dataPermissions-task

# 创建 DSP 算法相关 topics
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic dsp-alg-online-tasks
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic dsp-alg-offline-tasks
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic dsp-alg-image-tasks
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic dsp-alg-task-results
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic dsp-local

# 创建 DSP 录制相关 topics
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic dsp-recording-task
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic dsp-recording-result
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic dsp-recording-local

# 创建 DSP 推流相关 topics
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic dsp-push-stream-task
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic dsp-push-stream-result
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:${KAFKA_PORT} --topic dsp-push-stream-local

# 列出所有创建的 topics
echo "已创建的 topics 列表："
docker exec ${KAFKA_NAME} /opt/bitnami/kafka/bin/kafka-topics.sh --bootstrap-server 127.0.0.1:${KAFKA_PORT} --list

 