shad-go/distbuild/pkg/client/build.go

101 lines
2.1 KiB
Go
Raw Normal View History

2020-03-10 12:08:59 +00:00
package client
import (
"context"
"fmt"
2020-03-29 16:24:18 +00:00
"io"
2020-03-10 12:08:59 +00:00
2020-03-11 22:46:45 +00:00
"go.uber.org/zap"
2020-03-29 16:03:07 +00:00
"gitlab.com/slon/shad-go/distbuild/pkg/api"
2020-03-10 12:08:59 +00:00
"gitlab.com/slon/shad-go/distbuild/pkg/build"
)
type Client struct {
2020-03-29 16:24:18 +00:00
l *zap.Logger
client *api.Client
sourceDir string
}
func NewClient(
l *zap.Logger,
apiEndpoint string,
sourceDir string,
) *Client {
return &Client{
l: l,
client: &api.Client{Endpoint: apiEndpoint},
sourceDir: sourceDir,
}
2020-03-10 12:08:59 +00:00
}
type BuildListener interface {
OnJobStdout(jobID build.ID, stdout []byte) error
2020-03-11 22:46:45 +00:00
OnJobStderr(jobID build.ID, stderr []byte) error
2020-03-10 12:08:59 +00:00
OnJobFinished(jobID build.ID) error
OnJobFailed(jobID build.ID, code int, error string) error
}
2020-03-29 16:24:18 +00:00
func (c *Client) uploadSources(ctx context.Context, started *api.BuildStarted) error {
2020-03-11 22:46:45 +00:00
return nil
2020-03-10 12:08:59 +00:00
}
func (c *Client) Build(ctx context.Context, graph build.Graph, lsn BuildListener) error {
2020-03-29 16:24:18 +00:00
started, r, err := c.client.StartBuild(ctx, &api.BuildRequest{Graph: graph})
2020-03-10 12:08:59 +00:00
if err != nil {
return err
}
2020-03-29 16:24:18 +00:00
c.l.Debug("build started", zap.String("build_id", started.ID.String()))
if err := c.uploadSources(ctx, started); err != nil {
2020-03-10 12:08:59 +00:00
return err
}
for {
2020-03-29 16:24:18 +00:00
u, err := r.Next()
if err == io.EOF {
return fmt.Errorf("unexpected end of status stream")
} else if err != nil {
return err
2020-03-10 12:08:59 +00:00
}
2020-03-29 16:24:18 +00:00
c.l.Debug("received status update", zap.String("build_id", started.ID.String()), zap.Any("update", u))
2020-03-10 12:08:59 +00:00
switch {
2020-03-29 16:24:18 +00:00
case u.BuildFailed != nil:
return fmt.Errorf("build failed: %s", u.BuildFailed.Error)
2020-03-10 12:08:59 +00:00
2020-03-29 16:24:18 +00:00
case u.BuildFinished != nil:
2020-03-14 10:24:44 +00:00
return nil
2020-03-29 16:24:18 +00:00
case u.JobFinished != nil:
jf := u.JobFinished
2020-03-10 12:08:59 +00:00
if jf.Stdout != nil {
if err := lsn.OnJobStdout(jf.ID, jf.Stdout); err != nil {
return err
}
}
if jf.Stderr != nil {
if err := lsn.OnJobStderr(jf.ID, jf.Stderr); err != nil {
return err
}
}
if jf.Error != nil {
if err := lsn.OnJobFailed(jf.ID, jf.ExitCode, *jf.Error); err != nil {
return err
}
} else {
if err := lsn.OnJobFinished(jf.ID); err != nil {
return err
}
}
default:
return fmt.Errorf("build failed: unexpected status update")
}
}
}