重构架构

This commit is contained in:
2026-02-20 18:44:43 +08:00
parent aba7b68439
commit cc71248ef4
52 changed files with 1404 additions and 2360 deletions

View File

@@ -0,0 +1,33 @@
package soft
import (
"fmt"
"os"
"os/exec"
"strings"
)
// extractBinary 解压二进制压缩包到目标目录
func extractBinary(binPath, destDir string) error {
// 确保目标目录存在
if err := os.MkdirAll(destDir, 0755); err != nil {
return err
}
// 根据扩展名选择解压命令
var cmd *exec.Cmd
switch {
case strings.HasSuffix(binPath, ".tar.gz"), strings.HasSuffix(binPath, ".tgz"):
cmd = exec.Command("tar", "xzf", binPath, "-C", destDir)
case strings.HasSuffix(binPath, ".tar.bz2"):
cmd = exec.Command("tar", "xjf", binPath, "-C", destDir)
case strings.HasSuffix(binPath, ".zip"):
cmd = exec.Command("unzip", binPath, "-d", destDir)
default:
return fmt.Errorf("不支持的压缩格式: %s", binPath)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

View File

@@ -0,0 +1,49 @@
package soft
import (
log "sunhpc/pkg/logger"
)
// InstallContext 安装上下文,包含所有命令行参数
type InstallContext struct {
Force bool // 强制安装
DryRun bool // 干运行模式
KeepSource bool // 保留源码文件
Jobs int // 编译线程数
Offline bool // 离线模式
}
// InstallFromSource 从源码编译安装
func InstallFromSource(name, srcPath, prefix, version string, ctx *InstallContext) error {
log.Infof("正在从源码安装 %s路径: %s", name, srcPath)
if ctx != nil && ctx.DryRun {
log.Infof("[干运行] 将执行: configure && make -j%d && make install", ctx.Jobs)
return nil
}
// TODO: 实现具体逻辑:下载、解压、./configure、make、make install
log.Info("源码安装模拟完成(需实现具体步骤)")
return nil
}
// InstallFromBinary 从二进制压缩包安装
func InstallFromBinary(name, binPath, prefix string, ctx *InstallContext) error {
log.Infof("正在安装二进制包 %s路径: %s", name, binPath)
if ctx != nil && ctx.DryRun {
log.Infof("[干运行] 将解压 %s 到 %s", binPath, prefix)
return nil
}
// TODO: 解压到 prefix
log.Info("二进制安装模拟完成(需实现具体步骤)")
return nil
}
// InstallFromPackage 通过系统包管理器安装
func InstallFromPackage(name, pkgType string, ctx *InstallContext) error {
log.Infof("正在通过包管理器安装 %s (%s)", name, pkgType)
if ctx != nil && ctx.DryRun {
log.Infof("[干运行] 将执行包管理器安装 %s", name)
return nil
}
// 具体实现在下面的 package.go 中
return installViaPackageManager(name, pkgType)
}

View File

@@ -0,0 +1,38 @@
package soft
import (
"fmt"
"os"
"os/exec"
log "sunhpc/pkg/logger"
"sunhpc/pkg/utils"
)
// installViaPackageManager 使用系统包管理器安装软件
func installViaPackageManager(name, pkgType string) error {
var cmd *exec.Cmd
switch pkgType {
case "rpm":
// RHEL/CentOS
if utils.CommandExists("yum") {
cmd = exec.Command("yum", "install", "-y", name)
} else if utils.CommandExists("dnf") {
cmd = exec.Command("dnf", "install", "-y", name)
} else {
return fmt.Errorf("未找到 yum 或 dnf 包管理器")
}
case "deb":
// Debian/Ubuntu
if !utils.CommandExists("apt-get") {
return fmt.Errorf("未找到 apt-get 包管理器")
}
cmd = exec.Command("apt-get", "install", "-y", name)
default:
return fmt.Errorf("不支持的包类型: %s", pkgType)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Infof("执行命令: %s", cmd.String())
return cmd.Run()
}

View File

@@ -0,0 +1,48 @@
package soft
import (
"fmt"
"os"
"os/exec"
log "sunhpc/pkg/logger"
"sunhpc/pkg/utils"
)
// compileFromSource 通用源码编译流程
func compileFromSource(srcDir, prefix string, jobs int) error {
// 切换到源码目录
if err := os.Chdir(srcDir); err != nil {
return fmt.Errorf("进入源码目录失败: %v", err)
}
// 检测 configure 脚本是否存在
if utils.FileExists("./configure") {
log.Debug("执行 configure ...")
cmd := exec.Command("./configure", "--prefix="+prefix)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("configure 失败: %v", err)
}
}
// make
log.Debugf("执行 make -j%d ...", jobs)
cmd := exec.Command("make", fmt.Sprintf("-j%d", jobs))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("make 失败: %v", err)
}
// make install
log.Debug("执行 make install ...")
cmd = exec.Command("make", "install")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("make install 失败: %v", err)
}
return nil
}