working commit
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
func makeTmpFileName(prefix string) string {
|
||||
randBytes := make([]byte, 6)
|
||||
rand.Read(randBytes)
|
||||
suffix := hex.EncodeToString(randBytes)
|
||||
return fmt.Sprintf("%s.tmp.%s", prefix, suffix)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type roundTripper struct{}
|
||||
|
||||
func (t *roundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
tlsConfig := &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
httpTransport := &http.Transport{
|
||||
TLSClientConfig: tlsConfig,
|
||||
}
|
||||
return httpTransport.RoundTrip(r)
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/authn"
|
||||
"github.com/google/go-containerregistry/pkg/crane"
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
|
||||
)
|
||||
|
||||
func (cli *Client) PullImage(ctx context.Context, filepath, imagepath string, timeout time.Duration) error {
|
||||
var err error
|
||||
ctx, _ = context.WithTimeout(ctx, timeout)
|
||||
|
||||
options := make([]crane.Option, 0)
|
||||
options = append(options, crane.WithContext(ctx))
|
||||
|
||||
if cli.username != "" && cli.password != "" {
|
||||
ref, err := name.ParseReference(imagepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
repo := ref.Context()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defaultTransport := &roundTripper{}
|
||||
|
||||
scopes := []string{repo.Scope(transport.PullScope)}
|
||||
|
||||
regName := repo.RegistryStr()
|
||||
reg, err := name.NewRegistry(regName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
basicAuth := &authn.Basic{
|
||||
Username: cli.username,
|
||||
Password: cli.password,
|
||||
}
|
||||
authTransport, err := transport.NewWithContext(ctx, reg, basicAuth, defaultTransport, scopes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options = append(options, crane.WithTransport(authTransport))
|
||||
} else {
|
||||
transport := &roundTripper{}
|
||||
options = append(options, crane.WithTransport(transport))
|
||||
}
|
||||
|
||||
image, err := crane.Pull(imagepath, options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dstdir := makeTmpFileName(filepath)
|
||||
err = os.MkdirAll(dstdir, 0750)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = crane.SaveOCI(image, dstdir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = archiveDir(dstdir, filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.RemoveAll(dstdir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/authn"
|
||||
"github.com/google/go-containerregistry/pkg/crane"
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
pkg "github.com/google/go-containerregistry/pkg/v1"
|
||||
"github.com/google/go-containerregistry/pkg/v1/layout"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
|
||||
)
|
||||
|
||||
func (cli *Client) PushImage(ctx context.Context, filepath, imagepath string, timeout time.Duration) error {
|
||||
var err error
|
||||
ctx, _ = context.WithTimeout(ctx, timeout)
|
||||
|
||||
options := make([]crane.Option, 0)
|
||||
options = append(options, crane.WithContext(ctx))
|
||||
if cli.username != "" && cli.password != "" {
|
||||
ref, err := name.ParseReference(imagepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
repo := ref.Context()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defaultTransport := &roundTripper{}
|
||||
|
||||
scopes := []string{
|
||||
repo.Scope(transport.PushScope),
|
||||
repo.Scope(transport.PullScope),
|
||||
}
|
||||
|
||||
regName := repo.RegistryStr()
|
||||
reg, err := name.NewRegistry(regName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
basicAuth := &authn.Basic{
|
||||
Username: cli.username,
|
||||
Password: cli.password,
|
||||
}
|
||||
|
||||
authTransport, err := transport.NewWithContext(ctx, reg, basicAuth, defaultTransport, scopes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options = append(options, crane.WithTransport(authTransport))
|
||||
} else {
|
||||
defaultTransport := &roundTripper{}
|
||||
options = append(options, crane.WithTransport(defaultTransport))
|
||||
}
|
||||
|
||||
dstdir := makeTmpFileName(filepath)
|
||||
err = unarchive(filepath, dstdir)
|
||||
if err != nil {
|
||||
os.RemoveAll(dstdir)
|
||||
return err
|
||||
}
|
||||
image, err := imageLoader(dstdir)
|
||||
if err != nil {
|
||||
os.RemoveAll(dstdir)
|
||||
return err
|
||||
}
|
||||
err = crane.Push(image, imagepath, options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func imageLoader(dirPath string) (pkg.Image, error) {
|
||||
var err error
|
||||
var res pkg.Image
|
||||
layoutPath, err := layout.FromPath(dirPath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
imageIndex, err := layoutPath.ImageIndex()
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
indexManifest, err := imageIndex.IndexManifest()
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if indexManifest == nil {
|
||||
err := fmt.Errorf("Empty indexManifest referency")
|
||||
return res, err
|
||||
}
|
||||
|
||||
manifest := indexManifest.Manifests[0]
|
||||
image, err := layoutPath.Image(manifest.Digest)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res = image
|
||||
return res, err
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func archiveDir(srcDir, dstPath string) error {
|
||||
var err error
|
||||
srcDir = filepath.Clean(srcDir)
|
||||
dstPath = filepath.Clean(dstPath)
|
||||
|
||||
err = os.MkdirAll(filepath.Dir(dstPath), 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tarFile, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0640)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tarFile.Close()
|
||||
|
||||
tarWriter := tar.NewWriter(tarFile)
|
||||
defer tarWriter.Close()
|
||||
|
||||
walker := func(filePath string, fileInfo os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !fileInfo.Mode().IsRegular() {
|
||||
return nil
|
||||
}
|
||||
header, err := tar.FileInfoHeader(fileInfo, fileInfo.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
header.Name = strings.TrimPrefix(filePath, filepath.Clean(srcDir))
|
||||
header.Name = strings.TrimPrefix(header.Name, string(filepath.Separator))
|
||||
|
||||
err = tarWriter.WriteHeader(header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
_, err = io.Copy(tarWriter, file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
err = filepath.Walk(srcDir, walker)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func unarchive(filePath, dstDir string) error {
|
||||
var err error
|
||||
err = os.MkdirAll(dstDir, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file, err := os.OpenFile(filePath, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
tarReader := tar.NewReader(file)
|
||||
for {
|
||||
header, err := tarReader.Next()
|
||||
switch {
|
||||
case err == io.EOF:
|
||||
return nil
|
||||
case err != nil:
|
||||
return err
|
||||
case header == nil:
|
||||
continue
|
||||
}
|
||||
target := filepath.Join(dstDir, header.Name)
|
||||
target = filepath.Clean(target)
|
||||
//fileInfo := header.FileInfo()
|
||||
switch header.Typeflag {
|
||||
case tar.TypeDir:
|
||||
_, err := os.Stat(target)
|
||||
if err != nil {
|
||||
err := os.MkdirAll(target, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
case tar.TypeReg:
|
||||
err := os.MkdirAll(filepath.Dir(target), 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(header.Mode))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = io.Copy(file, tarReader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file.Close()
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user