working commit
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
package repocli
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
type xxxAuthenticator interface {
|
||||
MakeHeader(user, pass string) (key, value string, err error)
|
||||
}
|
||||
|
||||
type BasicAuthenticator struct{}
|
||||
|
||||
func xxxNewBasicAuthenticator() *BasicAuthenticator {
|
||||
return &BasicAuthenticator{}
|
||||
}
|
||||
|
||||
func (auth *BasicAuthenticator) MakeHeader(user, pass string) (string, string, error) {
|
||||
pair := base64.StdEncoding.EncodeToString([]byte(user + ":" + pass))
|
||||
return "Authorization", "Basic " + pair, nil
|
||||
}
|
||||
+13
-1
@@ -130,6 +130,18 @@ func (ima *Imager) ReadManifest(ctx context.Context, digstr string) (bool, strin
|
||||
}
|
||||
|
||||
func (ima *Imager) WriteManifest(ctx context.Context, digest, mime string, payload []byte) error {
|
||||
err := ima.writeManifest(ctx, digest, mime, payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ima.writeIndex(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (ima *Imager) writeManifest(ctx context.Context, digest, mime string, payload []byte) error {
|
||||
var err error
|
||||
digobj, err := ocidigest.Parse(digest)
|
||||
if err != nil {
|
||||
@@ -169,7 +181,7 @@ func (ima *Imager) WriteManifest(ctx context.Context, digest, mime string, paylo
|
||||
return err
|
||||
}
|
||||
|
||||
func (ima *Imager) WriteIndex(ctx context.Context) error {
|
||||
func (ima *Imager) writeIndex(ctx context.Context) error {
|
||||
indexdat, err := json.Marshal(ima.index)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -34,7 +34,7 @@ func TestDigest(t *testing.T) {
|
||||
err := imager.WriteLayer(ctx, digest, size, reader)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = imager.WriteIndex(ctx)
|
||||
err = imager.writeIndex(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
package repocli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (cli *Client) PatchUpload(ctx context.Context, rawrepo string, src io.Reader, uploc string, size int64) (string, error) {
|
||||
var err error
|
||||
var ouloc string
|
||||
|
||||
ref, err := NewRepository(rawrepo)
|
||||
if err != nil {
|
||||
return ouloc, err
|
||||
}
|
||||
uri, err := ref.Patch(uploc)
|
||||
if err != nil {
|
||||
return ouloc, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, uri, src)
|
||||
if err != nil {
|
||||
return ouloc, err
|
||||
}
|
||||
req.Header.Set("User-Agent", cli.userAgent)
|
||||
req.Header.Set("Content-Type", "application/octet-stream")
|
||||
req.Header.Set("Content-Length", strconv.FormatInt(size, 10))
|
||||
resp, err := cli.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return ouloc, err
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusAccepted {
|
||||
err = fmt.Errorf("Upload not accepted, code %d", resp.StatusCode)
|
||||
return ouloc, err
|
||||
}
|
||||
ouloc = resp.Header.Get("Location")
|
||||
if ouloc == "" {
|
||||
err := fmt.Errorf("Empty blob location declaration")
|
||||
return ouloc, err
|
||||
}
|
||||
return ouloc, err
|
||||
}
|
||||
+38
-13
@@ -5,7 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
)
|
||||
@@ -28,7 +28,7 @@ func NewDownloader(client *Client) *Downloader {
|
||||
}
|
||||
}
|
||||
|
||||
func (down *Downloader) Pull(ctx context.Context, rawref, dir, os, arch string) error {
|
||||
func (down *Downloader) Pull(ctx context.Context, rawref, dir, osname, arch string) error {
|
||||
var err error
|
||||
ref, err := ParseReference(rawref)
|
||||
if err != nil {
|
||||
@@ -46,6 +46,7 @@ func (down *Downloader) Pull(ctx context.Context, rawref, dir, os, arch string)
|
||||
return err
|
||||
}
|
||||
|
||||
var digstr string
|
||||
if mime == MediaTypeOIIv1 || mime == MediaTypeDDMLv2 {
|
||||
var index ocispec.Index
|
||||
err = json.Unmarshal(man, &index)
|
||||
@@ -55,9 +56,10 @@ func (down *Downloader) Pull(ctx context.Context, rawref, dir, os, arch string)
|
||||
for _, descr := range index.Manifests {
|
||||
if descr.Platform != nil {
|
||||
cond := descr.Platform.Architecture == arch
|
||||
cond = cond && descr.Platform.OS == os
|
||||
cond = cond && descr.Platform.OS == osname
|
||||
if cond {
|
||||
tag = descr.Digest.String()
|
||||
digstr = descr.Digest.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,24 +83,47 @@ func (down *Downloader) Pull(ctx context.Context, rawref, dir, os, arch string)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//"oci-layout"
|
||||
//"index.json"
|
||||
// Write image manifest
|
||||
imager := NewEmptyImager(dir)
|
||||
err = imager.WriteManifest(ctx, digstr, mime, man)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
layers := make([]ocispec.Descriptor, 0)
|
||||
layers = append(layers, manifest.Config)
|
||||
layers = append(layers, manifest.Layers...)
|
||||
for _, layer := range layers {
|
||||
digest := layer.Digest.String()
|
||||
exist, err := down.cli.GetBlob(ctx, rawrepo, io.Discard, digest)
|
||||
wrapfunc := func() error {
|
||||
tmpfile, err := os.CreateTemp(dir, "*.bin")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tmpfile.Close()
|
||||
tmpname := tmpfile.Name()
|
||||
defer os.Remove(tmpname)
|
||||
|
||||
digstr := layer.Digest.String()
|
||||
exist, err := down.cli.GetBlob(ctx, rawrepo, tmpfile, digstr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exist {
|
||||
err = errors.New("Layer not found")
|
||||
return err
|
||||
}
|
||||
tmpfile.Seek(0, 0)
|
||||
size := layer.Size
|
||||
err = imager.WriteLayer(ctx, digstr, size, tmpfile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
err = wrapfunc()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exist {
|
||||
err = errors.New("Layer not found")
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Layer type: %s\n", layer.MediaType)
|
||||
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func xxxTestPullImage(t *testing.T) {
|
||||
func TestPullImage(t *testing.T) {
|
||||
|
||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
cli := NewClientWithTransport(nil, nil)
|
||||
@@ -19,7 +19,7 @@ func xxxTestPullImage(t *testing.T) {
|
||||
require.NotNil(t, down)
|
||||
|
||||
rawref := "mirror.gcr.io/alpine:3.20.0"
|
||||
destdir := "qwert"
|
||||
destdir := "aaa"
|
||||
err := down.Pull(ctx, rawref, destdir, "linux", "amd64")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user