Skip to content

Commit

Permalink
Report launching and connecting messages to the API
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed May 3, 2024
1 parent 84ad4d3 commit bbba6de
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 8 deletions.
6 changes: 4 additions & 2 deletions examples/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ func main() {
}
defer cancel()

reportingWriter := progresshelper.NewReportingWriter(printer, build.ID, build.Token)

// 3. Acquire a buildkit machine.
var buildkit *machine.Machine
buildErr = progresshelper.WithLog(printer, "[depot] launching amd64 machine", func() error {
buildErr = progresshelper.WithLog(reportingWriter, "[depot] launching amd64 machine", func() error {
buildkit, buildErr = machine.Acquire(ctx, build.ID, build.Token, "amd64")
return buildErr
})
Expand All @@ -64,7 +66,7 @@ func main() {
// 4. Check buildkitd readiness. When the buildkitd starts, it may take
// quite a while to be ready to accept connections when it loads a large boltdb.
var buildkitClient *client.Client
buildErr = progresshelper.WithLog(printer, "[depot] connecting to amd64 machine", func() error {
buildErr = progresshelper.WithLog(reportingWriter, "[depot] connecting to amd64 machine", func() error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
buildkitClient, buildErr = buildkit.Connect(ctx)
Expand Down
7 changes: 5 additions & 2 deletions pkg/buildxdriver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
depotbuild "github.com/depot/cli/pkg/build"
"github.com/depot/cli/pkg/debuglog"
"github.com/depot/cli/pkg/machine"
"github.com/depot/cli/pkg/progresshelper"
"github.com/docker/buildx/driver"
"github.com/docker/buildx/util/progress"
"github.com/moby/buildkit/client"
Expand Down Expand Up @@ -41,12 +42,14 @@ func (d *Driver) Bootstrap(ctx context.Context, reporter progress.Logger) error
d.cfg.Auth = depotbuild.NewAuthProvider(credentials, d.cfg.Auth)
}

reportingLogger := progresshelper.NewReportingLogger(reporter, buildID, token)

message := "[depot] launching " + platform + " machine"

// Try to acquire machine twice
var err error
for i := 0; i < 2; i++ {
finishLog := StartLog(message, reporter)
finishLog := StartLog(message, reportingLogger)
d.buildkit, err = machine.Acquire(ctx, buildID, token, platform)
finishLog(err)
if err == nil {
Expand All @@ -59,7 +62,7 @@ func (d *Driver) Bootstrap(ctx context.Context, reporter progress.Logger) error
}

message = "[depot] connecting to " + platform + " machine"
finishLog := StartLog(message, reporter)
finishLog := StartLog(message, reportingLogger)

ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
Expand Down
6 changes: 4 additions & 2 deletions pkg/cmd/buildctl/dial-stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ func run() error {
}
state.Reporter = progresshelper.Tee(state.Reporter, status)

reportingWriter := progresshelper.NewReportingWriter(state.Reporter, build.ID, build.Token)

state.SummaryURL = build.BuildURL
buildFinish = build.Finish

Expand All @@ -153,7 +155,7 @@ func run() error {
}

var builder *machine.Machine
state.Err = progresshelper.WithLog(state.Reporter, "[depot] launching "+platform+" machine", func() error {
state.Err = progresshelper.WithLog(reportingWriter, "[depot] launching "+platform+" machine", func() error {
for i := 0; i < 2; i++ {
builder, state.Err = machine.Acquire(ctx, build.ID, build.Token, platform)
if state.Err == nil {
Expand All @@ -169,7 +171,7 @@ func run() error {

machineRelease = builder.Release

state.Err = progresshelper.WithLog(state.Reporter, "[depot] connecting to "+platform+" machine", func() error {
state.Err = progresshelper.WithLog(reportingWriter, "[depot] connecting to "+platform+" machine", func() error {
buildkitConn, err := tlsConn(ctx, builder)
if err != nil {
state.Err = fmt.Errorf("unable to connect: %w", err)
Expand Down
6 changes: 4 additions & 2 deletions pkg/cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ func NewCmdExec(dockerCli command.Cli) *cobra.Command {
return buildErr
}

reportingWriter := progresshelper.NewReportingWriter(printer, build.ID, build.Token)

var builder *machine.Machine
buildErr = progresshelper.WithLog(printer, fmt.Sprintf("[depot] launching %s machine", platform), func() error {
buildErr = progresshelper.WithLog(reportingWriter, fmt.Sprintf("[depot] launching %s machine", platform), func() error {
for i := 0; i < 2; i++ {
builder, buildErr = machine.Acquire(ctx, build.ID, build.Token, platform)
if buildErr == nil {
Expand All @@ -104,7 +106,7 @@ func NewCmdExec(dockerCli command.Cli) *cobra.Command {

// Wait for connection to be ready.
var conn net.Conn
buildErr = progresshelper.WithLog(printer, fmt.Sprintf("[depot] connecting to %s machine", platform), func() error {
buildErr = progresshelper.WithLog(reportingWriter, fmt.Sprintf("[depot] connecting to %s machine", platform), func() error {
conn, buildErr = connection.TLSConn(ctx, builder)
if buildErr != nil {
return fmt.Errorf("unable to connect: %w", buildErr)
Expand Down
87 changes: 87 additions & 0 deletions pkg/progresshelper/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package progresshelper

import (
"context"
"time"

"connectrpc.com/connect"
depotapi "github.com/depot/cli/pkg/api"
cliv1 "github.com/depot/cli/pkg/proto/depot/cli/v1"
cliv1connect "github.com/depot/cli/pkg/proto/depot/cli/v1/cliv1connect"
controlapi "github.com/moby/buildkit/api/services/control"
"github.com/moby/buildkit/client"
)

func reportToAPI(client cliv1connect.BuildServiceClient, status *client.SolveStatus, buildID, token string) {
if buildID != "" && token != "" {
req := &cliv1.ReportStatusRequest{
BuildId: buildID,
Statuses: []*controlapi.StatusResponse{toStatusResponse(status)},
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

_, _ = client.ReportStatus(ctx, depotapi.WithAuthentication(connect.NewRequest(req), token))
}
}

func toStatusResponse(status *client.SolveStatus) *controlapi.StatusResponse {
vertexes := make([]*controlapi.Vertex, 0, len(status.Vertexes))
for _, v := range status.Vertexes {
vertexes = append(vertexes, &controlapi.Vertex{
Digest: v.Digest,
Inputs: v.Inputs,
Name: v.Name,
Cached: v.Cached,
Started: v.Started,
Completed: v.Completed,
Error: v.Error,
ProgressGroup: v.ProgressGroup,
})
}

statuses := make([]*controlapi.VertexStatus, 0, len(status.Statuses))
for _, s := range status.Statuses {
statuses = append(statuses, &controlapi.VertexStatus{
ID: s.ID,
Vertex: s.Vertex,
Name: s.Name,
Current: s.Current,
Total: s.Total,
Timestamp: s.Timestamp,
Started: s.Started,
Completed: s.Completed,
})
}

logs := make([]*controlapi.VertexLog, 0, len(status.Logs))
for _, l := range status.Logs {
logs = append(logs, &controlapi.VertexLog{
Vertex: l.Vertex,
Timestamp: l.Timestamp,
Stream: int64(l.Stream),
Msg: l.Data,
})
}

warnings := make([]*controlapi.VertexWarning, 0, len(status.Warnings))
for _, w := range status.Warnings {
warnings = append(warnings, &controlapi.VertexWarning{
Vertex: w.Vertex,
Level: int64(w.Level),
Short: w.Short,
Detail: w.Detail,
Url: w.URL,
Info: w.SourceInfo,
Ranges: w.Range,
})
}

return &controlapi.StatusResponse{
Vertexes: vertexes,
Statuses: statuses,
Logs: logs,
Warnings: warnings,
}
}
15 changes: 15 additions & 0 deletions pkg/progresshelper/reportinglogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package progresshelper

import (
depotapi "github.com/depot/cli/pkg/api"
"github.com/docker/buildx/util/progress"
"github.com/moby/buildkit/client"
)

func NewReportingLogger(w progress.Logger, buildID, token string) progress.Logger {
depotClient := depotapi.NewBuildClient()
return func(status *client.SolveStatus) {
w(status)
reportToAPI(depotClient, status, buildID, token)
}
}
32 changes: 32 additions & 0 deletions pkg/progresshelper/reportingwriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package progresshelper

import (
depotapi "github.com/depot/cli/pkg/api"
cliv1connect "github.com/depot/cli/pkg/proto/depot/cli/v1/cliv1connect"
"github.com/docker/buildx/util/progress"
"github.com/moby/buildkit/client"
)

var _ progress.Writer = (*reportingWriter)(nil)

type reportingWriter struct {
progress.Writer

buildID string
token string
client cliv1connect.BuildServiceClient
}

func NewReportingWriter(w progress.Writer, buildID, token string) progress.Writer {
return &reportingWriter{
Writer: w,
buildID: buildID,
token: token,
client: depotapi.NewBuildClient(),
}
}

func (s *reportingWriter) Write(status *client.SolveStatus) {
s.Writer.Write(status)
reportToAPI(s.client, status, s.buildID, s.token)
}

0 comments on commit bbba6de

Please sign in to comment.