From 3ecc8648893db62ab0630582213e5c03505b808a Mon Sep 17 00:00:00 2001 From: Mahmoud Mousa Date: Sun, 13 Oct 2024 11:02:38 +0900 Subject: [PATCH] Fix: issues with env file encryption leading to failed launch/depoloy --- cmd/deploy.go | 3 ++- cmd/init.go | 24 +++++++++++++++++++++--- cmd/launch.go | 6 +++++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cmd/deploy.go b/cmd/deploy.go index 5cb9b30..81d2d2c 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -91,7 +91,8 @@ Run sidekick launch`) envCmd := exec.Command("sh", "-s", "-", viper.Get("publicKey").(string), fmt.Sprintf("./%s", appConfig.Env.File)) envCmd.Stdin = strings.NewReader(utils.EnvEncryptionScript) if envCmdErr := envCmd.Run(); envCmdErr != nil { - panic(envCmdErr) + pterm.Error.Printfln("Something went wrong handling your env file: %s", envCmdErr) + os.Exit(1) } encryptSync := exec.Command("rsync", "encrypted.env", fmt.Sprintf("%s@%s:%s", "sidekick", viper.Get("serverAddress").(string), fmt.Sprintf("./%s", appConfig.Name))) encryptSync.Run() diff --git a/cmd/init.go b/cmd/init.go index 1104a7d..d42d28c 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -19,6 +19,7 @@ import ( "fmt" "log" "os" + "os/exec" "strings" "github.com/mightymoud/sidekick/render" @@ -129,7 +130,8 @@ var initCmd = &cobra.Command{ } multi := pterm.DefaultMultiPrinter - rootLoginSpinner, _ := pterm.DefaultSpinner.Start("Logging in with root") + localReqsChecks, _ := utils.GetSpinner().WithWriter(multi.NewWriter()).Start("Setting up your local env") + rootLoginSpinner, _ := utils.GetSpinner().WithWriter(multi.NewWriter()).Start("Logging in with root") stage0Spinner, _ := utils.GetSpinner().WithWriter(multi.NewWriter()).Start("Adding user Sidekick") sidekickLoginSpinner, _ := utils.GetSpinner().WithWriter(multi.NewWriter()).Start("Logging into with sidekick user") stage1Spinner, _ := utils.GetSpinner().WithWriter(multi.NewWriter()).Start("Setting up VPS") @@ -138,6 +140,23 @@ var initCmd = &cobra.Command{ pterm.Println() multi.Start() + localReqsChecks.Sequence = []string{"▀ ", " ▀", " ▄", "▄ "} + brewCheckCmd := exec.Command("brew", "-v") + _, brewCheckCmdErr := brewCheckCmd.CombinedOutput() + if brewCheckCmdErr != nil { + log.Fatalf("Failed to run brew. Brew is required to use Sidekick: %s", brewCheckCmd) + os.Exit(1) + } + + installSopsCmd := exec.Command("brew", "install", "sops") + _, installSopsCmdErr := installSopsCmd.CombinedOutput() + if installSopsCmdErr != nil { + log.Fatalf("Failed to install Sops. Sops is need to encrypt your local env: %s", installSopsCmd) + os.Exit(1) + } + + localReqsChecks.Success("Installed local requirements successfully") + rootLoginSpinner.Success("Logged in successfully!") stage0Spinner.Sequence = []string{"▀ ", " ▀", " ▄", "▄ "} @@ -153,7 +172,6 @@ var initCmd = &cobra.Command{ hasSidekickUser = true } } - } if !hasSidekickUser && loggedInUser == "root" { if err := utils.RunStage(sshClient, utils.UsersetupStage); err != nil { @@ -168,7 +186,7 @@ var initCmd = &cobra.Command{ sidekickSshClient, err := utils.Login(server, "sidekick") if err != nil { sidekickLoginSpinner.Fail("Something went wrong logging in to your VPS") - // panic(err) + log.Fatal(err) } sidekickLoginSpinner.Success("Logged in successfully with new user!") diff --git a/cmd/launch.go b/cmd/launch.go index a13a4a8..1a7cd79 100644 --- a/cmd/launch.go +++ b/cmd/launch.go @@ -100,7 +100,11 @@ var launchCmd = &cobra.Command{ if utils.FileExists(fmt.Sprintf("./%s", envFileName)) { hasEnvFile = true pterm.Info.Printfln("Env file detected - Loading env vars from %s", envFileName) - utils.HandleEnvFile(envFileName, envVariables, &dockerEnvProperty, &envFileChecksum) + envHandleErr := utils.HandleEnvFile(envFileName, envVariables, &dockerEnvProperty, &envFileChecksum) + if envHandleErr != nil { + pterm.Error.Printfln("Something went wrong handling your env file: %s", envHandleErr) + os.Exit(1) + } defer os.Remove("encrypted.env") } else { pterm.Info.Println("No env file detected - Skipping env parsing")