This commit is contained in:
2026-02-15 07:18:14 +08:00
parent d7cd899983
commit 8a7bf8a39c
39 changed files with 1578 additions and 2868 deletions

View File

@@ -1,128 +1,60 @@
package config
// SunHPC 主配置
type SunHPCConfig struct {
Hostname string `yaml:"hostname"`
MOTD string `yaml:"motd"`
Sysctl map[string]string `yaml:"sysctl"`
SELinux string `yaml:"selinux"` // enforcing, permissive, disabled
SSH SSHConfig `yaml:"ssh"`
}
import (
"os"
"path/filepath"
type SSHConfig struct {
PermitRootLogin string `yaml:"permit_root_login"`
PasswordAuth string `yaml:"password_authentication"`
}
"gopkg.in/yaml.v3"
)
func DefaultSunHPC() *SunHPCConfig {
return &SunHPCConfig{
Hostname: "sunhpc-master",
MOTD: "Welcome to SunHPC Cluster\n",
Sysctl: map[string]string{
"net.ipv4.ip_forward": "1",
"vm.swappiness": "10",
// DefaultConfig 返回 SunHPC 的默认配置结构体
func DefaultConfig() *Config {
return &Config{
DB: DBConfig{
Type: "sqlite",
Path: "/var/lib/sunhpc", // SQLite 数据库存放目录
Name: "sunhpc.db", // 数据库文件名
User: "", // SQLite 不需要
Password: "",
Host: "",
Port: 0,
Socket: "",
Verbose: false,
},
SELinux: "enforcing",
SSH: SSHConfig{
PermitRootLogin: "yes",
PasswordAuth: "yes",
Log: LogConfig{
Level: "info",
Format: "text", // or "json"
Output: "stdout",
FilePath: "/var/log/sunhpc/sunhpc.log",
},
Cluster: ClusterConfig{
Name: "default-cluster",
AdminEmail: "admin@example.com",
TimeZone: "Asia/Shanghai",
NodePrefix: "node",
},
}
}
// Nodes 节点配置
type NodesConfig struct {
Nodes []Node `yaml:"nodes"`
}
type Node struct {
Hostname string `yaml:"hostname"`
MAC string `yaml:"mac"`
IP string `yaml:"ip"`
Role string `yaml:"role"` // master, compute, login
}
func DefaultNodes() *NodesConfig {
return &NodesConfig{
Nodes: []Node{
{Hostname: "master", MAC: "00:11:22:33:44:55", IP: "192.168.1.1", Role: "master"},
},
// WriteDefaultConfig 将默认配置写入指定路径
// 如果目录不存在,会自动创建(需有权限)
// 如果文件已存在且非空,会返回错误(除非调用方先删除)
func WriteDefaultConfig(path string) error {
// 确保目录存在
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
}
// Network 网络配置
type NetworkConfig struct {
Interface string `yaml:"interface"`
Subnet string `yaml:"subnet"`
Netmask string `yaml:"netmask"`
Gateway string `yaml:"gateway"`
DNSServers []string `yaml:"dns_servers"`
}
// 生成默认配置
cfg := DefaultConfig()
func DefaultNetwork() *NetworkConfig {
return &NetworkConfig{
Interface: "eth0",
Subnet: "192.168.1.0",
Netmask: "255.255.255.0",
Gateway: "192.168.1.1",
DNSServers: []string{"8.8.8.8", "114.114.114.114"},
// 序列化为 YAML
data, err := yaml.Marshal(cfg)
if err != nil {
return err
}
}
// Disks 磁盘配置
type DisksConfig struct {
Disks []Disk `yaml:"disks"`
}
type Disk struct {
Device string `yaml:"device"`
Mount string `yaml:"mount"`
FSType string `yaml:"fstype"`
Options string `yaml:"options"`
}
func DefaultDisks() *DisksConfig {
return &DisksConfig{
Disks: []Disk{
{Device: "/dev/sda1", Mount: "/", FSType: "ext4", Options: "defaults"},
},
}
}
// Services 服务配置
type ServicesConfig struct {
HTTPD Service `yaml:"httpd"`
TFTPD Service `yaml:"tftpd"`
DHCPD Service `yaml:"dhcpd"`
}
type Service struct {
Enabled bool `yaml:"enabled"`
Config string `yaml:"config,omitempty"`
}
func DefaultServices() *ServicesConfig {
return &ServicesConfig{
HTTPD: Service{Enabled: true},
TFTPD: Service{Enabled: true},
DHCPD: Service{Enabled: true},
}
}
// Firewall 防火墙配置
type FirewallConfig struct {
DefaultPolicy string `yaml:"default_policy"`
Rules []string `yaml:"rules"`
}
func DefaultFirewall() *FirewallConfig {
return &FirewallConfig{
DefaultPolicy: "DROP",
Rules: []string{
"-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT",
"-A INPUT -p icmp -j ACCEPT",
"-A INPUT -i lo -j ACCEPT",
"-A INPUT -p tcp --dport 22 -j ACCEPT",
},
}
// 写入文件0644 权限)
return os.WriteFile(path, data, 0644)
}