Add init subcommand

This commit is contained in:
yo 2024-09-22 15:10:53 +02:00
parent 9fcc7a6572
commit 8cabae7134

View File

@ -14,7 +14,7 @@ import (
) )
const ( const (
gVersion = "0.41" gVersion = "0.42b"
// TODO : Get from $jail_zpool/defaults.json // TODO : Get from $jail_zpool/defaults.json
MIN_DYN_DEVFS_RULESET = 1000 MIN_DYN_DEVFS_RULESET = 1000
@ -56,6 +56,9 @@ var (
gTimeZone string gTimeZone string
gSnapshotName string gSnapshotName string
gZPool string
gBridge string
gInterface string
gMigrateDestDatastore string gMigrateDestDatastore string
gYesToAll bool gYesToAll bool
@ -97,19 +100,13 @@ It support iocage jails and can coexist with iocage.`,
}, },
} }
/* TODO
Initialize datastore(s) /iocage, /iocage/jails
Put defaults.json, update it with hostid,interfaces, and maybe other necessary fields
Initialize bridge
initCmd = &cobra.Command{ initCmd = &cobra.Command{
Use: "init", Use: "init",
Short: "Initialize GoCage", Short: "Initialize GoCage",
//Long: `Let this show you how much fail I had to get this *cough* perfect`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fv, _ := getFreeBSDVersion() InitGoCage(args)
fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s\n", gVersion, fv.major, fv.minor, fv.flavor)
}, },
}*/ }
listCmd = &cobra.Command{ listCmd = &cobra.Command{
Use: "list", Use: "list",
@ -379,6 +376,10 @@ func init() {
rootCmd.PersistentFlags().BoolVar(&gDebug, "debug", false, "Debug mode") rootCmd.PersistentFlags().BoolVar(&gDebug, "debug", false, "Debug mode")
// Command dependant switches // Command dependant switches
initCmd.Flags().StringVarP(&gZPool, "pool", "p", "", "ZFS pool to create datastore on")
initCmd.Flags().StringVarP(&gBridge, "bridge", "b", "", "bridge to create for jails networking")
initCmd.Flags().StringVarP(&gInterface, "interface", "i", "", "interface to add as bridge member. This should be your main interface")
initCmd.MarkFlagsRequiredTogether("bridge", "interface")
// We reuse these flags in "gocage snapshot list myjail" and 'gocage datastore list" commands // We reuse these flags in "gocage snapshot list myjail" and 'gocage datastore list" commands
listCmd.Flags().StringVarP(&gDisplayJColumns, "outcol", "o", "JID,Name,Config.Release,Config.Ip4_addr,Running", "Show these columns in output") listCmd.Flags().StringVarP(&gDisplayJColumns, "outcol", "o", "JID,Name,Config.Release,Config.Ip4_addr,Running", "Show these columns in output")
@ -424,6 +425,7 @@ func init() {
createCmd.Flags().StringVarP(&gCreateArgs.Datastore, "datastore", "d", "", "Datastore to create the jail on. Defaults to first declared in config.") createCmd.Flags().StringVarP(&gCreateArgs.Datastore, "datastore", "d", "", "Datastore to create the jail on. Defaults to first declared in config.")
// Now declare commands // Now declare commands
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(listCmd) rootCmd.AddCommand(listCmd)
listCmd.AddCommand(listPropsCmd) listCmd.AddCommand(listPropsCmd)
@ -441,7 +443,7 @@ func init() {
rootCmd.AddCommand(updateCmd) rootCmd.AddCommand(updateCmd)
rootCmd.AddCommand(upgradeCmd) rootCmd.AddCommand(upgradeCmd)
rootCmd.AddCommand(createCmd) rootCmd.AddCommand(createCmd)
rootCmd.AddCommand(apiCmd)
rootCmd.AddCommand(testCmd) rootCmd.AddCommand(testCmd)
snapshotCmd.AddCommand(snapshotListCmd) snapshotCmd.AddCommand(snapshotListCmd)
@ -472,17 +474,6 @@ func initConfig() {
os.Exit(1) os.Exit(1)
} }
// Load default configs from datastores
err := ListDatastores(viper.GetStringSlice("datastore"), false)
if err != nil {
fmt.Printf("ERROR: error checking datastores: %v\n", err)
os.Exit(1)
}
// fmt.Println("Using config file:", viper.ConfigFileUsed())
// fmt.Printf("datastore in config : %s\n", viper.GetStringSlice("datastore"))
// fmt.Printf("datastore.0 in config : %s\n", viper.GetStringSlice("datastore.0"))
// Command line flags have priority on config file // Command line flags have priority on config file
if rootCmd.Flags().Lookup("sudo") != nil && false == rootCmd.Flags().Lookup("sudo").Changed { if rootCmd.Flags().Lookup("sudo") != nil && false == rootCmd.Flags().Lookup("sudo").Changed {
gUseSudo = viper.GetBool("sudo") gUseSudo = viper.GetBool("sudo")
@ -516,11 +507,25 @@ func initConfig() {
os.Exit(1) os.Exit(1)
} }
if gDebug { if gDebug {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
log.Debugf("Debug mode enabled\n") log.Debugf("Debug mode enabled\n")
} }
// no need to check prerequesites if we are initializing gocage
for _, rc := range rootCmd.Commands() {
//fmt.Printf("DEBUG: rootCmd subcommand: %v. Was it called? %s\n", rc.Use, rootCmd.Commands()[i].CalledAs())
if len(rc.CalledAs()) > 0 && strings.EqualFold("init", rc.CalledAs()) {
return
}
}
// Load default configs from datastores
err := ListDatastores(viper.GetStringSlice("datastore"), false)
if err != nil {
fmt.Printf("ERROR: error checking datastores: %v\n", err)
os.Exit(1)
}
} }