110 lines
3.3 KiB
Go
110 lines
3.3 KiB
Go
// cmd/soft/install.go
|
|
package soft
|
|
|
|
import (
|
|
"fmt"
|
|
"sunhpc/internal/auth"
|
|
"sunhpc/internal/log"
|
|
"sunhpc/internal/soft"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
installType string // --type, -t
|
|
srcPath string // --src-path, -s
|
|
binPath string // --bin-path, -b
|
|
prefix string // --prefix, -p
|
|
version string // --version, -v
|
|
forceInstall bool // --force, -f
|
|
dryRun bool // --dry-run, -n
|
|
keepSource bool // --keep-source, -k
|
|
jobs int // --jobs, -j
|
|
offlineMode bool // --offline, -o
|
|
)
|
|
|
|
var installCmd = &cobra.Command{
|
|
Use: "install <software>",
|
|
Short: "安装软件",
|
|
Long: `安装指定的软件包,支持多种安装方式。
|
|
|
|
安装类型:
|
|
source - 从源码编译安装
|
|
binary - 从二进制压缩包安装
|
|
rpm - 通过 RPM 包管理器安装
|
|
deb - 通过 APT 包管理器安装
|
|
|
|
示例:
|
|
sunhpc soft install vasp --type source --src-path /tmp/vasp.tar.gz
|
|
sunhpc soft install openmpi --type binary --bin-path openmpi.tar.gz -p /opt/openmpi
|
|
sunhpc soft install htop --type rpm --force
|
|
sunhpc soft install nginx --type deb --dry-run`,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if err := auth.RequireRoot(); err != nil {
|
|
return err
|
|
}
|
|
|
|
software := args[0]
|
|
|
|
if dryRun {
|
|
log.Infof("[干运行] 将要安装 %s", software)
|
|
log.Infof(" 安装类型: %s", installType)
|
|
if srcPath != "" {
|
|
log.Infof(" 源码路径: %s", srcPath)
|
|
}
|
|
if binPath != "" {
|
|
log.Infof(" 二进制包: %s", binPath)
|
|
}
|
|
if prefix != "" {
|
|
log.Infof(" 安装路径: %s", prefix)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
ctx := &soft.InstallContext{
|
|
Force: forceInstall,
|
|
DryRun: dryRun,
|
|
KeepSource: keepSource,
|
|
Jobs: jobs,
|
|
Offline: offlineMode,
|
|
}
|
|
|
|
switch installType {
|
|
case "source":
|
|
return soft.InstallFromSource(software, srcPath, prefix, version, ctx)
|
|
case "binary":
|
|
return soft.InstallFromBinary(software, binPath, prefix, ctx)
|
|
case "rpm", "deb":
|
|
return soft.InstallFromPackage(software, installType, ctx)
|
|
default:
|
|
return fmt.Errorf("不支持的安装类型: %s", installType)
|
|
}
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
// 必选参数
|
|
installCmd.Flags().StringVarP(&installType, "type", "t", "", "安装类型: source/binary/rpm/deb")
|
|
installCmd.MarkFlagRequired("type")
|
|
|
|
// 路径参数
|
|
installCmd.Flags().StringVarP(&srcPath, "src-path", "s", "", "源码路径或URL")
|
|
installCmd.Flags().StringVarP(&binPath, "bin-path", "b", "", "二进制压缩包路径")
|
|
installCmd.Flags().StringVarP(&prefix, "prefix", "p", "/opt/sunhpc/software", "安装路径")
|
|
|
|
// 版本参数
|
|
installCmd.Flags().StringVarP(&version, "version", "v", "", "软件版本号")
|
|
|
|
// 行为控制
|
|
installCmd.Flags().BoolVarP(&forceInstall, "force", "f", false, "强制安装,覆盖已有版本")
|
|
installCmd.Flags().BoolVarP(&dryRun, "dry-run", "n", false, "仅显示将要执行的操作")
|
|
installCmd.Flags().BoolVarP(&keepSource, "keep-source", "k", false, "保留源码文件")
|
|
installCmd.Flags().IntVarP(&jobs, "jobs", "j", 4, "编译线程数")
|
|
installCmd.Flags().BoolVarP(&offlineMode, "offline", "o", false, "离线模式,不联网下载")
|
|
|
|
// 参数互斥
|
|
installCmd.MarkFlagsMutuallyExclusive("src-path", "bin-path")
|
|
installCmd.MarkFlagsOneRequired("src-path", "bin-path")
|
|
}
|