argus/deployment/build/build_images.sh

99 lines
3.8 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
. "$ROOT_DIR/deployment/build/common.sh"
usage() {
cat <<EOF
Build Argus images (optional node-bundle)
Usage: build_images.sh [--with-node-bundle] [--client-version YYYYMMDD] [--base-image NAME[:TAG]]
Examples:
./deployment/build/build_images.sh --with-node-bundle --client-version 20251106
EOF
}
WITH_BUNDLE=false
CLIENT_VERSION=""
BASE_IMAGE="argus-sys-metric-test-node:latest"
while [[ $# -gt 0 ]]; do
case "$1" in
--with-node-bundle) WITH_BUNDLE=true; shift;;
--client-version) CLIENT_VERSION="$2"; shift 2;;
--base-image) BASE_IMAGE="$2"; shift 2;;
-h|--help) usage; exit 0;;
*) err "unknown arg: $1"; usage; exit 1;;
esac
done
if [[ "$WITH_BUNDLE" == true ]]; then
require_cmd docker tar gzip
BUNDLE_DIR="$ROOT_DIR/src/sys/build/node-bundle"
CTX_DIR="$BUNDLE_DIR"
TMP_BUNDLE="$BUNDLE_DIR/bundle"
rm -rf "$TMP_BUNDLE"; mkdir -p "$TMP_BUNDLE"
# Build or locate client artifact
PLUGIN_DIR="$ROOT_DIR/src/metric/client-plugins/all-in-one-full"
# CLIENT_VERSION 支持两种形式:
# - 形如 1.42.0 的 artifact 版本(默认)
# - 形如 YYYYMMDD 的打包日期,将从 deployment/artifact/client/ 下解析出内部 artifact 版本
if [[ -z "$CLIENT_VERSION" ]]; then
pushd "$PLUGIN_DIR" >/dev/null
bash scripts/package_artifact.sh --force
CLIENT_VERSION=$(cat artifact/*/version.json 2>/dev/null | sed -n 's/.*"version"\s*:\s*"\([^"]\+\)".*/\1/p' | tail -n1)
popd >/dev/null
[[ -n "$CLIENT_VERSION" ]] || { err "failed to detect client version"; exit 1; }
else
if [[ "$CLIENT_VERSION" =~ ^[0-9]{8}$ ]]; then
PKG_DIR="$ROOT_DIR/deployment/artifact/client/$CLIENT_VERSION"
TAR_PKG="$PKG_DIR/argus-metric_${CLIENT_VERSION}.tar.gz"
[[ -f "$TAR_PKG" ]] || { err "client date package not found: $TAR_PKG"; exit 1; }
# 解包读取内部 version.json
tmpd=$(mktemp -d); trap 'rm -rf "$tmpd"' EXIT
tar -xzf "$TAR_PKG" -C "$tmpd"
if [[ -f "$tmpd/version.json" ]]; then
ART_VER=$(sed -n 's/.*"version"\s*:\s*"\([^"]\+\)".*/\1/p' "$tmpd/version.json" | head -n1)
[[ -n "$ART_VER" ]] || { err "failed to parse artifact version from date package"; exit 1; }
CLIENT_VERSION="$ART_VER"
# 直接使用该 tar 作为 bundle 源
cp "$TAR_PKG" "$TMP_BUNDLE/argus-metric_$(echo "$ART_VER" | tr '.' '_').tar.gz"
# 同时尝试复制 setup.sh若存在
[[ -f "$PKG_DIR/setup.sh" ]] && cp "$PKG_DIR/setup.sh" "$TMP_BUNDLE/" || true
else
err "version.json missing in client date package"
exit 1
fi
else
# 假定为 artifact 版本目录
pushd "$PLUGIN_DIR" >/dev/null
[[ -d "artifact/$CLIENT_VERSION" ]] || bash scripts/package_artifact.sh --force
popd >/dev/null
fi
fi
# 若未通过日期包预置 tar则从插件 artifact 目录取
TAR_NAME="argus-metric_$(echo "$CLIENT_VERSION" | tr '.' '_').tar.gz"
if [[ ! -f "$TMP_BUNDLE/$TAR_NAME" ]]; then
SRC_TAR="$PLUGIN_DIR/artifact/$CLIENT_VERSION/$TAR_NAME"
[[ -f "$SRC_TAR" ]] || { err "missing client tar: $SRC_TAR"; exit 1; }
cp "$SRC_TAR" "$TMP_BUNDLE/"
# also include setup.sh for fallback
if [[ -f "$ROOT_DIR/deployment/artifact/client/$(today_version)/setup.sh" ]]; then
cp "$ROOT_DIR/deployment/artifact/client/$(today_version)/setup.sh" "$TMP_BUNDLE/" || true
fi
fi
log "Building node-bundle image with client version: $CLIENT_VERSION"
DOCKER_BUILDKIT=0 docker build \
--build-arg CLIENT_VER="$CLIENT_VERSION" \
--build-arg BASE_IMAGE="$BASE_IMAGE" \
-t argus-sys-metric-test-node-bundle:latest \
-f "$BUNDLE_DIR/Dockerfile" "$BUNDLE_DIR"
log "Built image: argus-sys-metric-test-node-bundle:latest"
fi
log "Done."