1
0
mirror of https://github.com/drone/drone-cli.git synced 2024-11-23 01:11:57 +01:00

sync builder.B for parallel use

This commit is contained in:
Brad Rydzewski 2015-03-14 17:42:00 -07:00
parent 6044e0a17b
commit 8c3cb5f0dc
3 changed files with 26 additions and 50 deletions

@ -2,6 +2,7 @@ package builder
import (
"io"
"sync"
"time"
"github.com/drone/drone-cli/common"
@ -11,6 +12,8 @@ import (
// B is a type passed to build nodes. B implements an io.Writer
// and will accumulate build output during execution.
type B struct {
sync.Mutex
Repo *common.Repo
Commit *common.Commit
Config *common.Config
@ -39,6 +42,9 @@ func NewB(client dockerclient.Client, w io.Writer) *B {
// Run creates and runs a Docker container.
func (b *B) Run(conf *dockerclient.ContainerConfig) (string, error) {
b.Lock()
defer b.Unlock()
name, err := b.client.CreateContainer(conf, "")
if err != nil {
// on error try to pull the Docker image.
@ -77,6 +83,9 @@ func (b *B) Remove(name string) {
// RemoveAll stops and removes all Docker containers that were
// created and started during the build process.
func (b *B) RemoveAll() {
b.Lock()
defer b.Unlock()
for _, name := range b.containers {
b.Remove(name)
}
@ -98,6 +107,9 @@ func (b *B) Logs(name string) (io.ReadCloser, error) {
// before a build starts, but it can also used to resume timing after
// a call to StopTimer.
func (b *B) StartTimer() {
b.Lock()
defer b.Unlock()
if !b.timerOn {
b.start = time.Now()
b.timerOn = true
@ -107,6 +119,9 @@ func (b *B) StartTimer() {
// StopTimer stops timing a build. This can be used to pause the timer
// while performing complex initialization that you don't want to measure.
func (b *B) StopTimer() {
b.Lock()
defer b.Unlock()
if b.timerOn {
b.duration += time.Now().Sub(b.start)
b.timerOn = false
@ -120,6 +135,9 @@ func (b *B) Write(p []byte) (n int, err error) {
// Exit writes the function as having failed but continues execution.
func (b *B) Exit(code int) {
b.Lock()
defer b.Unlock()
if code != 0 { // never override non-zero exit
b.exitCode = code
}
@ -128,5 +146,8 @@ func (b *B) Exit(code int) {
// ExitCode reports the build exit code. A non-zero value indicates
// the build exited with errors.
func (b *B) ExitCode() int {
b.Lock()
defer b.Unlock()
return b.exitCode
}

@ -14,10 +14,6 @@ var errNop = errors.New("Operation not supported")
type mockClient struct{}
func newMockClient() (dockerclient.Client, error) {
return &mockClient{}, nil
}
// CreateContainer creates a container.
func (c *mockClient) CreateContainer(config *dockerclient.ContainerConfig, name string) (string, error) {
return config.Image, nil

@ -4,6 +4,7 @@ import (
"os"
"github.com/drone/drone-cli/builder"
"github.com/drone/drone-cli/builder/ambassador"
"github.com/drone/drone-cli/common"
"github.com/drone/drone-cli/parser"
@ -23,13 +24,6 @@ func main() {
return
}
client, err := newMockClient() //dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
if err != nil {
log.Errorln(err)
return
}
// TODO DEFER AMABASSADOR DESTROY
var builds []*builder.B
var builders []*builder.Builder
@ -43,6 +37,10 @@ func main() {
// list of builds and builders for each item
// in the matrix
for _, conf := range matrix {
client, err := ambassador.Create(&mockClient{})
if err != nil {
return
}
b := builder.NewB(client, os.Stdout)
b.Repo = repo
b.Clone = clone
@ -70,45 +68,6 @@ func main() {
log.Println("")
}
//
// func run(build *builder.Build) {
//
// amb, err := ambassador.Create(build.Client)
// if err != nil {
// fmt.Println(err)
// return
// }
// defer amb.Destroy()
// build.Client = amb // TODO remove this
//
// // response writer
// res := builder.NewResultWriter(os.Stdout)
//
// // builder
// b := runner.Builder(build)
// defer b.Cancel()
// err = b.Build(res)
// if err != nil {
// fmt.Println(err)
// }
//
// // deployer
// d := runner.Deployer(build)
// defer d.Cancel()
// err = d.Build(res)
// if err != nil {
// fmt.Println(err)
// }
//
// // notifier
// n := runner.Notifier(build)
// defer n.Cancel()
// err = n.Build(res)
// if err != nil {
// fmt.Println(err)
// }
// }
var repo = &common.Repo{
Remote: "github.com",
Host: "github.com",