in example/ecr-pull/main.go [39:115]
func main() {
ctx := namespaces.NamespaceFromEnv(context.Background())
if len(os.Args) < 2 {
log.G(ctx).Fatal("Must provide image to pull as argument")
} else if len(os.Args) > 2 {
log.G(ctx).Fatal("Must provide only the image to pull")
}
ref := os.Args[1]
parallelism := defaultParallelism
parseEnvInt(ctx, "ECR_PULL_PARALLEL", ¶llelism)
enableDebug := defaultEnableDebug
parseEnvInt(ctx, "ECR_PULL_DEBUG", &enableDebug)
if enableDebug == 1 {
log.L.Logger.SetLevel(logrus.TraceLevel)
}
address := "/run/containerd/containerd.sock"
if newAddress := os.Getenv("CONTAINERD_ADDRESS"); newAddress != "" {
address = newAddress
}
client, err := containerd.New(address)
if err != nil {
log.G(ctx).WithError(err).Fatal("Failed to connect to containerd")
}
defer client.Close()
ongoing := newJobs(ref)
pctx, stopProgress := context.WithCancel(ctx)
progress := make(chan struct{})
go func() {
showProgress(pctx, ongoing, client.ContentStore(), os.Stdout)
close(progress)
}()
h := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
if desc.MediaType != images.MediaTypeDockerSchema1Manifest {
ongoing.add(desc)
}
return nil, nil
})
resolver, err := ecr.NewResolver(ecr.WithLayerDownloadParallelism(parallelism))
if err != nil {
log.G(ctx).WithError(err).Fatal("Failed to create resolver")
}
log.G(ctx).WithField("ref", ref).Info("Pulling from Amazon ECR")
img, err := client.Pull(ctx, ref,
containerd.WithResolver(resolver),
containerd.WithImageHandler(h),
containerd.WithSchema1Conversion)
stopProgress()
if err != nil {
log.G(ctx).WithError(err).WithField("ref", ref).Fatal("Failed to pull")
}
<-progress
log.G(ctx).WithField("img", img.Name()).Info("Pulled successfully!")
if skipUnpack := os.Getenv("ECR_SKIP_UNPACK"); skipUnpack != "" {
return
}
snapshotter := containerd.DefaultSnapshotter
if newSnapshotter := os.Getenv("CONTAINERD_SNAPSHOTTER"); newSnapshotter != "" {
snapshotter = newSnapshotter
}
log.G(ctx).
WithField("img", img.Name()).
WithField("snapshotter", snapshotter).
Info("unpacking...")
err = img.Unpack(ctx, snapshotter)
if err != nil {
log.G(ctx).WithError(err).WithField("img", img.Name).Fatal("Failed to unpack")
}
}