2026-04-29

This commit is contained in:
2026-04-29 22:52:33 +08:00
parent e762cbdfe3
commit 3ffefa66c3
28 changed files with 1442 additions and 19 deletions

View File

@@ -0,0 +1,19 @@
package clean
import (
"gitea.sunhpc.com/kelvin/sunhpc/internal/pkg/clean"
"github.com/spf13/cobra"
)
func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err error) {
return func(cmd *cobra.Command, args []string) (err error) {
if err = clean.CleanOciBlobCacheDir(); err != nil {
return err
} else if err = clean.CleanOverlays(); err != nil {
return err
} else {
return nil
}
}
}

View File

@@ -0,0 +1,23 @@
package clean
import (
"github.com/spf13/cobra"
"gitea.sunhpc.com/kelvin/sunhpc/internal/app/control/completions"
)
type variables struct {
}
func GetCommand() *cobra.Command {
vars := variables{}
baseCmd := &cobra.Command{
DisableFlagsInUseLine: true,
Use: "clean",
Short: "Clean up",
Long: "This command cleans the OCI cache and removes leftovers from deleted nodes",
RunE: CobraRunE(&vars),
Args: cobra.NoArgs,
ValidArgsFunction: completions.None,
}
return baseCmd
}

View File

@@ -0,0 +1,9 @@
package completions
import (
"github.com/spf13/cobra"
)
func None(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
}

View File

@@ -0,0 +1,10 @@
package dhcp
import (
"github.com/spf13/cobra"
"gitea.sunhpc.com/kelvin/sunhpc/internal/pkg/configure"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
return configure.DHCP()
}

View File

@@ -0,0 +1,24 @@
package dhcp
import (
"github.com/spf13/cobra"
"gitea.sunhpc.com/kelvin/sunhpc/internal/app/control/completions"
)
var (
baseCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Use: "dhcp [OPTIONS]",
Short: "Manage and initialize DHCP",
Long: "DHCP is a dependent service to Warewulf. This command will configure DHCP as defined\n" +
"in the warewulf.conf file.",
RunE: CobraRunE,
Args: cobra.NoArgs,
ValidArgsFunction: completions.None,
}
)
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
return baseCmd
}

View File

@@ -0,0 +1,32 @@
package configure
import (
"github.com/spf13/cobra"
sunhpcconf "gitea.sunhpc.com/kelvin/sunhpc/internal/pkg/config"
"gitea.sunhpc.com/kelvin/sunhpc/internal/pkg/configure"
"gitea.sunhpc.com/kelvin/sunhpc/internal/pkg/splog"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
var err error
conf := sunhpcconf.Get()
if conf.Autodetected() && conf.InitializedFromFile() {
if err = conf.PersistToFile(conf.GetSunhpcConf()); err != nil {
splog.Warn("error when persisting auto-detected settings: %s", err)
}
}
if allFunctions {
if _, err = configure.TLS(false); err != nil {
return err
}
err = configure.SUNHPCD()
if err != nil {
return err
}
}
return nil
}

View File

@@ -0,0 +1,30 @@
package configure
import (
"github.com/spf13/cobra"
"gitea.sunhpc.com/kelvin/sunhpc/internal/app/control/configure/dhcp"
)
var (
baseCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Use: "config [options]",
Short: "Manage sunhpc configuration",
Long: "This application allows you to manage and initialize Sunhpc configuration\n" +
"services based on the configuration in the sunhpc.conf file.",
RunE: CobraRunE,
Args: cobra.NoArgs,
}
allFunctions bool
)
func init() {
baseCmd.AddCommand(dhcp.GetCommand())
baseCmd.Flags().BoolVarP(&allFunctions, "all", "a", false, "Configure all services")
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
return baseCmd
}

View File

@@ -4,7 +4,10 @@ import (
"os"
"github.com/spf13/cobra"
"gitea.sunhpc.com/kelvin/sunhpc/internal/app/control/version"
"gitea.sunhpc.com/kelvin/sunhpc/internal/app/control/clean"
"gitea.sunhpc.com/kelvin/sunhpc/internal/app/control/configure"
sunhpcconf "gitea.sunhpc.com/kelvin/sunhpc/internal/pkg/config"
"gitea.sunhpc.com/kelvin/sunhpc/internal/pkg/help"
"gitea.sunhpc.com/kelvin/sunhpc/internal/pkg/splog"
)
@@ -16,18 +19,19 @@ var (
Short: "Sunhpc Control",
Long: `Control interface to the SunHPC Cluster Provisioning System.`,
PersistentPreRunE: rootPersistentPreRunE,
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.NoArgs,
}
verboseArg bool
DebugFlag bool
SunhpcConfArg string
AllowEmptyConf bool
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.NoArgs,
}
verboseArg bool
DebugFlag bool
LogLevel int
SunhpcConfArg string
AllowEmptyConf bool
)
func init() {
rootCmd.CompletionOptions.HiddenDefaultCmd = true
//rootCmd.CompletionOptions.HiddenDefaultCmd = true
rootCmd.PersistentFlags().BoolVarP(&verboseArg, "verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().BoolVarP(&DebugFlag, "debug", "d", false, "debug output")
rootCmd.PersistentFlags().IntVar(&LogLevel, "loglevel", splog.INFO, "Set log level to given string")
@@ -35,6 +39,42 @@ func init() {
rootCmd.PersistentFlags().StringVar(&SunhpcConfArg, "sunhpcconf", "", "Set the sunhpc configuration file")
rootCmd.PersistentFlags().BoolVar(&AllowEmptyConf, "emptyconf", false, "Allow empty configuration")
_ = rootCmd.PersistentFlags().MarkHidden("emptyconf")
rootCmd.SetUsageTemplate(help.UsageTemplate)
rootCmd.SetHelpTemplate(help.HelpTemplate)
}
rootCmd.AddCommand(clean.GetCommand())
rootCmd.AddCommand(configure.GetCommand())
}
func GetRootCommand() *cobra.Command {
return rootCmd
}
func rootPersistentPreRunE(cmd *cobra.Command, args []string) (err error) {
if DebugFlag {
splog.SetLogLevel(splog.DEBUG)
} else if verboseArg {
splog.SetLogLevel(splog.VERBOSE)
} else {
splog.SetLogLevel(splog.INFO)
}
if LogLevel != splog.INFO {
splog.SetLogLevel(LogLevel)
}
if SunhpcConfArg != "" {
sunhpcconf.ConfigFile = SunhpcConfArg
} else if os.Getenv("SUNHPC_CONF") != "" {
sunhpcconf.ConfigFile = os.Getenv("SUNHPC_CONF")
}
conf := sunhpcconf.Get()
if !AllowEmptyConf && !conf.InitializedFromFile() {
if err = conf.Read(sunhpcconf.ConfigFile, true); err != nil {
splog.Error("error reading configuration file: %s", err)
return
}
}
return
}