服务器批量导入: - 后端 BatchCreateServers handler: 逐条校验/查重/GB→MB转换/错误明细 - 前端 CSV 解析器 + IP校验 + 模板下载 + 真实API导入 - 删除孤立占位组件 BatchImport.tsx SQL审计: - 修复 TableName 字段与 GORM 接口冲突 → TargetTable - 补全路由注册 + AutoMigrate + 种子数据 审批流程增强 + 告警/菜单微调 // Logan.Li
114 lines
4.8 KiB
Go
114 lines
4.8 KiB
Go
package main
|
|
|
|
import (
|
|
"ezops/internal/models"
|
|
|
|
"go.uber.org/zap"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// seedData 初始化种子数据(admin用户 + LDAP配置)
|
|
func seedData(db *gorm.DB) {
|
|
// 1. 创建默认 admin 用户(仅首次)
|
|
var userCount int64
|
|
db.Model(&models.User{}).Count(&userCount)
|
|
if userCount == 0 {
|
|
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("123456"), bcrypt.DefaultCost)
|
|
admin := models.User{
|
|
Username: "admin",
|
|
Password: string(hashedPassword),
|
|
Email: "admin@ezops.local",
|
|
RealName: "管理员",
|
|
Status: 1,
|
|
IsAdmin: true,
|
|
AuthSource: "local",
|
|
}
|
|
if err := db.Create(&admin).Error; err != nil {
|
|
zap.L().Warn("failed to seed admin user", zap.Error(err))
|
|
} else {
|
|
zap.L().Info("seeded default admin user (admin/123456)")
|
|
}
|
|
}
|
|
|
|
// 2. 创建默认 LDAP 配置
|
|
var ldapCount int64
|
|
db.Model(&models.LDAPConfig{}).Count(&ldapCount)
|
|
if ldapCount == 0 {
|
|
port := 389
|
|
useLDAPS := false
|
|
searchFilter := "(objectClass=inetOrgPerson)"
|
|
userMapping := `{"username":"uid","email":"mail","display_name":"cn"}`
|
|
ldapCfg := models.LDAPConfig{
|
|
LDAPHost: "8.217.205.92",
|
|
LDAPPort: &port,
|
|
UseLDAPS: &useLDAPS,
|
|
AdminDN: "cn=admin,dc=opsx,dc=vip",
|
|
AdminPassword: "LQl2GX7EEg2N",
|
|
BaseDN: "dc=opsx,dc=vip",
|
|
SearchFilter: &searchFilter,
|
|
UserMapping: &userMapping,
|
|
}
|
|
if err := db.Create(&ldapCfg).Error; err != nil {
|
|
zap.L().Warn("failed to seed LDAP config", zap.Error(err))
|
|
} else {
|
|
zap.L().Info("seeded default LDAP config (8.217.205.92:389)")
|
|
}
|
|
}
|
|
|
|
// 3. 创建默认审批模板(资源申请)
|
|
var tplCount int64
|
|
db.Model(&models.ApprovalTemplate{}).Count(&tplCount)
|
|
if tplCount == 0 {
|
|
templates := []models.ApprovalTemplate{
|
|
{
|
|
Name: "云服务器申请",
|
|
Code: "server_apply",
|
|
Description: "申请云服务器资源(ECS/EC2),包含规格、数量、用途等信息",
|
|
Type: "server_apply",
|
|
FormSchema: `{
|
|
"fields": [
|
|
{"key": "provider", "label": "云厂商", "type": "select", "options": ["阿里云","腾讯云","华为云","AWS"], "required": true},
|
|
{"key": "region", "label": "地域", "type": "text", "placeholder": "如 cn-hangzhou", "required": true},
|
|
{"key": "spec", "label": "规格", "type": "select", "options": ["2C4G","4C8G","8C16G","16C32G","32C64G"], "required": true},
|
|
{"key": "disk", "label": "系统盘(GB)", "type": "number", "default": 40, "required": true},
|
|
{"key": "count", "label": "数量(台)", "type": "number", "default": 1, "required": true},
|
|
{"key": "duration", "label": "时长", "type": "select", "options": ["1个月","3个月","6个月","1年"], "required": true},
|
|
{"key": "purpose", "label": "用途说明", "type": "textarea", "required": true}
|
|
]
|
|
}`,
|
|
FlowConfig: `[{"name":"直接主管","approver_role":"team_lead"},{"name":"运维负责人","approver_role":"ops_manager"}]`,
|
|
Enabled: true,
|
|
},
|
|
{
|
|
Name: "应用部署申请",
|
|
Code: "app_deploy",
|
|
Description: "申请新应用部署,包含 Git 地址、端口、Nacos 等配置信息",
|
|
Type: "app_deploy",
|
|
FormSchema: `{
|
|
"fields": [
|
|
{"key": "app_name", "label": "应用名称", "type": "text", "placeholder": "如 user-service", "required": true},
|
|
{"key": "git_url", "label": "Git 地址", "type": "text", "placeholder": "如 https://github.com/org/repo.git", "required": true},
|
|
{"key": "branch", "label": "分支", "type": "text", "default": "main", "required": true},
|
|
{"key": "language", "label": "开发语言", "type": "select", "options": ["Java","Go","Python","Node.js","C++"], "required": true},
|
|
{"key": "http_port", "label": "HTTP 端口", "type": "number", "placeholder": "如 8080", "required": false},
|
|
{"key": "grpc_port", "label": "gRPC 端口", "type": "number", "placeholder": "如 9090", "required": false},
|
|
{"key": "nacos_addr", "label": "Nacos 地址", "type": "text", "placeholder": "如 nacos.opsx.vip:8848", "required": true},
|
|
{"key": "nacos_namespace", "label": "Nacos 命名空间", "type": "text", "placeholder": "如 production", "required": false},
|
|
{"key": "env", "label": "部署环境", "type": "select", "options": ["dev","staging","prod"], "required": true},
|
|
{"key": "replicas", "label": "副本数", "type": "number", "default": 2, "required": true},
|
|
{"key": "remark", "label": "备注", "type": "textarea", "required": false}
|
|
]
|
|
}`,
|
|
FlowConfig: `[{"name":"技术负责人","approver_role":"tech_lead"},{"name":"运维负责人","approver_role":"ops_manager"}]`,
|
|
Enabled: true,
|
|
},
|
|
}
|
|
if err := db.Create(&templates).Error; err != nil {
|
|
zap.L().Warn("failed to seed approval templates", zap.Error(err))
|
|
} else {
|
|
zap.L().Info("seeded default approval templates (server_apply + app_deploy)")
|
|
}
|
|
}
|
|
}
|