Files
aiops/backend/internal/models/deploy_tool.go
T
attacker 451f65f9c8 feat: 路由改名/kubectl预装/导出YAML精简/Service标签页优化/Alpine镜像源换清华
- 路由 /deploy/credentials → /deploy/configureTools
- kubectl 改为预装(Dockerfile) + 种子数据 preinstall
- 导出YAML精简: Service/Ingress/Deployment 全面清理运行时字段
- DeploymentEditService 只显示匹配 selector 的关联 Service
- Alpine 镜像源 mirrors.aliyun.com → mirrors.tuna.tsinghua.edu.cn
2026-06-23 02:22:52 +00:00

189 lines
8.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package models
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)
// ============================================
// PredefinedTools 返回系统预置的工具配置列表
func PredefinedTools() []DeployTool {
return []DeployTool{
{
Name: "kubectl",
DisplayName: "Kubectl",
Description: "Kubernetes 命令行工具,用于 K8s 集群交互(已预装在镜像中)",
Category: "k8s",
InstallMethod: ToolPreinstall,
BinaryName: "kubectl",
InstallPath: "/usr/local/bin/kubectl",
CheckCommand: "kubectl version --client --short 2>/dev/null || kubectl version --client 2>/dev/null",
VersionURL: "https://dl.k8s.io/release/stable.txt",
DownloadURL: "https://dl.k8s.io/release/{version}/bin/linux/amd64/kubectl",
DefaultVersion: "v1.32.0",
SortOrder: 1,
},
{
Name: "maven",
DisplayName: "Maven",
Description: "Java 项目构建工具,流水线 Java 构建阶段使用",
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,
},
{
Name: "nodejs",
DisplayName: "Node.js + npm",
Description: "前端构建工具,流水线 npm install/build 阶段使用",
Category: "build",
InstallMethod: ToolLazy,
BinaryName: "node",
InstallPath: "/usr/local/bin/node",
CheckCommand: "node --version 2>/dev/null",
DownloadURL: "https://nodejs.org/dist/{version}/node-{version}-linux-x64.tar.xz",
VersionURL: "https://nodejs.org/dist/latest-v22.x/",
DefaultVersion: "v22.14.0",
InstallScript: `#!/bin/sh
# Node.js lazy install — download & extract
NODE_VERSION="${TOOL_VERSION:-v22.14.0}"
NODE_URL="https://nodejs.org/dist/${NODE_VERSION}/node-${NODE_VERSION}-linux-x64.tar.xz"
echo "[Node.js] Downloading ${NODE_VERSION}..."
cd /tmp
curl -sL "$NODE_URL" -o node.tar.xz
tar xJf node.tar.xz
mkdir -p /usr/local/share
mv node-${NODE_VERSION}-linux-x64 /usr/local/share/node
ln -sf /usr/local/share/node/bin/node /usr/local/bin/node
ln -sf /usr/local/share/node/bin/npm /usr/local/bin/npm
ln -sf /usr/local/share/node/bin/npx /usr/local/bin/npx
rm -f node.tar.xz
npm config set registry https://registry.npmmirror.com
echo "[Node.js] Installed: $(node --version) / npm $(npm --version)"
`,
SortOrder: 3,
},
{
Name: "helm",
DisplayName: "Helm",
Description: "Kubernetes 包管理工具,Chart 部署使用",
Category: "k8s",
InstallMethod: ToolLazy,
BinaryName: "helm",
InstallPath: "/usr/local/bin/helm",
CheckCommand: "helm version --short 2>/dev/null",
VersionURL: "https://get.helm.sh/",
DownloadURL: "https://get.helm.sh/helm-{version}-linux-amd64.tar.gz",
DefaultVersion: "v3.16.0",
InstallScript: `#!/bin/sh
# Helm lazy install — download & extract
HELM_VERSION="${TOOL_VERSION:-v3.16.0}"
HELM_URL="https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz"
echo "[Helm] Downloading ${HELM_VERSION}..."
cd /tmp
curl -sL "$HELM_URL" -o helm.tar.gz
tar xzf helm.tar.gz
mv linux-amd64/helm /usr/local/bin/helm
rm -rf helm.tar.gz linux-amd64
echo "[Helm] Installed: $(helm version --short 2>&1)"
`,
SortOrder: 4,
},
{
Name: "git",
DisplayName: "Git",
Description: "版本控制工具,流水线代码拉取使用",
Category: "build",
InstallMethod: ToolPreinstall,
BinaryName: "git",
InstallPath: "/usr/bin/git",
CheckCommand: "git --version 2>/dev/null",
SortOrder: 5,
},
{
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,
},
}
}