Skip to content

Commit

Permalink
Added deploy flag functionality to kubernetes
Browse files Browse the repository at this point in the history
  • Loading branch information
Shah Newaz Khan committed Oct 28, 2019
1 parent e45bf63 commit 489516b
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 2 deletions.
1 change: 1 addition & 0 deletions internal/config/config.go
Expand Up @@ -62,6 +62,7 @@ type Commit0Config struct {
type Kubernetes struct {
ClusterName string
DNSName string
Deploy bool
}

func LoadConfig(filePath string) *Commit0Config {
Expand Down
98 changes: 96 additions & 2 deletions internal/generate/kubernetes/generate.go
@@ -1,13 +1,107 @@
package kubernetes

import (
//"github.com/commitdev/commit0/util"

"bytes"
"fmt"
"io"
"sync"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/commitdev/commit0/config"
"github.com/commitdev/commit0/templator"
)

func Generate(templator *templator.Templator, config *config.Commit0Config) {
templator.Kubernetes.TemplateFiles(config, false)


if config.Kubernetes.Deploy {
_tf_init := tf_init()
_tf_plan := tf_plan()
execute(_tf_init)
execute(_tf_plan)
}

}

// Terraform init cmd
func tf_init() *exec.Cmd {

return exec.Command("terraform","init")
}

// Terraform plan cmd
func tf_plan() *exec.Cmd {

return exec.Command("terraform","plan")
}

// Executes cmd passed in
func execute( cmd *exec.Cmd){
dir, err1 := filepath.Abs(filepath.Dir(os.Args[0]))
if err1 != nil {
log.Fatal(err1)
}

cmd.Dir = dir + "/kubernetes/terraform"


var errStdout, errStderr error
stdoutIn, _ := cmd.StdoutPipe()
stderrIn, _ := cmd.StderrPipe()
stdout := NewCapturingPassThroughWriter(os.Stdout)
stderr := NewCapturingPassThroughWriter(os.Stderr)
err := cmd.Start()
if err != nil {
log.Fatalf("cmd.Start() failed with '%s'\n", err)
}

var wg sync.WaitGroup
wg.Add(1)

go func() {
_, errStdout = io.Copy(stdout, stdoutIn)
wg.Done()
}()

_, errStderr = io.Copy(stderr, stderrIn)
wg.Wait()

err = cmd.Wait()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
if errStdout != nil || errStderr != nil {
log.Fatal("failed to capture stdout or stderr\n")
}
outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
fmt.Printf("\nout:\n%s\nerr:\n%s\n", outStr, errStr)

}

// CapturingPassThroughWriter is a writer that remembers
// data written to it and passes it to w
type CapturingPassThroughWriter struct {
buf bytes.Buffer
w io.Writer
}

// NewCapturingPassThroughWriter creates new CapturingPassThroughWriter
func NewCapturingPassThroughWriter(w io.Writer) *CapturingPassThroughWriter {
return &CapturingPassThroughWriter{
w: w,
}
}

// Write writes data to the writer, returns number of bytes written and an error
func (w *CapturingPassThroughWriter) Write(d []byte) (int, error) {
w.buf.Write(d)
return w.w.Write(d)
}

// Bytes returns bytes written to the writer
func (w *CapturingPassThroughWriter) Bytes() []byte {
return w.buf.Bytes()
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 489516b

Please sign in to comment.