fix: 将 k3s-ansible 作为普通目录添加

This commit is contained in:
fei
2026-02-04 23:43:40 +08:00
commit 7f6c8b9b92
40 changed files with 10909 additions and 0 deletions

333
scripts/deploy-all.sh Executable file
View File

@@ -0,0 +1,333 @@
#!/bin/bash
set -euo pipefail
# Load common functions
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
# Source common library
source "$SCRIPT_DIR/lib/common.sh"
# Configuration
CONFIG_FILE="$PROJECT_DIR/config/cluster-vars.yml"
# Step definitions
STEPS=(
"check_prerequisites:检查前置条件"
"generate_inventory:生成Ansible Inventory"
"deploy_k3s:部署K3s集群"
"deploy_gitea:部署Gitea"
"setup_gitea:初始化Gitea"
"deploy_argocd:部署ArgoCD"
"deploy_https:配置HTTPS"
"create_demo_app:创建示例应用"
)
# Step functions
check_prerequisites() {
log_step "检查前置条件"
# Check configuration file
check_config_file "$CONFIG_FILE" || return 1
# Check required tools
check_required_tools || return 1
# Check network connectivity
check_network_with_retry "https://www.google.com" 3 || {
log_warn "Network connectivity check failed, but continuing..."
}
# Install Python YAML library
if ! python3 -c "import yaml" 2>/dev/null; then
log "Installing python3-yaml..."
sudo apt update && sudo apt install -y python3-yaml
fi
log "✓ All prerequisites checked"
return 0
}
generate_inventory() {
log_step "生成Ansible Inventory"
if [ ! -f "$SCRIPT_DIR/generate-inventory.py" ]; then
log_error "generate-inventory.py not found"
return 1
fi
cd "$PROJECT_DIR"
python3 "$SCRIPT_DIR/generate-inventory.py" || return 1
log "✓ Ansible inventory generated"
return 0
}
deploy_k3s() {
log_step "部署K3s集群"
if [ ! -d "$PROJECT_DIR/k3s-ansible" ]; then
log "Cloning k3s-ansible repository..."
cd "$PROJECT_DIR"
git clone https://github.com/k3s-io/k3s-ansible.git || return 1
fi
# Check if kubectl is already available and cluster is running
if check_kubectl; then
log "K3s cluster is already running, skipping deployment"
return 0
fi
log "Running Ansible playbook..."
cd "$PROJECT_DIR/k3s-ansible"
ansible-playbook site.yml \
-i inventory/hosts.ini \
-e "@$CONFIG_FILE" || return 1
# Configure kubectl
log "Configuring kubectl..."
mkdir -p ~/.kube
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown $USER:$USER ~/.kube/config
# Verify cluster
log "Verifying cluster..."
sleep 10
kubectl get nodes || return 1
log "✓ K3s cluster deployed successfully"
return 0
}
deploy_gitea() {
log_step "部署Gitea"
if [ ! -f "$SCRIPT_DIR/deploy-gitea.sh" ]; then
log_error "deploy-gitea.sh not found"
return 1
fi
# Check if Gitea is already deployed
if kubectl get namespace gitea &>/dev/null && \
kubectl get deployment gitea -n gitea &>/dev/null 2>&1; then
log "Gitea is already deployed, skipping"
return 0
fi
bash "$SCRIPT_DIR/deploy-gitea.sh" || return 1
log "✓ Gitea deployed successfully"
return 0
}
setup_gitea() {
log_step "初始化Gitea"
if [ ! -f "$SCRIPT_DIR/setup-gitea.sh" ]; then
log_error "setup-gitea.sh not found"
return 1
fi
bash "$SCRIPT_DIR/setup-gitea.sh" || return 1
log "✓ Gitea initialized successfully"
return 0
}
deploy_argocd() {
log_step "部署ArgoCD"
if [ ! -f "$SCRIPT_DIR/deploy-argocd.sh" ]; then
log_error "deploy-argocd.sh not found"
return 1
fi
# Check if ArgoCD is already deployed
if kubectl get namespace argocd &>/dev/null && \
kubectl get deployment argocd-server -n argocd &>/dev/null 2>&1; then
log "ArgoCD is already deployed, skipping"
return 0
fi
bash "$SCRIPT_DIR/deploy-argocd.sh" || return 1
log "✓ ArgoCD deployed successfully"
return 0
}
deploy_https() {
log_step "配置HTTPS"
if [ ! -f "$SCRIPT_DIR/deploy-https.sh" ]; then
log_warn "deploy-https.sh not found, skipping HTTPS configuration"
return 0
fi
bash "$SCRIPT_DIR/deploy-https.sh" || {
log_warn "HTTPS configuration failed, but continuing..."
return 0
}
log "✓ HTTPS configured successfully"
return 0
}
create_demo_app() {
log_step "创建示例应用"
if [ ! -f "$SCRIPT_DIR/create-argocd-app.sh" ]; then
log_warn "create-argocd-app.sh not found, skipping demo app creation"
return 0
fi
bash "$SCRIPT_DIR/create-argocd-app.sh" || {
log_warn "Demo app creation failed, but continuing..."
return 0
}
log "✓ Demo app created successfully"
return 0
}
# Execute step
execute_step() {
local step_name="$1"
if type "$step_name" &>/dev/null; then
"$step_name"
return $?
else
log_error "Step function not found: $step_name"
return 1
fi
}
# Main function
main() {
echo "=========================================="
echo " K3s集群自动化部署"
echo "=========================================="
echo ""
log "开始部署流程"
log "日志文件: $LOG_FILE"
log "状态文件: $STATE_FILE"
echo ""
local failed_steps=()
local completed_steps=()
local skipped_steps=()
for step in "${STEPS[@]}"; do
step_name="${step%%:*}"
step_desc="${step##*:}"
echo ""
echo "=========================================="
if is_step_completed "$step_name"; then
log "✓ 跳过已完成的步骤: $step_desc"
skipped_steps+=("$step_desc")
continue
fi
log_step "执行步骤: $step_desc"
if execute_step "$step_name"; then
mark_step_completed "$step_name"
log "✓ 完成: $step_desc"
completed_steps+=("$step_desc")
else
log_error "✗ 失败: $step_desc"
failed_steps+=("$step_desc")
echo ""
echo "=========================================="
echo " 部署失败"
echo "=========================================="
echo ""
log_error "步骤失败: $step_desc"
log_error "请检查日志文件: $LOG_FILE"
log_error "修复问题后,可以重新运行此脚本继续部署"
echo ""
print_summary
echo "已完成步骤: ${#completed_steps[@]}"
for s in "${completed_steps[@]}"; do
echo "$s"
done
echo ""
echo "跳过步骤: ${#skipped_steps[@]}"
for s in "${skipped_steps[@]}"; do
echo " - $s"
done
echo ""
echo "失败步骤: ${#failed_steps[@]}"
for s in "${failed_steps[@]}"; do
echo "$s"
done
echo ""
exit 1
fi
done
echo ""
echo "=========================================="
echo " 部署完成!"
echo "=========================================="
echo ""
print_summary
echo "总步骤数: ${#STEPS[@]}"
echo "已完成: ${#completed_steps[@]}"
echo "已跳过: ${#skipped_steps[@]}"
echo ""
if [ ${#completed_steps[@]} -gt 0 ]; then
echo "本次完成的步骤:"
for s in "${completed_steps[@]}"; do
echo "$s"
done
echo ""
fi
if [ ${#skipped_steps[@]} -gt 0 ]; then
echo "跳过的步骤:"
for s in "${skipped_steps[@]}"; do
echo " - $s"
done
echo ""
fi
log "✓ K3s集群部署完成"
echo ""
echo "下一步操作:"
echo " 1. 验证部署: ./scripts/verify-deployment.sh"
echo " 2. 查看集群状态: kubectl get nodes"
echo " 3. 查看所有Pod: kubectl get pods -A"
echo ""
}
# Handle script arguments
case "${1:-}" in
--reset)
log "重置部署状态..."
reset_deployment_state
log "状态已重置,可以重新开始部署"
exit 0
;;
--help|-h)
echo "用法: $0 [选项]"
echo ""
echo "选项:"
echo " --reset 重置部署状态,从头开始"
echo " --help 显示此帮助信息"
echo ""
exit 0
;;
esac
# Run main function
main