Compare commits

5 Commits

Author SHA1 Message Date
8bc4f4fe04 Tui 模块开发完成,数据正常写入数据库 2026-03-06 16:33:08 +08:00
3f5e333a4d 添加数据到数据库,但无法插入,临时提交保存 2026-03-06 00:29:34 +08:00
f7dcfa4e7d Tui fix focus 2026-02-28 19:29:17 +08:00
13beeb67d1 fix tui quit display 2026-02-27 23:25:43 +08:00
d4e214fe23 Tui 重构代码逻辑 2026-02-27 22:52:15 +08:00
18 changed files with 2542 additions and 1167 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
main
sunhpc
testgui

31
build-sunhpc.sh Executable file
View File

@@ -0,0 +1,31 @@
#!/bin/bash
set -e # 出错立即退出,便于定位问题
# ========== 核心配置(根据你的项目修改) ==========
# 1. 必须和go.mod中的module名称一致关键
MODULE_NAME="sunhpc"
# 2. 编译的入口文件路径你的main.go在cmd/sunhpc下
ENTRY_FILE="./cmd/sunhpc/main.go"
# 3. 输出的可执行文件名称
APP_NAME="sunhpc"
# 4. 自定义版本号
APP_VERSION="v1.0.0"
# ========== 获取Git和编译信息兼容无Git环境 ==========
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
BUILD_TIME=$(date +"%Y-%m-%d-%H:%M:%S")
# ========== 编译核心修复ldflags格式 ==========
# 关键:去掉反斜杠,用单层双引号包裹,内部换行分隔参数
go build -ldflags "
-X ${MODULE_NAME}/pkg/info.Version=${APP_VERSION}
-X ${MODULE_NAME}/pkg/info.BuildTime=${BUILD_TIME}
-X ${MODULE_NAME}/pkg/info.GitCommit=${GIT_COMMIT}
-X ${MODULE_NAME}/pkg/info.GitBranch=${GIT_BRANCH}
" -o ${APP_NAME} ${ENTRY_FILE}
# ========== 验证提示 ==========
echo "- 执行文件:./${APP_NAME}"
echo "- 版本信息:${APP_VERSION} (Git: ${GIT_COMMIT} @ ${GIT_BRANCH})"
echo "- 编译时间:${BUILD_TIME}"

View File

@@ -1,66 +1,9 @@
package main
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"sunhpc/pkg/info"
)
// model 定义应用的状态
type model struct {
items []string // 列表数据
selectedIdx int // 当前选中的索引
}
// Init 初始化模型,返回初始命令(这里不需要,返回 nil
func (m model) Init() tea.Cmd {
return nil
}
// Update 处理用户输入和状态更新
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// 处理键盘输入
case tea.KeyMsg:
switch msg.String() {
// Tab 键:切换选中项(循环切换)
case "tab":
m.selectedIdx = (m.selectedIdx + 1) % len(m.items)
// Ctrl+C 或 q 键:退出程序
case "ctrl+c", "q":
return m, tea.Quit
}
}
return m, nil
}
// View 渲染界面
func (m model) View() string {
s := "网络接口列表(按 Tab 切换选择,按 q 退出)\n\n"
// 遍历列表项,渲染每一项
for i, item := range m.items {
// 标记当前选中的项
if i == m.selectedIdx {
s += fmt.Sprintf("→ %s (选中)\n", item)
} else {
s += fmt.Sprintf(" %s\n", item)
}
}
return s
}
func main() {
// 初始化模型,设置列表数据
initialModel := model{
items: []string{"eth0", "eth1", "eth2", "eth3"},
selectedIdx: 0, // 默认选中第一个项
}
// 启动 Bubble Tea 程序
p := tea.NewProgram(initialModel)
if _, err := p.Run(); err != nil {
fmt.Printf("程序运行出错: %v\n", err)
}
info.PrintAllInfo()
}

2
go.mod
View File

