diff --git a/clustertool/cmd/genconfig.go b/clustertool/cmd/genconfig.go index 8b60fed56fa49..9b500f5ee6141 100644 --- a/clustertool/cmd/genconfig.go +++ b/clustertool/cmd/genconfig.go @@ -3,10 +3,8 @@ package cmd import ( "strings" - "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/truecharts/public/clustertool/pkg/gencmd" - "github.com/truecharts/public/clustertool/pkg/sops" ) var genConfigLongHelp = strings.TrimSpace(` @@ -24,9 +22,6 @@ var genConfig = &cobra.Command{ Long: genConfigLongHelp, Example: "clustertool genconfig", Run: func(cmd *cobra.Command, args []string) { - if err := sops.DecryptFiles(); err != nil { - log.Info().Msgf("Error decrypting files: %v\n", err) - } gencmd.GenConfig(args) }, diff --git a/clustertool/pkg/gencmd/bootstrap.go b/clustertool/pkg/gencmd/bootstrap.go index 5f3fb79cb95a5..e842c0ef9ecad 100644 --- a/clustertool/pkg/gencmd/bootstrap.go +++ b/clustertool/pkg/gencmd/bootstrap.go @@ -28,9 +28,11 @@ func RunBootstrap(args []string) { if len(args) > 1 { extraArgs = args[1:] } + if err := sops.DecryptFiles(); err != nil { log.Info().Msgf("Error decrypting files: %v\n", err) } + bootstrapNode := talassist.TalConfig.Nodes[0].IPAddress bootstrapcmds := GenPlain("bootstrap", bootstrapNode, extraArgs) diff --git a/clustertool/pkg/gencmd/genconfig.go b/clustertool/pkg/gencmd/genconfig.go index da8f731dcfb9d..eb44478c39663 100644 --- a/clustertool/pkg/gencmd/genconfig.go +++ b/clustertool/pkg/gencmd/genconfig.go @@ -11,10 +11,18 @@ import ( "github.com/truecharts/public/clustertool/pkg/fluxhandler" "github.com/truecharts/public/clustertool/pkg/helper" "github.com/truecharts/public/clustertool/pkg/initfiles" + "github.com/truecharts/public/clustertool/pkg/sops" "github.com/truecharts/public/clustertool/pkg/talassist" ) func GenConfig(args []string) error { + if initfiles.CheckRunAgainFileExists() { + log.Fatal().Msg("You need to re-run Init. Exiting...") + os.Exit(1) + } + if err := sops.DecryptFiles(); err != nil { + log.Info().Msgf("Error decrypting files: %v\n", err) + } talassist.LoadTalConfig() talassist.GenSchema() initfiles.GenTalEnvConfigMap() diff --git a/clustertool/pkg/initfiles/initfiles.go b/clustertool/pkg/initfiles/initfiles.go index 60736f0eb278c..9b92fe22e3b51 100644 --- a/clustertool/pkg/initfiles/initfiles.go +++ b/clustertool/pkg/initfiles/initfiles.go @@ -23,6 +23,7 @@ import ( ) func InitFiles() error { + removeRunAgainFile() ageGen() genRootFiles() genBaseFiles() @@ -145,6 +146,7 @@ func genBaseFiles() error { clusterEnvPresent = true log.Debug().Msg("Detected existing cluster, continuing") } else if os.IsNotExist(err) { + createRunAgainFile() log.Warn().Msg("New cluster detected, creating clusterenv.yaml\n Please fill out ClusterEnv.yaml and run init again!") } else { log.Fatal().Err(err).Msgf("Error checking clusterenv file: %s", err) @@ -166,6 +168,38 @@ func genBaseFiles() error { return nil } +// Create the "RUNAGAIN" file +func createRunAgainFile() { + file, err := os.Create("RUNAGAIN") + if err != nil { + log.Err(err).Msg("error creating runagain file...") + return + } + defer file.Close() + return +} + +// Remove the "RUNAGAIN" file if it exists +func removeRunAgainFile() error { + if CheckRunAgainFileExists() { + err := os.Remove("RUNAGAIN") + if err != nil { + log.Err(err).Msg("error removing runagain file...") + return err + } + log.Debug().Msg("RUNAGAIN file removed.") + } else { + log.Debug().Msg("RUNAGAIN file does not exist.") + } + return nil +} + +// Check if the "RUNAGAIN" file exists +func CheckRunAgainFileExists() bool { + _, err := os.Stat("RUNAGAIN") + return !os.IsNotExist(err) +} + func UpdateBaseFiles() error { log.Info().Msg("Updating Base files for cluster: helper.ClusterPath") // Read filenames in source directory