Skip to content

Commit

Permalink
feat(clustertool): actually fully block genconfig unless first init i…
Browse files Browse the repository at this point in the history
…s finished
  • Loading branch information
PrivatePuffin committed Nov 6, 2024
1 parent d707b49 commit 269a895
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
5 changes: 0 additions & 5 deletions clustertool/cmd/genconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand All @@ -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)
},
Expand Down
2 changes: 2 additions & 0 deletions clustertool/pkg/gencmd/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions clustertool/pkg/gencmd/genconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
34 changes: 34 additions & 0 deletions clustertool/pkg/initfiles/initfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

func InitFiles() error {
removeRunAgainFile()
ageGen()
genRootFiles()
genBaseFiles()
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 269a895

Please sign in to comment.