Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Active github flavor on gitlab #593

Merged
merged 6 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/addCi.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ var addCiCmd = &cobra.Command{
return err
}

err = ciTools.ApplyTemplates(globalFlags)
if err != nil {
return err
}

if viper.GetString("gitprovider") == "gitlab" {
ciTools.DeployOnGitlab(globalFlags, bucketName)
}
Expand Down
12 changes: 11 additions & 1 deletion cmd/destroyCi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>

*/
package cmd

Expand Down Expand Up @@ -38,13 +37,24 @@ var destroyCiCmd = &cobra.Command{

ciTools.DestroyCITerraform(globalFlags.DryRun)

err = ciTools.DeleteTemplates(globalFlags)
if err != nil {
return err
}

if ciFlags.DestroyBucket {
err = ciTools.DestroyBucket()
if err != nil {
return err
}
}

err = ciTools.DestroyGitRepository(globalFlags)
if err != nil {
log.Panicf("error to destroy ci repostory: %s", err)
//return err
}

err = os.RemoveAll(ciDirectory)
if err != nil {
return fmt.Errorf("unable to delete %q folder, error is: %s", ciDirectory, err)
Expand Down
9 changes: 5 additions & 4 deletions cmd/postInstall.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cmd

import (
"github.com/kubefirst/kubefirst/internal/k8s"
"log"
"time"

"github.com/kubefirst/kubefirst/internal/k8s"

"github.com/kubefirst/kubefirst/internal/flagset"
"github.com/kubefirst/kubefirst/internal/reports"

Expand Down Expand Up @@ -56,7 +57,7 @@ var postInstallCmd = &cobra.Command{

err := pkg.OpenBrowser(pkg.LocalConsoleUI)
if err != nil {
return err
log.Println(err)
}

} else {
Expand All @@ -75,8 +76,8 @@ var postInstallCmd = &cobra.Command{
log.Println(err)
}
err = pkg.OpenBrowser(pkg.LocalConsoleUI)
if err != nil {
return err
if pkg.OpenBrowser(pkg.LocalConsoleUI) != nil {
log.Println(err)
}
}

Expand Down
107 changes: 101 additions & 6 deletions internal/ciTools/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"log"
"os"
"strings"
"time"

"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/internal/flagset"
"github.com/kubefirst/kubefirst/internal/gitlab"
"github.com/kubefirst/kubefirst/internal/repo"
"github.com/kubefirst/kubefirst/pkg"
"github.com/spf13/viper"
)

Expand All @@ -24,13 +26,39 @@ func DeployOnGitlab(globalFlags flagset.GlobalFlags, bucketName string) error {
repo.PrepareKubefirstTemplateRepo(globalFlags.DryRun, config, viper.GetString("gitops.owner"), "ci", viper.GetString("ci.branch"), viper.GetString("template.tag"))
log.Println("clone and detokenization of ci-template repository complete")

err := SedBucketName("<BUCKET_NAME>", bucketName)
secretProviderFile := fmt.Sprintf("%s/ci/terraform/secret/provider.tf", config.K1FolderPath)
baseProviderFile := fmt.Sprintf("%s/ci/terraform/base/provider.tf", config.K1FolderPath)

err := SedBucketName("<BUCKET_NAME>", bucketName, secretProviderFile)
if err != nil {
log.Panicf("Error sed bucket name on CI repository: %s", err)
return err
}

err = SedBucketName("<BUCKET_NAME>", bucketName, baseProviderFile)
if err != nil {
log.Panicf("Error sed bucket name on CI repository: %s", err)
return err
}

ciLocation := fmt.Sprintf("%s/ci/components/argo-gitlab/ci.yaml", config.K1FolderPath)
ciLocation := ""
workflowLocation := fmt.Sprintf("%s/ci/.gitlab-ci.yml", config.K1FolderPath)

if viper.GetString("ci.flavor") == "github" {
ciLocation = fmt.Sprintf("%s/ci/components/argo-github/ci.yaml", config.K1FolderPath)
} else {
ciLocation = fmt.Sprintf("%s/ci/components/argo-gitlab/ci.yaml", config.K1FolderPath)
}

err = DetokenizeCI("<CI_GITOPS_BRANCH>", viper.GetString("ci.gitops.branch"), ciLocation)
if err != nil {
log.Println(err)
}

err = DetokenizeCI("<CI_METAPHOR_BRANCH>", viper.GetString("ci.metaphor.branch"), ciLocation)
if err != nil {
log.Println(err)
}

err = DetokenizeCI("<CI_CLUSTER_NAME>", viper.GetString("ci.cluster.name"), ciLocation)
if err != nil {
Expand All @@ -44,6 +72,25 @@ func DeployOnGitlab(globalFlags flagset.GlobalFlags, bucketName string) error {
if err != nil {
log.Println(err)
}
err = DetokenizeCI("<FLAVOR>", viper.GetString("ci.flavor"), workflowLocation)
if err != nil {
log.Println(err)
}
err = DetokenizeCI("<CI_KUBEFIRST_BRANCH>", viper.GetString("ci.kubefirst.branch"), workflowLocation)
if err != nil {
log.Println(err)
}

if viper.GetString("ci.flavor") == "github" {
err = DetokenizeCI("<CI_GITHUB_USER>", viper.GetString("ci.github.user"), ciLocation)
if err != nil {
log.Println(err)
}
err = DetokenizeCI("<CI_GITHUB_OWNER>", viper.GetString("ci.github.owner"), ciLocation)
if err != nil {
log.Println(err)
}
}

if !viper.GetBool("gitlab.ci-pushed") {
log.Println("Pushing ci repo to origin gitlab")
Expand All @@ -56,10 +103,7 @@ func DeployOnGitlab(globalFlags flagset.GlobalFlags, bucketName string) error {
return nil
}

func SedBucketName(old, new string) error {
cfg := configs.ReadConfig()
providerFile := fmt.Sprintf("%s/ci/terraform/base/provider.tf", cfg.K1FolderPath)

func SedBucketName(old string, new string, providerFile string) error {
fileData, err := os.ReadFile(providerFile)
if err != nil {
return err
Expand Down Expand Up @@ -95,3 +139,54 @@ func DetokenizeCI(old, new, ciLocation string) error {
}
return nil
}

func DestroyGitRepository(globalFlags flagset.GlobalFlags) error {
domain := viper.GetString("aws.hostedzonename")
url := fmt.Sprintf("https://gitlab.%s/api/v4/projects/kubefirst%%2Fci", domain)
_, _, err := pkg.ExecShellReturnStrings("curl", "-H", "-vL", "-X", "DELETE", url, "-H", "Content-Type: application/json", "-H", fmt.Sprintf("Private-Token: %s", viper.GetString("gitlab.token")))
if err != nil {
log.Panicf("error: delete CI repository: %s", err)
return err
}
return nil
}

func ApplyTemplates(globalFlags flagset.GlobalFlags) error {
config := configs.ReadConfig()
if globalFlags.DryRun {
log.Printf("[#99] Dry-run mode, ApplyTemplates skipped.")
return nil
}

_, _, err := pkg.ExecShellReturnStrings(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "-n", "argo", "apply", "-f", fmt.Sprintf("%s/ci/components/templates/cwft-k1-ci.yaml", config.K1FolderPath))
if err != nil {
log.Printf("failed to execute kubectl apply of cwft-k1-ci: %s", err)
return err
}

time.Sleep(45 * time.Second)
viper.Set("ci.cwft-k1-ci.applied", true)
viper.WriteConfig()

return nil
}

func DeleteTemplates(globalFlags flagset.GlobalFlags) error {
config := configs.ReadConfig()
if globalFlags.DryRun {
log.Printf("[#99] Dry-run mode, ApplyTemplates skipped.")
return nil
}

_, _, err := pkg.ExecShellReturnStrings(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "-n", "argo", "delete", "-f", fmt.Sprintf("%s/ci/components/templates/cwft-k1-ci.yaml", config.K1FolderPath))
if err != nil {
log.Printf("failed to execute kubectl delete of cwft-k1-ci: %s", err)
return err
}

time.Sleep(45 * time.Second)
viper.Set("ci.cwft-k1-ci.deleted", true)
viper.WriteConfig()

return nil
}
70 changes: 69 additions & 1 deletion internal/ciTools/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"os/exec"

"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/pkg"
Expand All @@ -29,8 +30,23 @@ func ApplyCITerraform(dryRun bool, bucketName string) {
envs["TF_VAR_aws_region"] = viper.GetString("aws.region")
envs["TF_VAR_bucket_ci"] = bucketName

accessKeyCmd := "cat $HOME/.aws/credentials | grep aws_access_key_id | awk '{ printf $3 }'"
awsAccessKeyId, err := exec.Command("bash", "-c", accessKeyCmd).Output()
if err != nil {
log.Panicf("error: could not set aws key id: %s", err)
}

accessSecretCmd := "cat $HOME/.aws/credentials | grep aws_secret_access_key | awk '{ printf $3 }'"
awsSecret, err := exec.Command("bash", "-c", accessSecretCmd).Output()
if err != nil {
log.Panicf("error: could not set aws secret: %s", err)
}

envs["TF_VAR_aws_access_key_id"] = string(awsAccessKeyId)
envs["TF_VAR_aws_secret_access_key"] = string(awsSecret)

directory := fmt.Sprintf("%s/ci/terraform/base", config.K1FolderPath)
err := os.Chdir(directory)
err = os.Chdir(directory)
if err != nil {
log.Panic("error: could not change directory to " + directory)
}
Expand All @@ -45,6 +61,25 @@ func ApplyCITerraform(dryRun bool, bucketName string) {
}
os.RemoveAll(fmt.Sprintf("%s/.terraform", directory))

if viper.GetString("ci.flavor") == "github" {
envs["TF_VAR_github_token"] = os.Getenv("GITHUB_AUTH_TOKEN")
secretDirectory := fmt.Sprintf("%s/ci/terraform/secret", config.K1FolderPath)
err := os.Chdir(secretDirectory)
if err != nil {
log.Panic("error: could not change directory to " + secretDirectory)
}
err = pkg.ExecShellWithVars(envs, config.TerraformClientPath, "init")
if err != nil {
log.Panicf("error: terraform init for ci secret failed %s", err)
}

err = pkg.ExecShellWithVars(envs, config.TerraformClientPath, "apply", "-auto-approve")
if err != nil {
log.Panicf("error: terraform apply for ci secret failed %s", err)
}
os.RemoveAll(fmt.Sprintf("%s/.terraform", secretDirectory))
}

} else {
log.Println("Skipping: applyCITerraform")
}
Expand Down Expand Up @@ -72,6 +107,39 @@ func DestroyCITerraform(skipCITerraform bool) {
if err != nil {
log.Printf("[WARN]: failed to terraform destroy CI, was the CI not created (check AWS)?: %s", err)
}

accessKeyCmd := "cat $HOME/.aws/credentials | grep aws_access_key_id | awk '{ printf $3 }'"
awsAccessKeyId, err := exec.Command("bash", "-c", accessKeyCmd).Output()
if err != nil {
log.Panicf("error: could not set aws key id: %s", err)
}

accessSecretCmd := "cat $HOME/.aws/credentials | grep aws_secret_access_key | awk '{ printf $3 }'"
awsSecret, err := exec.Command("bash", "-c", accessSecretCmd).Output()
if err != nil {
log.Panicf("error: could not set aws secret: %s", err)
}

envs["TF_VAR_aws_access_key_id"] = string(awsAccessKeyId)
envs["TF_VAR_aws_secret_access_key"] = string(awsSecret)

if viper.GetString("ci.flavor") == "github" {
envs["TF_VAR_github_token"] = os.Getenv("GITHUB_AUTH_TOKEN")
secretDirectory := fmt.Sprintf("%s/ci/terraform/secret", config.K1FolderPath)
err = os.Chdir(secretDirectory)
if err != nil {
log.Panic("error: could not change directory to " + secretDirectory)
}
err = pkg.ExecShellWithVars(envs, config.TerraformClientPath, "init")
if err != nil {
log.Panicf("error: terraform init for ci secret failed %s", err)
}
err = pkg.ExecShellWithVars(envs, config.TerraformClientPath, "destroy", "-auto-approve")
if err != nil {
log.Panicf("error: terraform apply for ci secret failed %s", err)
}
}

viper.Set("gitlab.ci-pushed", false)
viper.WriteConfig()
} else {
Expand Down
Loading