FROM docker.elastic.co/elasticsearch/elasticsearch:8.13.4

# 切换到 root 用户进行系统级安装
USER root

# 修改elasticsearch用户的UID和GID
RUN usermod -u 2133 elasticsearch && \
    groupmod -g 2015 elasticsearch && \
    chown -R elasticsearch:elasticsearch /usr/share/elasticsearch

# 设置构建参数
ARG USE_INTRANET=false

# 配置内网 apt 源 (如果指定了内网选项)
RUN if [ "$USE_INTRANET" = "true" ]; then \
        echo "Configuring intranet apt sources..." && \
        cp /etc/apt/sources.list /etc/apt/sources.list.bak && \
        echo "deb [trusted=yes] http://10.68.64.1/ubuntu2204/ jammy main" > /etc/apt/sources.list && \
        echo 'Acquire::https::Verify-Peer "false";' > /etc/apt/apt.conf.d/99disable-ssl-check && \
        echo 'Acquire::https::Verify-Host "false";' >> /etc/apt/apt.conf.d/99disable-ssl-check; \
    fi

# 安装 supervisor, net-tools, vim
RUN apt-get update && \
    apt-get install -y supervisor net-tools inetutils-ping vim && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# 配置部署时使用的apt源
RUN if [ "$USE_INTRANET" = "true" ]; then \
	echo "deb [trusted=yes] https://10.92.132.52/mirrors/ubuntu2204/ jammy main" > /etc/apt/sources.list; \
    fi

# 创建 supervisor 日志目录
RUN mkdir -p /var/log/supervisor


# 复制 supervisor 配置文件
COPY src/log/elasticsearch/build/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# 复制启动脚本
COPY src/log/elasticsearch/build/start-es-supervised.sh /usr/local/bin/start-es-supervised.sh
RUN chmod +x /usr/local/bin/start-es-supervised.sh

# 复制DNS监控脚本
COPY src/log/elasticsearch/build/dns-monitor.sh /usr/local/bin/dns-monitor.sh
RUN chmod +x /usr/local/bin/dns-monitor.sh

# 保持 root 用户，由 supervisor 管理用户切换
USER root

# 暴露端口
EXPOSE 9200 9300

# 使用 supervisor 作为入口点
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
