[#7] 根目录统一构建镜像入口脚本
This commit is contained in:
138
build/build_images.sh
Executable file
138
build/build_images.sh
Executable file
@@ -0,0 +1,138 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# 帮助信息
|
||||
show_help() {
|
||||
cat << EOF
|
||||
ARGUS Unified Build System - Image Build Tool
|
||||
|
||||
Usage: $0 [OPTIONS]
|
||||
|
||||
Options:
|
||||
--intranet Use intranet mirror for Ubuntu 22.04 packages
|
||||
-h, --help Show this help message
|
||||
|
||||
Examples:
|
||||
$0 # Build with default sources
|
||||
$0 --intranet # Build with intranet mirror
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
# 解析命令行参数
|
||||
use_intranet=false
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
--intranet)
|
||||
use_intranet=true
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
show_help
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "Unknown option: $1"
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# 获取项目根目录
|
||||
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$root"
|
||||
|
||||
echo "======================================="
|
||||
echo "ARGUS Unified Build System"
|
||||
echo "======================================="
|
||||
|
||||
if [[ "$use_intranet" == true ]]; then
|
||||
echo "🌐 Mode: Intranet (Using internal mirror: 10.68.64.1)"
|
||||
build_args="--build-arg USE_INTRANET=true"
|
||||
else
|
||||
echo "🌐 Mode: Public (Using default package sources)"
|
||||
build_args=""
|
||||
fi
|
||||
|
||||
echo "📁 Build context: $root"
|
||||
echo ""
|
||||
|
||||
# 构建镜像的函数
|
||||
build_image() {
|
||||
local image_name=$1
|
||||
local dockerfile_path=$2
|
||||
local tag=$3
|
||||
|
||||
echo "🔄 Building $image_name image..."
|
||||
echo " Dockerfile: $dockerfile_path"
|
||||
echo " Tag: $tag"
|
||||
|
||||
if docker build $build_args -f "$dockerfile_path" -t "$tag" .; then
|
||||
echo "✅ $image_name image built successfully"
|
||||
return 0
|
||||
else
|
||||
echo "❌ Failed to build $image_name image"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 构建所有镜像
|
||||
images_built=()
|
||||
build_failed=false
|
||||
|
||||
# 构建 Elasticsearch 镜像
|
||||
if build_image "Elasticsearch" "src/log/elasticsearch/build/Dockerfile" "argus-elasticsearch:latest"; then
|
||||
images_built+=("argus-elasticsearch:latest")
|
||||
else
|
||||
build_failed=true
|
||||
fi
|
||||
|
||||
echo ""
|
||||
|
||||
# 构建 Kibana 镜像
|
||||
if build_image "Kibana" "src/log/kibana/build/Dockerfile" "argus-kibana:latest"; then
|
||||
images_built+=("argus-kibana:latest")
|
||||
else
|
||||
build_failed=true
|
||||
fi
|
||||
|
||||
echo ""
|
||||
|
||||
# 构建 BIND9 镜像
|
||||
if build_image "BIND9" "src/bind/build/Dockerfile" "argus-bind9:latest"; then
|
||||
images_built+=("argus-bind9:latest")
|
||||
else
|
||||
build_failed=true
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "======================================="
|
||||
echo "📦 Build Summary"
|
||||
echo "======================================="
|
||||
|
||||
if [[ ${#images_built[@]} -gt 0 ]]; then
|
||||
echo "✅ Successfully built images:"
|
||||
for image in "${images_built[@]}"; do
|
||||
echo " • $image"
|
||||
done
|
||||
fi
|
||||
|
||||
if [[ "$build_failed" == true ]]; then
|
||||
echo ""
|
||||
echo "❌ Some images failed to build. Please check the errors above."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ "$use_intranet" == true ]]; then
|
||||
echo ""
|
||||
echo "🌐 Built with intranet mirror configuration"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "🚀 Next steps:"
|
||||
echo " cd src/log && ./scripts/save_images.sh # Export log images"
|
||||
echo " cd src/bind && ./scripts/save_images.sh # Export bind images"
|
||||
echo " cd src/log/tests && ./scripts/02_up.sh # Start log services"
|
||||
echo ""
|
||||
222
build/save_images.sh
Executable file
222
build/save_images.sh
Executable file
@@ -0,0 +1,222 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# 帮助信息
|
||||
show_help() {
|
||||
cat << EOF
|
||||
ARGUS Unified Build System - Image Export Tool
|
||||
|
||||
Usage: $0 [OPTIONS]
|
||||
|
||||
Options:
|
||||
--compress Compress exported images with gzip
|
||||
-h, --help Show this help message
|
||||
|
||||
Examples:
|
||||
$0 # Export all images without compression
|
||||
$0 --compress # Export all images with gzip compression
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
# 解析命令行参数
|
||||
use_compression=false
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
--compress)
|
||||
use_compression=true
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
show_help
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "Unknown option: $1"
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# 获取项目根目录
|
||||
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$root"
|
||||
|
||||
# 创建镜像输出目录
|
||||
images_dir="$root/images"
|
||||
mkdir -p "$images_dir"
|
||||
|
||||
echo "======================================="
|
||||
echo "ARGUS Unified Build System - Image Export"
|
||||
echo "======================================="
|
||||
echo ""
|
||||
|
||||
if [[ "$use_compression" == true ]]; then
|
||||
echo "🗜️ Mode: With gzip compression"
|
||||
else
|
||||
echo "📦 Mode: No compression"
|
||||
fi
|
||||
|
||||
echo "📁 Output directory: $images_dir"
|
||||
echo ""
|
||||
|
||||
# 定义镜像列表
|
||||
declare -A images=(
|
||||
["argus-elasticsearch:latest"]="argus-elasticsearch-latest.tar"
|
||||
["argus-kibana:latest"]="argus-kibana-latest.tar"
|
||||
["argus-bind9:latest"]="argus-bind9-latest.tar"
|
||||
)
|
||||
|
||||
# 函数:检查镜像是否存在
|
||||
check_image() {
|
||||
local image_name="$1"
|
||||
if docker images --format "{{.Repository}}:{{.Tag}}" | grep -q "^$image_name$"; then
|
||||
echo "✅ Image found: $image_name"
|
||||
return 0
|
||||
else
|
||||
echo "❌ Image not found: $image_name"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 函数:显示镜像信息
|
||||
show_image_info() {
|
||||
local image_name="$1"
|
||||
echo "📋 Image info for $image_name:"
|
||||
docker images "$image_name" --format " Size: {{.Size}}, Created: {{.CreatedSince}}, ID: {{.ID}}"
|
||||
}
|
||||
|
||||
# 函数:保存镜像
|
||||
save_image() {
|
||||
local image_name="$1"
|
||||
local output_file="$2"
|
||||
local output_path="$images_dir/$output_file"
|
||||
|
||||
echo "🔄 Saving $image_name to $output_file..."
|
||||
|
||||
# 删除旧的镜像文件(如果存在)
|
||||
if [[ -f "$output_path" ]]; then
|
||||
echo " Removing existing file: $output_file"
|
||||
rm "$output_path"
|
||||
fi
|
||||
|
||||
if [[ "$use_compression" == true && -f "$output_path.gz" ]]; then
|
||||
echo " Removing existing compressed file: $output_file.gz"
|
||||
rm "$output_path.gz"
|
||||
fi
|
||||
|
||||
# 保存镜像
|
||||
docker save "$image_name" -o "$output_path"
|
||||
|
||||
if [[ "$use_compression" == true ]]; then
|
||||
echo " Compressing with gzip..."
|
||||
gzip "$output_path"
|
||||
output_path="$output_path.gz"
|
||||
output_file="$output_file.gz"
|
||||
fi
|
||||
|
||||
# 检查文件大小
|
||||
local file_size=$(du -h "$output_path" | cut -f1)
|
||||
echo "✅ Saved successfully: $output_file ($file_size)"
|
||||
}
|
||||
|
||||
echo "🔍 Checking for ARGUS images..."
|
||||
echo ""
|
||||
|
||||
# 检查所有镜像
|
||||
available_images=()
|
||||
missing_images=()
|
||||
|
||||
for image_name in "${!images[@]}"; do
|
||||
if check_image "$image_name"; then
|
||||
show_image_info "$image_name"
|
||||
available_images+=("$image_name")
|
||||
else
|
||||
missing_images+=("$image_name")
|
||||
fi
|
||||
echo ""
|
||||
done
|
||||
|
||||
# 如果没有镜像存在,提示构建
|
||||
if [[ ${#available_images[@]} -eq 0 ]]; then
|
||||
echo "❌ No ARGUS images found to export."
|
||||
echo ""
|
||||
echo "🔧 Please build the images first with:"
|
||||
echo " ./build/build_images.sh"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 显示缺失的镜像
|
||||
if [[ ${#missing_images[@]} -gt 0 ]]; then
|
||||
echo "⚠️ Missing images (will be skipped):"
|
||||
for image_name in "${missing_images[@]}"; do
|
||||
echo " • $image_name"
|
||||
done
|
||||
echo ""
|
||||
fi
|
||||
|
||||
echo "💾 Starting image export process..."
|
||||
echo ""
|
||||
|
||||
# 保存所有可用的镜像
|
||||
exported_files=()
|
||||
for image_name in "${available_images[@]}"; do
|
||||
output_file="${images[$image_name]}"
|
||||
save_image "$image_name" "$output_file"
|
||||
|
||||
if [[ "$use_compression" == true ]]; then
|
||||
exported_files+=("$output_file.gz")
|
||||
else
|
||||
exported_files+=("$output_file")
|
||||
fi
|
||||
echo ""
|
||||
done
|
||||
|
||||
echo "======================================="
|
||||
echo "📦 Export Summary"
|
||||
echo "======================================="
|
||||
|
||||
# 显示导出的文件
|
||||
echo "📁 Exported files in $images_dir:"
|
||||
total_size=0
|
||||
for file in "${exported_files[@]}"; do
|
||||
full_path="$images_dir/$file"
|
||||
if [[ -f "$full_path" ]]; then
|
||||
size=$(du -h "$full_path" | cut -f1)
|
||||
size_bytes=$(du -b "$full_path" | cut -f1)
|
||||
total_size=$((total_size + size_bytes))
|
||||
echo " ✅ $file ($size)"
|
||||
fi
|
||||
done
|
||||
|
||||
# 显示总大小
|
||||
if [[ $total_size -gt 0 ]]; then
|
||||
total_size_human=$(numfmt --to=iec --suffix=B $total_size)
|
||||
echo ""
|
||||
echo "📊 Total size: $total_size_human"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "🚀 Usage instructions:"
|
||||
echo " To load these images on another system:"
|
||||
|
||||
if [[ "$use_compression" == true ]]; then
|
||||
for file in "${exported_files[@]}"; do
|
||||
if [[ -f "$images_dir/$file" ]]; then
|
||||
base_name="${file%.gz}"
|
||||
echo " gunzip $file && docker load -i $base_name"
|
||||
fi
|
||||
done
|
||||
else
|
||||
for file in "${exported_files[@]}"; do
|
||||
if [[ -f "$images_dir/$file" ]]; then
|
||||
echo " docker load -i $file"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "✅ Image export completed successfully!"
|
||||
echo ""
|
||||
Reference in New Issue
Block a user