package model import "time" // ============================================ // DeployTool 工具配置(懒加载/预装/禁用) // 按 deploy_ 前缀,与 PipelineRepo/Registry 同属发布配置域 // ============================================ // ToolInstallMethod 安装方式 type ToolInstallMethod string const ( ToolPreinstall ToolInstallMethod = "preinstall" // 预装在 Dockerfile 中 ToolLazy ToolInstallMethod = "lazy" // 懒加载:首次使用时自动下载 ToolDisabled ToolInstallMethod = "disabled" // 禁用:不安装 ) // ToolStatus 安装状态 type ToolStatus string const ( ToolNotInstalled ToolStatus = "not_installed" // 未安装 ToolInstalling ToolStatus = "installing" // 安装中 ToolInstalled ToolStatus = "installed" // 已安装 ToolFailed ToolStatus = "failed" // 安装失败 ) // DeployTool 工具配置表 type DeployTool struct { ID uint `gorm:"primaryKey;autoIncrement;comment:主键ID" json:"id"` Name string `gorm:"type:varchar(64);uniqueIndex;not null;comment:工具名称(英文标识)" json:"name"` DisplayName string `gorm:"type:varchar(128);not null;comment:显示名称" json:"display_name"` Description string `gorm:"type:varchar(256);comment:工具描述" json:"description,omitempty"` Category string `gorm:"type:varchar(32);default:other;comment:分类: k8s/build/deploy/automation" json:"category"` InstallMethod ToolInstallMethod `gorm:"type:varchar(16);default:lazy;comment:安装方式: preinstall/lazy/disabled" json:"install_method"` CheckCommand string `gorm:"type:varchar(256);comment:检测命令(如 kubectl version --client)" json:"check_command,omitempty"` BinaryName string `gorm:"type:varchar(64);comment:二进制文件名(如 kubectl/mvn/node)" json:"binary_name,omitempty"` InstallPath string `gorm:"type:varchar(256);comment:安装目标路径(如 /usr/local/bin)" json:"install_path,omitempty"` DownloadURL string `gorm:"type:varchar(512);comment:下载地址模板(支持 {version})" json:"download_url,omitempty"` VersionURL string `gorm:"type:varchar(512);comment:版本查询地址(如 stable.txt)" json:"version_url,omitempty"` InstallScript string `gorm:"type:text;comment:自定义安装脚本(Shell)" json:"install_script,omitempty"` DefaultVersion string `gorm:"type:varchar(32);comment:默认版本号" json:"default_version,omitempty"` Status ToolStatus `gorm:"type:varchar(16);default:not_installed;comment:当前安装状态" json:"status"` InstalledVer string `gorm:"type:varchar(128);comment:已安装版本号" json:"installed_version,omitempty"` LastCheck *time.Time `gorm:"comment:最后检测时间" json:"last_check,omitempty"` ErrorMsg string `gorm:"type:varchar(512);comment:安装失败原因" json:"error_msg,omitempty"` Enabled bool `gorm:"default:true;comment:是否启用" json:"enabled"` SortOrder int `gorm:"default:0;comment:排序" json:"sort_order"` CreatedAt time.Time `gorm:"not null;comment:创建时间" json:"created_at"` UpdatedAt time.Time `gorm:"not null;comment:更新时间" json:"updated_at"` } // TableName 表名 func (DeployTool) TableName() string { return "deploy_tools" } // ============================================ // 预置工具定义(Seeder)— 精简版 v2 // 移除: kubectl(Dockerfile 预装)、helm(非核心) // 新增: nvm(管理 Node 版本)、jdk(Java 构建) // ============================================ // PredefinedTools 返回系统预置的工具配置列表 func PredefinedTools() []DeployTool { return []DeployTool{ // ──── JDK — Java 构建 // Logan.Li ──── { Name: "jdk", DisplayName: "JDK (Java)", Description: "Java 开发工具包,流水线 Java/Maven 构建阶段使用", Category: "build", InstallMethod: ToolLazy, BinaryName: "java", InstallPath: "/usr/local/bin/java", CheckCommand: "java -version 2>&1 | head -1", VersionURL: "https://api.adoptium.net/v3/info/release_versions?heap_size=normal&image_type=jdk&page=0&page_size=1&project=jdk&release_type=ga&semver=false&sort_method=DEFAULT&sort_order=DESC&version=%5B17%2C99%5D", DownloadURL: "https://github.com/adoptium/temurin17-binaries/releases/download/jdk-{version}/OpenJDK17U-jdk_x64_linux_hotspot_{version}.tar.gz", DefaultVersion: "17.0.13+7", InstallScript: `#!/bin/sh # JDK lazy install — Temurin OpenJDK JDK_VERSION="${TOOL_VERSION:-17.0.13+7}" JDK_MAJOR=$(echo "$JDK_VERSION" | cut -d. -f1) JDK_URL="https://github.com/adoptium/temurin${JDK_MAJOR}-binaries/releases/download/jdk-${JDK_VERSION}/OpenJDK${JDK_MAJOR}U-jdk_x64_linux_hotspot_${JDK_VERSION}.tar.gz" echo "[JDK] Downloading ${JDK_VERSION}..." cd /tmp curl -sL "$JDK_URL" -o jdk.tar.gz tar xzf jdk.tar.gz mkdir -p /usr/local/share mv jdk-${JDK_VERSION} /usr/local/share/jdk ln -sf /usr/local/share/jdk/bin/java /usr/local/bin/java ln -sf /usr/local/share/jdk/bin/javac /usr/local/bin/javac ln -sf /usr/local/share/jdk/bin/jar /usr/local/bin/jar rm -f jdk.tar.gz echo "[JDK] Installed: $(java -version 2>&1 | head -1)" `, SortOrder: 1, }, // ──── Maven — Java 构建 // Logan.Li ──── { Name: "maven", DisplayName: "Maven", Description: "Java 项目构建工具,流水线 Java 构建阶段使用(依赖 JDK)", Category: "build", InstallMethod: ToolLazy, BinaryName: "mvn", InstallPath: "/usr/local/bin/mvn", CheckCommand: "mvn --version 2>/dev/null", DownloadURL: "https://dlcdn.apache.org/maven/maven-3/{version}/binaries/apache-maven-{version}-bin.tar.gz", VersionURL: "https://dlcdn.apache.org/maven/maven-3/", DefaultVersion: "3.9.9", InstallScript: `#!/bin/sh # Maven lazy install — download & extract MAVEN_VERSION="${TOOL_VERSION:-3.9.9}" MAVEN_URL="https://dlcdn.apache.org/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz" echo "[Maven] Downloading ${MAVEN_VERSION}..." cd /tmp curl -sL "$MAVEN_URL" -o maven.tar.gz tar xzf maven.tar.gz mkdir -p /usr/local/share mv apache-maven-${MAVEN_VERSION} /usr/local/share/maven ln -sf /usr/local/share/maven/bin/mvn /usr/local/bin/mvn rm -f maven.tar.gz echo "[Maven] Installed: $(mvn --version 2>&1 | head -1)" `, SortOrder: 2, }, // ──── NVM — Node Version Manager // Logan.Li ──── { Name: "nvm", DisplayName: "NVM (Node 版本管理)", Description: "Node 版本管理器,流水线 Node.js 构建阶段使用(node 依赖此工具)", Category: "build", InstallMethod: ToolLazy, BinaryName: "nvm", InstallPath: "/usr/local/share/nvm/nvm.sh", CheckCommand: "source /usr/local/share/nvm/nvm.sh 2>/dev/null && nvm --version 2>/dev/null", VersionURL: "https://api.github.com/repos/nvm-sh/nvm/releases/latest", DefaultVersion: "0.40.1", InstallScript: `#!/bin/sh # NVM lazy install NVM_VERSION="${TOOL_VERSION:-0.40.1}" NVM_DIR="/usr/local/share/nvm" echo "[NVM] Downloading ${NVM_VERSION}..." mkdir -p "$NVM_DIR" curl -sL "https://raw.githubusercontent.com/nvm-sh/nvm/v${NVM_VERSION}/install.sh" | NVM_DIR="$NVM_DIR" bash # Make available system-wide via profile.d cat > /etc/profile.d/nvm.sh << 'NVM_PROFILE' export NVM_DIR="/usr/local/share/nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" NVM_PROFILE chmod +x /etc/profile.d/nvm.sh echo "[NVM] Installed: v${NVM_VERSION}" `, SortOrder: 3, }, // ──── Node.js — 通过 NVM 安装 // Logan.Li ──── { Name: "nodejs", DisplayName: "Node.js + npm", Description: "前端构建工具,流水线 npm install/build 阶段使用(通过 NVM 安装)", Category: "build", InstallMethod: ToolLazy, BinaryName: "node", InstallPath: "/usr/local/bin/node", CheckCommand: "node --version 2>/dev/null", DefaultVersion: "v22.14.0", InstallScript: `#!/bin/sh # Node.js via NVM install NODE_VERSION="${TOOL_VERSION:-v22.14.0}" echo "[Node.js] Installing via NVM ${NODE_VERSION}..." export NVM_DIR="/usr/local/share/nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" if ! command -v nvm >/dev/null 2>&1; then # NVM not found — install it first echo "[Node.js] NVM not found, installing NVM first..." curl -sL "https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh" | NVM_DIR="$NVM_DIR" bash \. "$NVM_DIR/nvm.sh" fi nvm install "${NODE_VERSION#v}" nvm alias default "${NODE_VERSION#v}" nvm use "${NODE_VERSION#v}" # Symlink for system-wide access ln -sf "$NVM_DIR/versions/node/$(nvm version)/bin/node" /usr/local/bin/node ln -sf "$NVM_DIR/versions/node/$(nvm version)/bin/npm" /usr/local/bin/npm ln -sf "$NVM_DIR/versions/node/$(nvm version)/bin/npx" /usr/local/bin/npx npm config set registry https://registry.npmmirror.com echo "[Node.js] Installed: $(node --version) / npm $(npm --version)" `, SortOrder: 4, }, // ──── Git — 版本控制(预装)// Logan.Li ──── { Name: "git", DisplayName: "Git", Description: "版本控制工具,流水线代码拉取使用", Category: "build", InstallMethod: ToolPreinstall, BinaryName: "git", InstallPath: "/usr/bin/git", CheckCommand: "git --version 2>/dev/null", SortOrder: 5, }, // ──── Docker CLI — 容器构建(预装)// Logan.Li ──── { Name: "docker-cli", DisplayName: "Docker CLI", Description: "Docker 命令行工具,流水线镜像构建/推送使用", Category: "deploy", InstallMethod: ToolPreinstall, BinaryName: "docker", InstallPath: "/usr/bin/docker", CheckCommand: "docker --version 2>/dev/null", SortOrder: 6, }, } }