build: sublogger to show docker load progress output
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>pull/1994/head
parent
e5419ef6d7
commit
cd539be9c5
@ -1,47 +1,73 @@
|
|||||||
package dockerutil
|
package dockerutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/buildx/util/progress"
|
"github.com/docker/buildx/util/progress"
|
||||||
"github.com/docker/cli/cli/streams"
|
|
||||||
"github.com/docker/docker/pkg/jsonmessage"
|
"github.com/docker/docker/pkg/jsonmessage"
|
||||||
"github.com/moby/buildkit/client"
|
"github.com/moby/buildkit/client"
|
||||||
"github.com/moby/buildkit/identity"
|
|
||||||
"github.com/opencontainers/go-digest"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func fromReader(w progress.Writer, name string, rc io.ReadCloser) error {
|
func fromReader(l progress.SubLogger, rc io.ReadCloser) error {
|
||||||
dgst := digest.FromBytes([]byte(identity.NewID()))
|
started := map[string]client.VertexStatus{}
|
||||||
tm := time.Now()
|
|
||||||
|
|
||||||
vtx := client.Vertex{
|
defer func() {
|
||||||
Digest: dgst,
|
for _, st := range started {
|
||||||
Name: name,
|
st := st
|
||||||
Started: &tm,
|
if st.Completed == nil {
|
||||||
}
|
now := time.Now()
|
||||||
|
st.Completed = &now
|
||||||
w.Write(&client.SolveStatus{
|
l.SetStatus(&st)
|
||||||
Vertexes: []*client.Vertex{&vtx},
|
}
|
||||||
})
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
err := jsonmessage.DisplayJSONMessagesToStream(rc, streams.NewOut(io.Discard), nil)
|
dec := json.NewDecoder(rc)
|
||||||
if err != nil {
|
var parsedErr error
|
||||||
if jerr, ok := err.(*jsonmessage.JSONError); ok {
|
var jm jsonmessage.JSONMessage
|
||||||
err = errors.New(jerr.Message)
|
for {
|
||||||
|
if err := dec.Decode(&jm); err != nil {
|
||||||
|
if parsedErr != nil {
|
||||||
|
return parsedErr
|
||||||
|
}
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if jm.Error != nil {
|
||||||
|
parsedErr = jm.Error
|
||||||
|
}
|
||||||
|
if jm.ID == "" {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
id := "loading layer " + jm.ID
|
||||||
|
st, ok := started[id]
|
||||||
|
if !ok {
|
||||||
|
if jm.Progress != nil {
|
||||||
|
now := time.Now()
|
||||||
|
st = client.VertexStatus{
|
||||||
|
ID: id,
|
||||||
|
Started: &now,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
st.Timestamp = time.Now()
|
||||||
|
if jm.Progress != nil && jm.Status == "Loading layer" {
|
||||||
|
st.Current = jm.Progress.Current
|
||||||
|
st.Total = jm.Progress.Total
|
||||||
|
}
|
||||||
|
if jm.Error != nil {
|
||||||
|
now := time.Now()
|
||||||
|
st.Completed = &now
|
||||||
|
}
|
||||||
|
started[id] = st
|
||||||
|
l.SetStatus(&st)
|
||||||
}
|
}
|
||||||
|
|
||||||
tm2 := time.Now()
|
return nil
|
||||||
vtx2 := vtx
|
|
||||||
vtx2.Completed = &tm2
|
|
||||||
if err != nil {
|
|
||||||
vtx2.Error = err.Error()
|
|
||||||
}
|
|
||||||
w.Write(&client.SolveStatus{
|
|
||||||
Vertexes: []*client.Vertex{&vtx2},
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue