1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-11-30 08:06:52 +02:00
woodpecker/build/pipeline.go

203 lines
3.6 KiB
Go
Raw Normal View History

2016-05-10 07:57:57 +02:00
package build
2016-05-08 09:01:45 +02:00
import (
"bufio"
2016-07-13 19:48:51 +02:00
"strconv"
2016-05-09 20:28:49 +02:00
"time"
2016-05-08 09:01:45 +02:00
2016-07-13 19:48:51 +02:00
"github.com/Sirupsen/logrus"
2016-05-08 09:01:45 +02: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 20:28:49 +02:00
pipe chan (*Line)
2016-05-08 09:01:45 +02:00
next chan (error)
done chan (error)
err error
containers []string
volumes []string
networks []string
engine Engine
2016-05-08 09:01:45 +02: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 20:28:49 +02:00
go func() {
2016-07-13 19:48:51 +02:00
defer func() {
if r := recover(); r != nil {
logrus.Errorln("recover executing build step", r)
}
}()
2016-05-09 20:28:49 +02:00
err := p.exec(p.head.Container)
if err != nil {
p.err = err
}
p.step()
}()
2016-05-08 09:01:45 +02:00
}
// Skip skips the current step.
func (p *Pipeline) Skip() {
p.step()
}
2016-05-09 20:28:49 +02:00
// Pipe returns the build output pipe.
func (p *Pipeline) Pipe() <-chan *Line {
return p.pipe
}
2016-05-08 09:01:45 +02: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 20:28:49 +02:00
go func() {
p.done <- ErrTerm
}()
2016-05-08 09:01:45 +02: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 09:01:45 +02:00
}
2016-05-09 20:28:49 +02: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 09:01:45 +02:00
}
// step steps through the pipeline to head.next
func (p *Pipeline) step() {
if p.head == p.tail {
2016-05-09 20:28:49 +02:00
go func() {
p.done <- nil
}()
} else {
go func() {
p.head = p.head.next
p.next <- nil
}()
2016-05-08 09:01:45 +02:00
}
}
// close closes open channels and signals the pipeline is done.
func (p *Pipeline) close(err error) {
go func() {
2016-07-13 19:48:51 +02:00
defer func() {
if r := recover(); r != nil {
logrus.Errorln("recover closing the pipeline", r)
}
}()
2016-05-09 20:28:49 +02:00
p.done <- err
2016-05-08 09:01:45 +02:00
}()
}
func (p *Pipeline) exec(c *yaml.Container) error {
name, err := p.engine.ContainerStart(c)
2016-05-08 09:01:45 +02:00
if err != nil {
return err
}
p.containers = append(p.containers, name)
2016-05-08 09:01:45 +02:00
go func() {
2016-07-13 19:48:51 +02:00
defer func() {
if r := recover(); r != nil {
logrus.Errorln("recover writing build output", r)
}
}()
rc, rerr := p.engine.ContainerLogs(name)
2016-05-08 09:01:45 +02:00
if rerr != nil {
return
}
defer rc.Close()
num := 0
2016-05-09 20:28:49 +02:00
now := time.Now().UTC()
2016-05-08 09:01:45 +02:00
scanner := bufio.NewScanner(rc)
for scanner.Scan() {
2016-05-09 20:28:49 +02:00
p.pipe <- &Line{
Proc: c.Name,
Time: int64(time.Since(now).Seconds()),
Pos: num,
Out: scanner.Text(),
}
2016-05-08 09:01:45 +02:00
num++
}
}()
// exit when running container in detached mode in background
2016-05-08 09:01:45 +02:00
if c.Detached {
return nil
}
state, err := p.engine.ContainerWait(name)
2016-05-08 09:01:45 +02:00
if err != nil {
return err
}
if state.OOMKilled {
2016-05-08 09:01:45 +02:00
return &OomError{c.Name}
} else if state.ExitCode != 0 {
return &ExitError{c.Name, state.ExitCode}
2016-05-08 09:01:45 +02:00
}
2016-07-13 19:48:51 +02:00
go func() {
defer func() {
if r := recover(); r != nil {
logrus.Errorln("recover writing exit code to output", r)
}
}()
p.pipe <- &Line{
Proc: c.Name,
Type: ExitCodeLine,
Out: strconv.Itoa(state.ExitCode),
}
}()
2016-05-08 09:01:45 +02:00
return nil
}