@@ -20,6 +20,7 @@ require (
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/BurntSushi/toml v1.6.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/colorprofile v0.4.1 // indirect
@@ -44,6 +45,7 @@ require (
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sagikazarmark/locafero v0.11.0 // indirect
github.com/sahilm/fuzzy v0.1.1 // indirect
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
github.com/spf13/afero v1.15.0 // indirect
github.com/spf13/cast v1.10.0 // indirect

4
go.sum
View File

@@ -1,5 +1,7 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
@@ -78,6 +80,8 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA=
github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w=
github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g=
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=

View File

@@ -35,7 +35,8 @@ func NewInitDBCmd() *cobra.Command {
defer db.Close()
if err := database.InitTables(db, force); err != nil {
return fmt.Errorf("数据库初始化失败: %w", err)
logger.Debug(err)
return err
}
// 测试数据库连接

View File

@@ -73,10 +73,28 @@ func InitConfigs() error {
return fmt.Errorf("创建默认配置文件失败: %w", err)
}
}
// 读取配置文件,根据配置文件内容初始化相关目录.
cfg, err := LoadConfig()
if err != nil {
return fmt.Errorf("读取配置文件失败: %w", err)
}
// 初始化日志
logger.Init(cfg.Log)
// 确保数据库目录存在
if err := os.MkdirAll(cfg.Database.Path, 0755); err != nil {
logger.Debugf("创建数据库目录 %s 失败: %v", cfg.Database.Path, err)
return err
}
logger.Debugf("创建数据库目录 %s 成功", cfg.Database.Path)
return nil
}
func createDefaultConfig(configPath string) error {
fmt.Printf("设置默认配置文件: %s\n", configPath)
defaultConfig := &Config{
Database: DatabaseConfig{
Path: utils.DefaultDBPath,
@@ -93,11 +111,6 @@ func createDefaultConfig(configPath string) error {
},
}
// 确保数据库目录存在
if err := os.MkdirAll(defaultConfig.Database.Path, 0755); err != nil {
return fmt.Errorf("创建数据库目录失败: %w", err)
}
// 序列号并写入
data, err := yaml.Marshal(defaultConfig)
if err != nil {
@@ -108,6 +121,7 @@ func createDefaultConfig(configPath string) error {
// ----------------------------------- 配置加载(只加载一次) -----------------------------------
func LoadConfig() (*Config, error) {
configMutex.RLock()
if GlobalConfig != nil {
// 如果已经加载过,直接返回

View File

@@ -1,10 +1,8 @@
package database
import (
"bufio"
"database/sql"
"fmt"
"os"
"strings"
"sync"
@@ -25,10 +23,113 @@ var (
dbErr error
)
// 封装数据库函数使用Go实现
// MapCategory - 根据类别名称查ID
// 查询方式: globalID, err := db.MapCategory(conn, "global")
func MapCategory(conn *sql.DB, catname string) (int, error) {
var id int
query := "select id from categories where name = ?"
fullSQL := ReplaceSQLQuery(query, catname)
err := conn.QueryRow(query, catname).Scan(&id)
if err == sql.ErrNoRows {
logger.Debugf("未找到类别 %s, 返回ID=0", catname)
return 0, nil // 无匹配返回0
}
logger.Debugf("查询语句: %s , CatName=%s, ID=%d", fullSQL, catname, id)
return id, nil
}
// MapCategoryIndex - 根据类别名称 + 索引名称查ID
// 调用方式: linuxOSID, err := db.MapCategoryIndex(conn, "os", "linux")
func MapCategoryIndex(conn *sql.DB, catindexName, categoryIndex string) (int, error) {
var id int
query := `
select index_id from vmapCategoryIndex
where categoryName = ? and categoryIndex = ?
`
fullSQL := ReplaceSQLQuery(query, catindexName, categoryIndex)
err := conn.QueryRow(query, catindexName, categoryIndex).Scan(&id)
if err == sql.ErrNoRows {
logger.Debugf("未找到索引 %s, 返回ID=0", catindexName)
return 0, nil // 无匹配返回0
}
logger.Debugf("查询语句: %s , CatIndexName=%s, CategoryIndex=%s, ID=%d",
fullSQL, catindexName, categoryIndex, id)
return id, nil
}
// ResolveFirewalls - 解析指定主机的防火墙规则
// 返回解析后的防火墙规则(fwresolved表数据),临时表使用后自动清理
// 调用方式: rows, err := db.ResolveFirewalls(conn, "compute-0-1", "default")
func ResolveFirewalls(conn *sql.DB, hostname, chainname string) (*sql.Rows, error) {
// 步骤1 创建临时表 fresolved1
_, err := conn.Exec(`
DROP TABLE IF EXISTS fresolved1;
CREATE TEMPORARY TABLE fresolved1 AS
SELECT
? AS hostname,
? AS Resolver,
f.*,
r.precedence
FROM
resolvechain r
inner join hostselections hs on r.category = hs.category and r.name = ?
inner join firewalls f on hs.category = f.category and hs.selection = f.catindex
where hs.host = ?;
`, hostname, chainname, chainname, hostname)
if err != nil {
return nil, fmt.Errorf("Create temporary table fresolved1 failed: %w", err)
}
// 步骤2创建临时表 fresolved2
_, err = conn.Exec(`
DROP TABLE IF EXISTS fresolved2;
CREATE TEMPORARY TABLE fresolved2 AS
SELECT
*
FROM
fresolved1;
`)
if err != nil {
return nil, fmt.Errorf("Create temporary table fresolved2 failed: %w", err)
}
// 步骤3创建最终结果表 fwresolved
_, err = conn.Exec(`
DROP TABLE IF EXISTS fwresolved;
CREATE TEMPORARY TABLE fwresolved AS
SELECT
r1.*,
cat.name AS categoryName
FROM
fresolved1 r1
inner join (
select Rulename, MAX(precedence) as precedence
from fresolved2
group by Rulename
) AS r2 on r1.Rulename = r2.Rulename and r1.precedence = r2.precedence
inner join categories cat on r1.category = cat.id;
`)
if err != nil {
return nil, fmt.Errorf("Create temporary table fwresolved failed: %w", err)
}
// 步骤4查询结果并返回
rows, err := conn.Query("SELECT * FROM fwresolved")
if err != nil {
return nil, fmt.Errorf("Query fwresolved failed: %w", err)
}
return rows, nil
}
// =========================================================
// GetDB - 获取数据库连接(单例模式)
// =========================================================
func GetDB() (*sql.DB, error) {
logger.Debug("获取数据库连接...")
dbOnce.Do(func() {
if dbInstance != nil {
return
@@ -64,6 +165,13 @@ func GetDB() (*sql.DB, error) {
return
}
var version string
err = sqlDB.QueryRow("select sqlite_version()").Scan(&version)
if err != nil {
version = "unknown"
}
logger.Debugf("数据库版本: %s", version)
logger.Debug("数据库连接成功")
dbInstance = sqlDB
})
@@ -75,46 +183,46 @@ func GetDB() (*sql.DB, error) {
return dbInstance, nil
}
func confirmAction(prompt string) bool {
reader := bufio.NewReader(os.Stdin)
logger.Warnf("%s [Y/Yes]: ", prompt)
response, err := reader.ReadString('\n')
if err != nil {
return false
}
response = strings.ToLower(strings.TrimSpace(response))
return response == "y" || response == "yes"
}
func InitTables(db *sql.DB, force bool) error {
if force {
// 确认是否强制删除
if !confirmAction("确认强制删除所有表和触发器?") {
logger.Info("操作已取消")
db.Close()
os.Exit(0)
return nil
// 临时关闭外键约束(解决外键依赖删除报错问题)
_, err := db.Exec("PRAGMA foreign_keys = OFF;")
if err != nil {
logger.Errorf("关闭外键约束失败: %v", err)
return err
}
// 强制删除所有表和触发器
logger.Debug("强制删除所有表和触发器...")
if err := dropTables(db); err != nil {
return fmt.Errorf("删除表失败: %w", err)
}
logger.Debug("删除所有表和触发器成功")
if err := dropTriggers(db); err != nil {
return fmt.Errorf("删除触发器失败: %w", err)
}
logger.Debug("删除所有触发器成功")
defer func() {
// 延迟恢复外键约束(确保在函数退出时恢复)
_, err := db.Exec("PRAGMA foreign_keys = ON;")
if err != nil {
logger.Errorf("恢复外键约束失败: %v", err)
}
}()
// ✅ 调用 schema.go 中的函数
for _, ddl := range CreateTableStatements() {
logger.Debugf("执行: %s", ddl)
for name, ddl := range BaseTables() {
// 删除表或者试图(如果存在)
logger.Debugf("执行删除 - %s", name)
// 先尝试作为表进行删除
query := fmt.Sprintf("DROP TABLE IF EXISTS %s;", name)
logger.Debugf("执行语句: %s", query)
_, err := db.Exec(query)
if err != nil {
// 如果作为表删除失败,尝试作为试图删除
logger.Debugf("删除表失败: %v", err)
query = fmt.Sprintf("DROP VIEW IF EXISTS %s;", name)
logger.Debugf("执行语句: %s", query)
_, err = db.Exec(query)
if err != nil {
return fmt.Errorf("删除失败: %w", err)
}
}
logger.Debugf("执行图表 - %s", name)
logger.Debugf("执行语句: %s", ddl)
if _, err := db.Exec(ddl); err != nil {
return fmt.Errorf("数据表创建失败: %w", err)
}
@@ -128,26 +236,12 @@ func InitTables(db *sql.DB, force bool) error {
select * from sqlite_master where type='table'; # 查看表定义
PRAGMA integrity_check; # 检查数据库完整性
*/
return nil
}
func dropTables(db *sql.DB) error {
// ✅ 调用 schema.go 中的函数
for _, table := range DropTableOrder() {
if _, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)); err != nil {
return err
}
}
return nil
}
func dropTriggers(db *sql.DB) error {
// ✅ 调用 schema.go 中的函数
for _, trigger := range DropTriggerStatements() {
if _, err := db.Exec(fmt.Sprintf("DROP TRIGGER IF EXISTS `%s`", trigger)); err != nil {
return err
}
// 添加基础数据
if err := InitBaseData(db); err != nil {
return fmt.Errorf("初始化基础数据失败: %w", err)
}
logger.Info("基础数据初始化成功")
return nil
}
@@ -212,3 +306,87 @@ func TestNodeInsert(db *sql.DB) error {
return nil
})
}
// =========================================================
// 带事务执行 SQL 语句,自动提交/回滚
// =========================================================
// 执行单条SQL语句带事务管理
func ExecSingleWithTransaction(sqlStr string) error {
// 复用批量函数将单条SQL语句包装为数组执行
return ExecWithTransaction([]string{sqlStr})
}
// 批量执行 DDL 语句,带事务管理
func ExecWithTransaction(ddl []string) error {
conn, err := GetDB()
if err != nil {
logger.Errorf("获取数据库连接失败: %v", err)
return err
}
// 开始事务
tx, err := conn.Begin()
if err != nil {
logger.Errorf("开始事务失败: %v", err)
return err
}
var finished bool
// 延迟处理:如果函数异常,回滚事务
defer func() {
if r := recover(); r != nil {
if !finished {
// 捕获 panic 并回滚事务
tx.Rollback()
logger.Errorf("事务执行中发生 panic: %v", r)
}
panic(r)
}
}()
// 遍历执行 DDL 语句
for idx, sql := range ddl {
logger.Debugf("执行 DDL 语句 %d: %s", idx+1, sql)
_, err = tx.Exec(sql)
if err != nil {
// 执行失败时,回滚事务
rollbackErr := tx.Rollback()
finished = true // 标记事务已完成
if rollbackErr != nil {
logger.Errorf("执行失败: 回滚失败: %v (原错误: %v, SQL: %s)", rollbackErr, err, sql)
} else {
logger.Errorf("执行失败: 回滚事务: %v, SQL: %s", err, sql)
}
logger.Errorf("执行 %d 条, 失败: %w (SQL: %s)", idx+1, err, sql)
return fmt.Errorf("执行 %d 条, 失败: %w (SQL: %s)", idx+1, err, sql)
}
}
// 所有SQL语句执行成功提交事务
logger.Info("所有SQL语句执行成功,提交事务")
if err := tx.Commit(); err != nil {
logger.Errorf("提交事务失败: %w", err)
return err
}
finished = true // 标记事务已完成
logger.Debugf("成功执行 %d 条 SQL 语句, 事务已提交.", len(ddl))
return nil
}
func ReplaceSQLQuery(query string, args ...interface{}) string {
for _, arg := range args {
switch v := arg.(type) {
case string:
query = strings.Replace(query, "?", fmt.Sprintf("'%s'", v), 1)
case int, int64, float64:
query = strings.Replace(query, "?", fmt.Sprintf("%v", v), 1)
default:
query = strings.Replace(query, "?", fmt.Sprintf("%v", v), 1)
}
}
return strings.TrimSpace(strings.ReplaceAll(query, "\n", " "))
}

View File

@@ -1,294 +1,542 @@
// Package db defines the database schema.
package database
// CurrentSchemaVersion returns the current schema version (for migrations)
func CurrentSchemaVersion() int {
return 1
}
import (
"database/sql"
"fmt"
"sunhpc/pkg/logger"
)
// CreateTableStatements returns a list of CREATE TABLE statements.
func CreateTableStatements() []string {
return []string{
createAliasesTable(),
createAttributesTable(),
createBootactionTable(),
createDistributionsTable(),
createFirewallsTable(),
createNetworksTable(),
createPartitionsTable(),
createPublicKeysTable(),
createSoftwareTable(),
createNodesTable(),
createSubnetsTable(),
createTrg_nodes_before_delete(),
func BaseTables() map[string]string {
return map[string]string{
"appliances": `
CREATE TABLE IF NOT EXISTS appliances (
ID integer primary key autoincrement,
Name varchar(32) not null default '',
Graph varchar(64) not null default 'default',
Node varchar(64) not null default '',
OS varchar(64) not null default 'linux'
);
`,
"memberships": `
CREATE TABLE IF NOT EXISTS memberships (
ID integer primary key autoincrement,
Name varchar(64) not null default '',
Appliance integer(11) default '0',
Distribution integer(11) default '1',
Public varchar(64) not null default 'no'
);
`,
"categories": `
CREATE TABLE IF NOT EXISTS categories (
ID integer primary key autoincrement,
Name varchar(64) not null unique default '0',
Description varchar(255) default null,
UNIQUE(Name)
);
`,
"catindex": `
CREATE TABLE IF NOT EXISTS catindex (
ID integer primary key autoincrement,
Name varchar(64) not null unique default '0',
Category integer not null,
Foreign key(Category) references categories(ID) on delete cascade
);
`,
"resolvechain": `
CREATE TABLE IF NOT EXISTS resolvechain (
ID integer primary key autoincrement,
Name varchar(64) not null default '0',
Category integer(11) not null,
Precedence integer(11) not null default '10',
UNIQUE(Name, Category)
Foreign key(Category) references categories(ID) on delete cascade
);
`,
"nodes": `
CREATE TABLE IF NOT EXISTS nodes (
ID integer primary key autoincrement,
Name varchar default null,
Membership integer default '2',
CPUs integer not null default '1',
Rack varchar default null,
Rank integer default null,
Arch varchar default null,
OS varchar default null,
RunAction varchar(64) default 'os',
InstallAction varchar(64) default 'install'
);
create index if not exists idx_nodes_name on nodes(Name);
`,
"aliases": `
CREATE TABLE IF NOT EXISTS aliases (
ID integer primary key autoincrement,
Node integer not null default '0',
Name varchar default null,
Foreign key(Node) references nodes(ID) on delete cascade
);
create index if not exists idx_aliases_name on aliases(Name);
`,
"networks": `
CREATE TABLE IF NOT EXISTS networks (
ID integer primary key autoincrement,
Node integer not null default '0',
MAC varchar default null,
IP varchar default null,
Name varchar default null,
Device varchar default null,
Subnet integer default null,
Module varchar default null,
VlanID integer default null,
Options varchar default null,
Channel varchar default null,
Foreign key(Node) references nodes(ID) on delete cascade,
Foreign key(Subnet) references subnets(ID) on delete cascade
);
create index if not exists idx_networks_name on networks(Name);
`,
"globalroutes": `
CREATE TABLE IF NOT EXISTS globalroutes (
Network varchar(32) not null default '',
Netmask varchar(32) not null default '',
Gateway varchar(32) not null default '',
Subnet integer default null,
Primary key(Network, Netmask)
Foreign key(Subnet) references subnets(ID) on delete cascade
);
`,
"osroutes": `
CREATE TABLE IF NOT EXISTS osroutes (
OS varchar(64) not null default 'linux',
Network varchar(32) not null default '',
Netmask varchar(32) not null default '',
Gateway varchar(32) not null default '',
Subnet integer default null,
Primary key(OS, Network, Netmask)
Foreign key(Subnet) references subnets(ID) on delete cascade
);
`,
"applianceroutes": `
CREATE TABLE IF NOT EXISTS applianceroutes (
Appliance varchar(11) not null default '0',
Network varchar(32) not null default '',
Netmask varchar(32) not null default '',
Gateway varchar(32) not null default '',
Subnet integer default null,
Primary key(Appliance, Network, Netmask)
Foreign key(Subnet) references subnets(ID) on delete cascade
);
`,
"noderoutes": `
CREATE TABLE IF NOT EXISTS noderoutes (
Node varchar(11) not null default '0',
Network varchar(32) not null default '',
Netmask varchar(32) not null default '',
Gateway varchar(32) not null default '',
Subnet integer default null,
Primary key(Node, Network, Netmask)
Foreign key(Subnet) references subnets(ID) on delete cascade
);
`,
"subnets": `
CREATE TABLE IF NOT EXISTS subnets (
ID integer primary key autoincrement,
name varchar(32) unique not null,
dnszone varchar(64) unique not null,
subnet varchar(32) default null,
netmask varchar(32) default null,
mtu integer(11) default '1500',
servedns boolean default false
);
`,
"publickeys": `
CREATE TABLE IF NOT EXISTS publickeys (
ID integer primary key autoincrement,
Node integer(11) not null default '0',
Public_Key varchar(8192) default null,
Description varchar(8192) default null,
Foreign key(Node) references nodes(ID) on delete cascade
);
`,
"secglobal": `
CREATE TABLE IF NOT EXISTS secglobal (
Attr varchar(128) default null,
Value text,
Enc varchar(128) default null,
Primary key(Attr)
);
`,
"secnodes": `
CREATE TABLE IF NOT EXISTS secnodes (
Attr varchar(128) default null,
Enc varchar(128) default null,
Value text,
Node integer(15) not null default '0',
Primary key(Attr, Node)
);
`,
"attributes": `
CREATE TABLE IF NOT EXISTS attributes (
ID integer primary key autoincrement,
Attr varchar(128) not null,
Value text,
Shadow text,
Category integer(11) not null,
Catindex integer(11) not null,
UNIQUE(Attr, Category, Catindex),
Foreign key(Catindex) references catindex(ID) on delete cascade
);
`,
"partitions": `
CREATE TABLE IF NOT EXISTS partitions (
ID integer primary key autoincrement,
Node integer(15) not null default '0',
Device varchar(128) not null default '',
MountPoint varchar(128) not null default '',
SectorStart varchar(128) not null default '',
PartitionSize varchar(128) not null default '',
FsType varchar(128) not null default '',
PartitionFlags varchar(128) not null default '',
FormatFlags varchar(128) not null default ''
);
`,
"firewalls": `
CREATE TABLE IF NOT EXISTS firewalls (
ID integer primary key autoincrement,
Rulename varchar(128) not null,
Rulesrc varchar(256) not null default 'custom',
InSubnet int(11),
OutSubnet int(11),
Service varchar(256),
Protocol varchar(256),
Action varchar(256),
Chain varchar(256),
Flags varchar(256),
Comment varchar(256),
Category integer(11) not null,
Catindex integer(11) not null,
Check(rulesrc IN ('system', 'custom'))
UNIQUE(Rulename, Category, Catindex),
Foreign key(Catindex) references catindex(ID) on delete cascade
);
`,
"rolls": `
CREATE TABLE IF NOT EXISTS rolls (
ID integer primary key autoincrement,
Name varchar(128) not null default '',
Version varchar(32) not null default '',
Arch varchar(32) not null default '',
OS varchar(64) not null default 'linux',
Enabled varchar(3) not null default 'yes',
Check(Enabled IN ('yes', 'no'))
Check(OS IN ('linux', 'other'))
);
`,
"noderolls": `
CREATE TABLE IF NOT EXISTS noderolls (
Node varchar(11) not null default '0',
RollID varchar(11) not null,
Primary key(Node, RollID)
);
`,
"bootactions": `
CREATE TABLE IF NOT EXISTS bootactions (
ID integer primary key autoincrement,
Action varchar(256) default null,
Kernel varchar(256) default null,
Ramdisk varchar(256) default null,
Args varchar(1024) default null
);
`,
"bootflags": `
CREATE TABLE IF NOT EXISTS bootflags (
ID integer primary key autoincrement,
Node integer(11) not null default '0',
Flags varchar(256) default null
);
`,
"distributions": `
CREATE TABLE IF NOT EXISTS distributions (
ID integer primary key autoincrement,
Name varchar(32) not null default '',
OS varchar(32) default '',
Release varchar(32) default ''
);
`,
"vnet": `
DROP VIEW IF EXISTS vnet;
CREATE VIEW vnet AS
SELECT
n.name AS nodename, /* 查询nodes表中name字段,将字段改名为nodename */
m.name AS membership,
a.name AS appliance,
n.rack, n.rank, /* 查询nodes表中rack和rank字段,使用原始字段名 */
s.name AS subnet,
nt.ip, nt.device, nt.module,
nt.name AS hostname,
s.dnszone AS domainname,
s.netmask, s.mtu
FROM
nodes n /* 主表: 先查询nodes表,别名n */
inner join memberships m on n.membership=m.id /* 连接memberships表,on只保留满足条件的行 */
inner join appliances a on m.appliance=a.id /* 连接appliances表,on只保留满足条件的行 */
inner join networks nt on n.id=nt.node /* 连接networks表,on只保留满足条件的行 */
inner join subnets s on nt.subnet=s.id /* 连接subnets表,on只保留满足条件的行 */
;
`,
"hostselections": `
DROP VIEW IF EXISTS hostselections;
CREATE VIEW hostselections AS
SELECT
n.name AS host,
c.id as category,
ci.id as selection
FROM
nodes n
inner join memberships m on n.membership=m.id -- 节点表关联所属分组
inner join appliances a on m.appliance=a.id -- 分组关联所属应用角色
inner join categories c on
-- 匹配4类分层配置的category(全局/OS/应用/主机)
c.name in ('global', 'os', 'appliance', 'host')
inner join catindex ci on
-- 核心匹配逻辑: category和catindex的name字段一一对应
(c.name = 'global' and ci.name = 'global') or
(c.name = 'os' and ci.name = n.os) or
(c.name = 'appliance' and ci.name = a.name) or
(c.name = 'host' and ci.name = n.name)
;
`,
"vcatindex": `
-- 视图vcatindex: 类别索引可读试图
DROP VIEW IF EXISTS vcatindex;
CREATE VIEW vcatindex AS
SELECT
c.id AS ID,
cat.Name AS Category,
ci.Name AS catindex
FROM
categories cat
inner join catindex ci on ci.category=cat.id
;
`,
"vresolvechain": `
-- 视图vresolvechain: 解析链可读试图
DROP VIEW IF EXISTS vresolvechain;
CREATE VIEW vresolvechain AS
SELECT
r.name AS chain,
cat.name AS category,
precedence
FROM
resolvechain r
inner join categories cat on r.category=cat.id
order by chain, precedence
;
`,
"vattributes": `
-- 视图vattributes: 属性可读试图
DROP VIEW IF EXISTS vattributes;
CREATE VIEW vattributes AS
SELECT
a.id,
attr,
value,
shadow,
cat.name AS category,
ci.name AS catindex
FROM
attributes a
inner join catindex ci on a.catindex=ci.id
inner join categories cat on a.category=cat.id
order by attr, catindex, category
;
`,
"vfirewalls": `
-- 视图vfirewalls: 防火墙规则可读试图
DROP VIEW IF EXISTS vfirewalls;
CREATE VIEW vfirewalls AS
SELECT
f.id,
f.Rulename,
f.Rulesrc,
f.InSubnet,
f.OutSubnet,
f.Service,
f.Protocol,
f.Action,
f.Chain,
f.Flags,
f.Comment,
cat.name AS category,
ci.name AS catindex
FROM
firewalls f
inner join catindex ci on f.catindex=ci.id
inner join categories cat on f.category=cat.id
order by f.Rulename, catindex, category
;
`,
"vhostselections": `
-- 视图vhostselections: 主机选择可读试图
DROP VIEW IF EXISTS vhostselections;
CREATE VIEW vhostselections AS
SELECT
hs.host AS host,
cat.name AS category,
ci.name AS selection
FROM
hostselections hs
inner join categories cat on hs.category=cat.id
inner join catindex ci on hs.selection=ci.id
order by host, category, selection
;
`,
"vmapcategoryindex": `
-- 视图vmapcategoryindex: 类别索引映射可读试图
DROP VIEW IF EXISTS vmapcategoryindex;
CREATE VIEW vmapCategoryIndex AS
SELECT
cat.name AS categoryName,
ci.name AS categoryIndex,
ci.ID AS index_ID
FROM
cateindex ci
inner join categories cat on ci.category=cat.id
;
`,
}
}
// DropTableOrder returns table names in reverse dependency order for safe DROP.
func DropTableOrder() []string {
return []string{
"aliases",
"attributes",
"bootactions",
"distributions",
"firewalls",
"networks",
"partitions",
"publickeys",
"software",
"nodes",
"subnets",
func InitBaseData(conn *sql.DB) error {
logger.Debug("初始化基础数据...")
// ========== 第一步:插入 categories 数据 ==========
categoryData := []struct {
Name string
Description string
}{
{"global", "Global Defaults"},
{"os", "OS Choice(Linux,Sunos)"},
{"appliance", "Logical Appliances"},
{"rack", "Machine Room Racks"},
{"host", "Hosts - Physical AND Virtual"},
}
}
func DropTriggerStatements() []string {
return []string{
"trg_nodes_before_delete",
// 批量插入 categories (忽略重复)
logger.Debug("插入 categories 数据...")
for _, cd := range categoryData {
query := `
insert or ignore into categories (Name, Description)
values (?, ?)
`
fullSQL := ReplaceSQLQuery(query, cd.Name, cd.Description)
logger.Debugf("执行语句: %s", fullSQL)
// 执行 SQL 语句仍用占位符避免SQL注入
result, err := conn.Exec(query, cd.Name, cd.Description)
if err != nil {
return fmt.Errorf("error inserting category %s: %w", cd.Name, err)
}
}
// --- Private DDL Functions ---
func createAliasesTable() string {
return `
CREATE TABLE IF NOT EXISTS aliases (
id INTEGER PRIMARY KEY AUTOINCREMENT,
node_id INTEGER NOT NULL,
alias TEXT NOT NULL,
CONSTRAINT fk_aliases_node FOREIGN KEY(node_id) REFERENCES nodes(id),
UNIQUE(node_id, alias)
);
create index if not exists idx_aliases_node on aliases (node_id);
`
}
func createAttributesTable() string {
return `
CREATE TABLE IF NOT EXISTS attributes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
node_id INTEGER NOT NULL,
attr TEXT NOT NULL,
value TEXT,
shadow TEXT,
CONSTRAINT fk_attributes_node FOREIGN KEY(node_id) REFERENCES nodes(id)
);
create index if not exists idx_attributes_node on attributes (node_id);
`
}
func createBootactionTable() string {
return `
CREATE TABLE IF NOT EXISTS bootactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
node_id INTEGER NOT NULL,
action TEXT,
kernel TEXT,
initrd TEXT,
cmdline TEXT,
CONSTRAINT fk_bootactions_node FOREIGN KEY(node_id) REFERENCES nodes(id),
UNIQUE(node_id)
);
create index if not exists idx_bootactions_node on bootactions (node_id);
`
}
func createDistributionsTable() string {
return `
CREATE TABLE IF NOT EXISTS distributions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
node_id INTEGER,
name TEXT NOT NULL,
version TEXT,
lang TEXT,
os_release TEXT,
constraint distributions_nodes_fk FOREIGN KEY(node_id) REFERENCES nodes(id)
);
create index if not exists idx_distributions_node on distributions (node_id);
`
}
func createFirewallsTable() string {
return `
CREATE TABLE IF NOT EXISTS firewalls (
id INTEGER PRIMARY KEY AUTOINCREMENT,
node_id INTEGER,
rulename TEXT NOT NULL,
rulesrc TEXT NOT NULL,
insubnet INTEGER,
outsubnet INTEGER,
service TEXT,
protocol TEXT,
action TEXT,
chain TEXT,
flags TEXT,
comment TEXT,
constraint firewalls_nodes_fk FOREIGN KEY(node_id) REFERENCES nodes(id)
);
create index if not exists idx_firewalls_node on firewalls (node_id);
`
}
func createNetworksTable() string {
return `
CREATE TABLE IF NOT EXISTS networks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
node_id INTEGER,
subnet_id INTEGER,
mac TEXT,
ip TEXT,
name TEXT,
device TEXT,
module TEXT,
vlanid INTEGER,
options TEXT,
channel TEXT,
disable_kvm INTEGER NOT NULL DEFAULT 0 CHECK (disable_kvm IN (0, 1)),
constraint networks_nodes_fk FOREIGN KEY(node_id) REFERENCES nodes(id),
constraint networks_subnets_fk FOREIGN KEY(subnet_id) REFERENCES subnets(id)
);
create index if not exists idx_networks_node on networks (node_id);
`
}
func createNodesTable() string {
return `
CREATE TABLE IF NOT EXISTS nodes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
cpus INTEGER NOT NULL,
rack INTEGER NOT NULL,
rank INTEGER NOT NULL,
arch TEXT,
os TEXT,
runaction TEXT,
installaction TEXT
);
create index if not exists idx_nodes_name on nodes (name);
`
}
func createPartitionsTable() string {
return `
CREATE TABLE IF NOT EXISTS partitions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
node_id INTEGER,
device TEXT NOT NULL,
formatflags TEXT NOT NULL,
fstype TEXT NOT NULL,
mountpoint TEXT NOT NULL,
partitionflags TEXT NOT NULL,
partitionid TEXT NOT NULL,
partitionsize TEXT NOT NULL,
sectorstart TEXT NOT NULL,
constraint partitions_nodes_fk FOREIGN KEY(node_id) REFERENCES nodes(id)
);
create index if not exists idx_partitions_node on partitions (node_id);
`
}
func createPublicKeysTable() string {
return `
CREATE TABLE IF NOT EXISTS publickeys (
id INTEGER PRIMARY KEY AUTOINCREMENT,
node_id INTEGER,
publickey TEXT NOT NULL,
description TEXT,
constraint publickeys_nodes_fk FOREIGN KEY(node_id) REFERENCES nodes(id)
);
create index if not exists idx_publickeys_node on publickeys (node_id);
`
}
func createSubnetsTable() string {
return `
CREATE TABLE IF NOT EXISTS subnets (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
dnszone TEXT NOT NULL,
subnet TEXT NOT NULL,
netmask TEXT NOT NULL,
mtu INTEGER NOT NULL DEFAULT 1500,
servedns INTEGER NOT NULL DEFAULT 0 CHECK (servedns IN (0, 1)),
UNIQUE(name, dnszone)
);`
}
func createSoftwareTable() string {
return `
CREATE TABLE IF NOT EXISTS software (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
pathversion TEXT,
fullversion TEXT,
description TEXT,
website TEXT,
license TEXT,
install_method TEXT NOT NULL CHECK (install_method IN (
'source',
'binary',
'rpm',
'docker',
'apptainer',
'conda',
'mamba',
'spack',
'tarball',
'zipball',
'pip',
'npm',
'custom'
)),
-- 源码编译相关参数
source_url TEXT, -- 源码下载地址
source_checksum TEXT, -- 源码校验和
source_checksum_type TEXT NOT NULL CHECK (source_checksum_type IN (
'md5',
'sha1',
'sha256',
'sha512'
)),
build_dependencies TEXT, -- 编译依赖(JSON格式)
configure_params TEXT, -- 配置参数(JSON格式)
make_params TEXT, -- make参数(JSON格式)
make_install_params TEXT, -- make install参数(JSON格式)
-- 安装路径参数
install_path TEXT NOT NULL, -- 安装路径
env_vars TEXT, -- 环境变量(JSON格式)
-- 状态信息
is_installed INTEGER NOT NULL DEFAULT 0 CHECK (is_installed IN (0, 1)), -- 是否安装
install_date TEXT, -- 安装日期
updated_date TEXT, -- 更新日期
install_user TEXT, -- 安装用户
notes TEXT, -- 安装备注
-- 元数据
created_at DATETIME DEFAULT CURRENT_TIMESTAMP, -- 创建时间
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, -- 更新时间
UNIQUE(name)
);
create index if not exists idx_software_name on software (name);
create index if not exists idx_software_install_method on software (install_method);
create index if not exists idx_software_is_installed on software (is_installed);
`
}
func createTrg_nodes_before_delete() string {
return `
CREATE TRIGGER IF NOT EXISTS trg_nodes_before_delete
BEFORE DELETE ON nodes
FOR EACH ROW
BEGIN
-- 先删除子表的关联记录
DELETE FROM aliases WHERE node_id = OLD.id;
DELETE FROM attributes WHERE node_id = OLD.id;
DELETE FROM bootactions WHERE node_id = OLD.id;
DELETE FROM distributions WHERE node_id = OLD.id;
DELETE FROM firewalls WHERE node_id = OLD.id;
DELETE FROM networks WHERE node_id = OLD.id;
DELETE FROM partitions WHERE node_id = OLD.id;
DELETE FROM publickeys WHERE node_id = OLD.id;
END;
`
id, err := result.LastInsertId()
if err != nil {
return fmt.Errorf("error getting last insert ID: %w", err)
}
logger.Debugf("执行语句: %s, 插入ID: %d", fullSQL, id)
}
// ========== 第二步:插入 catindex 数据 ==========
catindexData := []struct {
Name string
Category string
}{
{"global", "global"},
{"linux", "os"},
{"sunos", "os"},
{"frontend", "appliance"},
{"compute", "appliance"},
{"nas", "appliance"},
{"network", "appliance"},
{"power", "appliance"},
{"devel-server", "appliance"},
{"login", "appliance"},
}
// 批量插入 catindex (忽略重复)
logger.Debug("插入 Catindex 数据...")
for _, ci := range catindexData {
// 动态获取类别ID (复用MapCategory函数)
catID, err := MapCategory(conn, ci.Category)
if err != nil {
return fmt.Errorf("error mapping category %s: %w", ci.Category, err)
}
if catID == 0 {
return fmt.Errorf("category %s not found", ci.Category)
}
// 插入 catindex (忽略重复)
query := `
insert or ignore into catindex (Name, Category)
values (?, ?)
`
fullSQL := ReplaceSQLQuery(query, ci.Name, catID)
// 执行 SQL 语句仍用占位符避免SQL注入
result, err := conn.Exec(query, ci.Name, catID)
if err != nil {
return fmt.Errorf("error inserting catindex %s: %w", ci.Name, err)
}
id, err := result.LastInsertId()
if err != nil {
return fmt.Errorf("error getting last insert ID: %w", err)
}
logger.Debugf("执行语句: %s, 插入ID: %d", fullSQL, id)
}
// ========== 第三步:插入 resolvechain 数据 ==========
resolveChainData := []struct {
Name string // 解析链名称,global/linux/sunos
Category string // 类别名称,linux/sunos
Precedence int // 优先级,数值越大优先级越高
}{
{"default", "global", 10},
{"default", "os", 20},
{"default", "appliance", 30},
{"default", "rack", 40},
{"default", "host", 50},
}
// 批量插入 resolvechain (忽略重复)
logger.Debugf("插入 resolvechain 数据...")
for _, rcd := range resolveChainData {
// 动态获取类别ID (复用MapCategory函数)
catID, err := MapCategory(conn, rcd.Category)
if err != nil {
return fmt.Errorf("error mapping category %s: %w", rcd.Category, err)
}
if catID == 0 {
return fmt.Errorf("category %s not found", rcd.Category)
}
// 插入 resolvechain (忽略重复)
query := `
insert or ignore into resolvechain (Name, Category, Precedence)
values (?, ?, ?)
`
fullSQL := ReplaceSQLQuery(query, rcd.Name, catID, rcd.Precedence)
// 执行 SQL 语句仍用占位符避免SQL注入
result, err := conn.Exec(query, rcd.Name, catID, rcd.Precedence)
if err != nil {
return fmt.Errorf("error inserting resolvechain %s: %w", rcd.Name, err)
}
id, err := result.LastInsertId()
if err != nil {
return fmt.Errorf("error getting last insert ID: %w", err)
}
logger.Debugf("执行语句: %s, 插入ID: %d", fullSQL, id)
}
return nil
}

162
pkg/info/info.go Normal file
View File

@@ -0,0 +1,162 @@
package info
import (
"bufio"
"fmt"
"os"
"runtime"
"strings"
)
// -------------------------- 编译注入的静态信息 --------------------------
var (
Version = "dev" // 应用版本号
BuildTime = "unknown" // 编译时间
GitCommit = "unknown" // Git提交ID
GitBranch = "unknown" // Git分支
)
// -------------------------- 固定常量 --------------------------
const (
AppName = "sunhpc"
linuxProcVersion = "/proc/version"
linuxProcCpuinfo = "/proc/cpuinfo"
)
// -------------------------- 系统信息结构体 --------------------------
// SystemInfo 封装所有系统相关信息Linux专属
type SystemInfo struct {
OS string // 操作系统
Arch string // 系统架构
KernelVersion string // 内核版本
CPUModel string // CPU型号
NumCPU int // CPU核心数
MemTotal string // 总内存
Hostname string // 主机名
GoVersion string // Go运行时版本
}
// -------------------------- 通用工具函数(无错误返回) --------------------------
// readFileFirstLine 读取文件第一行,失败返回空字符串
func readFileFirstLine(path string) string {
file, err := os.Open(path)
if err != nil {
return ""
}
defer file.Close()
scanner := bufio.NewScanner(file)
if scanner.Scan() {
return strings.TrimSpace(scanner.Text())
}
return ""
}
// -------------------------- 核心函数(无错误返回) --------------------------
// readCPUModel 读取CPU型号失败返回 "unknown"
func readCPUModel() string {
file, err := os.Open(linuxProcCpuinfo)
if err != nil {
return "unknown"
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "model name") {
parts := strings.SplitN(line, ":", 2)
if len(parts) == 2 {
return strings.TrimSpace(parts[1])
}
}
}
return "unknown"
}
// readMemTotal 读取总内存,失败返回 "unknown"
func readMemTotal() string {
file, err := os.Open("/proc/meminfo")
if err != nil {
return "unknown"
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "MemTotal") {
parts := strings.Fields(line)
if len(parts) >= 2 {
memKB := strings.TrimSuffix(parts[1], "kB")
var memTotalKB int
fmt.Sscanf(memKB, "%d", &memTotalKB)
memTotalMB := memTotalKB / 1024
return fmt.Sprintf("%d MB", memTotalMB)
}
}
}
return "unknown"
}
// GetSystemInfo 获取Linux系统信息无错误返回异常字段用默认值填充
func GetSystemInfo() SystemInfo {
// 初始化结构体,先填充基础默认值
sysInfo := SystemInfo{
OS: runtime.GOOS,
Arch: runtime.GOARCH,
NumCPU: runtime.NumCPU(),
GoVersion: runtime.Version(),
// 以下字段先设为默认值,后续尝试覆盖
KernelVersion: "unknown",
CPUModel: "unknown",
MemTotal: "unknown",
Hostname: "unknown",
}
// 1. 获取主机名(失败保留默认值)
if hostname, err := os.Hostname(); err == nil {
sysInfo.Hostname = hostname
}
// 2. 获取内核版本(失败保留默认值)
procVersion := readFileFirstLine(linuxProcVersion)
if procVersion != "" {
versionParts := strings.Split(procVersion, " ")
if len(versionParts) >= 3 {
sysInfo.KernelVersion = versionParts[2]
} else {
sysInfo.KernelVersion = procVersion
}
}
// 3. 获取CPU型号失败已返回 "unknown"
sysInfo.CPUModel = readCPUModel()
// 4. 获取总内存(失败已返回 "unknown"
sysInfo.MemTotal = readMemTotal()
return sysInfo
}
// -------------------------- 辅助函数:打印所有信息 --------------------------
// PrintAllInfo 打印所有公共信息(调试用)
func PrintAllInfo() {
fmt.Println("=== 应用公共信息 ===")
fmt.Printf("应用名称 : %s\n", AppName)
fmt.Printf("版本号 : %s\n", Version)
fmt.Printf("编译时间 : %s\n", BuildTime)
fmt.Printf("Git提交ID : %s\n", GitCommit)
fmt.Printf("Git分支 : %s\n", GitBranch)
fmt.Println("\n=== 系统信息 ===")
sysInfo := GetSystemInfo()
fmt.Printf("操作系统 : %s\n", sysInfo.OS)
fmt.Printf("系统架构 : %s\n", sysInfo.Arch)
fmt.Printf("内核版本 : %s\n", sysInfo.KernelVersion)
fmt.Printf("CPU型号 : %s\n", sysInfo.CPUModel)
fmt.Printf("CPU核心数 : %d\n", sysInfo.NumCPU)
fmt.Printf("总内存 : %s\n", sysInfo.MemTotal)
fmt.Printf("主机名 : %s\n", sysInfo.Hostname)
fmt.Printf("Go版本 : %s\n", sysInfo.GoVersion)
}

View File

@@ -80,6 +80,72 @@ type LogConfig struct {
ShowColor bool `mapstructure:"show_color" yaml:"show_color"`
}
type LevelFilterWriter struct {
writer io.Writer
maxLevel logrus.Level // 控制台: 只输出 <= 该级别
minLevel logrus.Level // 文件: 只输出 >= 该级别
isConsole bool // 是否是控制台输出
}
// Write 实现io.Writer接口核心过滤逻辑
func (f *LevelFilterWriter) Write(p []byte) (n int, err error) {
// 解析日志级别适配logrus默认格式和CustomFormatter
logLevel := parseLogLevelFromContent(p)
// 控制台:只输出 Info 及以下级别Trace/Debug/Info
if f.isConsole {
if logLevel <= f.maxLevel {
return f.writer.Write(p)
}
return len(p), nil // 过滤掉返回长度避免Writer报错
}
// 文件:只输出 Warn 及以上级别Warn/Error/Fatal/Panic
if logLevel >= f.minLevel {
return f.writer.Write(p)
}
return len(p), nil
}
// parseLogLevelFromContent 解析日志内容中的级别(兼容自定义格式)
func parseLogLevelFromContent(p []byte) logrus.Level {
content := string(p)
// 适配常见的级别关键字兼容你的CustomFormatter
switch {
case contains(content, "TRACE"):
return logrus.TraceLevel
case contains(content, "DEBUG"):
return logrus.DebugLevel
case contains(content, "INFO"):
return logrus.InfoLevel
case contains(content, "WARN") || contains(content, "WARNING"):
return logrus.WarnLevel
case contains(content, "ERROR"):
return logrus.ErrorLevel
case contains(content, "FATAL"):
return logrus.FatalLevel
case contains(content, "PANIC"):
return logrus.PanicLevel
default:
return logrus.InfoLevel // 解析失败默认Info级别
}
}
// contains 辅助函数:判断字符串是否包含子串
func contains(s, substr string) bool {
return len(s) >= len(substr) && indexOf(s, substr) != -1
}
// indexOf 简易字符串查找(避免依赖额外库)
func indexOf(s, substr string) int {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return i
}
}
return -1
}
// 默认配置
var defaultConfig = LogConfig{
Level: "info",
@@ -216,34 +282,7 @@ func Init(cfg LogConfig) {
// 1. 创建logrus实例
logrusInst := logrus.New()
// 2. 配置输出(控制台 + 文件,可选)
var outputs []io.Writer
outputs = append(outputs, os.Stdout) // 控制台输出
// 如果配置了日志文件,添加文件输出
if cfg.LogFile != "" {
// 确保日志目录存在
dir := filepath.Dir(cfg.LogFile)
if err := os.MkdirAll(dir, 0755); err != nil {
// 目录创建失败,只输出警告,不影响程序运行
logrusInst.Warnf("创建日志目录失败: %v,仅输出到控制台", err)
} else {
file, err := os.OpenFile(cfg.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err == nil {
outputs = append(outputs, file)
} else {
logrusInst.Warnf("打开日志文件失败: %v,仅输出到控制台", err)
}
}
}
logrusInst.SetOutput(io.MultiWriter(outputs...))
// 3. 配置格式
logrusInst.SetFormatter(&CustomFormatter{
ShowColor: cfg.ShowColor,
})
// 4. 配置日志级别
// 2. 配置日志级别(总开关,必须在输出配置前)
lvl, err := logrus.ParseLevel(cfg.Level)
if err != nil {
lvl = logrus.InfoLevel // 解析失败默认Info级别
@@ -257,6 +296,45 @@ func Init(cfg LogConfig) {
// 启用文件行号(必须开启否则getCallerInfo拿不到数据)
logrusInst.SetReportCaller(true)
// 3. 配置输出(控制台 + 文件,可选)
var outputs []io.Writer
// 控制台输出: 只输出 Info 及以下级别
consoleWriter := &LevelFilterWriter{
writer: os.Stdout,
minLevel: logrus.InfoLevel,
isConsole: true,
}
outputs = append(outputs, consoleWriter)
// 如果配置了日志文件,添加文件输出
if cfg.LogFile != "" {
// 确保日志目录存在
dir := filepath.Dir(cfg.LogFile)
if err := os.MkdirAll(dir, 0755); err != nil {
// 目录创建失败,只输出警告,不影响程序运行
logrusInst.Warnf("创建日志目录失败: %v,仅输出到控制台", err)
} else {
file, err := os.OpenFile(cfg.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err == nil {
fileWriter := &LevelFilterWriter{
writer: file,
minLevel: logrus.WarnLevel,
isConsole: false,
}
outputs = append(outputs, fileWriter)
} else {
logrusInst.Warnf("打开日志文件失败: %v,仅输出到控制台", err)
}
}
}
logrusInst.SetOutput(io.MultiWriter(outputs...))
// 4. 配置格式
logrusInst.SetFormatter(&CustomFormatter{
ShowColor: cfg.ShowColor,
})
// 5. 赋值给全局默认实例
DefaultLogger = &logrusLogger{logrusInst}
})

View File

@@ -3,6 +3,7 @@ package utils
import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"os/exec"
"time"
@@ -32,6 +33,29 @@ func GetTimestamp() string {
return time.Now().Format("2006-01-02 15:04:05")
}
func OutputMaps(maps map[string]string) []string {
output := []string{}
maxLen := 0
for key := range maps {
if len(key) > maxLen {
maxLen = len(key)
}
}
// 使用动态宽度的格式化字符串输出
// %-*s 的含义
// %: 格式化开始
// -: 左对齐,默认是右对齐
// *: 表示宽度由后续参数指定(maxLen)
// s: 表示字符串类型
for key, value := range maps {
output = append(output, fmt.Sprintf("%-*s: %s", maxLen, key, value))
}
return output
}
// 定义短语
const (
NoAvailableNetworkInterfaces = "No available network interfaces"

View File

@@ -1,194 +1,326 @@
package wizard
import (
"encoding/json"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"sunhpc/pkg/database"
"sunhpc/pkg/info"
"sunhpc/pkg/logger"
"sunhpc/pkg/utils"
"github.com/charmbracelet/bubbles/textinput"
"github.com/BurntSushi/toml"
)
// saveConfig 保存配置到文件
// 配置项映射:定义每个配置项对应的表名、键名
type ConfigMapping struct {
Title string `toml:"title"`
Base struct {
ClusterName string `toml:"cluster_name"`
Country string `toml:"country"`
State string `toml:"state"`
City string `toml:"city"`
HomePage string `toml:"homepage"`
Contact string `toml:"contact"`
License string `toml:"license"`
BaseDir string `toml:"base_dir"`
WorkDir string `toml:"work_dir"`
DistroDir string `toml:"distro_dir"`
Partition string `toml:"partition"`
Distribution string `toml:"distribution"`
Timezone string `toml:"timezone"`
SafePort string `toml:"safe_port"`
SafeDirs string `toml:"safe_dirs"`
SafeSecurity string `toml:"safe_security"`
PluginDirs string `toml:"plugin_dirs"`
PluginPort string `toml:"plugin_port"`
GangliaAddr string `toml:"ganglia_addr"`
} `toml:"base"`
Pxelinux struct {
NextServer string `toml:"next_server"`
PxeFilename string `toml:"pxe_filename"`
PxeLinuxDir string `toml:"pxelinux_dir"`
BootArgs string `toml:"boot_args"`
} `toml:"pxelinux"`
Public struct {
PublicHostname string `toml:"public_hostname"`
PublicInterface string `toml:"public_interface"`
PublicAddress string `toml:"public_address"`
PublicNetmask string `toml:"public_netmask"`
PublicGateway string `toml:"public_gateway"`
PublicNetwork string `toml:"public_network"`
PublicDomain string `toml:"public_domain"`
PublicCIDR string `toml:"public_cidr"`
PublicDNS string `toml:"public_dns"`
PublicMac string `toml:"public_mac"`
PublicMTU string `toml:"public_mtu"`
PublicNTP string `toml:"public_ntp"`
} `toml:"public"`
Private struct {
PrivateHostname string `toml:"private_hostname"`
PrivateInterface string `toml:"private_interface"`
PrivateAddress string `toml:"private_address"`
PrivateNetmask string `toml:"private_netmask"`
PrivateNetwork string `toml:"private_network"`
PrivateDomain string `toml:"private_domain"`
PrivateCIDR string `toml:"private_cidr"`
PrivateMac string `toml:"private_mac"`
PrivateMTU string `toml:"private_mtu"`
} `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",
Base: struct {
ClusterName string `toml:"cluster_name"`
Country string `toml:"country"`
State string `toml:"state"`
City string `toml:"city"`
HomePage string `toml:"homepage"`
Contact string `toml:"contact"`
License string `toml:"license"`
BaseDir string `toml:"base_dir"`
WorkDir string `toml:"work_dir"`
DistroDir string `toml:"distro_dir"`
Partition string `toml:"partition"`
Distribution string `toml:"distribution"`
Timezone string `toml:"timezone"`
SafePort string `toml:"safe_port"`
SafeDirs string `toml:"safe_dirs"`
SafeSecurity string `toml:"safe_security"`
PluginDirs string `toml:"plugin_dirs"`
PluginPort string `toml:"plugin_port"`
GangliaAddr string `toml:"ganglia_addr"`
}{
ClusterName: "SunHPC_Cluster",
Country: "CN",
State: "Beijing",
City: "Beijing",
HomePage: "https://www.sunhpc.com",
Contact: "admin@sunhpc.com",
License: "MIT",
BaseDir: "install",
WorkDir: "/export",
DistroDir: "/export/sunhpc",
Partition: "default",
Distribution: "sunhpc-dist",
Timezone: "Asia/Shanghai",
SafePort: "372",
SafeDirs: "safe.d",
SafeSecurity: "safe-security",
PluginDirs: "/etc/sunhpc/plugin",
PluginPort: "12123",
GangliaAddr: "224.0.0.3",
},
Pxelinux: struct {
NextServer string `toml:"next_server"`
PxeFilename string `toml:"pxe_filename"`
PxeLinuxDir string `toml:"pxelinux_dir"`
BootArgs string `toml:"boot_args"`
}{
NextServer: "192.168.1.1",
PxeFilename: "pxelinux.0",
PxeLinuxDir: "/tftpboot/pxelinux",
BootArgs: "net.ifnames=0 biosdevname=0",
},
Public: struct {
PublicHostname string `toml:"public_hostname"`
PublicInterface string `toml:"public_interface"`
PublicAddress string `toml:"public_address"`
PublicNetmask string `toml:"public_netmask"`
PublicGateway string `toml:"public_gateway"`
PublicNetwork string `toml:"public_network"`
PublicDomain string `toml:"public_domain"`
PublicCIDR string `toml:"public_cidr"`
PublicDNS string `toml:"public_dns"`
PublicMac string `toml:"public_mac"`
PublicMTU string `toml:"public_mtu"`
PublicNTP string `toml:"public_ntp"`
}{
PublicHostname: "cluster.hpc.org",
PublicInterface: "eth0",
PublicAddress: "",
PublicNetmask: "",
PublicGateway: "",
PublicNetwork: "",
PublicDomain: "hpc.org",
PublicCIDR: "",
PublicDNS: "",
PublicMac: "00:11:22:33:44:55",
PublicMTU: "1500",
PublicNTP: "pool.ntp.org",
},
Private: struct {
PrivateHostname string `toml:"private_hostname"`
PrivateInterface string `toml:"private_interface"`
PrivateAddress string `toml:"private_address"`
PrivateNetmask string `toml:"private_netmask"`
PrivateNetwork string `toml:"private_network"`
PrivateDomain string `toml:"private_domain"`
PrivateCIDR string `toml:"private_cidr"`
PrivateMac string `toml:"private_mac"`
PrivateMTU string `toml:"private_mtu"`
}{
PrivateHostname: "sunhpc",
PrivateInterface: "eth1",
PrivateAddress: "172.16.9.254",
PrivateNetmask: "255.255.255.0",
PrivateNetwork: "172.16.9.0",
PrivateDomain: "example.com",
PrivateCIDR: "172.16.9.0/24",
PrivateMac: "00:11:22:33:44:66",
PrivateMTU: "1500",
},
}
}
func loadConfig() (*ConfigMapping, error) {
configs := NewConfigWithDefault()
cfgfile := "/etc/sunhpc/config.toml"
// 尝试解析配置文件
if _, err := toml.DecodeFile(cfgfile, configs); err != nil {
if errors.Is(err, os.ErrNotExist) {
// 文件不存在,直接返回默认配置
logger.Debugf("Config file %s not exist, use default config", cfgfile)
return configs, nil
}
// 其他错误,返回错误
logger.Debugf("[DEBUG] Parse config file %s failed: %v", cfgfile, err)
return nil, err
}
logger.Debugf("Load config file %s success", cfgfile)
return configs, nil
}
// saveConfig 入口函数:保存所有配置到数据库
func (m *model) saveConfig() error {
configPath := GetConfigPath()
// 确保目录存在
dir := filepath.Dir(configPath)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("创建配置目录失败:%w", err)
}
m.force = false // 初始化全量覆盖标识
// 序列化配置
data, err := json.MarshalIndent(m.config, "", " ")
c, err := loadConfig()
if err != nil {
return fmt.Errorf("序列化配置失败:%w", err)
logger.Debugf("[DEBUG] Load config file failed: %v", err)
return err
}
// 写入文件
if err := os.WriteFile(configPath, data, 0644); err != nil {
return fmt.Errorf("保存配置文件失败:%w", err)
// 合并配置项
result := make(map[string]string)
// 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
// 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
}
for _, value := range utils.OutputMaps(result) {
logger.Debugf("%s", value)
}
return nil
}
// loadConfig 从文件加载配置
func loadConfig() (*Config, error) {
configPath := GetConfigPath()
data, err := os.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("读取配置文件失败:%w", err)
}
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("解析配置文件失败:%w", err)
}
return &cfg, nil
}
// 以下是 model.go 中调用的保存方法
func (m *model) saveCurrentPage() {
switch m.currentPage {
case PageData:
m.saveDataPage()
case PagePublicNetwork:
m.savePublicNetworkPage()
case PageInternalNetwork:
m.saveInternalNetworkPage()
case PageDNS:
m.saveDNSPage()
}
}
func (m *model) saveDataPage() {
if len(m.textInputs) >= 8 {
m.config.HomePage = m.textInputs[0].Value()
m.config.Hostname = m.textInputs[1].Value()
m.config.Country = m.textInputs[2].Value()
m.config.Region = m.textInputs[3].Value()
m.config.Timezone = m.textInputs[4].Value()
m.config.DBAddress = m.textInputs[5].Value()
m.config.DataAddress = m.textInputs[6].Value()
}
}
func (m *model) savePublicNetworkPage() {
if len(m.textInputs) >= 4 {
m.config.PublicInterface = m.textInputs[0].Value()
m.config.PublicIPAddress = m.textInputs[1].Value()
m.config.PublicNetmask = m.textInputs[2].Value()
m.config.PublicGateway = m.textInputs[3].Value()
}
}
func (m *model) saveInternalNetworkPage() {
if len(m.textInputs) >= 3 {
m.config.InternalInterface = m.textInputs[0].Value()
m.config.InternalIPAddress = m.textInputs[1].Value()
m.config.InternalNetmask = m.textInputs[2].Value()
}
}
func (m *model) saveDNSPage() {
if len(m.textInputs) >= 2 {
m.config.DNSPrimary = m.textInputs[0].Value()
m.config.DNSSecondary = m.textInputs[1].Value()
}
}
// initPageInputs 初始化当前页面的输入框
func (m *model) initPageInputs() {
m.textInputs = make([]textinput.Model, 0)
switch m.currentPage {
case PageData:
fields := []struct{ label, value string }{
{"Homepage", m.config.HomePage},
{"Hostname", m.config.Hostname},
{"Country", m.config.Country},
{"Region", m.config.Region},
{"Timezone", m.config.Timezone},
{"DB Path", m.config.DBAddress},
{"Software", m.config.DataAddress},
}
for _, f := range fields {
ti := textinput.New()
ti.Placeholder = ""
ti.Placeholder = f.label
ti.SetValue(f.value)
ti.Width = 50
m.textInputs = append(m.textInputs, ti)
}
m.focusIndex = 0
if len(m.textInputs) > 0 {
m.textInputs[0].Focus()
}
m.inputLabels = []string{"Homepage", "Hostname", "Country", "Region", "Timezone", "DBPath", "Software"}
case PagePublicNetwork:
fields := []struct{ label, value string }{
{"Public Interface", m.config.PublicInterface},
{"Public IP Address", m.config.PublicIPAddress},
{"Public Netmask", m.config.PublicNetmask},
{"Public Gateway", m.config.PublicGateway},
}
for _, f := range fields {
ti := textinput.New()
ti.Placeholder = ""
ti.Placeholder = f.label
ti.SetValue(f.value)
ti.Width = 50
m.textInputs = append(m.textInputs, ti)
}
m.focusIndex = 0
if len(m.textInputs) > 0 {
m.textInputs[0].Focus()
}
m.inputLabels = []string{"Public Interface", "Public IP Address", "Public Netmask", "Public Gateway"}
case PageInternalNetwork:
fields := []struct{ label, value string }{
{"Internal Interface", m.config.InternalInterface},
{"Internal IP Address", m.config.InternalIPAddress},
{"Internal Netmask", m.config.InternalNetmask},
}
for _, f := range fields {
ti := textinput.New()
ti.Placeholder = f.label
ti.SetValue(f.value)
ti.Width = 50
m.textInputs = append(m.textInputs, ti)
}
m.focusIndex = 0
if len(m.textInputs) > 0 {
m.textInputs[0].Focus()
}
m.inputLabels = []string{"Internal Interface", "Internal IP", "Internal Mask"}
case PageDNS:
fields := []struct{ label, value string }{
{"Primary DNS", m.config.DNSPrimary},
{"Secondary DNS", m.config.DNSSecondary},
}
for _, f := range fields {
ti := textinput.New()
ti.Placeholder = f.label
ti.SetValue(f.value)
ti.Width = 50
m.textInputs = append(m.textInputs, ti)
}
m.focusIndex = 0
if len(m.textInputs) > 0 {
m.textInputs[0].Focus()
}
m.inputLabels = []string{"Pri DNS", "Sec DNS"}
func mergeValue(tui_value, cfg_value string) string {
if tui_value == "" {
return cfg_value
}
return tui_value
}
// 获取系统网络接口
@@ -215,3 +347,225 @@ 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 or replace into bootactions values (1, 'install', '%s', '%s', '%s');",
vmlinuz, initrds, insArgs),
"insert or replace into bootactions values (2, 'os', 'localboot 0', '', '');",
"insert or replace into bootactions values (3, 'memtest', 'kernel memtest', '', '');",
fmt.Sprintf("insert or replace into bootactions values (4, 'install headless', '%s', '%s', '%s');",
vmlinuz, initrds, lesArgs),
fmt.Sprintf("insert or replace into bootactions values (5, 'rescue', '%s', '%s', '%s');",
vmlinuz, initrds, resArgs),
"insert or replace 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 or replace into attributes values (NULL, '%s', '%s', '%s', %d, %d);",
key, value, shadow, category, catindex))
}
nodes := []string{
fmt.Sprintf(
"insert or replace into nodes values (1, '%s', '2', '%d', 0, 0, '%s', '%s', '', 'install');",
result["private_hostname"],
info.GetSystemInfo().NumCPU,
info.GetSystemInfo().Arch,
info.GetSystemInfo().OS),
fmt.Sprintf(
`insert or replace 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 or replace into subnets values (2, 'public', '%s', '%s', '%s', '%s', '0');`,
result["public_domain"],
result["public_network"],
result["public_netmask"],
result["public_mtu"]),
fmt.Sprintf(
`insert or replace into networks values (1, 1, '%s', '%s', '%s', '%s', '2', NULL, NULL,NULL,NULL);`,
result["public_mac"],
result["public_address"],
result["private_hostname"],
result["public_interface"]),
fmt.Sprintf(
`insert or replace into networks values (2, 1, '%s', '%s', '%s', '%s', '1', NULL, NULL, NULL, NULL);`,
result["private_mac"],
result["private_address"],
result["private_hostname"],
result["private_interface"]),
}
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
}

349
pkg/wizard/focused.go Normal file
View File

@@ -0,0 +1,349 @@
package wizard
import (
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
// Focusable 定义可聚焦组件的通用接口
type Focusable interface {
// Focus 激活焦点(比如输入框闪烁光标、按钮高亮)
Focus() tea.Cmd
// Blur 失活焦点(取消高亮/闪烁)
Blur()
// IsFocused 判断是否处于焦点状态
IsFocused() bool
// View 渲染组件(和 bubbletea 统一)
View() string
}
// --------------- 为常用组件实现 Focusable 接口 ---------------
// TextInput 适配 bubbles/textinput
type TextInput struct {
textinput.Model
focused bool
}
func NewTextInput(placeholder string, defaultValue string) *TextInput {
ti := textinput.New()
ti.Placeholder = placeholder
ti.SetValue(defaultValue)
ti.Blur()
return &TextInput{Model: ti, focused: false}
}
func (t *TextInput) Focus() tea.Cmd {
t.focused = true
return t.Model.Focus()
}
func (t *TextInput) Blur() {
t.focused = false
t.Model.Blur()
}
func (t *TextInput) IsFocused() bool {
return t.focused
}
// Button 适配 bubbles/button
type Button struct {
label string
focused bool
buttonBlur lipgloss.Style
buttonFocus lipgloss.Style
}
func NewButton(label string) *Button {
return &Button{
label: label,
focused: false,
buttonBlur: btnBaseStyle,
buttonFocus: btnSelectedStyle,
}
}
func (b *Button) Focus() tea.Cmd {
b.focused = true
b.buttonBlur = b.buttonFocus
return nil
}
func (b *Button) Blur() {
b.focused = false
b.buttonBlur = btnBaseStyle
}
func (b *Button) IsFocused() bool {
return b.focused
}
func (b *Button) View() string {
if b.focused {
return b.buttonFocus.Render(b.label)
}
return b.buttonBlur.Render(b.label)
}
// List 适配 bubbles/list
type List struct {
list.Model
focused bool
}
func NewList(items []list.Item) List {
l := list.New(items, list.NewDefaultDelegate(), 0, 0)
l.SetShowHelp(false)
return List{Model: l, focused: false}
}
func (l *List) Focus() tea.Cmd {
l.focused = true
return nil
}
func (l *List) Blur() {
l.focused = false
}
func (l *List) IsFocused() bool {
return l.focused
}
// FocusManager 焦点管理器
type FocusManager struct {
// 所有可聚焦组件key=唯一标识,比如 "form1.ip_input"、"form1.next_btn"
components map[string]Focusable
// 组件切换顺序(按这个顺序切换焦点)
order []string
// 当前焦点组件的标识
currentFocusID string
// 是否循环切换(到最后一个后回到第一个)
loop bool
}
// NewFocusManager 创建焦点管理器
func NewFocusManager(loop bool) *FocusManager {
return &FocusManager{
components: make(map[string]Focusable),
order: make([]string, 0),
currentFocusID: "",
loop: loop,
}
}
/*
Register 注册可聚焦组件(指定标识和切换顺序)
ID 组件的唯一标识,用于后续切换和获取焦点
例如 "form1.ip_input"、"form1.next_btn"
*/
func (fm *FocusManager) Register(id string, comp Focusable) {
// 防御性检查:避免 components 未初始化为nil导致 panic
if fm.components == nil {
fm.components = make(map[string]Focusable)
}
// 避免重复注册
if _, exists := fm.components[id]; exists {
return
}
// id : accept_btn, form1.reject_btn
// comp: 接受协议按钮, 拒绝协议按钮
fm.components[id] = comp
fm.order = append(fm.order, id)
// 如果是第一个注册的组件,默认聚焦
if fm.currentFocusID == "" {
fm.currentFocusID = id
comp.Focus()
}
}
// Next 切换到下一个组件
func (fm *FocusManager) Next() tea.Cmd {
if len(fm.order) == 0 {
return nil
}
// 1. 找到当前组件的索引
currentIdx := -1
for i, id := range fm.order {
if id == fm.currentFocusID {
currentIdx = i
break
}
}
// 2. 计算下一个索引
nextIdx := currentIdx + 1
if fm.loop && nextIdx >= len(fm.order) {
nextIdx = 0
}
if nextIdx >= len(fm.order) {
return nil // 不循环则到最后一个停止
}
// 3. 切换焦点(当前组件失活,下一个激活)
fm.components[fm.currentFocusID].Blur()
nextID := fm.order[nextIdx]
fm.currentFocusID = nextID
return fm.components[nextID].Focus()
}
// Prev 切换到上一个组件
func (fm *FocusManager) Prev() tea.Cmd {
if len(fm.order) == 0 {
return nil
}
currentIdx := -1
for i, id := range fm.order {
if id == fm.currentFocusID {
currentIdx = i
break
}
}
prevIdx := currentIdx - 1
if fm.loop && prevIdx < 0 {
prevIdx = len(fm.order) - 1
}
if prevIdx < 0 {
return nil
}
//fm.components[fm.currentFocusID].Blur()
fm.components[fm.currentFocusID].Blur()
prevID := fm.order[prevIdx]
fm.currentFocusID = prevID
return fm.components[prevID].Focus()
}
// GetCurrent 获取当前焦点组件
func (fm *FocusManager) GetCurrent() (Focusable, bool) {
comp, exists := fm.components[fm.currentFocusID]
return comp, exists
}
// HandleInput 统一处理焦点切换输入(比如 Tab/Shift+Tab
func (fm *FocusManager) HandleInput(msg tea.KeyMsg) tea.Cmd {
switch msg.String() {
case "tab": // Tab 下一个
return fm.Next()
case "shift+tab": // Shift+Tab 上一个
return fm.Prev()
case "left": // Left 上一个
return fm.Prev()
case "right": // Right 下一个
return fm.Next()
default:
return nil
}
}
func (m *model) switchPage(targetPage PageType) tea.Cmd {
// 边界检查(不能超出 1-6 页面)
if targetPage < PageAgreement || targetPage > PageSummary {
return nil
}
// 更新当前页面
m.currentPage = targetPage
// 初始化新页面的焦点
m.initPageFocus(targetPage)
// 返回空指令(或返回第一个组件的Focus命令)
return nil
}
func (m *model) initPageFocus(page PageType) {
m.focusManager = NewFocusManager(true)
pageComps, exists := m.pageComponents[page]
if !exists {
return
}
var componentOrder []string
var defaultFocusID string
switch page {
case PageAgreement:
componentOrder = []string{"accept_btn", "reject_btn"}
defaultFocusID = "accept_btn"
case PageData:
componentOrder = []string{
"Homepage_input",
"ClusterName_input",
"Country_input",
"State_input",
"City_input",
"Contact_input",
"Timezone_input",
"DistroDir_input",
"next_btn",
"prev_btn",
}
defaultFocusID = "next_btn"
case PagePublicNetwork:
componentOrder = []string{
"PublicHostname_input",
"PublicInterface_input",
"PublicIPAddress_input",
"PublicNetmask_input",
"PublicGateway_input",
"PublicDomain_input",
"PublicMTU_input",
"next_btn",
"prev_btn",
}
defaultFocusID = "next_btn"
case PageInternalNetwork:
componentOrder = []string{
"PrivateHostname_input",
"PrivateInterface_input",
"PrivateIPAddress_input",
"PrivateNetmask_input",
"PrivateDomain_input",
"PrivateMTU_input",
"next_btn",
"prev_btn",
}
defaultFocusID = "next_btn"
case PageDNS:
componentOrder = []string{
"Pri_DNS_input",
"Sec_DNS_input",
"next_btn",
"prev_btn",
}
defaultFocusID = "next_btn"
case PageSummary:
componentOrder = []string{"confirm_btn", "cancel_btn"}
defaultFocusID = "confirm_btn"
}
for _, compID := range componentOrder {
if comp, exists := pageComps[compID]; exists {
m.focusManager.Register(compID, comp)
}
}
if defaultFocusID != "" {
if currentComp, exists := m.focusManager.GetCurrent(); exists {
currentComp.Blur()
}
if targetComp, exists := pageComps[defaultFocusID]; exists {
m.focusManager.currentFocusID = defaultFocusID
targetComp.Focus()
}
}
}

View File

@@ -7,39 +7,51 @@ import (
tea "github.com/charmbracelet/bubbletea"
)
// PageType 页面类型
type PageType int
// 总页码
const TotalPages = 6
// Config 系统配置结构
type Config struct {
// 协议
License string `json:"license"`
AgreementAccepted bool `json:"agreement_accepted"`
// 数据接收
Hostname string `json:"hostname"`
ClusterName string `json:"cluster_name"`
Country string `json:"country"`
Region string `json:"region"`
State string `json:"state"`
City string `json:"city"`
Contact string `json:"contact"`
Timezone string `json:"timezone"`
HomePage string `json:"homepage"`
DBAddress string `json:"db_address"`
DataAddress string `json:"data_address"`
DistroDir string `json:"distro_dir"`
// 公网设置
PublicHostname string `json:"public_hostname"`
PublicInterface string `json:"public_interface"`
PublicIPAddress string `json:"ip_address"`
PublicNetmask string `json:"netmask"`
PublicGateway string `json:"gateway"`
PublicDomain string `json:"public_domain"`
PublicMTU string `json:"public_mtu"`
// 内网配置
InternalInterface string `json:"internal_interface"`
InternalIPAddress string `json:"internal_ip"`
InternalNetmask string `json:"internal_mask"`
PrivateHostname string `json:"private_hostname"`
PrivateInterface string `json:"private_interface"`
PrivateIPAddress string `json:"private_ip"`
PrivateNetmask string `json:"private_mask"`
PrivateDomain string `json:"private_domain"`
PrivateMTU string `json:"private_mtu"`
// DNS 配置
DNSPrimary string `json:"dns_primary"`
DNSSecondary string `json:"dns_secondary"`
}
// PageType 页面类型
type PageType int
const (
PageAgreement PageType = iota
PageData
@@ -49,29 +61,24 @@ const (
PageSummary
)
const (
FocusTypeInput int = 0
FocusTypePrev int = 1
FocusTypeNext int = 2
)
// model TUI 主模型
type model struct {
config Config
currentPage PageType
config Config // 全局配置
currentPage PageType // 当前页面
totalPages int
textInputs []textinput.Model // 当前页面的输入框
networkInterfaces []string // 所有系统网络接口
textInputs []textinput.Model
inputLabels []string // 存储标签
focusIndex int
focusType int // 0=输入框, 1=上一步按钮, 2=下一步按钮
agreementIdx int // 0=拒绝1=接受
width int
height int
err error
quitting bool
done bool
force bool
// 核心1: 按页面分组存储所有组件(6个页面 + 6个map)
pageComponents map[PageType]map[string]Focusable
// 核心2焦点管理器(每次切换页面时重置)
focusManager *FocusManager
}
// defaultConfig 返回默认配置
@@ -97,20 +104,29 @@ func defaultConfig() Config {
}
return Config{
Hostname: "cluster.hpc.org",
License: "This test license is for testing purposes only. Do not use it in production.",
ClusterName: "cluster.hpc.org",
Country: "China",
Region: "Beijing",
State: "Beijing",
City: "Beijing",
Contact: "admin@sunhpc.com",
Timezone: "Asia/Shanghai",
HomePage: "www.sunhpc.com",
DBAddress: "/var/lib/sunhpc/sunhpc.db",
DataAddress: "/export/sunhpc",
DistroDir: "/export/sunhpc",
PublicHostname: "cluster.hpc.org",
PublicInterface: defaultPublicInterface,
PublicIPAddress: "",
PublicNetmask: "",
PublicGateway: "",
InternalInterface: defaultInternalInterface,
InternalIPAddress: "172.16.9.254",
InternalNetmask: "255.255.255.0",
PublicDomain: "hpc.org",
PublicMTU: "1500",
PrivateHostname: "cluster",
PrivateInterface: defaultInternalInterface,
PrivateIPAddress: "172.16.9.254",
PrivateNetmask: "255.255.255.0",
PrivateDomain: "local",
PrivateMTU: "1500",
DNSPrimary: "8.8.8.8",
DNSSecondary: "8.8.4.4",
}
@@ -119,18 +135,83 @@ func defaultConfig() Config {
// initialModel 初始化模型
func initialModel() model {
cfg := defaultConfig()
// 1. 初始化所有页面组件(6个页面)
pageComponents := make(map[PageType]map[string]Focusable)
// ------------------ 页面1协议页面 --------------------
page1Comps := make(map[string]Focusable)
page1Comps["accept_btn"] = NewButton("接受协议")
page1Comps["reject_btn"] = NewButton("拒绝协议")
pageComponents[PageAgreement] = page1Comps
// ------------------ 页面2基础信息页面 --------------------
page2Comps := make(map[string]Focusable)
page2Comps["Homepage_input"] = NewTextInput("Homepage", cfg.HomePage)
page2Comps["ClusterName_input"] = NewTextInput("ClusterName", cfg.ClusterName)
page2Comps["Country_input"] = NewTextInput("Country", cfg.Country)
page2Comps["State_input"] = NewTextInput("State", cfg.State)
page2Comps["City_input"] = NewTextInput("City", cfg.City)
page2Comps["Contact_input"] = NewTextInput("Contact", cfg.Contact)
page2Comps["Timezone_input"] = NewTextInput("Timezone", cfg.Timezone)
page2Comps["DistroDir_input"] = NewTextInput("DistroDir", cfg.DistroDir)
page2Comps["next_btn"] = NewButton("下一步")
page2Comps["prev_btn"] = NewButton("上一步")
pageComponents[PageData] = page2Comps
// ------------------ 页面3公网网络页面 --------------------
page3Comps := make(map[string]Focusable)
page3Comps["PublicHostname_input"] = NewTextInput("PublicHostname", cfg.PublicHostname)
page3Comps["PublicInterface_input"] = NewTextInput("PublicInterface", cfg.PublicInterface)
page3Comps["PublicIPAddress_input"] = NewTextInput("PublicIPAddress", cfg.PublicIPAddress)
page3Comps["PublicNetmask_input"] = NewTextInput("PublicNetmask", cfg.PublicNetmask)
page3Comps["PublicGateway_input"] = NewTextInput("PublicGateway", cfg.PublicGateway)
page3Comps["PublicDomain_input"] = NewTextInput("PublicDomain", cfg.PublicDomain)
page3Comps["PublicMTU_input"] = NewTextInput("PublicMTU", cfg.PublicMTU)
page3Comps["next_btn"] = NewButton("下一步")
page3Comps["prev_btn"] = NewButton("上一步")
pageComponents[PagePublicNetwork] = page3Comps
// ------------------ 页面4内网网络页面 --------------------
page4Comps := make(map[string]Focusable)
page4Comps["PrivateHostname_input"] = NewTextInput("PrivateHostname", cfg.PrivateHostname)
page4Comps["PrivateInterface_input"] = NewTextInput("PrivateInterface", cfg.PrivateInterface)
page4Comps["PrivateIPAddress_input"] = NewTextInput("PrivateIPAddress", cfg.PrivateIPAddress)
page4Comps["PrivateNetmask_input"] = NewTextInput("PrivateNetmask", cfg.PrivateNetmask)
page4Comps["PrivateDomain_input"] = NewTextInput("PrivateDomain", cfg.PrivateDomain)
page4Comps["PrivateMTU_input"] = NewTextInput("PrivateMTU", cfg.PrivateMTU)
page4Comps["next_btn"] = NewButton("下一步")
page4Comps["prev_btn"] = NewButton("上一步")
pageComponents[PageInternalNetwork] = page4Comps
// ------------------ 页面5DNS页面 --------------------
page5Comps := make(map[string]Focusable)
page5Comps["Pri_DNS_input"] = NewTextInput("Pri DNS", cfg.DNSPrimary)
page5Comps["Sec_DNS_input"] = NewTextInput("Sec DNS", cfg.DNSSecondary)
page5Comps["next_btn"] = NewButton("下一步")
page5Comps["prev_btn"] = NewButton("上一步")
pageComponents[PageDNS] = page5Comps
// ------------------ 页面6Summary页面 --------------------
page6Comps := make(map[string]Focusable)
page6Comps["confirm_btn"] = NewButton("Confirm")
page6Comps["cancel_btn"] = NewButton("Cancel")
pageComponents[PageSummary] = page6Comps
// 创建焦点管理器(初始化聚焦页)
fm := NewFocusManager(true)
// 初始化模型
m := model{
config: cfg,
totalPages: 6,
textInputs: make([]textinput.Model, 0),
inputLabels: make([]string, 0),
agreementIdx: 1,
focusIndex: 0,
focusType: 0, // 0=输入框, 1=上一步按钮, 2=下一步按钮
width: 80,
height: 24,
currentPage: PageAgreement, // 初始化聚焦在协议页面
pageComponents: pageComponents,
focusManager: fm,
}
m.initPageInputs()
// 初始化当前页 (页1) 的焦点
m.initPageFocus(m.currentPage)
return m
}
@@ -141,214 +222,120 @@ func (m model) Init() tea.Cmd {
// Update 处理消息更新
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
m.quitting = true
//m.quitting = true
return m, tea.Quit
case "esc":
if m.currentPage > 0 {
return m.prevPage()
}
// 1. 焦点切换Tab/Shift+Tab交给管理器处理
case "tab", "shift+tab", "left", "right":
cmd := m.focusManager.HandleInput(msg)
return m, cmd
// 2. 回车键:处理当前焦点组件的点击/确认
case "enter":
return m.handleEnter()
case "tab", "shift+tab", "up", "down", "left", "right":
return m.handleNavigation(msg)
}
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
// 动态调整容器宽度
/*
if msg.Width > 100 {
containerStyle = containerStyle.Width(90)
} else if msg.Width > 80 {
containerStyle = containerStyle.Width(70)
} else {
containerStyle = containerStyle.Width(msg.Width - 10)
}
*/
// ✅ 动态计算容器宽度(终端宽度的 80%
containerWidth := msg.Width * 80 / 100
// ✅ 重新设置容器样式宽度
containerStyle = containerStyle.Width(containerWidth)
// 动态设置协议框宽度(容器宽度的 90%
agreementWidth := containerWidth * 80 / 100
agreementBox = agreementBox.Width(agreementWidth)
// 动态设置输入框宽度
inputWidth := containerWidth * 60 / 100
if inputWidth < 40 {
inputWidth = 40
}
inputBox = inputBox.Width(inputWidth)
// 动态设置总结框宽度
summaryWidth := containerWidth * 90 / 100
summaryBox = summaryBox.Width(summaryWidth)
return m, nil
}
// 更新当前焦点的输入框
if len(m.textInputs) > 0 && m.focusIndex < len(m.textInputs) {
var cmd tea.Cmd
m.textInputs[m.focusIndex], cmd = m.textInputs[m.focusIndex].Update(msg)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
// handleEnter 处理回车事件
func (m *model) handleEnter() (tea.Model, tea.Cmd) {
switch m.currentPage {
case PageAgreement:
if m.agreementIdx == 1 {
m.config.AgreementAccepted = true
return m.nextPage()
} else {
currentCompID := m.focusManager.currentFocusID
switch currentCompID {
// 页1accept → 进入页2
case "accept_btn":
return m, m.switchPage(PageData)
// 页1reject → 退出程序
case "reject_btn":
m.quitting = true
return m, tea.Quit
}
case PageData, PagePublicNetwork, PageInternalNetwork, PageDNS:
// 根据焦点类型执行不同操作
switch m.focusType {
case FocusTypeInput:
// 在输入框上,保存并下一页
m.saveCurrentPage()
return m.nextPage()
case FocusTypePrev:
// 上一步按钮,返回上一页
return m.prevPage()
case FocusTypeNext:
// 下一步按钮,切换到下一页
m.saveCurrentPage()
return m.nextPage()
}
// 通用上一页/下一页逻辑
case "prev_btn":
return m, m.switchPage(m.currentPage - 1)
case "next_btn":
return m, m.switchPage(m.currentPage + 1)
case PageSummary:
switch m.focusIndex {
case 0: // 执行
// 页6确认配置 → 退出并保存
case "confirm_btn":
m.saveConfig()
m.done = true
if err := m.saveConfig(); err != nil {
m.err = err
return m, nil
}
//m.quitting = true
return m, tea.Quit
case 1: // 取消
case "cancel_btn":
m.quitting = true
return m, tea.Quit
}
}
return m, nil
}
// handleNavigation 处理导航
func (m *model) handleNavigation(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
// debug
//fmt.Fprintf(os.Stderr, "DEBUG: key=%s page=%d\n", msg.String(), m.currentPage)
switch m.currentPage {
case PageAgreement:
switch msg.String() {
case "left", "right", "tab", "shift+tab", "up", "down":
m.agreementIdx = 1 - m.agreementIdx
// 其他消息(窗口大小、输入框输入等)...
}
case PageSummary:
switch msg.String() {
case "left", "right", "tab", "shift+tab":
m.focusIndex = 1 - m.focusIndex
}
// 处理当前焦点组件的内部更新(比如输入框打字、列表选值)
currentComp, exists := m.focusManager.GetCurrent()
if exists {
// 不同组件的内部更新逻辑(示例)
switch comp := currentComp.(type) {
case *TextInput:
// 输入框更新
newTI, cmd := comp.Model.Update(msg)
comp.Model = newTI
default:
// 输入框页面: 支持输入框和按钮之间切换
// totalFocusable := len(m.textInputs) + 2
// 保存输入值到全局配置(示例:主机名)
switch m.focusManager.currentFocusID {
// 页2基础信息
case "Homepage_input":
m.config.HomePage = comp.Value()
case "ClusterName_input":
m.config.ClusterName = comp.Value()
case "Country_input":
m.config.Country = comp.Value()
case "State_input":
m.config.State = comp.Value()
case "City_input":
m.config.City = comp.Value()
case "Contact_input":
m.config.Contact = comp.Value()
case "Timezone_input":
m.config.Timezone = comp.Value()
case "DistroDir_input":
m.config.DistroDir = comp.Value()
// 页3公网网络
case "PublicInterface_input":
m.config.PublicInterface = comp.Value()
case "PublicIPAddress_input":
m.config.PublicIPAddress = comp.Value()
case "PublicNetmask_input":
m.config.PublicNetmask = comp.Value()
case "PublicGateway_input":
m.config.PublicGateway = comp.Value()
case "PublicMTU_input":
m.config.PublicMTU = comp.Value()
// 页4内网网络
case "PrivateInterface_input":
m.config.PrivateInterface = comp.Value()
case "PrivateIPAddress_input":
m.config.PrivateIPAddress = comp.Value()
case "PrivateNetmask_input":
m.config.PrivateNetmask = comp.Value()
case "PrivateMTU_input":
m.config.PrivateMTU = comp.Value()
// 页5DNS
case "Pri_DNS_input":
m.config.DNSPrimary = comp.Value()
case "Sec_DNS_input":
m.config.DNSSecondary = comp.Value()
switch msg.String() {
case "down", "tab":
// 当前在输入框
switch m.focusType {
case FocusTypeInput:
if m.focusIndex < len(m.textInputs)-1 {
// 切换到下一个输入框
m.textInputs[m.focusIndex].Blur()
m.focusIndex++
m.textInputs[m.focusIndex].Focus()
} else {
// 最后一个输入框,切换到“下一步”按钮
m.textInputs[m.focusIndex].Blur()
m.focusIndex = 0
m.focusType = FocusTypeNext // 下一步按钮
}
case FocusTypePrev:
// 当前在“上一步”按钮,切换到第一个输入框
m.focusType = FocusTypeInput
m.focusIndex = 0
m.textInputs[0].Focus()
case FocusTypeNext:
// 当前在“下一步”按钮,切换到“上一步”按钮
m.focusType = FocusTypePrev
}
case "up", "shift+tab":
// 当前在输入框
switch m.focusType {
case FocusTypeInput:
if m.focusIndex > 0 {
// 切换到上一个输入框
m.textInputs[m.focusIndex].Blur()
m.focusIndex--
m.textInputs[m.focusIndex].Focus()
} else {
// 第一个输入框,切换到“上一步”按钮
m.textInputs[m.focusIndex].Blur()
m.focusIndex = 0
m.focusType = FocusTypePrev // 上一步按钮
}
case FocusTypeNext:
// 当前在“下一步”按钮,切换到最后一个输入框
m.focusType = FocusTypeInput
m.focusIndex = len(m.textInputs) - 1
m.textInputs[m.focusIndex].Focus()
case FocusTypePrev:
// 当前在“上一步”按钮,切换到“下一步”按钮
m.focusType = FocusTypeNext
}
return m, cmd
case *List:
// 列表更新
newList, cmd := comp.Model.Update(msg)
comp.Model = newList
return m, cmd
}
}
return m, nil
}
// nextPage 下一页
func (m *model) nextPage() (tea.Model, tea.Cmd) {
if m.currentPage < PageSummary {
m.currentPage++
m.focusIndex = 0
m.initPageInputs()
}
return m, textinput.Blink
}
// prevPage 上一页
func (m *model) prevPage() (tea.Model, tea.Cmd) {
if m.currentPage > 0 {
m.saveCurrentPage()
m.currentPage--
m.focusIndex = 0
m.initPageInputs()
}
return m, textinput.Blink
}

View File

@@ -1,12 +1,14 @@
package wizard
import (
"fmt"
"strings"
"github.com/charmbracelet/lipgloss"
)
var split_line = splitlineStyle.Render(
"───────────────────────────────────────────────────────────────")
// View 渲染视图
func (m model) View() string {
if m.done {
@@ -19,38 +21,39 @@ func (m model) View() string {
return errorView(m.err)
}
var page string
var pageContent string
switch m.currentPage {
case PageAgreement:
page = m.agreementView()
pageContent = renderLicensePage(m)
case PageData:
page = m.dataView()
pageContent = renderDataInfoPage(m)
case PagePublicNetwork:
page = m.publicNetworkView()
pageContent = renderPublicNetworkPage(m)
case PageInternalNetwork:
page = m.internalNetworkView()
pageContent = renderInternalNetworkPage(m)
case PageDNS:
page = m.dnsView()
pageContent = renderDNSPage(m)
case PageSummary:
page = m.summaryView()
pageContent = renderSummaryPage(m)
default:
pageContent = appStyle.Render("无效页面")
}
content := strings.Builder{}
content.WriteString(page)
content.WriteString("\n\n")
content.WriteString(progressView(m.currentPage, m.totalPages))
return containerStyle.Render(content.String())
return appStyle.Render(pageContent)
}
// agreementView 协议页面
func (m model) agreementView() string {
func makeRow(label, value string) string {
return lipgloss.JoinHorizontal(lipgloss.Left,
labelStyle.Render(label+":"),
valueStyle.Render(value),
)
}
func renderLicensePage(m model) string {
title := titleStyle.Render("SunHPC Software License Agreement")
agreement := agreementBox.Render(`
─────────────────────────────────────────────────────────────
│ SunHPC License Agreement │
└─────────────────────────────────────────────────────────────┘
licenseText := `
───────────────────────────────────────────────────────────────
1. License Grant
This software grants you a non-exclusive, non-transferable
license to use it.
@@ -69,288 +72,288 @@ func (m model) agreementView() string {
PLEASE READ THE ABOVE TERMS CAREFULLY AND CLICK "ACCEPT"
TO AGREE AND FOLLOW THIS AGREEMENT.
───────────────────────────────────────────────────────────────
`)
`
var acceptBtn, rejectBtn string
if m.agreementIdx == 0 {
rejectBtn = selectedButton.Render(">> Reject <<")
acceptBtn = " Accept "
} else {
rejectBtn = " Reject "
acceptBtn = selectedButton.Render(">> Accept <<")
}
buttonGroup := lipgloss.JoinHorizontal(
lipgloss.Center,
acceptBtn, " ", rejectBtn)
pageComps := m.pageComponents[PageAgreement]
acceptBtn := pageComps["accept_btn"].View()
rejectBtn := pageComps["reject_btn"].View()
// ✅ 添加调试信息(确认 agreementIdx 的值)
// debugInfo := lipgloss.NewStyle().Foreground(lipgloss.Color("#888888")).
// Render(fmt.Sprintf("[DEBUG: idx=%d]", m.agreementIdx),)
hint := hintStyle.Render("Use Up/Down OR Tab Change,Enter Confirm")
return lipgloss.JoinVertical(lipgloss.Center,
hint := hintStyle.Render("Use Left/Right OR Tab Change,Enter Confirm")
pageContent := lipgloss.JoinVertical(lipgloss.Center,
title, "",
agreement, "",
buttonGroup, "",
// debugInfo, "", // ✅ 显示调试信息
licenseTextStyle.Render(licenseText),
lipgloss.JoinHorizontal(lipgloss.Center, acceptBtn, rejectBtn),
hint,
)
return appStyle.Render(pageContent)
}
// dataView 数据接收页面
func (m model) dataView() string {
title := titleStyle.Render("Cluster Information")
// ---------------- 页2基础信息页渲染 ----------------
func renderDataInfoPage(m model) string {
pageComps := m.pageComponents[PageData]
var inputs strings.Builder
for i, ti := range m.textInputs {
info := fmt.Sprintf("%-10s|", m.inputLabels[i])
input := inputBox.Render(info + ti.View())
inputs.WriteString(input + "\n")
}
// 拼接基础信息表单
formContent := lipgloss.JoinVertical(lipgloss.Center,
split_line,
makeRow("Homepage", pageComps["Homepage_input"].View()),
split_line,
makeRow("ClusterName", pageComps["ClusterName_input"].View()),
split_line,
makeRow("Country", pageComps["Country_input"].View()),
split_line,
makeRow("State", pageComps["State_input"].View()),
split_line,
makeRow("City", pageComps["City_input"].View()),
split_line,
makeRow("Contact", pageComps["Contact_input"].View()),
split_line,
makeRow("Timezone", pageComps["Timezone_input"].View()),
split_line,
makeRow("DistroDir", pageComps["DistroDir_input"].View()),
split_line,
)
buttons := m.renderNavButtons()
hint := hintStyle.Render("Use Up/Down OR Tab Change,Enter Confirm")
return lipgloss.JoinVertical(lipgloss.Center,
title, "",
inputs.String(), "",
buttons, "",
// 按钮区域
btnArea := lipgloss.JoinHorizontal(
lipgloss.Center,
pageComps["next_btn"].View(),
pageComps["prev_btn"].View(),
)
hint := hintStyle.Render("Use Left/Right OR Tab Change,Enter Confirm")
// 页面整体
pageContent := lipgloss.JoinVertical(
lipgloss.Center,
titleStyle.Render("基础信息配置(页2/6)"),
formContent,
btnArea,
hint,
)
return appStyle.Render(pageContent)
}
// publicNetworkView 公网设置页面
func (m model) publicNetworkView() string {
title := titleStyle.Render("Public Network Configuration")
func renderPublicNetworkPage(m model) string {
pageComps := m.pageComponents[PagePublicNetwork]
// 拼接公网网络表单
formContent := lipgloss.JoinVertical(lipgloss.Center,
split_line,
makeRow("PublicHostname", pageComps["PublicHostname_input"].View()),
split_line,
makeRow("PublicInterface", pageComps["PublicInterface_input"].View()),
split_line,
makeRow("PublicIPAddress", pageComps["PublicIPAddress_input"].View()),
split_line,
makeRow("PublicNetmask", pageComps["PublicNetmask_input"].View()),
split_line,
makeRow("PublicGateway", pageComps["PublicGateway_input"].View()),
split_line,
makeRow("PublicDomain", pageComps["PublicDomain_input"].View()),
split_line,
makeRow("PublicMTU", pageComps["PublicMTU_input"].View()),
split_line,
)
// 按钮区域
btnArea := lipgloss.JoinHorizontal(
lipgloss.Center,
pageComps["next_btn"].View(),
pageComps["prev_btn"].View(),
)
networkInterfaces := getNetworkInterfaces()
autoDetect := infoStyle.Render(
"[*] Auto Detect Network Interfaces: " + strings.Join(networkInterfaces, ", "))
"[*] Auto Detect Interfaces: " + strings.Join(networkInterfaces, ", "))
var inputs strings.Builder
for i, ti := range m.textInputs {
info := fmt.Sprintf("%-20s|", m.inputLabels[i])
input := inputBox.Render(info + ti.View())
inputs.WriteString(input + "\n")
}
hint := hintStyle.Render("Use Left/Right OR Tab Change,Enter Confirm")
buttons := m.renderNavButtons()
hint := hintStyle.Render("Use Up/Down OR Tab Change,Enter Confirm")
return lipgloss.JoinVertical(lipgloss.Center,
title, "",
autoDetect, "",
inputs.String(), "",
buttons, "",
// 页面整体
pageContent := lipgloss.JoinVertical(
lipgloss.Center,
titleStyle.Render("公网网络配置(页3/6)"),
autoDetect,
formContent,
btnArea,
hint,
)
return appStyle.Render(pageContent)
}
// internalNetworkView 内网配置页面
func (m model) internalNetworkView() string {
title := titleStyle.Render("Internal Network Configuration")
func renderInternalNetworkPage(m model) string {
pageComps := m.pageComponents[PageInternalNetwork]
networkInterfaces := getNetworkInterfaces()
autoDetect := infoStyle.Render(
"[*] Auto Detect Network Interfaces: " + strings.Join(networkInterfaces, ", "))
// 拼接内网网络表单
formContent := lipgloss.JoinVertical(lipgloss.Center,
split_line,
makeRow("PrivateHostname", pageComps["PrivateHostname_input"].View()),
split_line,
makeRow("PrivateInterface", pageComps["PrivateInterface_input"].View()),
split_line,
makeRow("PrivateIPAddress", pageComps["PrivateIPAddress_input"].View()),
split_line,
makeRow("PrivateNetmask", pageComps["PrivateNetmask_input"].View()),
split_line,
makeRow("PrivateDomain", pageComps["PrivateDomain_input"].View()),
split_line,
makeRow("PrivateMTU", pageComps["PrivateMTU_input"].View()),
split_line,
)
var inputs strings.Builder
for i, ti := range m.textInputs {
info := fmt.Sprintf("%-20s|", m.inputLabels[i])
input := inputBox.Render(info + ti.View())
inputs.WriteString(input + "\n")
}
// 按钮区域
btnArea := lipgloss.JoinHorizontal(
lipgloss.Center,
pageComps["next_btn"].View(),
pageComps["prev_btn"].View(),
)
buttons := m.renderNavButtons()
hint := hintStyle.Render("Use Up/Down OR Tab Change,Enter Confirm")
hint := hintStyle.Render("Use Left/Right OR Tab Change,Enter Confirm")
return lipgloss.JoinVertical(lipgloss.Center,
title, "",
autoDetect, "",
inputs.String(), "",
buttons, "",
// 页面整体
pageContent := lipgloss.JoinVertical(
lipgloss.Center,
titleStyle.Render("内网网络配置(页4/6)"),
formContent,
btnArea,
hint,
)
return appStyle.Render(pageContent)
}
// dnsView DNS 配置页面
func (m model) dnsView() string {
title := titleStyle.Render("DNS Configuration")
func renderDNSPage(m model) string {
pageComps := m.pageComponents[PageDNS]
var inputs strings.Builder
for i, ti := range m.textInputs {
info := fmt.Sprintf("%-10s|", m.inputLabels[i])
input := inputBox.Render(info + ti.View())
inputs.WriteString(input + "\n")
}
// 拼接 DNS 表单
formContent := lipgloss.JoinVertical(lipgloss.Center,
split_line,
makeRow("Pri DNS", pageComps["Pri_DNS_input"].View()),
split_line,
makeRow("Sec DNS", pageComps["Sec_DNS_input"].View()),
split_line,
)
buttons := m.renderNavButtons()
hint := hintStyle.Render("Use Up/Down OR Tab Change,Enter Confirm")
// 按钮区域
btnArea := lipgloss.JoinHorizontal(
lipgloss.Center,
pageComps["next_btn"].View(),
pageComps["prev_btn"].View(),
)
return lipgloss.JoinVertical(lipgloss.Center,
title, "",
inputs.String(), "",
buttons, "",
hint := hintStyle.Render("Use Left/Right OR Tab Change,Enter Confirm")
// 页面整体
pageContent := lipgloss.JoinVertical(
lipgloss.Center,
titleStyle.Render("DNS 配置(页5/6)"),
formContent,
btnArea,
hint,
)
return appStyle.Render(pageContent)
}
// summaryView 总结页面
func (m model) summaryView() string {
title := titleStyle.Render("Summary")
subtitle := subTitleStyle.Render("Please confirm the following configuration information")
func renderSummaryPage(m model) string {
pageComps := m.pageComponents[PageSummary]
summary := summaryBox.Render(fmt.Sprintf(`
+----------------------------------------------------+
Basic Information
+----------------------------------------------------+
Homepage : %-38s
Hostname : %-35s
Country : %-31s
Region : %-31s
Timezone : %-38s
Homepage : %-38s
+----------------------------------------------------+
Database Configuration
+----------------------------------------------------+
Database Path : %-38s
Software : %-33s
+----------------------------------------------------+
Public Network Configuration
+----------------------------------------------------+
Public Interface : %-38s
Public IP : %-41s
Public Netmask : %-38s
Public Gateway : %-38s
+----------------------------------------------------+
Internal Network Configuration
+----------------------------------------------------+
Internal Interface: %-38s
Internal IP : %-41s
Internal Netmask : %-38s
+----------------------------------------------------+
DNS Configuration
+----------------------------------------------------+
Primary DNS : %-37s
Secondary DNS : %-37s
+----------------------------------------------------+
`,
m.config.HomePage,
m.config.Hostname,
m.config.Country,
m.config.Region,
m.config.Timezone,
m.config.HomePage,
m.config.DBAddress,
m.config.DataAddress,
m.config.PublicInterface,
m.config.PublicIPAddress,
m.config.PublicNetmask,
m.config.PublicGateway,
m.config.InternalInterface,
m.config.InternalIPAddress,
m.config.InternalNetmask,
m.config.DNSPrimary,
m.config.DNSSecondary,
))
// 拼接 Summary 表单
formContent := lipgloss.JoinVertical(lipgloss.Center,
split_line,
makeRow("ClusterName", m.config.ClusterName),
split_line,
makeRow("Country", m.config.Country),
split_line,
makeRow("State", m.config.State),
split_line,
makeRow("City", m.config.City),
split_line,
makeRow("Contact", m.config.Contact),
split_line,
makeRow("Timezone", m.config.Timezone),
split_line,
makeRow("Homepage", m.config.HomePage),
split_line,
makeRow("DBPath", m.config.DBAddress),
split_line,
makeRow("DistroDir", m.config.DistroDir),
split_line,
makeRow("PublicInterface", m.config.PublicInterface),
split_line,
makeRow("PublicIPAddress", m.config.PublicIPAddress),
split_line,
makeRow("PublicNetmask", m.config.PublicNetmask),
split_line,
makeRow("PublicGateway", m.config.PublicGateway),
split_line,
makeRow("PrivateInterface", m.config.PrivateInterface),
split_line,
makeRow("PrivateIPAddress", m.config.PrivateIPAddress),
split_line,
makeRow("PrivateNetmask", m.config.PrivateNetmask),
split_line,
makeRow("PrivateMTU", m.config.PrivateMTU),
split_line,
makeRow("Pri DNS", m.config.DNSPrimary),
split_line,
makeRow("Sec DNS", m.config.DNSSecondary),
split_line,
)
var buttons string
if m.focusIndex == 0 {
buttons = selectedButton.Render("[>] Start Initialization") + " " + normalButton.Render("[ ] Cancel")
} else {
buttons = normalButton.Render("[>] Start Initialization") + " " + selectedButton.Render("[ ] Cancel")
}
// 按钮区域
btnArea := lipgloss.JoinHorizontal(
lipgloss.Center,
pageComps["confirm_btn"].View(),
pageComps["cancel_btn"].View(),
)
hint := hintStyle.Render("Use Up/Down OR Tab Change,Enter Confirm")
hint := hintStyle.Render("Use Left/Right OR Tab Change,Enter Confirm")
return lipgloss.JoinVertical(lipgloss.Center,
title, "",
subtitle, "",
summary, "",
buttons, "",
// 页面整体
pageContent := lipgloss.JoinVertical(
lipgloss.Center,
titleStyle.Render("确认信息(页6/6)"),
formContent,
btnArea,
hint,
)
return appStyle.Render(pageContent)
}
// progressView 进度条
func progressView(current PageType, total int) string {
progress := ""
for i := 0; i < total; i++ {
if i < int(current) {
progress += "[+]"
} else if i == int(current) {
progress += "[-]"
} else {
progress += "[ ]"
}
if i < total-1 {
progress += " "
}
}
labels := []string{"License", "Data", "Network", "Network", "DNS", "Summary"}
label := labelStyle.Render(labels[current])
return progressStyle.Render(progress) + " " + label
}
// successView 成功视图
func successView() string {
return containerStyle.Render(lipgloss.JoinVertical(lipgloss.Center,
content := lipgloss.JoinVertical(lipgloss.Center,
successTitle.Render("Initialization Completed!"), "",
successMsg.Render("System configuration has been saved, and the system is initializing..."), "",
hintStyle.Render("Press any key to exit"),
))
successMsg.Render(
"System configuration has been saved, and the system is initializing..."), "",
)
return appStyle.Render(content)
}
// quitView 退出视图
func quitView() string {
return containerStyle.Render(lipgloss.JoinVertical(lipgloss.Center,
content := lipgloss.JoinVertical(lipgloss.Center,
split_line,
errorTitle.Render("Canceled"), "",
errorMsg.Render("Initialization canceled, no configuration saved"),
))
split_line,
"\n",
)
return quitStyle.Render(content)
}
// errorView 错误视图
func errorView(err error) string {
return containerStyle.Render(lipgloss.JoinVertical(lipgloss.Center,
content := lipgloss.JoinVertical(lipgloss.Center,
errorTitle.Render("Error"), "",
errorMsg.Render(err.Error()), "",
hintStyle.Render("Press Ctrl+C to exit"),
))
}
// navButtons 导航按钮
func navButtons(m model, prev, next string) string {
var btns string
if m.currentPage == 0 {
btns = normalButton.Render(next) + " " + selectedButton.Render(prev)
} else {
btns = selectedButton.Render(next) + " " + normalButton.Render(prev)
}
return btns
}
func (m model) renderNavButtons() string {
var prevBtn, nextBtn string
switch m.focusType {
case FocusTypePrev:
// 焦点在"上一步"
nextBtn = normalButton.Render(" Next ")
prevBtn = selectedButton.Render(" << Prev >>")
case FocusTypeNext:
// 焦点在"下一步"
nextBtn = selectedButton.Render(" << Next >>")
prevBtn = normalButton.Render(" Prev ")
default:
// 焦点在输入框
nextBtn = normalButton.Render(" Next ")
prevBtn = normalButton.Render(" Prev ")
}
return lipgloss.JoinHorizontal(
lipgloss.Center,
nextBtn,
" ",
prevBtn,
)
return appStyle.Render(content)
}

View File

@@ -6,8 +6,12 @@ import "github.com/charmbracelet/lipgloss"
var (
primaryColor = lipgloss.Color("#7C3AED")
secondaryColor = lipgloss.Color("#10B981")
titleColor = lipgloss.Color("#8b19a2")
errorColor = lipgloss.Color("#EF4444")
warnColor = lipgloss.Color("#F59E0B")
btnTextColor = lipgloss.Color("#666666") // 深灰色
btnbordColor = lipgloss.Color("#3b4147")
btnFocusColor = lipgloss.Color("#ffffff")
// 背景色设为无,让终端自己的背景色生效,避免黑块
bgColor = lipgloss.Color("#1F2937")
@@ -16,82 +20,99 @@ var (
)
// 容器样式
var containerStyle = lipgloss.NewStyle().
Padding(2, 4).
var (
// 基础布局样式
appStyle = lipgloss.NewStyle().
Padding(1, 1).
MarginBottom(1).
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(primaryColor).
//Background(bgColor). // 注释掉背景色,防止在某些终端出现黑块
Foreground(textColor).
//Width(80).
Align(lipgloss.Center)
//Height(40)
// 标题样式
titleStyle = lipgloss.NewStyle().
Foreground(titleColor).
Padding(0, 1).
Bold(true).
Align(lipgloss.Center)
// 标题样式
var titleStyle = lipgloss.NewStyle().
Bold(true).
Foreground(primaryColor).
MarginBottom(1)
// 标题/标签样式
labelStyle = lipgloss.NewStyle().
Width(30).
Align(lipgloss.Right).
PaddingRight(2)
var subTitleStyle = lipgloss.NewStyle().
Foreground(mutedColor).
MarginBottom(2)
valueStyle = lipgloss.NewStyle().
Foreground(textColor).
Width(50)
// 按钮样式
var normalButton = lipgloss.NewStyle().
// 输入框/列表内容样式
inputBoxStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(btnbordColor).
Padding(0, 1).
Width(50)
// 按钮基础样式
btnBaseStyle = lipgloss.NewStyle().
Foreground(btnTextColor).
Padding(0, 2).
Foreground(lipgloss.Color("#666666")) // 深灰色,更暗
Margin(1, 1).
Border(lipgloss.RoundedBorder()).
BorderForeground(btnbordColor)
var selectedButton = lipgloss.NewStyle().
// 按钮选中/聚焦样式
btnSelectedStyle = lipgloss.NewStyle().
Foreground(btnFocusColor).
Padding(0, 2).
Margin(1, 1).
Border(lipgloss.RoundedBorder()).
BorderForeground(btnbordColor)
splitlineStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#888888"))
// 协议文本样式
licenseTextStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#f8f8f2")).
Width(76)
// 提示文本样式
hintStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#888888")).
Width(76)
infoStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#888888")).
Border(lipgloss.RoundedBorder()).
BorderForeground(btnbordColor)
// 成功/错误提示样式
successTitle = lipgloss.NewStyle().
Foreground(secondaryColor).
Bold(true)
// 输入框样式
var inputBox = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(primaryColor).
Padding(0, 1)
var labelStyle = lipgloss.NewStyle().
Foreground(mutedColor).
Width(12).
Align(lipgloss.Right)
// 协议框样式
var agreementBox = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(warnColor).
Padding(1, 2).
//Width(70).
Align(lipgloss.Left)
// 总结框样式
var summaryBox = lipgloss.NewStyle().
BorderStyle(lipgloss.DoubleBorder()).
BorderForeground(primaryColor).
Padding(0, 0).
successMsg = lipgloss.NewStyle().
Foreground(textColor)
// 进度条样式
var progressStyle = lipgloss.NewStyle().Foreground(primaryColor)
// 提示信息样式
var hintStyle = lipgloss.NewStyle().
Foreground(mutedColor).
Italic(true)
// 成功/错误样式
var successTitle = lipgloss.NewStyle().
// quit 提示样式
quitStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#888888")).
Bold(true).
Foreground(secondaryColor)
Width(76)
var successMsg = lipgloss.NewStyle().
Foreground(textColor)
// 错误提示样式
errorStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#ff5555")).
Bold(true).
Width(76)
var errorTitle = lipgloss.NewStyle().
errorTitle = lipgloss.NewStyle().
Bold(true).
Foreground(errorColor)
var errorMsg = lipgloss.NewStyle().
errorMsg = lipgloss.NewStyle().
Foreground(textColor)
var infoStyle = lipgloss.NewStyle().
Foreground(primaryColor).
Bold(true)
)

View File

@@ -2,20 +2,12 @@ package wizard
import (
"fmt"
"os"
tea "github.com/charmbracelet/bubbletea"
)
// Run 启动初始化向导
func Run(force bool) error {
// 检查是否已有配置
if !force && ConfigExists() {
fmt.Println("⚠️ 检测到已有配置文件")
fmt.Println(" 使用 --force 参数强制重新初始化")
fmt.Println(" 或运行 sunhpc init tui --force")
return nil
}
// 创建程序实例
p := tea.NewProgram(initialModel())
@@ -27,20 +19,3 @@ func Run(force bool) error {
return nil
}
// getConfigPath 获取配置文件路径
func GetConfigPath() string {
// 优先使用环境变量
if path := os.Getenv("SUNHPC_CONFIG"); path != "" {
return path
}
// 默认路径
return "/etc/sunhpc/config.json"
}
// configExists 检查配置文件是否存在
func ConfigExists() bool {
configPath := GetConfigPath()
_, err := os.Stat(configPath)
return err == nil
}