#!/bin/bash
#################################################################
# 脚本名称: pingtai_install.sh
# 创建用户: hexiaoshi
# 创建时间: 2024-07-18
# 脚本描述: 安装 系统 及相关组件
# 使用方法: sudo ./pingtai_install.sh
#################################################################

set -euo pipefail

install_dir="/home"
download_url="http://183.3.220.117:8090/ilcp-km-kd-dev.tar.gz"
archive_name="ilcp-km-kd-dev.tar.gz"
target_dir="ilcp-km-kd-dev"

main() {
    echo "Starting platform installation..."
    cd "$install_dir" || fail "Failed to access $install_dir"
    
    # Download archive
    if ! wget -q --show-progress "$download_url"; then
        fail "Download failed from $download_url"
    fi
    echo "Download completed: $(du -h "$archive_name")"
    
    # Extract archive
    if ! tar -zxf "$archive_name"; then
        fail "Failed to extract $archive_name"
    fi
    echo "Extraction completed to directory: $target_dir"
    
    # Apply Kubernetes configuration
    cd "$target_dir" || fail "Cannot enter directory $target_dir"
    if kubectl apply -f ilcp-km-kd-dev.yaml; then
        echo "✅ Kubernetes resources deployed successfully"
    else
        fail "kubectl apply failed with exit code $?"
    fi
}

fail() {
    echo "❌ ERROR: $1" >&2
    exit 1
}

main
