#!/bin/bash set -e # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # 日志函数 log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # 检查必要的文件 check_prerequisites() { log_info "检查前置条件..." if [ ! -f "configuration/inventories/production/nomad-cluster.ini" ]; then log_error "找不到 Nomad 集群配置文件" exit 1 fi if [ ! -f "configuration/playbooks/applications/configure-nomad-cluster.yml" ]; then log_error "找不到 Nomad 配置 playbook" exit 1 fi log_success "前置条件检查完成" } # 生成加密密钥 generate_encrypt_key() { log_info "生成 Nomad 加密密钥..." if command -v nomad >/dev/null 2>&1; then ENCRYPT_KEY=$(nomad operator gossip keyring generate) log_success "生成加密密钥: $ENCRYPT_KEY" # 更新配置文件中的加密密钥 sed -i "s|YOUR_NOMAD_ENCRYPT_KEY_HERE|$ENCRYPT_KEY|g" configuration/inventories/production/nomad-cluster.ini log_success "已更新配置文件中的加密密钥" else log_warning "本地未安装 Nomad,将在远程节点生成密钥" fi } # 测试连接 test_connectivity() { log_info "测试目标主机连接性..." ansible -i configuration/inventories/production/nomad-cluster.ini nomad_cluster -m ping if [ $? -eq 0 ]; then log_success "所有主机连接正常" else log_error "部分主机连接失败,请检查网络和SSH配置" exit 1 fi } # 配置 Nomad 集群 configure_cluster() { log_info "开始配置 Nomad 集群..." ansible-playbook -i configuration/inventories/production/nomad-cluster.ini \ configuration/playbooks/applications/configure-nomad-cluster.yml \ -v if [ $? -eq 0 ]; then log_success "Nomad 集群配置完成" else log_error "Nomad 集群配置失败" exit 1 fi } # 验证集群状态 verify_cluster() { log_info "验证集群状态..." # 等待服务启动 sleep 10 log_info "检查 Nomad 服务状态..." ansible -i configuration/inventories/production/nomad-cluster.ini nomad_servers \ -m shell -a "systemctl status nomad --no-pager" log_info "检查集群成员..." ansible -i configuration/inventories/production/nomad-cluster.ini nomad_servers \ -m shell -a "nomad server members" --limit 1 log_info "检查节点状态..." ansible -i configuration/inventories/production/nomad-cluster.ini nomad_servers \ -m shell -a "nomad node status" --limit 1 } # 主函数 main() { echo "🚀 开始配置 Nomad 集群..." echo "==================================" check_prerequisites generate_encrypt_key test_connectivity configure_cluster verify_cluster echo "==================================" log_success "Nomad 集群配置完成!" echo "" echo "访问 Nomad UI:" echo "- Master: http://100.117.106.136:4646" echo "- Semaphore: http://100.116.158.95:4646" echo "" echo "常用命令:" echo "- 查看集群状态: nomad server members" echo "- 查看节点状态: nomad node status" echo "- 运行作业: nomad job run " } # 运行主函数 main "$@"