* feat(archied): read-only run-scoped sandbox view over the harness contract Serve RunTarget: the daemon resolves a running task and attempt to its Kit container and follows its output, refusing a finished or unknown task with a stated reason. The dashboard adds a Sandbox tab behind the task log's read authority. Takeover stays out of scope until the sod1 authority is wired. * fix(archied): scope and close sandbox views correctly Forward harness activity into container output, bind containers and dashboard views to their attempt, enforce the asserted org, and report unavailable dependencies and interrupted streams accurately. Refs: archie-core-qi91, archie-core-252tj
98 lines
3.6 KiB
Go
98 lines
3.6 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/containerd/errdefs"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/samcharles93/archie-core/internal/container"
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
)
|
|
|
|
// runTail is how much recent output a view opens with, in lines.
|
|
const runTail = 200
|
|
|
|
// taskLookup reads the task a run target names.
|
|
type taskLookup interface {
|
|
TaskByID(ctx context.Context, id int64) (*workflow.Task, error)
|
|
}
|
|
|
|
// runView resolves a run target to its container and follows its output. It
|
|
// is read-only by construction: it holds no exec and no stdin path, so a view
|
|
// confers no authority over the container.
|
|
type runView struct {
|
|
tasks taskLookup
|
|
follow func(ctx context.Context, taskID, attempt int64) (io.ReadCloser, error)
|
|
}
|
|
|
|
// open refuses a task the daemon does not know, one that is not running, and
|
|
// an attempt that is not the running one. Each refusal states its reason: the
|
|
// container is gone with the attempt, so there is nothing to answer
|
|
// "unavailable" about.
|
|
func (v *runView) open(ctx context.Context, target *pb.RunTarget) (io.ReadCloser, error) {
|
|
t, err := v.tasks.TaskByID(ctx, target.GetTaskId())
|
|
if err != nil {
|
|
return nil, status.Error(codes.Unavailable, "the task store is unavailable: "+err.Error())
|
|
}
|
|
switch {
|
|
case t == nil || t.OrgOrDefault() != string(org.OrgFromContext(ctx)):
|
|
return nil, status.Errorf(codes.NotFound, "task %d is unknown", target.GetTaskId())
|
|
case t.Status != workflow.StatusRunning:
|
|
return nil, status.Errorf(codes.NotFound, "task %d is %s, not running; its sandbox is gone", t.ID, t.Status)
|
|
case int64(t.Attempt) != target.GetAttempt():
|
|
return nil, status.Errorf(codes.NotFound, "attempt %d is not running; task %d is on attempt %d", target.GetAttempt(), t.ID, t.Attempt)
|
|
}
|
|
logs, err := v.follow(ctx, t.ID, target.GetAttempt())
|
|
if err != nil {
|
|
if errdefs.IsNotFound(err) || errors.Is(err, container.ErrNotRunning) {
|
|
return nil, status.Errorf(codes.NotFound, "task %d has no running sandbox: %v", t.ID, err)
|
|
}
|
|
return nil, status.Errorf(codes.Unavailable, "the sandbox view is unavailable: %v", err)
|
|
}
|
|
return logs, nil
|
|
}
|
|
|
|
// openRun streams a running task's sandbox output until the task's container
|
|
// ends or the client leaves. Client frames are ignored: this is the peek, not
|
|
// a takeover.
|
|
func (h *harnessServer) openRun(stream pb.HarnessService_OpenServer, orgID string, target *pb.RunTarget) error {
|
|
if h.runs == nil {
|
|
return status.Error(codes.Unavailable, "run-scoped views need the task store and the container pool")
|
|
}
|
|
logs, err := h.runs.open(org.WithOrg(stream.Context(), org.OrgID(orgID)), target)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = logs.Close() }()
|
|
// An empty frame tells the client the view opened before the sandbox has
|
|
// printed anything.
|
|
if err := stream.Send(&pb.OpenResponse{Frame: &pb.OpenResponse_Stdout{}}); err != nil {
|
|
return err
|
|
}
|
|
h.log.Info("run view opened", "task", target.GetTaskId(), "attempt", target.GetAttempt())
|
|
defer h.log.Info("run view closed", "task", target.GetTaskId(), "attempt", target.GetAttempt())
|
|
|
|
buf := make([]byte, 32<<10)
|
|
for {
|
|
n, err := logs.Read(buf)
|
|
if n > 0 {
|
|
if sendErr := stream.Send(&pb.OpenResponse{Frame: &pb.OpenResponse_Stdout{Stdout: append([]byte(nil), buf[:n]...)}}); sendErr != nil {
|
|
return sendErr
|
|
}
|
|
}
|
|
if err != nil {
|
|
if err == io.EOF || stream.Context().Err() != nil {
|
|
return nil
|
|
}
|
|
return status.Error(codes.Unavailable, fmt.Sprintf("the sandbox output ended: %v", err))
|
|
}
|
|
}
|
|
}
|