添加数据到数据库,但无法插入,临时提交保存
This commit is contained in:
@@ -2,9 +2,11 @@ package wizard
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"sunhpc/pkg/database"
|
||||
"sunhpc/pkg/info"
|
||||
"sunhpc/pkg/logger"
|
||||
"sunhpc/pkg/utils"
|
||||
|
||||
@@ -68,6 +70,23 @@ type ConfigMapping struct {
|
||||
} `toml:"private"`
|
||||
}
|
||||
|
||||
type IPMaskInfo struct {
|
||||
NetworkAddress string // 网络地址 192.168.1.0
|
||||
CIDR string // CIDR 格式 192.168.1.0/24
|
||||
IPAddress string // IP 地址 192.168.1.100
|
||||
MacAddress string // MAC 地址 00:11:22:33:44:55
|
||||
Netmask string // 子网掩码 255.255.255.0
|
||||
PrefixLength int // 前缀长度 24
|
||||
}
|
||||
|
||||
type AttrItem struct {
|
||||
Key string
|
||||
Value string
|
||||
Shadow string
|
||||
Category int
|
||||
Catindex int
|
||||
}
|
||||
|
||||
func NewConfigWithDefault() *ConfigMapping {
|
||||
return &ConfigMapping{
|
||||
Title: "Cluster Configuration",
|
||||
@@ -197,32 +216,111 @@ func loadConfig() (*ConfigMapping, error) {
|
||||
// saveConfig 入口函数:保存所有配置到数据库
|
||||
func (m *model) saveConfig() error {
|
||||
|
||||
conn, err := database.GetDB() // 假设database包已实现getDB()获取连接
|
||||
if err != nil {
|
||||
logger.Errorf("Database connection failed: %v", err)
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
m.force = false // 初始化全量覆盖标识
|
||||
|
||||
config, err := loadConfig()
|
||||
c, err := loadConfig()
|
||||
if err != nil {
|
||||
logger.Debugf("[DEBUG] Load config file failed: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
t_value := m.config.PrivateIPAddress
|
||||
c_value := config.Private.PrivateAddress
|
||||
// 合并配置项
|
||||
result := make(map[string]string)
|
||||
|
||||
logger.Debugf("t_value: %s\n", t_value)
|
||||
logger.Debugf("c_value: %s\n", c_value)
|
||||
// base 配置
|
||||
result["country"] = mergeValue(m.config.Country, c.Base.Country)
|
||||
result["state"] = mergeValue(m.config.State, c.Base.State)
|
||||
result["city"] = mergeValue(m.config.City, c.Base.City)
|
||||
result["contact"] = mergeValue(m.config.Contact, c.Base.Contact)
|
||||
result["homepage"] = mergeValue(m.config.HomePage, c.Base.HomePage)
|
||||
result["cluster_name"] = mergeValue(m.config.ClusterName, c.Base.ClusterName)
|
||||
result["license"] = c.Base.License
|
||||
result["distribution"] = c.Base.Distribution
|
||||
result["timezone"] = mergeValue(m.config.Timezone, c.Base.Timezone)
|
||||
result["base_dir"] = c.Base.BaseDir
|
||||
result["work_dir"] = c.Base.WorkDir
|
||||
result["distro_dir"] = mergeValue(m.config.DistroDir, c.Base.DistroDir)
|
||||
result["partition"] = c.Base.Partition
|
||||
|
||||
logger.Debugf("config.Base.ClusterName: %s\n", config.Base.ClusterName)
|
||||
// safe 配置
|
||||
result["safe_port"] = c.Base.SafePort
|
||||
result["safe_dirs"] = c.Base.SafeDirs
|
||||
result["safe_security"] = c.Base.SafeSecurity
|
||||
|
||||
// plugin 配置
|
||||
result["plugin_dirs"] = c.Base.PluginDirs
|
||||
result["plugin_port"] = c.Base.PluginPort
|
||||
|
||||
// monitor 配置
|
||||
result["ganglia_addr"] = c.Base.GangliaAddr
|
||||
|
||||
// public 配置
|
||||
result["public_hostname"] = mergeValue(m.config.PublicHostname, c.Public.PublicHostname)
|
||||
result["public_interface"] = mergeValue(m.config.PublicInterface, c.Public.PublicInterface)
|
||||
result["public_address"] = mergeValue(m.config.PublicIPAddress, c.Public.PublicAddress)
|
||||
result["public_netmask"] = mergeValue(m.config.PublicNetmask, c.Public.PublicNetmask)
|
||||
result["public_gateway"] = mergeValue(m.config.PublicGateway, c.Public.PublicGateway)
|
||||
|
||||
// 获取公网网络信息
|
||||
publicIface := mergeValue(m.config.PublicInterface, c.Public.PublicInterface)
|
||||
publicInfo, err := GetNetworkInfo(
|
||||
publicIface, c.Public.PublicAddress, c.Public.PublicNetmask)
|
||||
if err != nil {
|
||||
logger.Debugf("[DEBUG] Get public interface %s IP mask info failed: %v",
|
||||
publicIface, err)
|
||||
}
|
||||
|
||||
result["public_network"] = publicInfo.NetworkAddress
|
||||
result["public_domain"] = mergeValue(m.config.PublicDomain, c.Public.PublicDomain)
|
||||
|
||||
result["public_cidr"] = mergeValue(c.Public.PublicCIDR, publicInfo.CIDR)
|
||||
result["public_dns"] = c.Public.PublicDNS
|
||||
result["public_mac"] = publicInfo.MacAddress
|
||||
result["public_mtu"] = mergeValue(m.config.PublicMTU, c.Public.PublicMTU)
|
||||
result["public_ntp"] = c.Public.PublicNTP
|
||||
|
||||
// private 配置
|
||||
// 获取内网网络信息
|
||||
privateIface := mergeValue(m.config.PrivateInterface, c.Private.PrivateInterface)
|
||||
privateInfo, err := GetNetworkInfo(
|
||||
privateIface, c.Private.PrivateAddress, c.Private.PrivateNetmask)
|
||||
if err != nil {
|
||||
logger.Debugf("[DEBUG] Get private interface %s IP mask info failed: %v",
|
||||
privateIface, err)
|
||||
}
|
||||
result["private_hostname"] = mergeValue(m.config.PrivateHostname, c.Private.PrivateHostname)
|
||||
result["private_interface"] = mergeValue(m.config.PrivateInterface, c.Private.PrivateInterface)
|
||||
result["private_address"] = mergeValue(m.config.PrivateIPAddress, c.Private.PrivateAddress)
|
||||
result["private_netmask"] = mergeValue(m.config.PrivateNetmask, c.Private.PrivateNetmask)
|
||||
result["private_network"] = privateInfo.NetworkAddress
|
||||
result["private_domain"] = mergeValue(m.config.PrivateDomain, c.Private.PrivateDomain)
|
||||
result["private_cidr"] = mergeValue(c.Private.PrivateCIDR, privateInfo.CIDR)
|
||||
result["private_mac"] = privateInfo.MacAddress
|
||||
result["private_mtu"] = mergeValue(m.config.PrivateMTU, c.Private.PrivateMTU)
|
||||
|
||||
// pxe 配置
|
||||
result["next_server"] = mergeValue(privateInfo.IPAddress, c.Pxelinux.NextServer)
|
||||
result["pxe_filename"] = c.Pxelinux.PxeFilename
|
||||
result["pxelinux_dir"] = c.Pxelinux.PxeLinuxDir
|
||||
result["boot_args"] = c.Pxelinux.BootArgs
|
||||
|
||||
// 插入数据到数据库
|
||||
if err := insertDataToDB(result); err != nil {
|
||||
logger.Debugf("[DEBUG] Insert config data to database failed: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Debugf("Result: %v", result)
|
||||
return nil
|
||||
}
|
||||
|
||||
func mergeValue(tui_value, cfg_value string) string {
|
||||
if tui_value == "" {
|
||||
return cfg_value
|
||||
}
|
||||
return tui_value
|
||||
}
|
||||
|
||||
// 获取系统网络接口
|
||||
func getNetworkInterfaces() []string {
|
||||
// 实现获取系统网络接口的逻辑
|
||||
@@ -247,3 +345,226 @@ func getNetworkInterfaces() []string {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func GetNetworkInfo(iface, ip, mask string) (*IPMaskInfo, error) {
|
||||
|
||||
logger.Debugf("Get Network %s, IP %s, mask %s", iface, ip, mask)
|
||||
|
||||
// 解析IP
|
||||
ipAddr := net.ParseIP(ip)
|
||||
if ipAddr == nil {
|
||||
logger.Debugf("Invalid IP address: %s", ip)
|
||||
return nil, fmt.Errorf("invalid IP address: %s", ip)
|
||||
}
|
||||
|
||||
// 解析子网掩码
|
||||
maskAddr := net.ParseIP(mask)
|
||||
if maskAddr == nil {
|
||||
logger.Debugf("Invalid subnet mask: %s", mask)
|
||||
return nil, fmt.Errorf("invalid subnet mask: %s", mask)
|
||||
}
|
||||
|
||||
// 确保是IPv4地址
|
||||
ipv4 := ipAddr.To4()
|
||||
maskv4 := maskAddr.To4()
|
||||
if ipv4 == nil || maskv4 == nil {
|
||||
logger.Debugf("Only support IPv4 address")
|
||||
return nil, fmt.Errorf("only support IPv4 address")
|
||||
}
|
||||
|
||||
// 计算网络地址 (IP & 子网掩码)
|
||||
network := make([]byte, 4)
|
||||
for i := 0; i < 4; i++ {
|
||||
network[i] = ipv4[i] & maskv4[i]
|
||||
}
|
||||
networkAddr := fmt.Sprintf(
|
||||
"%d.%d.%d.%d", network[0], network[1], network[2], network[3])
|
||||
|
||||
// 计算前缀长度
|
||||
prefixLen := 0
|
||||
for i := 0; i < 4; i++ {
|
||||
for j := 7; j >= 0; j-- {
|
||||
if maskv4[i]&(1<<uint(j)) != 0 {
|
||||
prefixLen++
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 计算CIDR格式
|
||||
cidr := fmt.Sprintf("%s/%d", networkAddr, prefixLen)
|
||||
|
||||
var mac string
|
||||
// 获取Mac地址
|
||||
ifaceName, err := net.InterfaceByName(iface)
|
||||
if err == nil {
|
||||
mac = ifaceName.HardwareAddr.String()
|
||||
if mac == "" {
|
||||
logger.Debugf("Network interface %s has no MAC address", iface)
|
||||
}
|
||||
} else {
|
||||
logger.Debugf("Invalid network interface: %s", iface)
|
||||
mac = ""
|
||||
}
|
||||
|
||||
return &IPMaskInfo{
|
||||
NetworkAddress: networkAddr,
|
||||
CIDR: cidr,
|
||||
IPAddress: ip,
|
||||
MacAddress: mac,
|
||||
Netmask: mask,
|
||||
PrefixLength: prefixLen,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func insertDataToDB(result map[string]string) error {
|
||||
|
||||
insertData := []string{}
|
||||
|
||||
infos := info.GetSystemInfo()
|
||||
|
||||
// initrd 配置
|
||||
bootver := fmt.Sprintf("%s-%s", info.Version, infos.Arch)
|
||||
vmlinuz := fmt.Sprintf("vmlinuz-%s", bootver)
|
||||
initrds := fmt.Sprintf("initrd-%s", bootver)
|
||||
insArgs := fmt.Sprintf("%s inst.ks.sendmac ksdevice=bootif", result["boot_args"])
|
||||
resArgs := fmt.Sprintf("%s rescue", result["boot_args"])
|
||||
lesArgs := fmt.Sprintf("%s vnc vncip=%s vncpassword=sunhpc", result["boot_args"], result["private_address"])
|
||||
|
||||
bootaction := []string{
|
||||
fmt.Sprintf("insert into bootactions values (1, 'install', '%s', '%s', '%s');",
|
||||
vmlinuz, initrds, insArgs),
|
||||
"insert into bootactions values (2, 'os', 'localboot 0', '', '');",
|
||||
"insert into bootactions values (3, 'memtest', 'kernel memtest', '', '');",
|
||||
fmt.Sprintf("insert into bootactions values (4, 'install headless', '%s', '%s', '%s');",
|
||||
vmlinuz, initrds, lesArgs),
|
||||
fmt.Sprintf("insert into bootactions values (5, 'rescue', '%s', '%s', '%s');",
|
||||
vmlinuz, initrds, resArgs),
|
||||
"insert into bootactions values (6, 'pxeflash', 'kernel memdisk bigraw', 'pxeflash.img', 'keeppxe');",
|
||||
}
|
||||
insertData = append(insertData, bootaction...)
|
||||
|
||||
attrs := GetAttrs(result)
|
||||
for _, item := range attrs {
|
||||
key := item.Key
|
||||
value := item.Value
|
||||
shadow := item.Shadow
|
||||
category := item.Category
|
||||
catindex := item.Catindex
|
||||
insertData = append(insertData,
|
||||
fmt.Sprintf("insert into attributes values ('%s', '%s', '%s', %d, %d);",
|
||||
key, value, shadow, category, catindex))
|
||||
}
|
||||
|
||||
nodes := []string{
|
||||
fmt.Sprintf(
|
||||
"insert into nodes values (1, '%s', '%d', 0, 0, '%s', '%s', '', 'install');",
|
||||
result["private_hostname"],
|
||||
info.GetSystemInfo().NumCPU,
|
||||
info.GetSystemInfo().Arch,
|
||||
info.GetSystemInfo().OS),
|
||||
fmt.Sprintf(
|
||||
"insert into networks values (1, 1, '%s', '%s', '%s', '%s', '2');",
|
||||
result["public_mac"],
|
||||
result["public_address"],
|
||||
result["private_hostname"],
|
||||
result["public_interface"]),
|
||||
fmt.Sprintf(
|
||||
"insert into networks values (2, 1, '%s', '%s', '%s', '%s', '1');",
|
||||
result["private_mac"],
|
||||
result["private_address"],
|
||||
result["private_hostname"],
|
||||
result["private_interface"]),
|
||||
|
||||
fmt.Sprintf(
|
||||
"insert into subnets values (1, 'private', '%s', '%s', '%s', '%s', '1');",
|
||||
result["private_domain"],
|
||||
result["private_network"],
|
||||
result["private_netmask"],
|
||||
result["private_mtu"]),
|
||||
fmt.Sprintf(
|
||||
"insert into subnets values (2, 'public', '%s', '%s', '%s', '%s', '0');",
|
||||
result["public_domain"],
|
||||
result["public_network"],
|
||||
result["public_netmask"],
|
||||
result["public_mtu"]),
|
||||
}
|
||||
insertData = append(insertData, nodes...)
|
||||
|
||||
if err := database.ExecWithTransaction(insertData); err != nil {
|
||||
logger.Debugf("[DEBUG] Insert config data to database failed: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAttrs(results map[string]string) []AttrItem {
|
||||
attrs := []AttrItem{
|
||||
{Key: "Info_CertificateCountry", Value: results["country"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Info_CertificateState", Value: results["state"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Info_CertificateCity", Value: results["city"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Info_CertificateOrganization", Value: "DLHP", Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Info_Contact", Value: results["contact"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Info_ClusterHostname", Value: results["ClusterHostname"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_WorkDir", Value: results["work_dir"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_DistroDir", Value: results["distro_dir"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_Partition", Value: results["partition"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
|
||||
{Key: "Kickstart_PublicHostname", Value: results["public_hostname"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PublicInterface", Value: results["public_interface"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PublicAddress", Value: results["public_address"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PublicMacAddr", Value: results["public_mac"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PublicNetmask", Value: results["public_netmask"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PublicGateway", Value: results["public_gateway"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PublicNetwork", Value: results["public_network"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PublicDomain", Value: results["public_domain"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PublicCIDR", Value: results["public_cidr"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PublicDNS", Value: results["public_dns"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PublicMTU", Value: results["public_mtu"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PublicNTP", Value: results["public_ntp"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
|
||||
{Key: "Kickstart_PrivateHostname", Value: results["private_hostname"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PrivateInterface", Value: results["private_interface"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PrivateAddress", Value: results["private_address"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PrivateMacAddr", Value: results["private_mac"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PrivateNetmask", Value: results["private_netmask"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PrivateGateway", Value: results["private_gateway"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PrivateNetwork", Value: results["private_network"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PrivateDomain", Value: results["private_domain"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PrivateCIDR", Value: results["private_cidr"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_PrivateMTU", Value: results["private_mtu"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
|
||||
{Key: "Kickstart_Timezone", Value: results["timezone"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_Bootargs", Value: results["boot_args"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_Distribution", Value: results["distribution"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Kickstart_BaseDir", Value: results["base_dir"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
|
||||
{Key: "SafePort", Value: results["safe_port"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "SafeDirs", Value: results["safe_dirs"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "SafeSecurity", Value: results["safe_security"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
|
||||
{Key: "Plugin_dirs", Value: results["plugin_dirs"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Plugin_port", Value: results["plugin_port"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
|
||||
{Key: "Ganglia_addr", Value: results["ganglia_addr"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
|
||||
{Key: "Sunhpc_version", Value: results["sunhpc_version"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
|
||||
{Key: "DHCP_filename", Value: results["pxe_filename"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "DHCP_nextserver", Value: results["next_server"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "Pxelinuxdir", Value: results["pxelinux_dir"], Shadow: "false", Category: 1, Catindex: 1},
|
||||
|
||||
{Key: "Kickstartable", Value: "yes", Shadow: "false", Category: 3, Catindex: 4},
|
||||
{Key: "Kickstartable", Value: "yes", Shadow: "false", Category: 3, Catindex: 5},
|
||||
{Key: "Kickstartable", Value: "yes", Shadow: "false", Category: 3, Catindex: 6},
|
||||
{Key: "Kickstartable", Value: "no", Shadow: "false", Category: 3, Catindex: 7},
|
||||
{Key: "Kickstartable", Value: "no", Shadow: "false", Category: 3, Catindex: 8},
|
||||
{Key: "Kickstartable", Value: "yes", Shadow: "false", Category: 3, Catindex: 9},
|
||||
{Key: "Kickstartable", Value: "yes", Shadow: "false", Category: 3, Catindex: 10},
|
||||
|
||||
{Key: "Managed", Value: "true", Shadow: "false", Category: 1, Catindex: 1},
|
||||
{Key: "OS", Value: "linux", Shadow: "false", Category: 1, Catindex: 1},
|
||||
}
|
||||
return attrs
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user