Skip to content

Commit 280c651

Browse files
authored
Integrated terraform outputs in frontend code-gen (#75)
* updated frontend templates * updated readme and configs * added s3 hosting module and organized terraform
1 parent 5aabcfc commit 280c651

File tree

30 files changed

+15169
-141
lines changed

30 files changed

+15169
-141
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ The application starts at `cmd/generate.go`
9494
2. loads the config from the commit0.yml config file
9595
3. based on the configs, run the appropriate generators
9696
- templator is passed in to the Generate function for dependency injection
97+
- `internal/generate/generate_helper.go` iterates through all the configs and runs each generator
9798
4. each generator (`react/generate.go`, `ci/generate.go` etc) further delegates and actually executes the templating based on the configs passed in.
9899
- `internal/templator/templator.go` is the base class and includes generic templating handling logic
99100
- it CI is required, it'll also call a CI generator and pass in the service specific CI configs

internal/config/config.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,23 @@ type aws struct {
7777
AccountId string `yaml:"accountId"`
7878
Region string
7979
EKS eks
80-
Cognito bool
80+
Cognito cognito
81+
S3Hosting s3Hosting `yaml:"s3_hosting"`
8182
Terraform terraform
8283
}
8384

8485
type terraform struct {
8586
RemoteState bool
8687
}
8788

89+
type cognito struct {
90+
Deploy bool
91+
}
92+
93+
type s3Hosting struct {
94+
Deploy bool
95+
}
96+
8897
type eks struct {
8998
ClusterName string `yaml:"clusterName"`
9099
WorkerAMI string `yaml:"workerAMI"`

internal/config/react.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ type reactApp struct {
44
Name string
55
}
66

7-
type reactAccount struct {
8-
Enabled bool
9-
Required bool
10-
}
11-
12-
type reactView struct {
13-
Path string
14-
Component string
7+
type environment struct {
8+
CognitoPoolID string
9+
CognitoClientID string
1510
}
1611

1712
type frontend struct {
1813
Framework string
14+
Hostname string
1915
App reactApp
2016
CI CI
17+
Env environment
2118
}

internal/generate/generate_helper.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ func GenerateArtifactsHelper(t *templator.Templator, cfg *config.Commit0Config,
4545
kubernetes.Generate(t, cfg, &wg, pathPrefix)
4646
}
4747

48-
// @TODO : This strucuture probably needs to be adjusted. Probably too generic.
49-
switch cfg.Frontend.Framework {
50-
case util.React:
51-
log.Println(aurora.Cyan(emoji.Sprintf("Creating React frontend")))
52-
react.Generate(t, cfg, &wg, pathPrefix)
53-
}
54-
5548
util.TemplateFileIfDoesNotExist(pathPrefix, "README.md", t.Readme, &wg, templator.GenericTemplateData{*cfg})
5649

5750
// Wait for all the templates to be generated
@@ -63,4 +56,22 @@ func GenerateArtifactsHelper(t *templator.Templator, cfg *config.Commit0Config,
6356
terraform.Execute(cfg, pathPrefix)
6457
kubernetes.Execute(cfg, pathPrefix)
6558
}
59+
60+
if cfg.Infrastructure.AWS.Cognito.Deploy {
61+
outputs := []string{
62+
"cognito_pool_id",
63+
"cognito_client_id",
64+
}
65+
outputValues := terraform.GetOutputs(cfg, pathPrefix, outputs)
66+
cfg.Frontend.Env.CognitoPoolID = outputValues["cognito_pool_id"]
67+
cfg.Frontend.Env.CognitoClientID = outputValues["cognito_client_id"]
68+
}
69+
70+
// @TODO : This strucuture probably needs to be adjusted. Probably too generic.
71+
switch cfg.Frontend.Framework {
72+
case util.React:
73+
log.Println(aurora.Cyan(emoji.Sprintf("Creating React frontend")))
74+
react.Generate(t, cfg, &wg, pathPrefix)
75+
}
76+
6677
}

internal/generate/terraform/generate.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/commitdev/commit0/internal/config"
1010
"github.com/commitdev/commit0/internal/templator"
1111
"github.com/commitdev/commit0/internal/util"
12+
1213
"github.com/kyokomi/emoji"
1314
"github.com/logrusorgru/aurora"
1415
)
@@ -36,6 +37,23 @@ func Generate(t *templator.Templator, cfg *config.Commit0Config, wg *sync.WaitGr
3637
t.Terraform.TemplateFiles(data, false, wg, pathPrefix)
3738
}
3839

40+
func GetOutputs(config *config.Commit0Config, pathPrefix string, outputs []string) map[string]string {
41+
outputsMap := make(map[string]string)
42+
43+
log.Println("Preparing aws environment...")
44+
45+
envars := util.MakeAwsEnvars(util.GetSecrets())
46+
47+
path := filepath.Join(pathPrefix, "terraform")
48+
49+
for _, output := range outputs {
50+
outputValue := util.ExecuteCommandOutput(exec.Command("terraform", "output", output), path, envars)
51+
outputsMap[output] = outputValue
52+
}
53+
54+
return outputsMap
55+
}
56+
3957
// Execute terrafrom init & plan
4058
func Execute(config *config.Commit0Config, pathPrefix string) {
4159
// @TODO : Change this check. Most likely we should discover the accountid

internal/templator/templator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func NewCITemplator(box *packr.Box) *CITemplator {
120120

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

125125
return &CITemplator{
126126
CircleCI: circleciTemplate,
@@ -170,7 +170,7 @@ func NewEJSDirectoryTemplator(box *packr.Box, dir string) *DirectoryTemplator {
170170
templates := []*template.Template{}
171171
for _, file := range getFileNames(box, dir) {
172172
templateSource, _ := box.FindString(file)
173-
template, err := template.New(file).Delims("<%=", "%>").Funcs(util.FuncMap).Parse(templateSource)
173+
template, err := template.New(file).Delims("<%", "%>").Funcs(util.FuncMap).Parse(templateSource)
174174
if err != nil {
175175
panic(err)
176176
}

internal/util/util.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,19 @@ func ExecuteCommand(cmd *exec.Cmd, pathPrefix string, envars []string) {
124124
log.Printf("Failed to capture stderr: %v\n", errStderr)
125125
}
126126
}
127+
128+
func ExecuteCommandOutput(cmd *exec.Cmd, pathPrefix string, envars []string) string {
129+
dir := GetCwd()
130+
131+
cmd.Dir = path.Join(dir, pathPrefix)
132+
133+
if envars != nil {
134+
cmd.Env = envars
135+
}
136+
137+
out, err := cmd.CombinedOutput()
138+
if err != nil {
139+
log.Fatalf("Executing terraform output failed: %v\n", err)
140+
}
141+
return string(out)
142+
}

templates/ci/github.tmpl

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,29 @@ jobs:
2626
uses: actions/upload-artifact@v1
2727
with:
2828
name: build
29-
path: build
29+
path: build
30+
31+
publish:
32+
needs: build
33+
runs-on: ubuntu-latest
34+
steps:
35+
- name: Checkout
36+
uses: actions/checkout@v1
37+
with:
38+
fetch-depth: 1
39+
40+
- name: Download build artifacts
41+
uses: actions/download-artifact@v1
42+
with:
43+
name: build
44+
45+
# https://github.com/opspresso/action-s3-sync
46+
- name: Publish to AWS S3
47+
uses: opspresso/action-s3-sync@master
48+
env:
49+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
50+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
51+
AWS_REGION: "<% .Config.AWS.Region %>"
52+
FROM_PATH: "./build"
53+
DEST_PATH: "s3://<% .Config.Frontend.App.Name %>"
54+
OPTIONS: "--acl public-read"

templates/commit0/commit0.tmpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ infrastructure:
1414
eks:
1515
clusterName: staging
1616
deploy: true
17+
cognito:
18+
deploy: true
19+
s3_hosting:
20+
deploy: true
1721

1822
frontend:
1923
framework: {{.FrontendFramework}}
2024
ci:
21-
system: circleci
25+
system: github
2226
app:
2327
name: {{.ProjectName}}
2428

templates/react/.env

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ REACT_APP_HOST=http://localhost:3000
33
REACT_APP_AUTH_DOMAIN=
44
REACT_APP_AUTH_CLIENT_ID=
55
REACT_APP_GRAPHQL_HOST=http://localhost:4000
6-
REACT_APP_FILE_HOST=http://localhost:4000
6+
REACT_APP_FILE_HOST=http://localhost:4000
7+
<% if .Config.Frontend.Env.CognitoPoolID %>REACT_APP_COGNITO_POOL_ID=<% .Config.Frontend.Env.CognitoPoolID %><%- end %>
8+
<% if .Config.Frontend.Env.CognitoClientID %>REACT_APP_COGNITO_CLIENT_ID=<% .Config.Frontend.Env.CognitoClientID %><%- end %>

0 commit comments

Comments
 (0)