harness-drone/build/pipeline.go

170 lines
3 KiB
Go
Raw Normal View History

2016-05-10 05:57:57 +00:00
package build
2016-05-08 07:01:45 +00:00
import (
"bufio"
2016-05-09 18:28:49 +00:00
"time"
2016-05-08 07:01:45 +00:00
"github.com/drone/drone/yaml"
)
// element represents a link in the linked list.
type element struct {
*yaml.Container
next *element
}
// Pipeline represents a build pipeline.
type Pipeline struct {
conf *yaml.Config
head *element
tail *element
2016-05-09 18:28:49 +00:00
pipe chan (*Line)
2016-05-08 07:01:45 +00:00
next chan (error)
done chan (error)
err error
containers []string
volumes []string
networks []string
engine Engine
2016-05-08 07:01:45 +00:00
}
// Done returns when the process is done executing.
func (p *Pipeline) Done() <-chan error {
return p.done
}
// Err returns the error for the current process.
func (p *Pipeline) Err() error {
return p.err
}
// Next returns the next step in the process.
func (p *Pipeline) Next() <-chan error {
return p.next
}
// Exec executes the current step.
func (p *Pipeline) Exec() {
2016-05-09 18:28:49 +00:00
go func() {
err := p.exec(p.head.Container)
if err != nil {
p.err = err
}
p.step()
}()
2016-05-08 07:01:45 +00:00
}
// Skip skips the current step.
func (p *Pipeline) Skip() {
p.step()
}
2016-05-09 18:28:49 +00:00
// Pipe returns the build output pipe.
func (p *Pipeline) Pipe() <-chan *Line {
return p.pipe
}
2016-05-08 07:01:45 +00:00
// Head returns the head item in the list.
func (p *Pipeline) Head() *yaml.Container {
return p.head.Container
}
// Tail returns the tail item in the list.
func (p *Pipeline) Tail() *yaml.Container {
return p.tail.Container
}
// Stop stops the pipeline.
func (p *Pipeline) Stop() {
2016-05-09 18:28:49 +00:00
go func() {
p.done <- ErrTerm
}()
2016-05-08 07:01:45 +00:00
}
// Setup prepares the build pipeline environment.
func (p *Pipeline) Setup() error {
return nil
}
// Teardown removes the pipeline environment.
func (p *Pipeline) Teardown() {
for _, id := range p.containers {
p.engine.ContainerRemove(id)
2016-05-08 07:01:45 +00:00
}
2016-05-09 18:28:49 +00:00
close(p.next)
close(p.done)
// TODO we have a race condition here where the program can try to async
// write to a closed pipe channel. This package, in general, needs to be
// tested for race conditions.
// close(p.pipe)
2016-05-08 07:01:45 +00:00
}
// step steps through the pipeline to head.next
func (p *Pipeline) step() {
if p.head == p.tail {
2016-05-09 18:28:49 +00:00
go func() {
p.done <- nil
}()
} else {
go func() {
p.head = p.head.next
p.next <- nil
}()
2016-05-08 07:01:45 +00:00
}
}
// close closes open channels and signals the pipeline is done.
func (p *Pipeline) close(err error) {
go func() {
2016-05-09 18:28:49 +00:00
p.done <- err
2016-05-08 07:01:45 +00:00
}()
}
func (p *Pipeline) exec(c *yaml.Container) error {
name, err := p.engine.ContainerStart(c)
2016-05-08 07:01:45 +00:00
if err != nil {
return err
}
p.containers = append(p.containers, name)
2016-05-08 07:01:45 +00:00
go func() {
rc, rerr := p.engine.ContainerLogs(name)
2016-05-08 07:01:45 +00:00
if rerr != nil {
return
}
defer rc.Close()
num := 0
2016-05-09 18:28:49 +00:00
now := time.Now().UTC()
2016-05-08 07:01:45 +00:00
scanner := bufio.NewScanner(rc)
for scanner.Scan() {
2016-05-09 18:28:49 +00:00
p.pipe <- &Line{
Proc: c.Name,
Time: int64(time.Since(now).Seconds()),
Pos: num,
Out: scanner.Text(),
}
2016-05-08 07:01:45 +00:00
num++
}
}()
// exit when running container in detached mode in background
2016-05-08 07:01:45 +00:00
if c.Detached {
return nil
}
state, err := p.engine.ContainerWait(name)
2016-05-08 07:01:45 +00:00
if err != nil {
return err
}
if state.OOMKilled {
2016-05-08 07:01:45 +00:00
return &OomError{c.Name}
} else if state.ExitCode != 0 {
return &ExitError{c.Name, state.ExitCode}
2016-05-08 07:01:45 +00:00
}
return nil
}