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

Integrated terraform outputs in frontend code-gen #75

Merged
merged 11 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ The application starts at `cmd/generate.go`
2. loads the config from the commit0.yml config file
3. based on the configs, run the appropriate generators
- templator is passed in to the Generate function for dependency injection
- `internal/generate/generate_helper.go` iterates through all the configs and runs each generator
4. each generator (`react/generate.go`, `ci/generate.go` etc) further delegates and actually executes the templating based on the configs passed in.
- `internal/templator/templator.go` is the base class and includes generic templating handling logic
- it CI is required, it'll also call a CI generator and pass in the service specific CI configs
Expand Down
6 changes: 5 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,18 @@ type aws struct {
AccountId string `yaml:"accountId"`
Region string
EKS eks
Cognito bool
Cognito cognito
Terraform terraform
}

type terraform struct {
RemoteState bool
}

type cognito struct {
Deploy bool
}

type eks struct {
ClusterName string `yaml:"clusterName"`
WorkerAMI string `yaml:"workerAMI"`
Expand Down
13 changes: 5 additions & 8 deletions internal/config/react.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ type reactApp struct {
Name string
}

type reactAccount struct {
Enabled bool
Required bool
}

type reactView struct {
Path string
Component string
type environment struct {
CognitoPoolID string
CognitoClientID string
}

type frontend struct {
Framework string
Hostname string
App reactApp
CI CI
Env environment
}
25 changes: 18 additions & 7 deletions internal/generate/generate_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ func GenerateArtifactsHelper(t *templator.Templator, cfg *config.Commit0Config,
kubernetes.Generate(t, cfg, &wg, pathPrefix)
}

// @TODO : This strucuture probably needs to be adjusted. Probably too generic.
switch cfg.Frontend.Framework {
case util.React:
log.Println(aurora.Cyan(emoji.Sprintf("Creating React frontend")))
react.Generate(t, cfg, &wg, pathPrefix)
}

util.TemplateFileIfDoesNotExist(pathPrefix, "README.md", t.Readme, &wg, templator.GenericTemplateData{*cfg})

// Wait for all the templates to be generated
Expand All @@ -63,4 +56,22 @@ func GenerateArtifactsHelper(t *templator.Templator, cfg *config.Commit0Config,
terraform.Execute(cfg, pathPrefix)
kubernetes.Execute(cfg, pathPrefix)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a temporary solution. We'll need to figure out a system to the scenarios where one part of the code generation requires some output from another step.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm yeah, that's gonna get complicated..

if cfg.Infrastructure.AWS.Cognito.Deploy {
outputs := []string{
"cognito_pool_id",
"cognito_client_id",
}
outputValues := terraform.GetOutputs(cfg, pathPrefix, outputs)
cfg.Frontend.Env.CognitoPoolID = outputValues["cognito_pool_id"]
cfg.Frontend.Env.CognitoClientID = outputValues["cognito_client_id"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should write these values back to the commit0.yml file..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup for sure, I think we definitely should do that

}

// @TODO : This strucuture probably needs to be adjusted. Probably too generic.
switch cfg.Frontend.Framework {
case util.React:
log.Println(aurora.Cyan(emoji.Sprintf("Creating React frontend")))
react.Generate(t, cfg, &wg, pathPrefix)
}

}
18 changes: 18 additions & 0 deletions internal/generate/terraform/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/templator"
"github.com/commitdev/commit0/internal/util"

"github.com/kyokomi/emoji"
"github.com/logrusorgru/aurora"
)
Expand Down Expand Up @@ -36,6 +37,23 @@ func Generate(t *templator.Templator, cfg *config.Commit0Config, wg *sync.WaitGr
t.Terraform.TemplateFiles(data, false, wg, pathPrefix)
}

func GetOutputs(config *config.Commit0Config, pathPrefix string, outputs []string) map[string]string {
outputsMap := make(map[string]string)

log.Println("Preparing aws environment...")

envars := util.MakeAwsEnvars(util.GetSecrets())

path := filepath.Join(pathPrefix, "terraform")

for _, output := range outputs {
outputValue := util.ExecuteCommandOutput(exec.Command("terraform", "output", output), path, envars)
outputsMap[output] = outputValue
}

return outputsMap
}

// Execute terrafrom init & plan
func Execute(config *config.Commit0Config, pathPrefix string) {
// @TODO : Change this check. Most likely we should discover the accountid
Expand Down
4 changes: 2 additions & 2 deletions internal/templator/templator.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func NewCITemplator(box *packr.Box) *CITemplator {

githubTemplateSource, _ := box.FindString("ci/github.tmpl")
// Github also uses double curly braces for their templates
githubTemplate, _ := template.New("CIConfig").Delims("<%=", "%>").Parse(githubTemplateSource)
githubTemplate, _ := template.New("CIConfig").Delims("<%", "%>").Parse(githubTemplateSource)

return &CITemplator{
CircleCI: circleciTemplate,
Expand Down Expand Up @@ -170,7 +170,7 @@ func NewEJSDirectoryTemplator(box *packr.Box, dir string) *DirectoryTemplator {
templates := []*template.Template{}
for _, file := range getFileNames(box, dir) {
templateSource, _ := box.FindString(file)
template, err := template.New(file).Delims("<%=", "%>").Funcs(util.FuncMap).Parse(templateSource)
template, err := template.New(file).Delims("<%", "%>").Funcs(util.FuncMap).Parse(templateSource)
if err != nil {
panic(err)
}
Expand Down
16 changes: 16 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,19 @@ func ExecuteCommand(cmd *exec.Cmd, pathPrefix string, envars []string) {
log.Printf("Failed to capture stderr: %v\n", errStderr)
}
}

func ExecuteCommandOutput(cmd *exec.Cmd, pathPrefix string, envars []string) string {
dir := GetCwd()

cmd.Dir = path.Join(dir, pathPrefix)

if envars != nil {
cmd.Env = envars
}

out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Executing terraform output failed: %v\n", err)
}
return string(out)
}
4 changes: 3 additions & 1 deletion templates/commit0/commit0.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ infrastructure:
eks:
clusterName: staging
deploy: true
cognito:
deploy: true

frontend:
framework: {{.FrontendFramework}}
ci:
system: circleci
system: github
app:
name: {{.ProjectName}}

Expand Down
4 changes: 3 additions & 1 deletion templates/react/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ REACT_APP_HOST=http://localhost:3000
REACT_APP_AUTH_DOMAIN=
REACT_APP_AUTH_CLIENT_ID=
REACT_APP_GRAPHQL_HOST=http://localhost:4000
REACT_APP_FILE_HOST=http://localhost:4000
REACT_APP_FILE_HOST=http://localhost:4000
<% if .Config.Frontend.Env.CognitoPoolID %>REACT_APP_COGNITO_POOL_ID=<% .Config.Frontend.Env.CognitoPoolID %><%- end %>
<% if .Config.Frontend.Env.CognitoClientID %>REACT_APP_COGNITO_CLIENT_ID=<% .Config.Frontend.Env.CognitoClientID %><%- end %>
4 changes: 2 additions & 2 deletions templates/react/.github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ jobs:
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: "us-west-2"
FROM_PATH: "./build"
DEST_PATH: "s3://<%= .Config.Frontend.App.Name %>"
OPTIONS: "--acl public-read"
DEST_PATH: "s3://<% .Config.Frontend.App.Name %>"
OPTIONS: "--acl public-read"