Skip to content

Commit

Permalink
extract docker service from runner
Browse files Browse the repository at this point in the history
  • Loading branch information
bivas committed Apr 29, 2017
1 parent eca1d11 commit 3d4f383
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 134 deletions.
46 changes: 46 additions & 0 deletions helper/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package helper

import (
"io"
"log"
"os/exec"
"strings"
)

type Command interface {
Run() (string, error)
Stdin() io.WriteCloser
Kill()
}

type command struct {
name string
args []string
under *exec.Cmd
}

func (c *command) Stdin() io.WriteCloser {
pipe, e := c.under.StdinPipe()
if e != nil {
log.Fatal("error setting up stdin", e)
}
return pipe
}

func (c *command) Kill() {
c.under.Process.Kill()
}

func (c *command) Run() (string, error) {
log.Println("Running", c.name, c.args)
out, err := c.under.CombinedOutput()
return strings.TrimSpace(string(out)), err
}

func NewCommand(name string, args []string) Command {
return &command{
name: name,
args: args,
under: exec.Command(name, args...),
}
}
32 changes: 32 additions & 0 deletions helper/docker_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package helper

import (
"fmt"
"log"
"time"
)

const (
bootTimeout = 30
)

func NewDockerCommand(args ...string) Command {
return NewCommand("docker", args)
}

func WaitForContainer(container string) error {
filterPs := []string{"ps", "-aq", "--filter", "name=" + container}
id, _ := NewDockerCommand(filterPs...).Run()
for i := 0; ; i++ {
if i > bootTimeout {
return fmt.Errorf("Unable to start container after %d seconds", bootTimeout)
}
log.Println("Waiting for container to be available", container, id)
time.Sleep(1 * time.Second)
if id != "" {
return nil
}
id, _ = NewDockerCommand(filterPs...).Run()
}
return nil
}
6 changes: 3 additions & 3 deletions runner/file.go → helper/file.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package runner
package helper

import "os"

func exists(path string) bool {
func Exists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
Expand All @@ -13,7 +13,7 @@ func exists(path string) bool {
return true
}

func isFile(path string) bool {
func IsFile(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
Expand Down
56 changes: 56 additions & 0 deletions helper/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package helper

import (
"gopkg.in/src-d/go-git.v4"
"io"
"sort"
"time"
)

type timedHash struct {
When time.Time
Hash string
}

type timeSlice []timedHash

func (p timeSlice) Len() int {
return len(p)
}

// Define compare
func (p timeSlice) Less(i, j int) bool {
return p[i].When.Before(p[j].When)
}

// Define swap over an array
func (p timeSlice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}

func LatestCommitHash() (string, error) {
r, err := git.PlainOpen(".")
if err != nil {
return "", err
}

iter, e := r.CommitObjects()
if e != nil {
return "", e
}
defer iter.Close()
var hashes timeSlice
for {
commit, err := iter.Next()
if err != nil {
if err == io.EOF {
break
}
return "", err
}
hashes = append(hashes, timedHash{commit.Author.When, commit.Hash.String()})
}

sort.Sort(hashes)
return hashes[len(hashes)-1].Hash, nil
}
Loading

0 comments on commit 3d4f383

Please sign in to comment.