mgmt/infrastructure/consul/test-jinja2.sh

143 lines
3.3 KiB
Bash
Executable File
Raw Permalink 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.

#!/bin/bash
# Jinja2模板测试脚本
set -e
TEMPLATE_FILE="infrastructure/consul/baseline/consul.j2"
OUTPUT_DIR="infrastructure/consul/jinja2-output"
# 节点配置
declare -A NODE_CONFIGS
NODE_CONFIGS[ch4]="100.117.106.134:server:true"
NODE_CONFIGS[ash3c]="100.116.80.94:server:true"
NODE_CONFIGS[warden]="100.122.197.112:server:true"
NODE_CONFIGS[ash1d]="100.81.26.3:client:false"
NODE_CONFIGS[ash2e]="100.81.26.4:client:false"
NODE_CONFIGS[ch2]="100.117.106.135:client:false"
NODE_CONFIGS[ch3]="100.117.106.137:client:false"
NODE_CONFIGS[de]="100.98.209.52:client:false"
NODE_CONFIGS[onecloud1]="100.98.209.53:client:false"
NODE_CONFIGS[semaphore]="100.98.209.54:client:false"
NODE_CONFIGS[browser]="100.116.112.45:client:false"
NODE_CONFIGS[hcp1]="100.116.112.46:client:false"
NODE_CONFIGS[influxdb]="100.116.112.47:client:false"
# 颜色输出
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m'
log() {
echo -e "${BLUE}[$(date '+%H:%M:%S')]${NC} $1"
}
success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 创建输出目录
mkdir -p "$OUTPUT_DIR"
# 测试Jinja2模板
test_jinja2_template() {
local node=$1
local config=${NODE_CONFIGS[$node]}
if [ -z "$config" ]; then
error "未找到节点 $node 的配置"
return 1
fi
# 解析配置
IFS=':' read -r bind_addr node_zone ui_enabled <<< "$config"
log "测试节点: $node"
log "绑定地址: $bind_addr"
log "节点区域: $node_zone"
log "UI启用: $ui_enabled"
# 创建JSON配置文件
local json_file="$OUTPUT_DIR/${node}-config.json"
cat > "$json_file" << JSON
{
"node_name": "$node",
"bind_addr": "$bind_addr",
"node_zone": "$node_zone",
"ui_enabled": $ui_enabled
}
JSON
# 使用Jinja2渲染模板
local output_file="$OUTPUT_DIR/${node}-consul.hcl"
if command -v jinja2 >/dev/null 2>&1; then
jinja2 "$TEMPLATE_FILE" "$json_file" > "$output_file"
else
# 使用Python脚本
python3 -c "
import json
from jinja2 import Template
with open('$json_file', 'r') as f:
data = json.load(f)
with open('$TEMPLATE_FILE', 'r') as f:
template = Template(f.read())
with open('$output_file', 'w') as f:
f.write(template.render(**data))
"
fi
success "Jinja2模板渲染完成: $output_file"
# 显示前10行验证
echo "--- 验证前10行 ---"
head -10 "$output_file"
echo "--- 验证完成 ---"
echo ""
}
# 主函数
main() {
local target_node=${1:-"ch4"}
log "开始Jinja2模板测试"
log "目标节点: $target_node"
if [ "$target_node" = "all" ]; then
log "测试所有节点"
for node in "${!NODE_CONFIGS[@]}"; do
test_jinja2_template "$node"
done
else
test_jinja2_template "$target_node"
fi
success "Jinja2模板测试完成"
log "输出目录: $OUTPUT_DIR"
}
# 显示帮助
show_help() {
echo "使用方法: $0 [节点名|all]"
echo ""
echo "示例:"
echo " $0 ch4 # 测试ch4节点"
echo " $0 all # 测试所有节点"
echo ""
echo "支持的节点: ${!NODE_CONFIGS[@]}"
}
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
show_help
exit 0
fi
main "$@"