|
| 1 | +package dockerhub |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/containerd/containerd/content" |
| 15 | + "github.com/containerd/containerd/content/local" |
| 16 | + "github.com/containerd/containerd/images" |
| 17 | + "github.com/containerd/containerd/images/archive" |
| 18 | + "github.com/containerd/containerd/remotes" |
| 19 | + "github.com/containerd/containerd/remotes/docker" |
| 20 | + "github.com/containerd/platforms" |
| 21 | + "github.com/docker/model-runner/pkg/internal/jsonutil" |
| 22 | + v1 "github.com/opencontainers/image-spec/specs-go/v1" |
| 23 | + "github.com/sirupsen/logrus" |
| 24 | +) |
| 25 | + |
| 26 | +func PullPlatform(ctx context.Context, image, destination, requiredOs, requiredArch string) error { |
| 27 | + if err := os.MkdirAll(filepath.Dir(destination), 0o755); err != nil { |
| 28 | + return fmt.Errorf("creating destination directory %s: %w", filepath.Dir(destination), err) |
| 29 | + } |
| 30 | + output, err := os.Create(destination) |
| 31 | + if err != nil { |
| 32 | + return fmt.Errorf("creating destination file %s: %w", destination, err) |
| 33 | + } |
| 34 | + tmpDir, err := os.MkdirTemp("", "docker-pull") |
| 35 | + if err != nil { |
| 36 | + return fmt.Errorf("creating temp directory: %w", err) |
| 37 | + } |
| 38 | + defer os.RemoveAll(tmpDir) |
| 39 | + store, err := local.NewStore(tmpDir) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("creating new content store: %w", err) |
| 42 | + } |
| 43 | + desc, err := retry(ctx, 10, 1*time.Second, func() (*v1.Descriptor, error) { return fetch(ctx, store, image, requiredOs, requiredArch) }) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("fetching image: %w", err) |
| 46 | + } |
| 47 | + return archive.Export(ctx, store, output, archive.WithManifest(*desc, image), archive.WithSkipMissing(store)) |
| 48 | +} |
| 49 | + |
| 50 | +func retry(ctx context.Context, attempts int, sleep time.Duration, f func() (*v1.Descriptor, error)) (*v1.Descriptor, error) { |
| 51 | + var err error |
| 52 | + var result *v1.Descriptor |
| 53 | + for i := 0; i < attempts; i++ { |
| 54 | + if i > 0 { |
| 55 | + log.Printf("retry %d after error: %v\n", i, err) |
| 56 | + select { |
| 57 | + case <-ctx.Done(): |
| 58 | + return nil, ctx.Err() |
| 59 | + case <-time.After(sleep): |
| 60 | + } |
| 61 | + } |
| 62 | + result, err = f() |
| 63 | + if err == nil { |
| 64 | + return result, nil |
| 65 | + } |
| 66 | + } |
| 67 | + return nil, fmt.Errorf("after %d attempts, last error: %s", attempts, err) |
| 68 | +} |
| 69 | + |
| 70 | +func fetch(ctx context.Context, store content.Store, ref, requiredOs, requiredArch string) (*v1.Descriptor, error) { |
| 71 | + resolver := docker.NewResolver(docker.ResolverOptions{ |
| 72 | + Hosts: docker.ConfigureDefaultRegistries( |
| 73 | + docker.WithAuthorizer( |
| 74 | + docker.NewDockerAuthorizer( |
| 75 | + docker.WithAuthCreds(dockerCredentials)))), |
| 76 | + }) |
| 77 | + name, desc, err := resolver.Resolve(ctx, ref) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + fetcher, err := resolver.Fetcher(ctx, name) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + childrenHandler := images.ChildrenHandler(store) |
| 87 | + if requiredOs != "" && requiredArch != "" { |
| 88 | + requiredPlatform := platforms.Only(v1.Platform{OS: requiredOs, Architecture: requiredArch}) |
| 89 | + childrenHandler = images.LimitManifests(images.FilterPlatforms(images.ChildrenHandler(store), requiredPlatform), requiredPlatform, 1) |
| 90 | + } |
| 91 | + h := images.Handlers(remotes.FetchHandler(store, fetcher), childrenHandler) |
| 92 | + if err := images.Dispatch(ctx, h, nil, desc); err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + return &desc, nil |
| 96 | +} |
| 97 | + |
| 98 | +func dockerCredentials(host string) (string, string, error) { |
| 99 | + hubUsername, hubPassword := os.Getenv("DOCKER_HUB_USER"), os.Getenv("DOCKER_HUB_PASSWORD") |
| 100 | + if hubUsername != "" && hubPassword != "" { |
| 101 | + return hubUsername, hubPassword, nil |
| 102 | + } |
| 103 | + logrus.WithField("host", host).Debug("checking for registry auth config") |
| 104 | + home, err := os.UserHomeDir() |
| 105 | + if err != nil { |
| 106 | + return "", "", err |
| 107 | + } |
| 108 | + credentialConfig := filepath.Join(home, ".docker", "config.json") |
| 109 | + cfg := struct { |
| 110 | + Auths map[string]struct { |
| 111 | + Auth string |
| 112 | + } |
| 113 | + }{} |
| 114 | + if err := jsonutil.ReadFile(credentialConfig, &cfg); err != nil { |
| 115 | + if errors.Is(err, os.ErrNotExist) { |
| 116 | + return "", "", nil |
| 117 | + } |
| 118 | + return "", "", err |
| 119 | + } |
| 120 | + for h, r := range cfg.Auths { |
| 121 | + if h == host { |
| 122 | + creds, err := base64.StdEncoding.DecodeString(r.Auth) |
| 123 | + if err != nil { |
| 124 | + return "", "", err |
| 125 | + } |
| 126 | + parts := strings.SplitN(string(creds), ":", 2) |
| 127 | + if len(parts) != 2 { |
| 128 | + logrus.Debugf("skipping not user/password auth for registry %s: %s", host, parts[0]) |
| 129 | + return "", "", nil |
| 130 | + } |
| 131 | + logrus.Debugf("using auth for registry %s: user=%s", host, parts[0]) |
| 132 | + return parts[0], parts[1], nil |
| 133 | + } |
| 134 | + } |
| 135 | + return "", "", nil |
| 136 | +} |
0 commit comments