harness-drone/build/pipeline.go

242 lines
4.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-07-13 17:48:51 +00:00
"strconv"
"sync"
2016-05-09 18:28:49 +00:00
"time"
2016-05-08 07:01:45 +00:00
2016-07-13 17:48:51 +00:00
"github.com/Sirupsen/logrus"
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
wait sync.WaitGroup
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() {
2016-07-13 17:48:51 +00:00
defer func() {
if r := recover(); r != nil {
logrus.Errorln("recover executing build step", r)
}
}()
2016-05-09 18:28:49 +00:00
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() {
defer func() {
if r := recover(); r != nil {
logrus.Errorln("recover stopping the pipeline", r)
}
}()
2016-05-09 18:28:49 +00:00
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() {
2016-05-08 07:01:45 +00:00
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() {
defer func() {
if r := recover(); r != nil {
logrus.Errorln("recover executing step function", r)
}
}()
// stop all containers
for _, id := range p.containers {
p.engine.ContainerStop(id)
}
// wait for all logs to terminate
// p.wait.Done() // this is for the ambassador
p.wait.Wait()
// signal completion
2016-05-09 18:28:49 +00:00
p.done <- nil
}()
} else {
go func() {
defer func() {
if r := recover(); r != nil {
logrus.Errorln("recover executing step to head function", r)
}
}()
2016-05-09 18:28:49 +00:00
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-07-13 17:48:51 +00:00
defer func() {
if r := recover(); r != nil {
logrus.Errorln("recover closing the pipeline", r)
}
}()
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
p.wait.Add(1)
2016-05-08 07:01:45 +00:00
go func() {
2016-07-13 17:48:51 +00:00
defer func() {
if r := recover(); r != nil {
logrus.Errorln("recover writing build output", r)
}
p.wait.Done()
2016-07-13 17:48:51 +00:00
}()
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
}
2016-07-13 17:48:51 +00:00
p.wait.Add(1)
2016-07-13 17:48:51 +00:00
go func() {
defer func() {
if r := recover(); r != nil {
logrus.Errorln("recover writing exit code to output", r)
}
p.wait.Done()
2016-07-13 17:48:51 +00:00
}()
p.pipe <- &Line{
Proc: c.Name,
Type: ExitCodeLine,
Out: strconv.Itoa(state.ExitCode),
}
}()
if state.OOMKilled {
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
}