diff --git a/install.sh b/install.sh index 776ddea..71941e1 100755 --- a/install.sh +++ b/install.sh @@ -80,7 +80,6 @@ detect_fastest_mirror() { # Ubuntu/Debian 镜像源列表 UBUNTU_MIRRORS=( - "mirrors.aliyun.com" "mirrors.tuna.tsinghua.edu.cn" "mirrors.ustc.edu.cn" "mirrors.huaweicloud.com" @@ -90,11 +89,9 @@ detect_fastest_mirror() { # CentOS/RHEL 镜像源列表 CENTOS_MIRRORS=( - "mirrors.aliyun.com" "mirrors.tuna.tsinghua.edu.cn" "mirrors.ustc.edu.cn" "mirrors.huaweicloud.com" - "mirrorlist.centos.org" ) FASTEST_MIRROR="" @@ -374,29 +371,137 @@ install_dependencies() { echo -e "${GREEN}✓ 依赖安装完成${NC}" } +# 从官网下载安装 Go +install_go_from_official() { + echo -e "${BLUE}从 Go 官网下载安装...${NC}" + + # 检测架构 + local arch="" + case "$ARCH" in + amd64) + arch="amd64" + ;; + arm64) + arch="arm64" + ;; + *) + echo -e "${RED}不支持的架构: $ARCH${NC}" + return 1 + ;; + esac + + # Go 版本(可以根据需要修改) + local go_version="1.21.5" + local go_tar="go${go_version}.linux-${arch}.tar.gz" + local go_url="https://golang.google.cn/dl/${go_tar}" + + # 如果国内镜像访问失败,尝试官方源 + local download_success=false + + # 尝试从国内镜像下载 + echo -e "${BLUE}尝试从 Go 国内镜像下载...${NC}" + if curl -fsSL -o "/tmp/${go_tar}" "${go_url}" 2>/dev/null; then + download_success=true + else + # 尝试从官方源下载 + echo -e "${BLUE}尝试从 Go 官方源下载...${NC}" + go_url="https://go.dev/dl/${go_tar}" + if curl -fsSL -o "/tmp/${go_tar}" "${go_url}" 2>/dev/null; then + download_success=true + fi + fi + + if [ "$download_success" = false ]; then + echo -e "${RED}下载 Go 失败,请检查网络连接${NC}" + return 1 + fi + + # 删除旧版本(如果存在) + if [ -d "/usr/local/go" ]; then + echo -e "${BLUE}删除旧版本 Go...${NC}" + sudo rm -rf /usr/local/go + fi + + # 解压安装 + echo -e "${BLUE}安装 Go...${NC}" + sudo tar -C /usr/local -xzf "/tmp/${go_tar}" > /dev/null 2>&1 + rm -f "/tmp/${go_tar}" + + # 添加到 PATH(如果还没有) + if ! grep -q "/usr/local/go/bin" /etc/profile 2>/dev/null; then + echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee -a /etc/profile > /dev/null + fi + + # 设置当前会话的 PATH + export PATH=$PATH:/usr/local/go/bin + + # 验证安装 + if command -v go > /dev/null 2>&1; then + GO_VERSION=$(go version 2>/dev/null | head -1) + echo -e "${GREEN}✓ Go 安装完成: ${GO_VERSION}${NC}" + return 0 + else + echo -e "${YELLOW}⚠ Go 已安装但未在 PATH 中,请重新登录或执行: export PATH=\$PATH:/usr/local/go/bin${NC}" + # 临时添加到 PATH 继续执行 + export PATH=$PATH:/usr/local/go/bin + if command -v go > /dev/null 2>&1; then + GO_VERSION=$(go version 2>/dev/null | head -1) + echo -e "${GREEN}✓ Go 安装完成: ${GO_VERSION}${NC}" + return 0 + fi + return 1 + fi +} + # 安装 Go 环境 install_go() { echo -e "${BLUE}安装 Go 环境...${NC}" - if [ "$OS" = "ubuntu" ] || [ "$OS" = "debian" ]; then - sudo apt-get update -qq - sudo apt-get install -y -qq golang-go > /dev/null 2>&1 - elif [ "$OS" = "centos" ] || [ "$OS" = "rhel" ] || [ "$OS" = "rocky" ] || [ "$OS" = "almalinux" ]; then - sudo yum install -y -q golang > /dev/null 2>&1 - else - echo -e "${YELLOW}无法自动安装 Go,请手动安装: https://golang.org/dl/${NC}" - show_build_alternatives - exit 1 - fi - + # 先检查是否已安装 if command -v go > /dev/null 2>&1; then GO_VERSION=$(go version 2>/dev/null | head -1) - echo -e "${GREEN}✓ Go 安装完成: ${GO_VERSION}${NC}" - else - echo -e "${RED}Go 安装失败${NC}" - show_build_alternatives - exit 1 + echo -e "${GREEN}✓ Go 已安装: ${GO_VERSION}${NC}" + return 0 fi + + # 尝试从系统包管理器安装 + local install_success=false + + if [ "$OS" = "ubuntu" ] || [ "$OS" = "debian" ]; then + echo -e "${BLUE}尝试从 apt 安装 Go...${NC}" + sudo apt-get update -qq > /dev/null 2>&1 + if sudo apt-get install -y -qq golang-go > /dev/null 2>&1; then + install_success=true + fi + elif [ "$OS" = "centos" ] || [ "$OS" = "rhel" ] || [ "$OS" = "rocky" ] || [ "$OS" = "almalinux" ]; then + echo -e "${BLUE}尝试从 yum 安装 Go...${NC}" + # 检查 yum 源中是否有 golang 包 + if yum list available golang > /dev/null 2>&1; then + if sudo yum install -y -q golang > /dev/null 2>&1; then + install_success=true + fi + else + echo -e "${YELLOW}⚠ yum 源中未找到 golang 包,尝试从官网下载安装${NC}" + fi + fi + + # 检查安装是否成功 + if [ "$install_success" = true ] && command -v go > /dev/null 2>&1; then + GO_VERSION=$(go version 2>/dev/null | head -1) + echo -e "${GREEN}✓ Go 安装完成: ${GO_VERSION}${NC}" + return 0 + fi + + # 如果包管理器安装失败,从官网下载安装 + echo -e "${BLUE}包管理器安装失败,尝试从官网下载安装...${NC}" + if install_go_from_official; then + return 0 + fi + + # 所有方法都失败 + echo -e "${RED}Go 安装失败${NC}" + show_build_alternatives + exit 1 } # 显示替代方案 @@ -497,6 +602,11 @@ build_from_source() { install_go fi + # 确保 Go 在 PATH 中(如果从官网安装的) + if [ -d "/usr/local/go/bin" ] && ! echo "$PATH" | grep -q "/usr/local/go/bin"; then + export PATH=$PATH:/usr/local/go/bin + fi + # 检查 Go 版本 GO_VERSION=$(go version 2>/dev/null | head -1 || echo "") if [ -z "$GO_VERSION" ]; then @@ -507,6 +617,14 @@ build_from_source() { echo -e "${BLUE}检测到 Go 版本: ${GO_VERSION}${NC}" + # 确定 Go 的完整路径(用于 sudo 执行) + GO_BIN=$(command -v go) + if [ -z "$GO_BIN" ]; then + echo -e "${RED}无法找到 Go 可执行文件${NC}" + show_build_alternatives + exit 1 + fi + # 如果源码目录已存在,删除(卸载函数应该已经删除,这里作为保险) if [ -d "$SOURCE_DIR" ]; then echo -e "${YELLOW}清理旧的源码目录...${NC}" @@ -531,9 +649,14 @@ build_from_source() { sudo git config --global --add safe.directory "$SOURCE_DIR" 2>/dev/null || true git config --global --add safe.directory "$SOURCE_DIR" 2>/dev/null || true - # 下载依赖(使用 sudo 以 root 用户执行) + # 下载依赖(使用 sudo 以 root 用户执行,确保 PATH 包含 Go) echo -e "${BLUE}下载 Go 依赖...${NC}" - if ! sudo bash -c "cd '$SOURCE_DIR' && go mod download" 2>&1; then + # 构建包含 Go PATH 的环境变量 + GO_PATH_ENV="PATH=\$PATH:/usr/local/go/bin" + if [ -d "/usr/local/go/bin" ]; then + GO_PATH_ENV="PATH=/usr/local/go/bin:\$PATH" + fi + if ! sudo bash -c "cd '$SOURCE_DIR' && $GO_PATH_ENV && go mod download" 2>&1; then echo -e "${RED}下载依赖失败${NC}" show_build_alternatives exit 1 @@ -544,8 +667,8 @@ build_from_source() { TEMP_BINARY=$(mktemp) BINARY_PATH="$SOURCE_DIR/agent" - # 使用 sudo 以 root 用户编译,直接输出到目标位置 - if sudo bash -c "cd '$SOURCE_DIR' && GOOS=linux GOARCH=${ARCH} CGO_ENABLED=0 go build -buildvcs=false -ldflags='-w -s' -o '$BINARY_PATH' ./cmd/agent" 2>&1; then + # 使用 sudo 以 root 用户编译,直接输出到目标位置,确保 PATH 包含 Go + if sudo bash -c "cd '$SOURCE_DIR' && $GO_PATH_ENV && GOOS=linux GOARCH=${ARCH} CGO_ENABLED=0 go build -buildvcs=false -ldflags='-w -s' -o '$BINARY_PATH' ./cmd/agent" 2>&1; then if [ -f "$BINARY_PATH" ] && [ -s "$BINARY_PATH" ]; then sudo chmod +x "$BINARY_PATH" echo -e "${GREEN}✓ 编译成功${NC}"