From 6906657b06fa1afa757c442cebf8885496df4bf2 Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Thu, 30 Jun 2022 19:16:14 +0000 Subject: [PATCH 01/10] Add check tools and update outputs Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/checktools.go | 54 +++++++++++++++++++++++++++++++++ cmd/info.go | 23 +++++++------- pkg/flare/envvars.go | 24 ++++++++++++--- pkg/flare/flareFile.go | 16 ++++++++-- pkg/flare/kubefirstDirectory.go | 16 ++++++++-- 5 files changed, 112 insertions(+), 21 deletions(-) create mode 100644 cmd/checktools.go diff --git a/cmd/checktools.go b/cmd/checktools.go new file mode 100644 index 000000000..2944ef91e --- /dev/null +++ b/cmd/checktools.go @@ -0,0 +1,54 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "fmt" + "bytes" + "os/exec" + "github.com/spf13/cobra" +) + +// checktoolsCmd represents the checktools command +var checktoolsCmd = &cobra.Command{ + Use: "checktools", + Short: "use to check compatibility of .kubefirst/tools", + Long: `Execute a compatibility check of the tools downloaded by the installer. + Execute After callint "init". If executed before init, tools will not be available. + `, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Checking the tools installed used by installer:") + + kubectlVersion, kubectlStdErr,errKubectl := execShellReturnStrings(kubectlClientPath, "version", "--client", "--short") + fmt.Printf("-> kubectl version:\n\t%s\n\t%s\n",kubectlVersion,kubectlStdErr) + terraformVersion, terraformStdErr,errTerraform := execShellReturnStrings(terraformPath, "version") + fmt.Printf("-> terraform version:\n\t%s\n\t%s\n",terraformVersion,terraformStdErr) + helmVersion, helmStdErr,errHelm := execShellReturnStrings(helmClientPath, "version", "--client", "--short") + fmt.Printf("-> helm version:\n\t%s\n\t%s\n",helmVersion,helmStdErr) + + if errKubectl != nil { + fmt.Println("failed to call kubectlVersionCmd.Run(): %v", errKubectl) + } + if errHelm != nil { + fmt.Println("failed to call helmVersionCmd.Run(): %v", errHelm) + } + if errTerraform != nil { + fmt.Println("failed to call terraformVersionCmd.Run(): %v", errTerraform) + } + + }, +} + +func init() { + rootCmd.AddCommand(checktoolsCmd) +} +func execShellReturnStrings(command string, args ...string) (string, string, error) { + var outb, errb bytes.Buffer + k := exec.Command(command, args...) + k.Stdout = &outb + k.Stderr = &errb + err := k.Run() + return outb.String(), errb.String(), err +} diff --git a/cmd/info.go b/cmd/info.go index 319b9e78d..4db172863 100755 --- a/cmd/info.go +++ b/cmd/info.go @@ -5,7 +5,7 @@ Copyright © 2022 NAME HERE package cmd import ( - "log" + "fmt" "github.com/spf13/cobra" "github.com/kubefirst/nebulous/pkg/flare" ) @@ -18,16 +18,17 @@ var infoCmd = &cobra.Command{ and cli version runnig and its current state. Tool recommended for troubleshooting installations`, Run: func(cmd *cobra.Command, args []string) { - log.Printf("flare-cli golang utility version: v%s", NebolousVersion) - log.Printf("OS type: %s", localOs) - log.Printf("Arch: %s", localArchitecture) - log.Printf("$HOME folder: %s", home) - log.Printf("kubectl used: %s", kubectlClientPath) - log.Printf("terraform used: %s", terraformPath) - log.Printf("Kubeconfig in use: %s", kubeconfigPath) - flare.CheckFlareFile(home) - flare.CheckKubefirstDir(home) - flare.CheckEnvironment() + fmt.Printf("flare-cli golang utility version: v%s \n", NebolousVersion) + fmt.Printf("OS type: %s\n", localOs) + fmt.Printf("Arch: %s\n", localArchitecture) + fmt.Printf("$HOME folder: %s\n", home) + fmt.Printf("kubectl used: %s\n", kubectlClientPath) + fmt.Printf("terraform used: %s\n", terraformPath) + fmt.Printf("Kubeconfig in use: %s\n", kubeconfigPath) + flare.CheckFlareFile(home,true) + flare.CheckKubefirstDir(home,true) + flare.CheckEnvironment(true) + fmt.Printf("----------- \n") }, } diff --git a/pkg/flare/envvars.go b/pkg/flare/envvars.go index 6507910d5..d8f0dd5d5 100755 --- a/pkg/flare/envvars.go +++ b/pkg/flare/envvars.go @@ -2,6 +2,7 @@ package flare import ( "log" + "fmt" "os" ) @@ -9,19 +10,34 @@ import ( // // Output: // $PATH/.flare -func CheckEnvironment() bool { +func CheckEnvironment(printOut bool) bool { if value := os.Getenv("AWS_REGION"); value == "" { log.Printf("AWS_REGION env var not set.") - log.Printf("AWS_REGION is recommended for execution.") + log.Printf("AWS_REGION is recommended for execution.") + if printOut { + fmt.Printf("AWS_REGION env var not set.\n") + fmt.Printf("AWS_REGION is recommended for execution.\n") + } } else { - log.Printf("AWS_REGION env var set: %s",value) + log.Printf("AWS_REGION env var set: %s",value) + if printOut { + fmt.Printf("AWS_REGION env var set: %s\n",value) + } } + if value := os.Getenv("AWS_PROFILE"); value == "" { log.Printf("AWS_PROFILE env var not set.") - log.Printf("AWS_PROFILE is recommended for execution.") + log.Printf("AWS_PROFILE is recommended for execution.") + if printOut { + log.Printf("AWS_PROFILE env var not set. \n") + log.Printf("AWS_PROFILE is recommended for execution.\n") + } } else { log.Printf("AWS_PROFILE env var set: %s",value) + if printOut { + log.Printf("AWS_PROFILE env var set: %s\n",value) + } } diff --git a/pkg/flare/flareFile.go b/pkg/flare/flareFile.go index b45054731..2a37e9252 100755 --- a/pkg/flare/flareFile.go +++ b/pkg/flare/flareFile.go @@ -11,19 +11,29 @@ import ( // // Output: // $PATH/.flare -func CheckFlareFile(home string) string { +func CheckFlareFile(home string, printOut bool) string { flareFile := fmt.Sprintf("%s/.flare", home) if _, err := os.Stat(flareFile); err == nil { // path/to/whatever exists - log.Printf("\".flare\" file found: %s", flareFile) + log.Printf("\".flare\" file found: %s", flareFile) + if printOut { + fmt.Printf("\".flare\" file found: %s \n", flareFile) + } } else if errors.Is(err, os.ErrNotExist) { // path/to/whatever does *not* exist log.Printf("\".flare\" file not found: %s", flareFile) - log.Printf(" \".flare\" is needed to guide installation process" ) + log.Printf(" \".flare\" is needed to guide installation process" ) + if printOut { + fmt.Printf("\".flare\" file not found: %s\n", flareFile) + fmt.Printf(" \".flare\" is needed to guide installation process\n" ) + } } else { // Schrodinger: file may or may not exist. See err for details. // Therefore, do *NOT* use !os.IsNotExist(err) to test for file existence log.Printf("Unable to check is \".flare\" if file exists" ) + if printOut { + fmt.Printf("Unable to check is \".flare\" if file exists\n" ) + } } return flareFile } \ No newline at end of file diff --git a/pkg/flare/kubefirstDirectory.go b/pkg/flare/kubefirstDirectory.go index 9ed513fa5..7217ca57a 100755 --- a/pkg/flare/kubefirstDirectory.go +++ b/pkg/flare/kubefirstDirectory.go @@ -12,19 +12,29 @@ import ( // // Output: // $PATH/.kubefirst -func CheckKubefirstDir(home string) string { +func CheckKubefirstDir(home string, printOut bool) string { k1sDir := fmt.Sprintf("%s/.kubefirst", home) if _, err := os.Stat(k1sDir); err == nil { // path/to/whatever exists log.Printf("\".kubefirst\" file found: %s", k1sDir) log.Printf(" \".kubefirst\" will be generated by installation process, if exist means a installation may already be executed" ) + if printOut { + fmt.Printf("\".kubefirst\" file found: %s\n", k1sDir) + fmt.Printf(" \".kubefirst\" will be generated by installation process, if exist means a installation may already be executed\n" ) + } } else if errors.Is(err, os.ErrNotExist) { // path/to/whatever does *not* exist - log.Printf("\".kubefirst\" file not found: %s", k1sDir) + log.Printf("\".kubefirst\" file not found: %s", k1sDir) + if printOut { + fmt.Printf("\".kubefirst\" file not found: %s\n", k1sDir) + } } else { // Schrodinger: file may or may not exist. See err for details. // Therefore, do *NOT* use !os.IsNotExist(err) to test for file existence - log.Printf("Unable to check is \".kubefirst\" if file exists" ) + log.Printf("Unable to check is \".kubefirst\" if file exists" ) + if printOut { + fmt.Printf("Unable to check is \".kubefirst\" if file exists\n" ) + } } return k1sDir } \ No newline at end of file From ebf480b8965dba455e2b399c51f877c6efc9e2b2 Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Thu, 30 Jun 2022 20:15:48 +0000 Subject: [PATCH 02/10] simplify os.shell calls Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/init.go | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 912bd9099..64dac8ca5 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -745,13 +745,12 @@ func download() { os.Setenv("KUBECONFIG", kubeconfigPath) log.Println("going to print the kubeconfig env in runtime", os.Getenv("KUBECONFIG")) - kubectlVersionCmd := exec.Command(kubectlClientPath, "version", "--client", "--short") - kubectlVersionCmd.Stdout = os.Stdout - kubectlVersionCmd.Stderr = os.Stderr - err = kubectlVersionCmd.Run() - if err != nil { + kubectlStdOut, kubectlStdErr,errKubectl := execShellReturnStrings(kubectlClientPath, "version", "--client", "--short") + log.Printf("-> kubectl version:\n\t%s\n\t%s\n",kubectlStdOut,kubectlStdErr) + if errKubectl != nil { log.Println("failed to call kubectlVersionCmd.Run(): %v", err) } + Trackers[trackerStage5].Tracker.Increment(int64(1)) // argocdVersion := "v2.3.4" // argocdDownloadUrl := fmt.Sprintf("https://github.com/argoproj/argo-cd/releases/download/%s/argocd-%s-%s", argocdVersion, localOs, localArchitecture) @@ -795,15 +794,11 @@ func download() { } extractFileFromTarGz(helmTarDownload, fmt.Sprintf("%s-%s/helm", localOs, localArchitecture), helmClientPath) os.Chmod(helmClientPath, 0755) - helmVersionCmd := exec.Command(helmClientPath, "version", "--client", "--short") - + helmStdOut, helmStdErr,errHelm := execShellReturnStrings(helmClientPath, "version", "--client", "--short") + log.Printf("-> kubectl version:\n\t%s\n\t%s\n",helmStdOut,helmStdErr) // currently argocd init values is generated by flare nebulous ssh - // todo helm install argocd --create-namespace --wait --values ~/.kubefirst/argocd-init-values.yaml argo/argo-cd - helmVersionCmd.Stdout = os.Stdout - helmVersionCmd.Stderr = os.Stderr - err = helmVersionCmd.Run() - if err != nil { + if errHelm != nil { log.Println("failed to call helmVersionCmd.Run(): %v", err) } Trackers[trackerStage5].Tracker.Increment(int64(1)) @@ -933,13 +928,11 @@ func createSoftServe(kubeconfigPath string) { // create soft-serve stateful set softServePath := fmt.Sprintf("%s/.kubefirst/gitops/components/soft-serve/manifests.yaml", home) - kubectlCreateSoftServeCmd := exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "soft-serve", "apply", "-f", softServePath, "--wait") - kubectlCreateSoftServeCmd.Stdout = os.Stdout - kubectlCreateSoftServeCmd.Stderr = os.Stderr - err = kubectlCreateSoftServeCmd.Run() - if err != nil { + softServeApplyOut, softServeApplyErr,errSoftServeApply := execShellReturnStrings(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "soft-serve", "apply", "-f", softServePath, "--wait") + log.Printf("Result:\n\t%s\n\t%s\n",softServeApplyOut,softServeApplyErr) + if errSoftServeApply != nil { log.Println("failed to call kubectlCreateSoftServeCmd.Run(): %v", err) - } + } } func helmInstallArgocd(home string, kubeconfigPath string) { @@ -965,11 +958,9 @@ func helmInstallArgocd(home string, kubeconfigPath string) { log.Println("failed to call helmRepoUpdate.Run(): %v", err) } - helmInstallArgocdCmd := exec.Command(helmClientPath, "--kubeconfig", kubeconfigPath, "upgrade", "--install", "argocd", "--namespace", "argocd", "--create-namespace", "--wait", "--values", fmt.Sprintf("%s/.kubefirst/argocd-init-values.yaml", home), "argo/argo-cd") - helmInstallArgocdCmd.Stdout = os.Stdout - helmInstallArgocdCmd.Stderr = os.Stderr - err = helmInstallArgocdCmd.Run() - if err != nil { + helmInstallArgocdOut, helmInstallArgocdErr,errHelmInstallArgocd := execShellReturnStrings(helmClientPath, "--kubeconfig", kubeconfigPath, "upgrade", "--install", "argocd", "--namespace", "argocd", "--create-namespace", "--wait", "--values", fmt.Sprintf("%s/.kubefirst/argocd-init-values.yaml", home), "argo/argo-cd") + log.Printf("Result:\n\t%s\n\t%s\n",helmInstallArgocdOut,helmInstallArgocdErr) + if errHelmInstallArgocd != nil { log.Println("failed to call helmInstallArgocdCmd.Run(): %v", err) } From 3a0d9bf746654f2b1fdf5a39d12125789ca34682 Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Fri, 1 Jul 2022 11:50:42 +0000 Subject: [PATCH 03/10] minor notes tweaks Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/README.md | 12 ++++++++++++ cmd/version.go | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cmd/README.md b/cmd/README.md index ccaac1ea8..219d5a4bc 100644 --- a/cmd/README.md +++ b/cmd/README.md @@ -32,3 +32,15 @@ General overview of the code, to help shuffling parts around. |cfgFile|String| .flare config file| |NebolousVersion|String|CLI version| + +# Commands Available + +| Command | Short Description | Long Description| +|:---|:---|:---| +|checktools|Present, needs review.|Present, needs review.| +|clean|Missing Text|Missing Text| +|create|Missing Text|Missing Text| +|destroy|Missing Text|Missing Text| +|info|Present, needs review.|Present, needs review.| +|init|Missing Text|Missing Text| +|version|Present, needs review.|Present, needs review.| \ No newline at end of file diff --git a/cmd/version.go b/cmd/version.go index b700bc60f..fac8a3338 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -15,8 +15,8 @@ func init() { var versionCmd = &cobra.Command{ Use: "version", - Short: "Print the version number for flare", - Long: `All software has versions. This is flare's`, + Short: "Print the version number for kubefirst-cli", + Long: `All software has versions. This is kubefirst's`, Run: func(cmd *cobra.Command, args []string) { log.Printf("flare-cli golang utility version: v%s", NebolousVersion) From ef7b21ccdb2344635b68aa6206cfd3892e0303b3 Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Fri, 1 Jul 2022 12:34:08 +0000 Subject: [PATCH 04/10] cleaning create steps Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/checktools.go | 2 ++ cmd/create.go | 63 +++------------------------------------- cmd/init.go | 34 +++++++++++++++------- cmd/installationSteps.go | 50 +++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 70 deletions(-) create mode 100644 cmd/installationSteps.go diff --git a/cmd/checktools.go b/cmd/checktools.go index 2944ef91e..88f5083bf 100644 --- a/cmd/checktools.go +++ b/cmd/checktools.go @@ -6,6 +6,7 @@ package cmd import ( "fmt" + "log" "bytes" "os/exec" "github.com/spf13/cobra" @@ -50,5 +51,6 @@ func execShellReturnStrings(command string, args ...string) (string, string, err k.Stdout = &outb k.Stderr = &errb err := k.Run() + log.Println("Error executing command: %v", err) return outb.String(), errb.String(), err } diff --git a/cmd/create.go b/cmd/create.go index 366721c07..7b22f331e 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -58,68 +58,13 @@ to quickly create a Cobra application.`, directory := fmt.Sprintf("%s/.kubefirst/gitops/terraform/base", home) - applyBase := viper.GetBool("create.terraformapplied.base") - createSoftServeFlag := viper.GetBool("create.softserve.create") + + configureAndPushFlag := viper.GetBool("create.softserve.configure") - if applyBase != true { + applyBaseTerraform(cmd,directory) - terraformAction := "apply" - - os.Setenv("TF_VAR_aws_account_id", viper.GetString("aws.accountid")) - os.Setenv("TF_VAR_aws_region", viper.GetString("aws.region")) - os.Setenv("TF_VAR_hosted_zone_name", viper.GetString("aws.domainname")) - - err := os.Chdir(directory) - if err != nil { - fmt.Println("error changing dir") - } - - viperDestoryFlag := viper.GetBool("terraform.destroy") - cmdDestroyFlag, _ := cmd.Flags().GetBool("destroy") - - if viperDestoryFlag == true || cmdDestroyFlag == true { - terraformAction = "destroy" - } - - fmt.Println("terraform action: ", terraformAction, "destroyFlag: ", viperDestoryFlag) - tfInitCmd := exec.Command(terraformPath, "init") - tfInitCmd.Stdout = os.Stdout - tfInitCmd.Stderr = os.Stderr - err = tfInitCmd.Run() - if err != nil { - fmt.Println("failed to call tfInitCmd.Run(): ", err) - } - tfApplyCmd := exec.Command(terraformPath, fmt.Sprintf("%s", terraformAction), "-auto-approve") - tfApplyCmd.Stdout = os.Stdout - tfApplyCmd.Stderr = os.Stderr - err = tfApplyCmd.Run() - if err != nil { - fmt.Println("failed to call tfApplyCmd.Run(): ", err) - panic("tfApplyCmd.Run() failed") - } - keyIdBytes, err := exec.Command(terraformPath, "output", "vault_unseal_kms_key").Output() - if err != nil { - fmt.Println("failed to call tfOutputCmd.Run(): ", err) - } - keyId := strings.TrimSpace(string(keyIdBytes)) - - fmt.Println("keyid is:", keyId) - viper.Set("vault.kmskeyid", keyId) - viper.Set("create.terraformapplied.base", true) - viper.WriteConfig() - - detokenize(fmt.Sprintf("%s/.kubefirst/gitops", home)) - - } - if createSoftServeFlag != true { - createSoftServe(kubeconfigPath) - viper.Set("create.softserve.create", true) - viper.WriteConfig() - fmt.Println("waiting for soft-serve installation to complete...") - time.Sleep(60 * time.Second) - - } + createSoftServe(kubeconfigPath) if configureAndPushFlag != true { kPortForward := exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "soft-serve", "port-forward", "svc/soft-serve", "8022:22") diff --git a/cmd/init.go b/cmd/init.go index 64dac8ca5..51d819b33 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -918,21 +918,33 @@ func extractTarGz(gzipStream io.Reader) { } func createSoftServe(kubeconfigPath string) { + createSoftServeFlag := viper.GetBool("create.softserve.create") + + if createSoftServeFlag != true { + log.Println("Executing createSoftServe") + toolsDir := fmt.Sprintf("%s/.kubefirst/tools", home) - toolsDir := fmt.Sprintf("%s/.kubefirst/tools", home) + err := os.Mkdir(toolsDir, 0777) + if err != nil { + log.Println("error creating directory %s", toolsDir, err) + } - err := os.Mkdir(toolsDir, 0777) - if err != nil { - log.Println("error creating directory %s", toolsDir, err) + // create soft-serve stateful set + softServePath := fmt.Sprintf("%s/.kubefirst/gitops/components/soft-serve/manifests.yaml", home) + softServeApplyOut, softServeApplyErr,errSoftServeApply := execShellReturnStrings(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "soft-serve", "apply", "-f", softServePath, "--wait") + log.Printf("Result:\n\t%s\n\t%s\n",softServeApplyOut,softServeApplyErr) + if errSoftServeApply != nil { + log.Println("failed to call kubectlCreateSoftServeCmd.Run(): %v", err) + } + + viper.Set("create.softserve.create", true) + viper.WriteConfig() + fmt.Println("waiting for soft-serve installation to complete...") + time.Sleep(60 * time.Second) + } else { + log.Println("Skipping: createSoftServe") } - // create soft-serve stateful set - softServePath := fmt.Sprintf("%s/.kubefirst/gitops/components/soft-serve/manifests.yaml", home) - softServeApplyOut, softServeApplyErr,errSoftServeApply := execShellReturnStrings(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "soft-serve", "apply", "-f", softServePath, "--wait") - log.Printf("Result:\n\t%s\n\t%s\n",softServeApplyOut,softServeApplyErr) - if errSoftServeApply != nil { - log.Println("failed to call kubectlCreateSoftServeCmd.Run(): %v", err) - } } func helmInstallArgocd(home string, kubeconfigPath string) { diff --git a/cmd/installationSteps.go b/cmd/installationSteps.go new file mode 100644 index 000000000..cabbed7a4 --- /dev/null +++ b/cmd/installationSteps.go @@ -0,0 +1,50 @@ +package cmd + +import ( + "fmt" + "log" + "os" + "strings" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func applyBaseTerraform(cmd *cobra.Command,directory string){ + applyBase := viper.GetBool("create.terraformapplied.base") + if applyBase != true { + log.Println("Executing ApplyBaseTerraform") + terraformAction := "apply" + + os.Setenv("TF_VAR_aws_account_id", viper.GetString("aws.accountid")) + os.Setenv("TF_VAR_aws_region", viper.GetString("aws.region")) + os.Setenv("TF_VAR_hosted_zone_name", viper.GetString("aws.domainname")) + + err := os.Chdir(directory) + if err != nil { + fmt.Println("error changing dir") + } + + viperDestoryFlag := viper.GetBool("terraform.destroy") + cmdDestroyFlag, _ := cmd.Flags().GetBool("destroy") + + if viperDestoryFlag == true || cmdDestroyFlag == true { + terraformAction = "destroy" + } + + log.Println("terraform action: ", terraformAction, "destroyFlag: ", viperDestoryFlag) + execShellReturnStrings(terraformPath, "init") + execShellReturnStrings(terraformPath, fmt.Sprintf("%s", terraformAction), "-auto-approve") + keyOut, _, errKey := execShellReturnStrings(terraformPath, "output", "vault_unseal_kms_key") + if errKey != nil { + fmt.Println("failed to call tfOutputCmd.Run(): ", err) + } + keyId := strings.TrimSpace(keyOut) + fmt.Println("keyid is:", keyId) + viper.Set("vault.kmskeyid", keyId) + viper.Set("create.terraformapplied.base", true) + viper.WriteConfig() + detokenize(fmt.Sprintf("%s/.kubefirst/gitops", home)) + } else { + log.Println("Skipping: ApplyBaseTerraform") + } +} From a03550f2a77b51c8b18380208dde3638bedaf82b Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Fri, 1 Jul 2022 16:02:05 +0000 Subject: [PATCH 05/10] address zip slip Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/init.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 51d819b33..2133a0b74 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -21,6 +21,7 @@ import ( "strconv" "strings" "time" + "path" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/route53" @@ -894,11 +895,11 @@ func extractTarGz(gzipStream io.Reader) { switch header.Typeflag { case tar.TypeDir: - if err := os.Mkdir(header.Name, 0755); err != nil { + if err := os.Mkdir(path.Clean(header.Name), 0755); err != nil { log.Println("extractTarGz: Mkdir() failed: %s", err.Error()) } case tar.TypeReg: - outFile, err := os.Create(header.Name) + outFile, err := os.Create(path.Clean(header.Name)) if err != nil { log.Println("extractTarGz: Create() failed: %s", err.Error()) } From 0eecf34a3d854b35ab9ec111f4a322f7e6d9bdf1 Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Fri, 1 Jul 2022 16:11:31 +0000 Subject: [PATCH 06/10] address zip slip Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/init.go | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 2133a0b74..d14d9ad4c 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -21,7 +21,6 @@ import ( "strconv" "strings" "time" - "path" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/route53" @@ -888,31 +887,33 @@ func extractTarGz(gzipStream io.Reader) { if err == io.EOF { break } - if err != nil { log.Println("extractTarGz: Next() failed: %s", err.Error()) } - - switch header.Typeflag { - case tar.TypeDir: - if err := os.Mkdir(path.Clean(header.Name), 0755); err != nil { - log.Println("extractTarGz: Mkdir() failed: %s", err.Error()) - } - case tar.TypeReg: - outFile, err := os.Create(path.Clean(header.Name)) - if err != nil { - log.Println("extractTarGz: Create() failed: %s", err.Error()) - } - if _, err := io.Copy(outFile, tarReader); err != nil { - log.Println("extractTarGz: Copy() failed: %s", err.Error()) + p, _ := filepath.Abs(header.Name) + if !strings.Contains(p, "..") { + + switch header.Typeflag { + case tar.TypeDir: + if err := os.Mkdir(header.Name, 0755); err != nil { + log.Println("extractTarGz: Mkdir() failed: %s", err.Error()) + } + case tar.TypeReg: + outFile, err := os.Create(header.Name) + if err != nil { + log.Println("extractTarGz: Create() failed: %s", err.Error()) + } + if _, err := io.Copy(outFile, tarReader); err != nil { + log.Println("extractTarGz: Copy() failed: %s", err.Error()) + } + outFile.Close() + + default: + log.Println( + "extractTarGz: uknown type: %s in %s", + header.Typeflag, + header.Name) } - outFile.Close() - - default: - log.Println( - "extractTarGz: uknown type: %s in %s", - header.Typeflag, - header.Name) } } From 47561135bc7780248665211a3c81244081f3adf6 Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Fri, 1 Jul 2022 16:56:38 +0000 Subject: [PATCH 07/10] cleaning some steps Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/create.go | 85 ++++------------------------------------ cmd/installationSteps.go | 83 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 78 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 7b22f331e..a3ef5c290 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -13,11 +13,9 @@ import ( "html/template" "log" "net/http" - "net/url" "os" "os/exec" "strings" - "syscall" "time" b64 "encoding/base64" @@ -58,34 +56,16 @@ to quickly create a Cobra application.`, directory := fmt.Sprintf("%s/.kubefirst/gitops/terraform/base", home) - - - configureAndPushFlag := viper.GetBool("create.softserve.configure") + applyBaseTerraform(cmd,directory) createSoftServe(kubeconfigPath) - - if configureAndPushFlag != true { - kPortForward := exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "soft-serve", "port-forward", "svc/soft-serve", "8022:22") - kPortForward.Stdout = os.Stdout - kPortForward.Stderr = os.Stderr - err := kPortForward.Start() - defer kPortForward.Process.Signal(syscall.SIGTERM) - if err != nil { - fmt.Println("failed to call kPortForward.Run(): ", err) - } - time.Sleep(10 * time.Second) - - configureSoftServe() - pushGitopsToSoftServe() - viper.Set("create.softserve.configure", true) - viper.WriteConfig() - } - - time.Sleep(10 * time.Second) + + configureSoftserveAndPush() helmInstallArgocd(home, kubeconfigPath) + awaitGitlab() fmt.Println("discovering gitlab toolbox pod") @@ -152,60 +132,9 @@ to quickly create a Cobra application.`, fmt.Println("gitlabRunnerRegistrationToken", gitlabRunnerRegistrationToken) } - if !viper.GetBool("create.terraformapplied.gitlab") { - // Prepare for terraform gitlab execution - os.Setenv("GITLAB_TOKEN", viper.GetString("gitlab.token")) - os.Setenv("GITLAB_BASE_URL", fmt.Sprintf("https://gitlab.%s", viper.GetString("aws.domainname"))) - - directory = fmt.Sprintf("%s/.kubefirst/gitops/terraform/gitlab", home) - err = os.Chdir(directory) - if err != nil { - fmt.Println("error changing dir") - } - - tfInitCmd := exec.Command(terraformPath, "init") - tfInitCmd.Stdout = os.Stdout - tfInitCmd.Stderr = os.Stderr - err = tfInitCmd.Run() - if err != nil { - fmt.Println("failed to call tfInitCmd.Run(): ", err) - } - - tfApplyCmd := exec.Command(terraformPath, "apply", "-auto-approve") - tfApplyCmd.Stdout = os.Stdout - tfApplyCmd.Stderr = os.Stderr - err = tfApplyCmd.Run() - if err != nil { - fmt.Println("failed to call tfApplyCmd.Run(): ", err) - } - - viper.Set("create.terraformapplied.gitlab", true) - viper.WriteConfig() - } - - // upload ssh public key - if !viper.GetBool("gitlab.keyuploaded") { - fmt.Println("uploading ssh public key to gitlab") - data := url.Values{ - "title": {"kubefirst"}, - "key": {viper.GetString("botpublickey")}, - } - - gitlabUrlBase := fmt.Sprintf("https://gitlab.%s", viper.GetString("aws.domainname")) - - resp, err := http.PostForm(gitlabUrlBase+"/api/v4/user/keys?private_token="+gitlabToken, data) - if err != nil { - log.Fatal(err) - } - var res map[string]interface{} - json.NewDecoder(resp.Body).Decode(&res) - fmt.Println(res) - fmt.Println("ssh public key uploaded to gitlab") - viper.Set("gitlab.keyuploaded", true) - viper.WriteConfig() - } else { - fmt.Println("ssh public key already uploaded to gitlab") - } + applyGitlabTerraform(directory) + + gitlabKeyUpload() pushGitopsToGitLab() changeRegistryToGitLab() diff --git a/cmd/installationSteps.go b/cmd/installationSteps.go index cabbed7a4..5f0b238c6 100644 --- a/cmd/installationSteps.go +++ b/cmd/installationSteps.go @@ -7,6 +7,14 @@ import ( "strings" "github.com/spf13/cobra" "github.com/spf13/viper" + + "os/exec" + "syscall" + "time" + + "net/url" + "net/http" + "encoding/json" ) func applyBaseTerraform(cmd *cobra.Command,directory string){ @@ -48,3 +56,78 @@ func applyBaseTerraform(cmd *cobra.Command,directory string){ log.Println("Skipping: ApplyBaseTerraform") } } + + +func applyGitlabTerraform(directory string){ + if !viper.GetBool("create.terraformapplied.gitlab") { + log.Println("Executing applyGitlabTerraform") + // Prepare for terraform gitlab execution + os.Setenv("GITLAB_TOKEN", viper.GetString("gitlab.token")) + os.Setenv("GITLAB_BASE_URL", fmt.Sprintf("https://gitlab.%s", viper.GetString("aws.domainname"))) + + directory = fmt.Sprintf("%s/.kubefirst/gitops/terraform/gitlab", home) + err := os.Chdir(directory) + if err != nil { + fmt.Println("error changing dir") + } + execShellReturnStrings(terraformPath, "init") + execShellReturnStrings(terraformPath, "apply", "-auto-approve") + viper.Set("create.terraformapplied.gitlab", true) + viper.WriteConfig() + } else { + log.Println("Skipping: applyGitlabTerraform") + } +} + +func configureSoftserveAndPush(){ + configureAndPushFlag := viper.GetBool("create.softserve.configure") + if configureAndPushFlag != true { + log.Println("Executing configureSoftserveAndPush") + kPortForward := exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "soft-serve", "port-forward", "svc/soft-serve", "8022:22") + kPortForward.Stdout = os.Stdout + kPortForward.Stderr = os.Stderr + err := kPortForward.Start() + defer kPortForward.Process.Signal(syscall.SIGTERM) + if err != nil { + fmt.Println("failed to call kPortForward.Run(): ", err) + } + time.Sleep(10 * time.Second) + + configureSoftServe() + pushGitopsToSoftServe() + viper.Set("create.softserve.configure", true) + viper.WriteConfig() + time.Sleep(10 * time.Second) + } else { + log.Println("Skipping: configureSoftserveAndPush") + } +} + +func gitlabKeyUpload(){ + // upload ssh public key + if !viper.GetBool("gitlab.keyuploaded") { + log.Println("Executing gitlabKeyUpload") + log.Println("uploading ssh public key to gitlab") + gitlabToken := viper.GetString("gitlab.token") + data := url.Values{ + "title": {"kubefirst"}, + "key": {viper.GetString("botpublickey")}, + } + + gitlabUrlBase := fmt.Sprintf("https://gitlab.%s", viper.GetString("aws.domainname")) + + resp, err := http.PostForm(gitlabUrlBase+"/api/v4/user/keys?private_token="+gitlabToken, data) + if err != nil { + log.Fatal(err) + } + var res map[string]interface{} + json.NewDecoder(resp.Body).Decode(&res) + fmt.Println(res) + fmt.Println("ssh public key uploaded to gitlab") + viper.Set("gitlab.keyuploaded", true) + viper.WriteConfig() + } else { + log.Println("Skipping: gitlabKeyUpload") + log.Println("ssh public key already uploaded to gitlab") + } +} \ No newline at end of file From 410f0a39e65a3bc01de4438cc0c2ea211467eb21 Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Fri, 1 Jul 2022 18:36:34 +0000 Subject: [PATCH 08/10] reshuffle create steps Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/create.go | 76 +--------------------------------------- cmd/installationSteps.go | 74 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 75 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index a3ef5c290..c34f5690b 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -25,7 +25,6 @@ import ( gitConfig "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/plumbing/object" gitHttp "github.com/go-git/go-git/v5/plumbing/transport/http" - "github.com/google/uuid" vault "github.com/hashicorp/vault/api" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -55,87 +54,14 @@ to quickly create a Cobra application.`, flare.SendTelemetry(metricDomain, metricName) directory := fmt.Sprintf("%s/.kubefirst/gitops/terraform/base", home) - - - applyBaseTerraform(cmd,directory) - createSoftServe(kubeconfigPath) - configureSoftserveAndPush() - helmInstallArgocd(home, kubeconfigPath) - awaitGitlab() - - fmt.Println("discovering gitlab toolbox pod") - - var outb, errb bytes.Buffer - k := exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "gitlab", "get", "pod", "-lapp=toolbox", "-o", "jsonpath='{.items[0].metadata.name}'") - k.Stdout = &outb - k.Stderr = &errb - err := k.Run() - if err != nil { - fmt.Println("failed to call k.Run() to get gitlab pod: ", err) - } - gitlabPodName := outb.String() - gitlabPodName = strings.Replace(gitlabPodName, "'", "", -1) - fmt.Println("gitlab pod", gitlabPodName) - - gitlabToken := viper.GetString("gitlab.token") - if gitlabToken == "" { - - fmt.Println("getting gitlab personal access token") - - id := uuid.New() - gitlabToken = id.String()[:20] - - k = exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "gitlab", "exec", gitlabPodName, "--", "gitlab-rails", "runner", fmt.Sprintf("token = User.find_by_username('root').personal_access_tokens.create(scopes: [:write_registry, :write_repository, :api], name: 'Automation token'); token.set_token('%s'); token.save!", gitlabToken)) - k.Stdout = os.Stdout - k.Stderr = os.Stderr - err = k.Run() - if err != nil { - fmt.Println("failed to call k.Run() to set gitlab token: ", err) - } - - viper.Set("gitlab.token", gitlabToken) - viper.WriteConfig() - - fmt.Println("gitlabToken", gitlabToken) - } - - gitlabRunnerToken := viper.GetString("gitlab.runnertoken") - if gitlabRunnerToken == "" { - - fmt.Println("getting gitlab runner token") - - var tokenOut, tokenErr bytes.Buffer - k = exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "gitlab", "get", "secret", "gitlab-gitlab-runner-secret", "-o", "jsonpath='{.data.runner-registration-token}'") - k.Stdout = &tokenOut - k.Stderr = &tokenErr - err = k.Run() - if err != nil { - fmt.Println("failed to call k.Run() to get gitlabRunnerRegistrationToken: ", err) - } - encodedToken := tokenOut.String() - fmt.Println(encodedToken) - encodedToken = strings.Replace(encodedToken, "'", "", -1) - fmt.Println(encodedToken) - gitlabRunnerRegistrationTokenBytes, err := base64.StdEncoding.DecodeString(encodedToken) - gitlabRunnerRegistrationToken := string(gitlabRunnerRegistrationTokenBytes) - fmt.Println(gitlabRunnerRegistrationToken) - if err != nil { - panic(err) - } - viper.Set("gitlab.runnertoken", gitlabRunnerRegistrationToken) - viper.WriteConfig() - fmt.Println("gitlabRunnerRegistrationToken", gitlabRunnerRegistrationToken) - } - + produceGitlabTokens() applyGitlabTerraform(directory) - gitlabKeyUpload() - pushGitopsToGitLab() changeRegistryToGitLab() configureVault() diff --git a/cmd/installationSteps.go b/cmd/installationSteps.go index 5f0b238c6..0824144ce 100644 --- a/cmd/installationSteps.go +++ b/cmd/installationSteps.go @@ -15,6 +15,10 @@ import ( "net/url" "net/http" "encoding/json" + + "github.com/google/uuid" + "bytes" + "encoding/base64" ) func applyBaseTerraform(cmd *cobra.Command,directory string){ @@ -130,4 +134,74 @@ func gitlabKeyUpload(){ log.Println("Skipping: gitlabKeyUpload") log.Println("ssh public key already uploaded to gitlab") } +} + + +func produceGitlabTokens(){ + //TODO: Should this step be skipped if already executed? + + fmt.Println("discovering gitlab toolbox pod") + + var outb, errb bytes.Buffer + k := exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "gitlab", "get", "pod", "-lapp=toolbox", "-o", "jsonpath='{.items[0].metadata.name}'") + k.Stdout = &outb + k.Stderr = &errb + err := k.Run() + if err != nil { + fmt.Println("failed to call k.Run() to get gitlab pod: ", err) + } + gitlabPodName := outb.String() + gitlabPodName = strings.Replace(gitlabPodName, "'", "", -1) + fmt.Println("gitlab pod", gitlabPodName) + + gitlabToken := viper.GetString("gitlab.token") + if gitlabToken == "" { + + fmt.Println("getting gitlab personal access token") + + id := uuid.New() + gitlabToken = id.String()[:20] + + k = exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "gitlab", "exec", gitlabPodName, "--", "gitlab-rails", "runner", fmt.Sprintf("token = User.find_by_username('root').personal_access_tokens.create(scopes: [:write_registry, :write_repository, :api], name: 'Automation token'); token.set_token('%s'); token.save!", gitlabToken)) + k.Stdout = os.Stdout + k.Stderr = os.Stderr + err = k.Run() + if err != nil { + fmt.Println("failed to call k.Run() to set gitlab token: ", err) + } + + viper.Set("gitlab.token", gitlabToken) + viper.WriteConfig() + + fmt.Println("gitlabToken", gitlabToken) + } + + gitlabRunnerToken := viper.GetString("gitlab.runnertoken") + if gitlabRunnerToken == "" { + + fmt.Println("getting gitlab runner token") + + var tokenOut, tokenErr bytes.Buffer + k = exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "gitlab", "get", "secret", "gitlab-gitlab-runner-secret", "-o", "jsonpath='{.data.runner-registration-token}'") + k.Stdout = &tokenOut + k.Stderr = &tokenErr + err = k.Run() + if err != nil { + fmt.Println("failed to call k.Run() to get gitlabRunnerRegistrationToken: ", err) + } + encodedToken := tokenOut.String() + fmt.Println(encodedToken) + encodedToken = strings.Replace(encodedToken, "'", "", -1) + fmt.Println(encodedToken) + gitlabRunnerRegistrationTokenBytes, err := base64.StdEncoding.DecodeString(encodedToken) + gitlabRunnerRegistrationToken := string(gitlabRunnerRegistrationTokenBytes) + fmt.Println(gitlabRunnerRegistrationToken) + if err != nil { + panic(err) + } + viper.Set("gitlab.runnertoken", gitlabRunnerRegistrationToken) + viper.WriteConfig() + fmt.Println("gitlabRunnerRegistrationToken", gitlabRunnerRegistrationToken) + } + } \ No newline at end of file From 438fa3644d92435fd6d9a5fdde6dccc61ce96faf Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Mon, 4 Jul 2022 13:35:40 +0000 Subject: [PATCH 09/10] Redirecting out to logs Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/create.go | 47 ++++++++++++++++++++-------------------- cmd/destroy.go | 15 +++++++------ cmd/globals.go | 3 ++- cmd/init.go | 6 ++--- cmd/installationSteps.go | 38 ++++++++++++++++---------------- cmd/kubectl.go | 7 +++--- pkg/flare/aws.go | 8 +++---- pkg/flare/telemetry.go | 7 +++--- 8 files changed, 68 insertions(+), 63 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index c34f5690b..7bd408060 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -90,7 +90,7 @@ func hydrateGitlabMetaphorRepo() { // todo make global domainName := fmt.Sprintf("https://gitlab.%s", viper.GetString("aws.domainname")) - fmt.Println("git remote add origin", domainName) + log.Println("git remote add origin", domainName) _, err = metaphorTemplateRepo.CreateRemote(&gitConfig.RemoteConfig{ Name: "gitlab", URLs: []string{fmt.Sprintf("%s/kubefirst/metaphor.git", domainName)}, @@ -98,7 +98,7 @@ func hydrateGitlabMetaphorRepo() { w, _ := metaphorTemplateRepo.Worktree() - fmt.Println("Committing new changes...") + log.Println("Committing new changes...") w.Add(".") w.Commit("setting new remote upstream to gitlab", &git.CommitOptions{ Author: &object.Signature{ @@ -116,7 +116,7 @@ func hydrateGitlabMetaphorRepo() { }, }) if err != nil { - fmt.Println("error pushing to remote", err) + log.Println("error pushing to remote", err) } } @@ -169,7 +169,7 @@ func changeRegistryToGitLab() { if err := c.Execute(&secrets, creds); err != nil { log.Panic(err) } - fmt.Println(secrets.String()) + log.Println(secrets.String()) ba := []byte(secrets.String()) err = yaml.Unmarshal(ba, &argocdRepositoryAccessTokenSecret) @@ -200,7 +200,7 @@ func changeRegistryToGitLab() { if err := c.Execute(&repoSecrets, creds); err != nil { log.Panic(err) } - fmt.Println(repoSecrets.String()) + log.Println(repoSecrets.String()) ba = []byte(repoSecrets.String()) err = yaml.Unmarshal(ba, &argocdRepositoryAccessTokenSecret) @@ -215,7 +215,7 @@ func changeRegistryToGitLab() { k.Stderr = os.Stderr err = k.Run() if err != nil { - fmt.Println("failed to call k.Run() to apply argocd patch to adopt gitlab: ", err) + log.Println("failed to call k.Run() to apply argocd patch to adopt gitlab: ", err) } viper.Set("gitlab.registry", true) @@ -240,7 +240,7 @@ func addGitlabOidcApplications() { cb["vault"] = fmt.Sprintf("https://vault.%s:8250/oidc/callback http://localhost:8250/oidc/callback https://vault.%s/ui/vault/auth/oidc/oidc/callback http://localhost:8200/ui/vault/auth/oidc/oidc/callback", domain, domain) for _, app := range apps { - fmt.Println("checking to see if", app, "oidc application needs to be created in gitlab") + log.Println("checking to see if", app, "oidc application needs to be created in gitlab") appId := viper.GetString(fmt.Sprintf("gitlab.oidc.%s.applicationid", app)) if appId == "" { @@ -268,7 +268,7 @@ func addGitlabOidcApplications() { } } if created { - fmt.Println("created gitlab oidc application with applicationid", createdApp.ApplicationID) + log.Println("created gitlab oidc application with applicationid", createdApp.ApplicationID) viper.Set(fmt.Sprintf("gitlab.oidc.%s.applicationid", app), createdApp.ApplicationID) viper.Set(fmt.Sprintf("gitlab.oidc.%s.secret", app), createdApp.Secret) @@ -289,7 +289,7 @@ func addGitlabOidcApplications() { } func addVaultSecret(secretPath string, secretData map[string]interface{}) { - fmt.Println("vault called") + log.Println("vault called") config := vault.DefaultConfig() @@ -297,7 +297,7 @@ func addVaultSecret(secretPath string, secretData map[string]interface{}) { client, err := vault.NewClient(config) if err != nil { - fmt.Println("unable to initialize Vault client: ", err) + log.Println("unable to initialize Vault client: ", err) } client.SetToken(viper.GetString("vault.token")) @@ -305,9 +305,9 @@ func addVaultSecret(secretPath string, secretData map[string]interface{}) { // Writing a secret _, err = client.Logical().Write(secretPath, secretData) if err != nil { - fmt.Println("unable to write secret: ", err) + log.Println("unable to write secret: ", err) } else { - fmt.Println("secret written successfully.") + log.Println("secret written successfully.") } } @@ -327,32 +327,33 @@ func configureVault() { // ``` // ... obviously keep the sensitive values bound to vars + //TODO replace this command: var outb, errb bytes.Buffer k := exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "vault", "get", "secret", "vault-unseal-keys", "-o", "jsonpath='{.data.cluster-keys\\.json}'") k.Stdout = &outb k.Stderr = &errb err := k.Run() if err != nil { - fmt.Println("failed to call k.Run() to get gitlab pod: ", err) + log.Println("failed to call k.Run() to get gitlab pod: ", err) } vaultKeysEncoded := outb.String() vaultKeysEncoded = strings.Replace(vaultKeysEncoded, "'", "", -1) - fmt.Println("vault keys", vaultKeysEncoded) + log.Println("vault keys", vaultKeysEncoded) vaultKeysBytes, err := base64.StdEncoding.DecodeString(vaultKeysEncoded) - fmt.Println(vaultKeysBytes) + log.Println(vaultKeysBytes) if err != nil { panic(err) } vaultKeys := string(vaultKeysBytes) - fmt.Println(vaultKeys) + log.Println(vaultKeys) var dat map[string]interface{} if err := json.Unmarshal([]byte(vaultKeys), &dat); err != nil { panic(err) } vaultToken := dat["root_token"].(string) - fmt.Println(vaultToken) + log.Println(vaultToken) viper.Set("vault.token", vaultToken) viper.WriteConfig() @@ -374,7 +375,7 @@ func configureVault() { directory := fmt.Sprintf("%s/.kubefirst/gitops/terraform/vault", home) err = os.Chdir(directory) if err != nil { - fmt.Println("error changing dir") + log.Println("error changing dir") } tfInitCmd := exec.Command(terraformPath, "init") @@ -382,7 +383,7 @@ func configureVault() { tfInitCmd.Stderr = os.Stderr err = tfInitCmd.Run() if err != nil { - fmt.Println("failed to call vault terraform init: ", err) + log.Println("failed to call vault terraform init: ", err) } tfApplyCmd := exec.Command(terraformPath, "apply", "-target", "module.bootstrap", "-auto-approve") @@ -390,7 +391,7 @@ func configureVault() { tfApplyCmd.Stderr = os.Stderr err = tfApplyCmd.Run() if err != nil { - fmt.Println("failed to call vault terraform apply: ", err) + log.Println("failed to call vault terraform apply: ", err) } viper.Set("create.terraformapplied.vault", true) @@ -400,7 +401,7 @@ func configureVault() { func awaitGitlab() { - fmt.Println("awaitGitlab called") + log.Println("awaitGitlab called") max := 200 for i := 0; i < max; i++ { @@ -410,11 +411,11 @@ func awaitGitlab() { resp, _ := http.Get(fmt.Sprintf("https://gitlab.%s", hostedZoneName)) if resp != nil && resp.StatusCode == 200 { - fmt.Println("gitlab host resolved, 30 second grace period required...") + log.Println("gitlab host resolved, 30 second grace period required...") time.Sleep(time.Second * 30) i = max } else { - fmt.Println("gitlab host not resolved, sleeping 10s") + log.Println("gitlab host not resolved, sleeping 10s") time.Sleep(time.Second * 10) } } diff --git a/cmd/destroy.go b/cmd/destroy.go index d2b001c3a..3bbb2b0a8 100644 --- a/cmd/destroy.go +++ b/cmd/destroy.go @@ -6,6 +6,7 @@ package cmd import ( "fmt" + "log" "os" "os/exec" @@ -27,7 +28,7 @@ to quickly create a Cobra application.`, // todo this needs to be removed when we are no longer in the starter account os.Setenv("AWS_PROFILE", "starter") - fmt.Println("\n\nTODO -- need to setup and argocd delete against registry and wait?\n\n") + log.Println("\n\nTODO -- need to setup and argocd delete against registry and wait?\n\n") // kubeconfig := os.Getenv("HOME") + "/.kube/config" // config, err := argocdclientset.BuildConfigFromFlags("", kubeconfig) // argocdclientset, err := argocdclientset.NewForConfig(config) @@ -51,7 +52,7 @@ to quickly create a Cobra application.`, directory := fmt.Sprintf("%s/.kubefirst/gitops/terraform/gitlab", home) err := os.Chdir(directory) if err != nil { - fmt.Println("error changing dir: ", directory) + log.Println("error changing dir: ", directory) } os.Setenv("GITLAB_BASE_URL", fmt.Sprintf("https://gitlab.%s", viper.GetString("aws.domainname"))) @@ -62,7 +63,7 @@ to quickly create a Cobra application.`, tfInitGitlabCmd.Stderr = os.Stderr err = tfInitGitlabCmd.Run() if err != nil { - fmt.Println("failed to call terraform init gitlab: ", err) + log.Println("failed to call terraform init gitlab: ", err) panic("failed to terraform init gitlab") } @@ -71,7 +72,7 @@ to quickly create a Cobra application.`, tfDestroyGitlabCmd.Stderr = os.Stderr err = tfDestroyGitlabCmd.Run() if err != nil { - fmt.Println("failed to call terraform destroy gitlab: ", err) + log.Println("failed to call terraform destroy gitlab: ", err) panic("failed to terraform destroy gitlab") } @@ -83,7 +84,7 @@ to quickly create a Cobra application.`, directory = fmt.Sprintf("%s/.kubefirst/gitops/terraform/base", home) err = os.Chdir(directory) if err != nil { - fmt.Println("error changing dir: ", directory) + log.Println("error changing dir: ", directory) } tfInitBaseCmd := exec.Command(terraformPath, "init") @@ -91,7 +92,7 @@ to quickly create a Cobra application.`, tfInitBaseCmd.Stderr = os.Stderr err = tfInitBaseCmd.Run() if err != nil { - fmt.Println("failed to call terraform init base: ", err) + log.Println("failed to call terraform init base: ", err) } tfDestroyBaseCmd := exec.Command(terraformPath, "destroy", "-auto-approve") @@ -99,7 +100,7 @@ to quickly create a Cobra application.`, tfDestroyBaseCmd.Stderr = os.Stderr err = tfDestroyBaseCmd.Run() if err != nil { - fmt.Println("failed to call terraform destroy base: ", err) + log.Println("failed to call terraform destroy base: ", err) panic("failed to terraform destroy base") } diff --git a/cmd/globals.go b/cmd/globals.go index 547d0cb1a..628161f48 100644 --- a/cmd/globals.go +++ b/cmd/globals.go @@ -2,6 +2,7 @@ import ( "fmt" + "log" "os" "runtime" ) @@ -16,7 +17,7 @@ func setGlobals() { tmphome, err := os.UserHomeDir() home = tmphome if(err != nil){ - fmt.Printf("Error Defining home - %s", err) + log.Printf("Error Defining home - %s", err) os.Exit(1) } localOs = runtime.GOOS diff --git a/cmd/init.go b/cmd/init.go index d14d9ad4c..04667e30e 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -164,9 +164,9 @@ to quickly create a Cobra application.`, bucketRand() log.Println("bucketRand() complete\n\n") - fmt.Println("calling detokenize() ") + log.Println("calling detokenize() ") detokenize(fmt.Sprintf("%s/.kubefirst/gitops", home)) - fmt.Println("detokenize() complete\n\n") + log.Println("detokenize() complete\n\n") Trackers[trackerStage8].Tracker.Increment(int64(1)) // modConfigYaml() @@ -941,7 +941,7 @@ func createSoftServe(kubeconfigPath string) { viper.Set("create.softserve.create", true) viper.WriteConfig() - fmt.Println("waiting for soft-serve installation to complete...") + log.Println("waiting for soft-serve installation to complete...") time.Sleep(60 * time.Second) } else { log.Println("Skipping: createSoftServe") diff --git a/cmd/installationSteps.go b/cmd/installationSteps.go index 0824144ce..27adfcd24 100644 --- a/cmd/installationSteps.go +++ b/cmd/installationSteps.go @@ -33,7 +33,7 @@ func applyBaseTerraform(cmd *cobra.Command,directory string){ err := os.Chdir(directory) if err != nil { - fmt.Println("error changing dir") + log.Println("error changing dir") } viperDestoryFlag := viper.GetBool("terraform.destroy") @@ -48,10 +48,10 @@ func applyBaseTerraform(cmd *cobra.Command,directory string){ execShellReturnStrings(terraformPath, fmt.Sprintf("%s", terraformAction), "-auto-approve") keyOut, _, errKey := execShellReturnStrings(terraformPath, "output", "vault_unseal_kms_key") if errKey != nil { - fmt.Println("failed to call tfOutputCmd.Run(): ", err) + log.Println("failed to call tfOutputCmd.Run(): ", err) } keyId := strings.TrimSpace(keyOut) - fmt.Println("keyid is:", keyId) + log.Println("keyid is:", keyId) viper.Set("vault.kmskeyid", keyId) viper.Set("create.terraformapplied.base", true) viper.WriteConfig() @@ -72,7 +72,7 @@ func applyGitlabTerraform(directory string){ directory = fmt.Sprintf("%s/.kubefirst/gitops/terraform/gitlab", home) err := os.Chdir(directory) if err != nil { - fmt.Println("error changing dir") + log.Println("error changing dir") } execShellReturnStrings(terraformPath, "init") execShellReturnStrings(terraformPath, "apply", "-auto-approve") @@ -93,7 +93,7 @@ func configureSoftserveAndPush(){ err := kPortForward.Start() defer kPortForward.Process.Signal(syscall.SIGTERM) if err != nil { - fmt.Println("failed to call kPortForward.Run(): ", err) + log.Println("failed to call kPortForward.Run(): ", err) } time.Sleep(10 * time.Second) @@ -126,8 +126,8 @@ func gitlabKeyUpload(){ } var res map[string]interface{} json.NewDecoder(resp.Body).Decode(&res) - fmt.Println(res) - fmt.Println("ssh public key uploaded to gitlab") + log.Println(res) + log.Println("ssh public key uploaded to gitlab") viper.Set("gitlab.keyuploaded", true) viper.WriteConfig() } else { @@ -140,7 +140,7 @@ func gitlabKeyUpload(){ func produceGitlabTokens(){ //TODO: Should this step be skipped if already executed? - fmt.Println("discovering gitlab toolbox pod") + log.Println("discovering gitlab toolbox pod") var outb, errb bytes.Buffer k := exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "gitlab", "get", "pod", "-lapp=toolbox", "-o", "jsonpath='{.items[0].metadata.name}'") @@ -148,16 +148,16 @@ func produceGitlabTokens(){ k.Stderr = &errb err := k.Run() if err != nil { - fmt.Println("failed to call k.Run() to get gitlab pod: ", err) + log.Println("failed to call k.Run() to get gitlab pod: ", err) } gitlabPodName := outb.String() gitlabPodName = strings.Replace(gitlabPodName, "'", "", -1) - fmt.Println("gitlab pod", gitlabPodName) + log.Println("gitlab pod", gitlabPodName) gitlabToken := viper.GetString("gitlab.token") if gitlabToken == "" { - fmt.Println("getting gitlab personal access token") + log.Println("getting gitlab personal access token") id := uuid.New() gitlabToken = id.String()[:20] @@ -167,19 +167,19 @@ func produceGitlabTokens(){ k.Stderr = os.Stderr err = k.Run() if err != nil { - fmt.Println("failed to call k.Run() to set gitlab token: ", err) + log.Println("failed to call k.Run() to set gitlab token: ", err) } viper.Set("gitlab.token", gitlabToken) viper.WriteConfig() - fmt.Println("gitlabToken", gitlabToken) + log.Println("gitlabToken", gitlabToken) } gitlabRunnerToken := viper.GetString("gitlab.runnertoken") if gitlabRunnerToken == "" { - fmt.Println("getting gitlab runner token") + log.Println("getting gitlab runner token") var tokenOut, tokenErr bytes.Buffer k = exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "gitlab", "get", "secret", "gitlab-gitlab-runner-secret", "-o", "jsonpath='{.data.runner-registration-token}'") @@ -187,21 +187,21 @@ func produceGitlabTokens(){ k.Stderr = &tokenErr err = k.Run() if err != nil { - fmt.Println("failed to call k.Run() to get gitlabRunnerRegistrationToken: ", err) + log.Println("failed to call k.Run() to get gitlabRunnerRegistrationToken: ", err) } encodedToken := tokenOut.String() - fmt.Println(encodedToken) + log.Println(encodedToken) encodedToken = strings.Replace(encodedToken, "'", "", -1) - fmt.Println(encodedToken) + log.Println(encodedToken) gitlabRunnerRegistrationTokenBytes, err := base64.StdEncoding.DecodeString(encodedToken) gitlabRunnerRegistrationToken := string(gitlabRunnerRegistrationTokenBytes) - fmt.Println(gitlabRunnerRegistrationToken) + log.Println(gitlabRunnerRegistrationToken) if err != nil { panic(err) } viper.Set("gitlab.runnertoken", gitlabRunnerRegistrationToken) viper.WriteConfig() - fmt.Println("gitlabRunnerRegistrationToken", gitlabRunnerRegistrationToken) + log.Println("gitlabRunnerRegistrationToken", gitlabRunnerRegistrationToken) } } \ No newline at end of file diff --git a/cmd/kubectl.go b/cmd/kubectl.go index 856e9ef0c..c8e835113 100644 --- a/cmd/kubectl.go +++ b/cmd/kubectl.go @@ -8,6 +8,7 @@ import ( "context" "encoding/json" "fmt" + "log" metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" coreV1Types "k8s.io/client-go/kubernetes/typed/core/v1" ) @@ -25,7 +26,7 @@ var gitlabPodsClient coreV1Types.PodInterface func getPodNameByLabel(gitlabPodsClient coreV1Types.PodInterface, label string) string { pods, err := gitlabPodsClient.List(context.TODO(), metaV1.ListOptions{LabelSelector: fmt.Sprintf("app=%s", label)}) if err != nil { - fmt.Println(err) + log.Println(err) } gitlabToolboxPodName = pods.Items[0].Name @@ -35,7 +36,7 @@ func getPodNameByLabel(gitlabPodsClient coreV1Types.PodInterface, label string) func getVaultRootToken(vaultSecretClient coreV1Types.SecretInterface) string { name := "vault-unseal-keys" - fmt.Printf("Reading secret %s\n", name) + log.Printf("Reading secret %s\n", name) secret, err := vaultSecretClient.Get(context.TODO(), name, metaV1.GetOptions{}) if err != nil { @@ -56,7 +57,7 @@ func getVaultRootToken(vaultSecretClient coreV1Types.SecretInterface) string { func getSecretValue(k8sClient coreV1Types.SecretInterface, secretName, key string) string { secret, err := k8sClient.Get(context.TODO(), secretName, metaV1.GetOptions{}) if err != nil { - fmt.Println(fmt.Sprintf("error getting key: %s from secret: %s", key, secretName), err) + log.Println(fmt.Sprintf("error getting key: %s from secret: %s", key, secretName), err) } return string(secret.Data[key]) } diff --git a/pkg/flare/aws.go b/pkg/flare/aws.go index b70763d04..6d8aeed66 100644 --- a/pkg/flare/aws.go +++ b/pkg/flare/aws.go @@ -2,8 +2,8 @@ package flare import ( "context" - "fmt" + "log" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/eks" "github.com/aws/aws-sdk-go/aws" @@ -13,7 +13,7 @@ func DescribeCluster() { cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { - fmt.Println("failed to load configuration, error:", err) + log.Println("failed to load configuration, error:", err) } // https://aws.github.io/aws-sdk-go-v2/docs/making-requests/#overriding-configuration eksClient := eks.NewFromConfig(cfg, func(o *eks.Options) { @@ -24,8 +24,8 @@ func DescribeCluster() { Name: aws.String("kubefirst"), }) if err != nil { - fmt.Println("error describing cluster", err) + log.Println("error describing cluster", err) } // todo base64 encoded data : *cluster.Cluster.CertificateAuthority.Data, - fmt.Println("cluster:", *cluster.Cluster.Arn, *cluster.Cluster.Endpoint) + log.Println("cluster:", *cluster.Cluster.Arn, *cluster.Cluster.Endpoint) } diff --git a/pkg/flare/telemetry.go b/pkg/flare/telemetry.go index c1b0e0b36..ee138f1ed 100644 --- a/pkg/flare/telemetry.go +++ b/pkg/flare/telemetry.go @@ -2,6 +2,7 @@ package flare import ( "fmt" + "log" "io/ioutil" "net/http" "strings" @@ -18,7 +19,7 @@ func SendTelemetry(domain, metricName string) { req, err := http.NewRequest(method, url, payload) if err != nil { - fmt.Println(err) + log.Println(err) } req.Header.Add("Content-Type", "application/json") @@ -27,10 +28,10 @@ func SendTelemetry(domain, metricName string) { res, err := client.Do(req) if err != nil { - fmt.Println("error") + log.Println("error") } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) - fmt.Println(string(body)) + log.Println(string(body)) } From 97b5e70bd336c687713e874509432cd6db0a18e5 Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Mon, 4 Jul 2022 14:12:16 +0000 Subject: [PATCH 10/10] add create dry-run Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/create.go | 45 ++++++++++++++++++++++++++++++++++------ cmd/init.go | 18 +++++++++++++--- cmd/installationSteps.go | 26 +++++++++++++++++++---- 3 files changed, 76 insertions(+), 13 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 7bd408060..fcdabfa60 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -51,7 +51,12 @@ to quickly create a Cobra application.`, metricName := "kubefirst.mgmt_cluster_install.started" metricDomain := viper.GetString("aws.domainname") - flare.SendTelemetry(metricDomain, metricName) + if !dryrunMode { + flare.SendTelemetry(metricDomain, metricName) + } else { + log.Printf("[#99] Dry-run mode, telemetry skipped: %s", metricName) + } + directory := fmt.Sprintf("%s/.kubefirst/gitops/terraform/base", home) applyBaseTerraform(cmd,directory) @@ -68,13 +73,21 @@ to quickly create a Cobra application.`, addGitlabOidcApplications() hydrateGitlabMetaphorRepo() metricName = "kubefirst.mgmt_cluster_install.completed" - - flare.SendTelemetry(metricDomain, metricName) + + if !dryrunMode { + flare.SendTelemetry(metricDomain, metricName) + } else { + log.Printf("[#99] Dry-run mode, telemetry skipped: %s", metricName) + } }, } func hydrateGitlabMetaphorRepo() { - + //TODO: Should this be skipped if already executed? + if dryrunMode { + log.Printf("[#99] Dry-run mode, hydrateGitlabMetaphorRepo skipped.") + return + } metaphorTemplateDir := fmt.Sprintf("%s/.kubefirst/metaphor", home) url := "https://github.com/kubefirst/metaphor-template" @@ -123,6 +136,10 @@ func hydrateGitlabMetaphorRepo() { func changeRegistryToGitLab() { if !viper.GetBool("gitlab.registry") { + if dryrunMode { + log.Printf("[#99] Dry-run mode, changeRegistryToGitLab skipped.") + return + } type ArgocdGitCreds struct { PersonalAccessToken string @@ -220,10 +237,17 @@ func changeRegistryToGitLab() { viper.Set("gitlab.registry", true) viper.WriteConfig() + } else { + log.Println("Skipping: changeRegistryToGitLab") } } func addGitlabOidcApplications() { + //TODO: Should this skipped if already executed. + if dryrunMode { + log.Printf("[#99] Dry-run mode, addGitlabOidcApplications skipped.") + return + } domain := viper.GetString("aws.domainname") git, err := gitlab.NewClient( viper.GetString("gitlab.token"), @@ -313,7 +337,10 @@ func addVaultSecret(secretPath string, secretData map[string]interface{}) { func configureVault() { if !viper.GetBool("create.terraformapplied.vault") { - + if dryrunMode { + log.Printf("[#99] Dry-run mode, configureVault skipped.") + return + } // ``` // NOTE: the terraform here produces unnecessary $var.varname vars in the atlantis secret for nonsensitive values // the following atlantis secrets shouldn't have vars in the gitops source code for the atlantis secret, they @@ -396,11 +423,16 @@ func configureVault() { viper.Set("create.terraformapplied.vault", true) viper.WriteConfig() + } else { + log.Println("Skipping: configureVault") } } func awaitGitlab() { - + if dryrunMode { + log.Printf("[#99] Dry-run mode, awaitGitlab skipped.") + return + } log.Println("awaitGitlab called") max := 200 for i := 0; i < max; i++ { @@ -428,5 +460,6 @@ func init() { // createCmd.MarkFlagRequired("tf-entrypoint") // todo make this an optional switch and check for it or viper createCmd.Flags().Bool("destroy", false, "destroy resources") + createCmd.PersistentFlags().BoolVarP(&dryrunMode, "dry-run", "s", false, "set to dry-run mode, no changes done on cloud provider selected") } diff --git a/cmd/init.go b/cmd/init.go index 04667e30e..b36f41f04 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -583,6 +583,12 @@ func pushGitopsToSoftServe() { } func pushGitopsToGitLab() { + if dryrunMode { + log.Printf("[#99] Dry-run mode, pushGitopsToGitLab skipped.") + return + } + + //TODO: should this step to be skipped if already executed? domain := viper.GetString("aws.domainname") detokenize(fmt.Sprintf("%s/.kubefirst/gitops", home)) @@ -920,10 +926,13 @@ func extractTarGz(gzipStream io.Reader) { } func createSoftServe(kubeconfigPath string) { - createSoftServeFlag := viper.GetBool("create.softserve.create") - + createSoftServeFlag := viper.GetBool("create.softserve.create") if createSoftServeFlag != true { log.Println("Executing createSoftServe") + if dryrunMode { + log.Printf("[#99] Dry-run mode, createSoftServe skipped.") + return + } toolsDir := fmt.Sprintf("%s/.kubefirst/tools", home) err := os.Mkdir(toolsDir, 0777) @@ -950,9 +959,12 @@ func createSoftServe(kubeconfigPath string) { } func helmInstallArgocd(home string, kubeconfigPath string) { - argocdCreated := viper.GetBool("create.argocd.helm") if !argocdCreated { + if dryrunMode { + log.Printf("[#99] Dry-run mode, helmInstallArgocd skipped.") + return + } helmClientPath := fmt.Sprintf("%s/.kubefirst/tools/helm", home) // ! commenting out until a clean execution is necessary // create namespace diff --git a/cmd/installationSteps.go b/cmd/installationSteps.go index 27adfcd24..b7ef55b5c 100644 --- a/cmd/installationSteps.go +++ b/cmd/installationSteps.go @@ -25,6 +25,10 @@ func applyBaseTerraform(cmd *cobra.Command,directory string){ applyBase := viper.GetBool("create.terraformapplied.base") if applyBase != true { log.Println("Executing ApplyBaseTerraform") + if dryrunMode { + log.Printf("[#99] Dry-run mode, applyBaseTerraform skipped.") + return + } terraformAction := "apply" os.Setenv("TF_VAR_aws_account_id", viper.GetString("aws.accountid")) @@ -65,6 +69,10 @@ func applyBaseTerraform(cmd *cobra.Command,directory string){ func applyGitlabTerraform(directory string){ if !viper.GetBool("create.terraformapplied.gitlab") { log.Println("Executing applyGitlabTerraform") + if dryrunMode { + log.Printf("[#99] Dry-run mode, applyGitlabTerraform skipped.") + return + } // Prepare for terraform gitlab execution os.Setenv("GITLAB_TOKEN", viper.GetString("gitlab.token")) os.Setenv("GITLAB_BASE_URL", fmt.Sprintf("https://gitlab.%s", viper.GetString("aws.domainname"))) @@ -87,6 +95,10 @@ func configureSoftserveAndPush(){ configureAndPushFlag := viper.GetBool("create.softserve.configure") if configureAndPushFlag != true { log.Println("Executing configureSoftserveAndPush") + if dryrunMode { + log.Printf("[#99] Dry-run mode, configureSoftserveAndPush skipped.") + return + } kPortForward := exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "soft-serve", "port-forward", "svc/soft-serve", "8022:22") kPortForward.Stdout = os.Stdout kPortForward.Stderr = os.Stderr @@ -108,9 +120,13 @@ func configureSoftserveAndPush(){ } func gitlabKeyUpload(){ - // upload ssh public key + // upload ssh public key if !viper.GetBool("gitlab.keyuploaded") { log.Println("Executing gitlabKeyUpload") + if dryrunMode { + log.Printf("[#99] Dry-run mode, gitlabKeyUpload skipped.") + return + } log.Println("uploading ssh public key to gitlab") gitlabToken := viper.GetString("gitlab.token") data := url.Values{ @@ -139,9 +155,11 @@ func gitlabKeyUpload(){ func produceGitlabTokens(){ //TODO: Should this step be skipped if already executed? - - log.Println("discovering gitlab toolbox pod") - + log.Println("discovering gitlab toolbox pod") + if dryrunMode { + log.Printf("[#99] Dry-run mode, produceGitlabTokens skipped.") + return + } var outb, errb bytes.Buffer k := exec.Command(kubectlClientPath, "--kubeconfig", kubeconfigPath, "-n", "gitlab", "get", "pod", "-lapp=toolbox", "-o", "jsonpath='{.items[0].metadata.name}'") k.Stdout = &outb