diff --git a/internal/config/projectconfig/project_config_test.go b/internal/config/projectconfig/project_config_test.go index e44f845f8..3c94d0242 100644 --- a/internal/config/projectconfig/project_config_test.go +++ b/internal/config/projectconfig/project_config_test.go @@ -25,7 +25,7 @@ func TestLoadConfig(t *testing.T) { Modules: eksGoReactSampleModules(), } - t.Run("Should load and unmarshall config correctly", func(t *testing.T) { + t.Run("Should load and unmarshal config correctly", func(t *testing.T) { got := projectconfig.LoadConfig(filePath) if !cmp.Equal(want, got, cmpopts.EquateEmpty()) { t.Errorf("projectconfig.ZeroProjectConfig.Unmarshal mismatch (-want +got):\n%s", cmp.Diff(want, got)) diff --git a/internal/generate/generate_infrastructure.go b/internal/generate/generate_infrastructure.go deleted file mode 100644 index dce124555..000000000 --- a/internal/generate/generate_infrastructure.go +++ /dev/null @@ -1,80 +0,0 @@ -package generate - -import ( - "log" - "os/exec" - "path/filepath" - - "github.com/commitdev/zero/internal/config/projectconfig" - "github.com/commitdev/zero/internal/util" - "github.com/commitdev/zero/pkg/credentials" - project "github.com/commitdev/zero/pkg/credentials" - "github.com/commitdev/zero/pkg/util/flog" -) - -// @TODO: These are specific to a k8s version. If we make the version a config option we will need to change this -var amiLookup = map[string]string{ - "us-east-1": "ami-0392bafc801b7520f", - "us-east-2": "ami-082bb518441d3954c", - "us-west-2": "ami-05d586e6f773f6abf", - "eu-west-1": "ami-059c6874350e63ca9", - "eu-central-1": "ami-0e21bc066a9dbabfa", -} - -// GetOutputs captures the terraform output for the specific variables -func GetOutputs(cfg *projectconfig.ZeroProjectConfig, pathPrefix string, outputs []string) map[string]string { - outputsMap := make(map[string]string) - envars := credentials.MakeAwsEnvars(cfg, project.GetSecrets(util.GetCwd())) - pathPrefix = filepath.Join(pathPrefix, "environments/staging") - - for _, output := range outputs { - outputValue := util.ExecuteCommandOutput(exec.Command("terraform", "output", output), pathPrefix, envars) - outputsMap[output] = outputValue - } - - return outputsMap -} - -// Init sets up anything required by Execute -func Init(cfg *projectconfig.ZeroProjectConfig, pathPrefix string) { - if cfg.Infrastructure.AWS.AccountID != "" { - flog.Infof("Preparing aws environment...") - - envars := project.MakeAwsEnvars(cfg, project.GetSecrets(util.GetCwd())) - - pathPrefix = filepath.Join(pathPrefix, "terraform") - - // @TODO : A check here would be nice to see if this stuff exists first, mostly for testing - flog.Infof(":alarm_clock: Initializing remote backend...") - util.ExecuteCommand(exec.Command("terraform", "init"), filepath.Join(pathPrefix, "bootstrap/remote-state"), envars) - util.ExecuteCommand(exec.Command("terraform", "apply", "-auto-approve"), filepath.Join(pathPrefix, "bootstrap/remote-state"), envars) - - // flog.Infof("Creating users...") - // util.ExecuteCommand(exec.Command("terraform", "init"), filepath.Join(pathPrefix, "bootstrap/create-users"), envars) - // util.ExecuteCommand(exec.Command("terraform", "apply", "-auto-approve"), filepath.Join(pathPrefix, "bootstrap/create-users"), envars) - } -} - -// Execute terrafrom init & plan. May modify the config passed in -func Execute(cfg *projectconfig.ZeroProjectConfig, pathPrefix string) { - if cfg.Infrastructure.AWS.AccountID != "" { - log.Println("Preparing aws environment...") - - envars := project.MakeAwsEnvars(cfg, project.GetSecrets(util.GetCwd())) - - pathPrefix = filepath.Join(pathPrefix, "terraform") - - flog.Infof(":alarm_clock: Applying infrastructure configuration...") - util.ExecuteCommand(exec.Command("terraform", "init"), filepath.Join(pathPrefix, "environments/staging"), envars) - util.ExecuteCommand(exec.Command("terraform", "apply", "-auto-approve"), filepath.Join(pathPrefix, "environments/staging"), envars) - - // @TODO get output fields from `mapOutputs` param in configs, can't be hardcoded - outputs := []string{ - "cognito_pool_id", - "cognito_client_id", - } - outputValues := GetOutputs(cfg, pathPrefix, outputs) - cfg.Parameters["cognito_pool_id"] = outputValues["cognito_pool_id"] - cfg.Parameters["cognito_client_id"] = outputValues["cognito_client_id"] - } -} diff --git a/internal/generate/generate_modules.go b/internal/generate/generate_modules.go index 0a539a28c..83cd27eed 100644 --- a/internal/generate/generate_modules.go +++ b/internal/generate/generate_modules.go @@ -31,7 +31,7 @@ func Generate(projectConfig projectconfig.ZeroProjectConfig) error { } wg.Wait() - flog.Infof(":pencil: Rendering Modules") + flog.Infof(":memo: Rendering Modules") for _, mod := range projectConfig.Modules { // Load module configuration moduleConfig, err := module.ParseModuleConfig(mod.Files.Source) @@ -52,11 +52,15 @@ func Generate(projectConfig projectconfig.ZeroProjectConfig) error { mod.Parameters, } - fileTemplates := NewTemplates(moduleDir, outputDir, false) + fileTemplates := newTemplates(moduleDir, outputDir, false) - ExecuteTemplates(fileTemplates, templateData, delimiters) + executeTemplates(fileTemplates, templateData, delimiters) } + flog.Infof(":up_arrow: Done Rendering - committing repositories to version control") + // TODO : Integrate this work + + flog.Infof(":check_mark_button: Done - run zero apply to create any required infrastructure or execute any other remote commands to prepare your environments.") return nil } @@ -66,11 +70,11 @@ type TemplateConfig struct { isTemplate bool } -// NewTemplates walks the module directory to find all to be templated -func NewTemplates(moduleDir string, outputDir string, overwrite bool) []*TemplateConfig { +// newTemplates walks the module directory to find all to be templated +func newTemplates(moduleDir string, outputDir string, overwrite bool) []*TemplateConfig { templates := []*TemplateConfig{} - paths, err := GetAllFilePathsInDirectory(moduleDir) + paths, err := getAllFilePathsInDirectory(moduleDir) if err != nil { panic(err) } @@ -104,8 +108,8 @@ func NewTemplates(moduleDir string, outputDir string, overwrite bool) []*Templat return templates } -// GetAllFilePathsInDirectory Recursively get all file paths in directory, including sub-directories. -func GetAllFilePathsInDirectory(moduleDir string) ([]string, error) { +// getAllFilePathsInDirectory Recursively get all file paths in directory, including sub-directories. +func getAllFilePathsInDirectory(moduleDir string) ([]string, error) { var paths []string err := filepath.Walk(moduleDir, func(path string, info os.FileInfo, err error) error { if err != nil { @@ -123,7 +127,7 @@ func GetAllFilePathsInDirectory(moduleDir string) ([]string, error) { return paths, nil } -func ExecuteTemplates(templates []*TemplateConfig, data interface{}, delimiters []string) { +func executeTemplates(templates []*TemplateConfig, data interface{}, delimiters []string) { var wg sync.WaitGroup leftDelim := delimiters[0] rightDelim := delimiters[1] diff --git a/internal/generate/github/test/config1.yml b/internal/generate/github/test/config1.yml deleted file mode 100644 index 4d3798434..000000000 --- a/internal/generate/github/test/config1.yml +++ /dev/null @@ -1 +0,0 @@ -content1: {{ .ci }} \ No newline at end of file diff --git a/internal/generate/github/test/dir/config2.yml b/internal/generate/github/test/dir/config2.yml deleted file mode 100644 index f6138f0c2..000000000 --- a/internal/generate/github/test/dir/config2.yml +++ /dev/null @@ -1 +0,0 @@ -content2: