diff --git a/build/build_images.sh b/build/build_images.sh index efff851..603dc66 100755 --- a/build/build_images.sh +++ b/build/build_images.sh @@ -122,14 +122,22 @@ build_image() { local image_name=$1 local dockerfile_path=$2 local tag=$3 + local context="." shift 3 + + if [[ $# -gt 0 ]]; then + context=$1 + shift + fi + local extra_args=("$@") echo "🔄 Building $image_name image..." echo " Dockerfile: $dockerfile_path" echo " Tag: $tag" + echo " Context: $context" - if docker build "${build_args[@]}" "${extra_args[@]}" -f "$dockerfile_path" -t "$tag" .; then + if docker build "${build_args[@]}" "${extra_args[@]}" -f "$dockerfile_path" -t "$tag" "$context"; then echo "✅ $image_name image built successfully" return 0 else @@ -138,6 +146,28 @@ build_image() { fi } +pull_base_image() { + local image_ref=$1 + local attempts=${2:-3} + local delay=${3:-5} + + for ((i=1; i<=attempts; i++)); do + echo " Pulling base image ($i/$attempts): $image_ref" + if docker pull "$image_ref" >/dev/null; then + echo " Base image ready: $image_ref" + return 0 + fi + echo " Pull failed: $image_ref" + if (( i < attempts )); then + echo " Retrying in ${delay}s..." + sleep "$delay" + fi + done + + echo "❌ Unable to pull base image after ${attempts} attempts: $image_ref" + return 1 +} + images_built=() build_failed=false @@ -194,28 +224,34 @@ fi if [[ "$build_metric" == true ]]; then echo "" echo "Building Metric module images..." - metric_root="$root/src/metric" - pushd "$metric_root/tests" >/dev/null - - # 检查docker-compose.yml是否存在 - if [[ ! -f "docker-compose.yml" ]]; then - echo "docker-compose.yml not found in $metric_root/tests" >&2 - build_failed=true - else - # 构建metric镜像 - if docker compose build --no-cache; then - images_built+=("argus-metric-ftp:latest") - images_built+=("argus-metric-prometheus:latest") - images_built+=("argus-metric-grafana:latest") - images_built+=("argus-metric-test-node:latest") - images_built+=("argus-metric-test-gpu-node:latest") - else - echo "Failed to build metric module images" + + metric_base_images=( + "ubuntu:22.04" + "ubuntu/prometheus:3-24.04_stable" + "grafana/grafana:11.1.0" + ) + + for base_image in "${metric_base_images[@]}"; do + if ! pull_base_image "$base_image"; then build_failed=true fi - fi - - popd >/dev/null + done + + metric_builds=( + "Metric FTP|src/metric/ftp/build/Dockerfile|argus-metric-ftp:latest|src/metric/ftp/build" + "Metric Prometheus|src/metric/prometheus/build/Dockerfile|argus-metric-prometheus:latest|src/metric/prometheus/build" + "Metric Grafana|src/metric/grafana/build/Dockerfile|argus-metric-grafana:latest|src/metric/grafana/build" + ) + + for build_spec in "${metric_builds[@]}"; do + IFS='|' read -r image_label dockerfile_path image_tag build_context <<< "$build_spec" + if build_image "$image_label" "$dockerfile_path" "$image_tag" "$build_context"; then + images_built+=("$image_tag") + else + build_failed=true + fi + echo "" + done fi echo "=======================================" diff --git a/build/save_images.sh b/build/save_images.sh index 6ba3756..236e32e 100755 --- a/build/save_images.sh +++ b/build/save_images.sh @@ -71,8 +71,6 @@ declare -A images=( ["argus-metric-ftp:latest"]="argus-metric-ftp-latest.tar" ["argus-metric-prometheus:latest"]="argus-metric-prometheus-latest.tar" ["argus-metric-grafana:latest"]="argus-metric-grafana-latest.tar" - ["argus-metric-test-node:latest"]="argus-metric-test-node-latest.tar" - ["argus-metric-test-gpu-node:latest"]="argus-metric-test-gpu-node-latest.tar" ) # 函数:检查镜像是否存在 @@ -225,4 +223,4 @@ fi echo "" echo "✅ Image export completed successfully!" -echo "" \ No newline at end of file +echo "" diff --git a/src/metric/grafana/build/Dockerfile b/src/metric/grafana/build/Dockerfile index c9212dc..0615d08 100644 --- a/src/metric/grafana/build/Dockerfile +++ b/src/metric/grafana/build/Dockerfile @@ -2,6 +2,18 @@ FROM grafana/grafana:11.1.0 USER root +# 构建参数:是否使用内网镜像 +ARG USE_INTRANET=false + +# 根据是否为内网构建切换 apk 源 +RUN if [ "$USE_INTRANET" = "true" ]; then \ + echo "Configuring intranet apk repositories..." && \ + sed -i 's#https\?://[^/]\+#http://10.68.64.1#g' /etc/apk/repositories; \ + else \ + echo "Configuring public apk repositories..." && \ + sed -i 's#https\?://[^/]\+#https://mirrors.aliyun.com#g' /etc/apk/repositories; \ + fi + # 安装必要的工具 RUN apk add --no-cache \ supervisor \ @@ -10,6 +22,11 @@ RUN apk add --no-cache \ vim \ bash +# 部署镜像时恢复到部署侧使用的内网镜像源 +RUN if [ "$USE_INTRANET" = "true" ]; then \ + sed -i 's#https\?://[^/]\+#https://10.92.132.52/mirrors#g' /etc/apk/repositories; \ + fi + # supervisor 日志目录 RUN mkdir -p /var/log/supervisor diff --git a/src/metric/prometheus/build/Dockerfile b/src/metric/prometheus/build/Dockerfile index 9e609f3..2c8069a 100755 --- a/src/metric/prometheus/build/Dockerfile +++ b/src/metric/prometheus/build/Dockerfile @@ -61,10 +61,25 @@ RUN mkdir -p ${PROMETHEUS_BASE_PATH}/rules \ && ln -s ${PROMETHEUS_BASE_PATH} /prometheus # 修改 Prometheus 用户 UID/GID 并授权 -RUN usermod -u ${ARGUS_BUILD_UID} nobody && \ - groupmod -g ${ARGUS_BUILD_GID} nogroup && \ - chown -h nobody:nogroup /prometheus && \ - chown -R nobody:nogroup ${PROMETHEUS_BASE_PATH} && \ +RUN set -eux; \ + existing_user=""; \ + if getent passwd "${ARGUS_BUILD_UID}" >/dev/null 2>&1; then \ + existing_user="$(getent passwd "${ARGUS_BUILD_UID}" | cut -d: -f1)"; \ + fi; \ + if [ -n "$existing_user" ] && [ "$existing_user" != "nobody" ]; then \ + userdel -r "$existing_user" || true; \ + fi; \ + existing_group=""; \ + if getent group "${ARGUS_BUILD_GID}" >/dev/null 2>&1; then \ + existing_group="$(getent group "${ARGUS_BUILD_GID}" | cut -d: -f1)"; \ + fi; \ + if [ -n "$existing_group" ] && [ "$existing_group" != "nogroup" ]; then \ + groupdel "$existing_group" || true; \ + fi; \ + usermod -u ${ARGUS_BUILD_UID} nobody; \ + groupmod -g ${ARGUS_BUILD_GID} nogroup; \ + chown -h nobody:nogroup /prometheus; \ + chown -R nobody:nogroup ${PROMETHEUS_BASE_PATH}; \ chown -R nobody:nogroup /etc/prometheus # supervisor 配置