Skip to content

Commit

Permalink
Resolved yamlencode issue by upgrading to terraform 0.12.12 | Added r…
Browse files Browse the repository at this point in the history
…ead aws secrets & setting cmd envar for aws
  • Loading branch information
Shah Newaz Khan committed Nov 10, 2019
1 parent 581854b commit 8c187f5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Expand Up @@ -36,7 +36,7 @@ RUN mkdir -p /tmp/protoc && \

RUN GO111MODULE=off go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway

RUN curl -kLo /tmp/terraform.zip "https://releases.hashicorp.com/terraform/0.12.0/terraform_0.12.0_linux_amd64.zip" && \
RUN curl -kLo /tmp/terraform.zip "https://releases.hashicorp.com/terraform/0.12.12/terraform_0.12.12_linux_amd64.zip" && \
unzip -d /usr/local/bin/ /tmp/terraform.zip

RUN apk add --update bash ca-certificates git python && \
Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -79,6 +79,10 @@ As the templates are embeded into the binary you will need to ensure packr2 is i

You can run `make deps-go` to install this.

As there alot of dependencies it will be easier to use this tool within the provided image, clone the repo and then run `make build-docker-local`.

The best way then to use this is to add an alias, then you can use the CLI as if it was installed as usual on your machine:
`alias commit0='docker run -it -v "$(pwd):/project" commit0:v0'`

### Dependencies

Expand Down
51 changes: 47 additions & 4 deletions internal/generate/kubernetes/generate.go
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -46,13 +47,14 @@ func Execute(config *config.Commit0Config, pathPrefix string) {
writeSecrets(awsSecrets)
}

envars := getAwsEnvars(readSecrets())
log.Println("Planning infrastructure...")
execute(exec.Command("terraform", "init"), pathPrefix)
execute(exec.Command("terraform", "plan"), pathPrefix)
execute(exec.Command("terraform", "init"), pathPrefix, envars)
execute(exec.Command("terraform", "plan"), pathPrefix, envars)
}
}

func execute(cmd *exec.Cmd, pathPrefix string) {
func execute(cmd *exec.Cmd, pathPrefix string, envars []string) {
dir, err := os.Getwd()

if err != nil {
Expand All @@ -65,6 +67,12 @@ func execute(cmd *exec.Cmd, pathPrefix string) {
stderrPipe, _ := cmd.StderrPipe()

var errStdout, errStderr error

if envars != nil {
log.Println("Setting envars to cmd ...")
cmd.Env = envars
}

err = cmd.Start()
if err != nil {
log.Fatalf("Starting terraform command failed: %v\n", err)
Expand All @@ -91,6 +99,41 @@ func execute(cmd *exec.Cmd, pathPrefix string) {
}
}

func getAwsEnvars(awsSecrets Secrets) []string {
env := os.Environ()
env = append(env, fmt.Sprintf("AWS_ACCESS_KEY_ID=%s", awsSecrets.Aws.AwsAccessKeyID))
env = append(env, fmt.Sprintf("AWS_SECRET_ACCESS_KEY=%s", awsSecrets.Aws.AwsSecretAccessKey))
env = append(env, fmt.Sprintf("AWS_DEFAULT_REGION=%s", awsSecrets.Aws.Region))

return env
}

func readSecrets() Secrets {

dir, err := os.Getwd()

if err != nil {
log.Fatalf("Getting working directory failed: %v\n", err)
panic(err)
}

secretsFile := fmt.Sprintf("%s/secrets.yaml", dir)

data, err := ioutil.ReadFile(secretsFile)
if err != nil {
log.Fatalln(err)
}

awsSecrets := Secrets{}

err = yaml.Unmarshal(data, &awsSecrets)
if err != nil {
log.Fatalln(err)
}

return awsSecrets
}

func writeSecrets(s Secrets) {
secretsYaml, err := yaml.Marshal(&s)

Expand Down Expand Up @@ -120,7 +163,7 @@ func writeSecrets(s Secrets) {

n3, err := f.WriteString(string(secretsYaml))
f.Sync()
log.Printf("wrote %d bytes to %v", n3, secretsFile)
log.Printf("Wrote %d bytes to %v", n3, secretsFile)

}

Expand Down

0 comments on commit 8c187f5

Please sign in to comment.