2021-12-09 21:07:52 +01:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/reference"
|
|
|
|
"github.com/moby/buildkit/client/llb"
|
2021-12-17 01:20:00 +01:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-12-09 21:07:52 +01:00
|
|
|
"go.dagger.io/dagger/compiler"
|
|
|
|
"go.dagger.io/dagger/plancontext"
|
|
|
|
"go.dagger.io/dagger/solver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Register("Pull", func() Task { return &pullTask{} })
|
|
|
|
}
|
|
|
|
|
|
|
|
type pullTask struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *pullTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
|
2021-12-17 01:20:00 +01:00
|
|
|
lg := log.Ctx(ctx)
|
|
|
|
|
2021-12-09 21:07:52 +01:00
|
|
|
rawRef, err := v.Lookup("source").String()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-17 01:20:00 +01:00
|
|
|
// Read auth info
|
2022-01-31 16:22:19 +01:00
|
|
|
if auth := v.Lookup("auth"); auth.Exists() {
|
|
|
|
a, err := decodeAuthValue(pctx, auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Extract registry target from source
|
|
|
|
target, err := solver.ParseAuthHost(rawRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.AddCredentials(target, a.Username, a.Secret.PlainText())
|
|
|
|
lg.Debug().Str("target", target).Msg("add target credentials")
|
2021-12-17 01:20:00 +01:00
|
|
|
}
|
|
|
|
|
2021-12-09 21:07:52 +01:00
|
|
|
ref, err := reference.ParseNormalizedNamed(rawRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse ref %s: %w", rawRef, err)
|
|
|
|
}
|
|
|
|
// Add the default tag "latest" to a reference if it only has a repo name.
|
|
|
|
ref = reference.TagNameOnly(ref)
|
|
|
|
|
|
|
|
st := llb.Image(
|
|
|
|
ref.String(),
|
2021-12-14 15:41:44 +01:00
|
|
|
withCustomName(v, "Pull %s", rawRef),
|
2021-12-09 21:07:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Load image metadata and convert to to LLB.
|
|
|
|
platform := pctx.Platform.Get()
|
|
|
|
image, digest, err := s.ResolveImageConfig(ctx, ref.String(), llb.ResolveImageConfigOpt{
|
|
|
|
LogName: vertexNamef(v, "load metadata for %s", ref.String()),
|
|
|
|
Platform: &platform,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-17 01:20:00 +01:00
|
|
|
|
2021-12-09 21:07:52 +01:00
|
|
|
result, err := s.Solve(ctx, st, pctx.Platform.Get())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fs := pctx.FS.New(result)
|
|
|
|
|
2021-12-09 21:20:56 +01:00
|
|
|
return compiler.NewValue().FillFields(map[string]interface{}{
|
|
|
|
"output": fs.MarshalCUE(),
|
|
|
|
"digest": digest,
|
2022-01-26 11:22:09 +01:00
|
|
|
"config": ConvertImageConfig(image.Config),
|
2021-12-09 21:20:56 +01:00
|
|
|
})
|
2021-12-09 21:07:52 +01:00
|
|
|
}
|