driver: add logging support to bootstrap
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>pull/10/head
parent
f302881c0d
commit
a6d893efca
@ -0,0 +1,52 @@
|
||||
package progress
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/containerd/console"
|
||||
"github.com/moby/buildkit/client"
|
||||
"github.com/moby/buildkit/util/progress/progressui"
|
||||
)
|
||||
|
||||
type printer struct {
|
||||
status chan *client.SolveStatus
|
||||
done <-chan struct{}
|
||||
err error
|
||||
}
|
||||
|
||||
func (p *printer) Done() <-chan struct{} {
|
||||
return p.done
|
||||
}
|
||||
|
||||
func (p *printer) Err() error {
|
||||
return p.err
|
||||
}
|
||||
|
||||
func (p *printer) Status() chan *client.SolveStatus {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
return p.status
|
||||
}
|
||||
|
||||
func NewPrinter(ctx context.Context, out *os.File, mode string) Writer {
|
||||
statusCh := make(chan *client.SolveStatus)
|
||||
doneCh := make(chan struct{})
|
||||
|
||||
pw := &printer{
|
||||
status: statusCh,
|
||||
done: doneCh,
|
||||
}
|
||||
|
||||
go func() {
|
||||
var c console.Console
|
||||
if cons, err := console.ConsoleFromFile(out); err == nil && (mode == "auto" || mode == "tty") {
|
||||
c = cons
|
||||
}
|
||||
// not using shared context to not disrupt display but let is finish reporting errors
|
||||
pw.err = progressui.DisplaySolveStatus(ctx, "", c, out, statusCh)
|
||||
close(doneCh)
|
||||
}()
|
||||
return pw
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package progress
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/moby/buildkit/client"
|
||||
"github.com/moby/buildkit/identity"
|
||||
"github.com/opencontainers/go-digest"
|
||||
)
|
||||
|
||||
type Logger func(*client.SolveStatus)
|
||||
|
||||
type SubLogger interface {
|
||||
Wrap(name string, fn func() error) error
|
||||
Log(stream int, dt []byte)
|
||||
}
|
||||
|
||||
func Wrap(name string, l Logger, fn func(SubLogger) error) (err error) {
|
||||
dgst := digest.FromBytes([]byte(identity.NewID()))
|
||||
tm := time.Now()
|
||||
l(&client.SolveStatus{
|
||||
Vertexes: []*client.Vertex{{
|
||||
Digest: dgst,
|
||||
Name: name,
|
||||
Started: &tm,
|
||||
}},
|
||||
})
|
||||
|
||||
defer func() {
|
||||
tm2 := time.Now()
|
||||
errMsg := ""
|
||||
if err != nil {
|
||||
errMsg = err.Error()
|
||||
}
|
||||
l(&client.SolveStatus{
|
||||
Vertexes: []*client.Vertex{{
|
||||
Digest: dgst,
|
||||
Name: name,
|
||||
Started: &tm,
|
||||
Completed: &tm2,
|
||||
Error: errMsg,
|
||||
}},
|
||||
})
|
||||
}()
|
||||
|
||||
return fn(&subLogger{dgst, l})
|
||||
}
|
||||
|
||||
type subLogger struct {
|
||||
dgst digest.Digest
|
||||
logger Logger
|
||||
}
|
||||
|
||||
func (sl *subLogger) Wrap(name string, fn func() error) (err error) {
|
||||
tm := time.Now()
|
||||
sl.logger(&client.SolveStatus{
|
||||
Statuses: []*client.VertexStatus{{
|
||||
Vertex: sl.dgst,
|
||||
ID: name,
|
||||
Timestamp: time.Now(),
|
||||
Started: &tm,
|
||||
}},
|
||||
})
|
||||
|
||||
defer func() {
|
||||
tm2 := time.Now()
|
||||
sl.logger(&client.SolveStatus{
|
||||
Statuses: []*client.VertexStatus{{
|
||||
Vertex: sl.dgst,
|
||||
ID: name,
|
||||
Timestamp: time.Now(),
|
||||
Started: &tm,
|
||||
Completed: &tm2,
|
||||
}},
|
||||
})
|
||||
}()
|
||||
|
||||
return fn()
|
||||
}
|
||||
|
||||
func (sl *subLogger) Log(stream int, dt []byte) {
|
||||
sl.logger(&client.SolveStatus{
|
||||
Logs: []*client.VertexLog{{
|
||||
Vertex: sl.dgst,
|
||||
Stream: stream,
|
||||
Data: dt,
|
||||
Timestamp: time.Now(),
|
||||
}},
|
||||
})
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package progress
|
||||
|
||||
import (
|
||||
"github.com/moby/buildkit/client"
|
||||
)
|
||||
|
||||
type Writer interface {
|
||||
Done() <-chan struct{}
|
||||
Err() error
|
||||
Status() chan *client.SolveStatus
|
||||
}
|
Loading…
Reference in New Issue