Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/runbits/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ func Update(
q.Set("commitID", commitID.String())
u.RawQuery = q.Encode()
rtOpts = append(rtOpts, runtime.WithBuildProgressUrl(u.String()))
// Fallback for when the build-log stream is unavailable.
rtOpts = append(rtOpts, runtime.WithBuildPlanPoller(func() (*buildplan.BuildPlan, error) {
bpm := bpModel.NewBuildPlannerModel(prime.Auth(), prime.SvcModel())
if err := bpm.WaitForBuild(commitID, proj.Owner(), proj.Name(), nil); err != nil {
return nil, errs.Wrap(err, "Could not wait for the in-progress build to complete")
}
c, err := bpm.FetchCommit(commitID, proj.Owner(), proj.Name(), nil)
if err != nil {
return nil, errs.Wrap(err, "Could not fetch the completed build plan")
}
return c.BuildPlan(), nil
}))
}
if proj.IsPortable() {
rtOpts = append(rtOpts, runtime.WithPortable())
Expand Down
6 changes: 6 additions & 0 deletions pkg/runtime/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime

import (
"github.com/ActiveState/cli/pkg/buildplan"
"github.com/ActiveState/cli/pkg/runtime/events"
"github.com/go-openapi/strfmt"
)
Expand All @@ -15,6 +16,11 @@ func WithAuthToken(token string) SetOpt {
return func(opts *Opts) { opts.AuthToken = token }
}

// WithBuildPlanPoller polls for a buildplan when the build-log stream is unavailable.
func WithBuildPlanPoller(poll func() (*buildplan.BuildPlan, error)) SetOpt {
return func(opts *Opts) { opts.PollBuildPlan = poll }
}

// WithDecryptionKey supplies a function that lazily fetches the organization
// AES-256 key used to decrypt private artifacts during install.
func WithDecryptionKey(fetch func() ([]byte, error)) SetOpt {
Expand Down
65 changes: 57 additions & 8 deletions pkg/runtime/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ type Opts struct {
// the server can authorize the stream. Empty for unauthenticated callers.
AuthToken string

// PollBuildPlan waits for an in-progress build when the build-log stream is unavailable.
PollBuildPlan func() (*buildplan.BuildPlan, error)

// OrgKey lazily fetches the organization AES-256 key used to decrypt private
// artifacts during install. It is nil when no key service is configured.
OrgKey func() ([]byte, error)
Expand Down Expand Up @@ -302,15 +305,12 @@ func (s *setup) update() error {
// Wait for build to finish
if !s.buildplan.IsBuildReady() && len(s.toBuild) > 0 {
if err := blog.Wait(context.Background()); err != nil {
if buildlogstream.IsStreamDenied(err) {
if s.opts.AuthToken == "" {
return locale.WrapExternalError(err, "err_buildlog_stream_denied_unauthenticated",
"Could not monitor in-progress build. Please authenticate by running '[ACTIONABLE]state auth[/RESET]' and try again.")
}
return locale.WrapExternalError(err, "err_buildlog_stream_denied_unauthorized",
"Could not monitor in-progress build. If this is a private project, make sure your account has access to it.")
if !buildlogstream.IsStreamDenied(err) {
return errs.Wrap(err, "errors occurred during buildlog streaming")
}
Comment on lines +308 to +310

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this time, the Platform is not denying build log streams, so the code below won't get hit yet.

if err := s.completeWithoutStream(wp); err != nil {
return err
}
Comment thread
mitchell-as marked this conversation as resolved.
return errs.Wrap(err, "errors occurred during buildlog streaming")
}
}

Expand Down Expand Up @@ -355,6 +355,55 @@ func (s *setup) update() error {
return nil
}

// completeWithoutStream finishes an in-progress build without the build-log
// stream: it polls the build to completion, reads the resolved artifact
// download URLs, and drives the normal download/install path off them.
func (s *setup) completeWithoutStream(wp *workerpool.WorkerPool) error {
if s.opts.PollBuildPlan == nil {
return errs.New("no build plan poller configured")
}

logging.Debug("completing the in-progress build without the build-log stream")
resolved, err := s.opts.PollBuildPlan()
if err != nil {
return errs.Wrap(err, "Could not complete the in-progress build without the build-log stream")
}

toObtain, err := s.resolveDownloads(resolved.Artifacts().ToIDMap())
if err != nil {
Comment thread
mitchell-as marked this conversation as resolved.
return err
}
for _, a := range toObtain {
wp.Submit(func() error {
if err := s.obtain(a); err != nil {
return errs.Wrap(err, "obtain failed")
}
return nil
})
}
return nil
}

// resolveDownloads dresses each still-building artifact with the download URL
// from the completed build plan and returns the artifacts to obtain. Artifacts
// that weren't waiting on the build are left untouched (they were obtained
// already). It errors if a still-building artifact has no resolved URL.
func (s *setup) resolveDownloads(resolved buildplan.ArtifactIDMap) ([]*buildplan.Artifact, error) {
var toObtain []*buildplan.Artifact
for _, a := range s.toUnpack {
if _, building := s.toBuild[a.ArtifactID]; !building {
continue
}
ra, ok := resolved[a.ArtifactID]
if !ok || ra.URL == "" {
return nil, errs.New("completed build plan is missing a download URL for artifact %s", a.ArtifactID.String())
}
a.SetDownload(ra.URL, ra.Checksum)
Comment thread
mitchell-as marked this conversation as resolved.
toObtain = append(toObtain, a)
}
return toObtain, nil
}

func (s *setup) onArtifactBuildReady(blog *buildlog.BuildLog, artifact *buildplan.Artifact, cb func()) {
if _, ok := s.toBuild[artifact.ArtifactID]; !ok {
// No need to build, artifact can already be downloaded
Expand Down
73 changes: 73 additions & 0 deletions pkg/runtime/setup_denial_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package runtime

import (
"strings"
"testing"

"github.com/ActiveState/cli/internal/chanutils/workerpool"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/pkg/buildplan"
"github.com/go-openapi/strfmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestResolveDownloads verifies that the non-stream fallback copies download
// URLs from the completed build plan onto the artifacts that were waiting on
// the build, and leaves already-downloadable artifacts alone.
func TestResolveDownloads(t *testing.T) {
building := strfmt.UUID("11111111-1111-1111-1111-111111111111")
notBuilding := strfmt.UUID("22222222-2222-2222-2222-222222222222")

buildingArt := &buildplan.Artifact{ArtifactID: building}
notBuildingArt := &buildplan.Artifact{ArtifactID: notBuilding}

s := &setup{
toUnpack: buildplan.ArtifactIDMap{building: buildingArt, notBuilding: notBuildingArt},
toBuild: buildplan.ArtifactIDMap{building: buildingArt},
}

resolved := buildplan.ArtifactIDMap{
building: &buildplan.Artifact{ArtifactID: building, URL: "https://dl/building", Checksum: "sha256:abc"},
notBuilding: &buildplan.Artifact{ArtifactID: notBuilding, URL: "https://dl/other"},
}

toObtain, err := s.resolveDownloads(resolved)
require.NoError(t, err)

require.Len(t, toObtain, 1, "only the still-building artifact needs obtaining")
assert.Equal(t, building, toObtain[0].ArtifactID)
assert.Equal(t, "https://dl/building", buildingArt.URL, "building artifact must get its resolved download URL")
assert.Equal(t, "sha256:abc", buildingArt.Checksum)
assert.Empty(t, notBuildingArt.URL, "an artifact that wasn't being built must be left untouched")
}

// TestResolveDownloads_MissingURL verifies that a completed build plan missing a
// still-building artifact's URL is an error rather than a silent no-download.
func TestResolveDownloads_MissingURL(t *testing.T) {
building := strfmt.UUID("11111111-1111-1111-1111-111111111111")
buildingArt := &buildplan.Artifact{ArtifactID: building}

s := &setup{
toUnpack: buildplan.ArtifactIDMap{building: buildingArt},
toBuild: buildplan.ArtifactIDMap{building: buildingArt},
}
resolved := buildplan.ArtifactIDMap{building: &buildplan.Artifact{ArtifactID: building}} // no URL

_, err := s.resolveDownloads(resolved)
require.Error(t, err)
}

// TestCompleteWithoutStream_PollError verifies that a build that fails while
// polling (surfaced by the poller) is reported as a failure, not swallowed.
func TestCompleteWithoutStream_PollError(t *testing.T) {
s := &setup{opts: &Opts{
PollBuildPlan: func() (*buildplan.BuildPlan, error) {
return nil, errs.New("build failed while polling")
},
}}
err := s.completeWithoutStream(workerpool.New(1))
require.Error(t, err)
assert.Contains(t, strings.ToLower(errs.JoinMessage(err)), "build failed while polling",
"the underlying build failure must be preserved")
}
Loading