mirror of
https://github.com/mudler/luet.git
synced 2025-09-10 19:49:06 +00:00
Unpack local image (#277)
* [WIP] Unpack local docker images * unpack local image * PR feedback + missing new function call Co-authored-by: Ettore Di Giacinto <mudler@users.noreply.github.com>
This commit is contained in:
12
cmd/util.go
12
cmd/util.go
@@ -107,7 +107,7 @@ func NewUnpackCommand() *cobra.Command {
|
||||
util.DefaultContext.Error("Invalid path %s", destination)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
local, _ := cmd.Flags().GetBool("local")
|
||||
verify, _ := cmd.Flags().GetBool("verify")
|
||||
user, _ := cmd.Flags().GetString("auth-username")
|
||||
pass, _ := cmd.Flags().GetString("auth-password")
|
||||
@@ -126,6 +126,7 @@ func NewUnpackCommand() *cobra.Command {
|
||||
RegistryToken: registryToken,
|
||||
}
|
||||
|
||||
if !local {
|
||||
info, err := docker.DownloadAndExtractDockerImage(util.DefaultContext, image, destination, auth, verify)
|
||||
if err != nil {
|
||||
util.DefaultContext.Error(err.Error())
|
||||
@@ -133,6 +134,14 @@ func NewUnpackCommand() *cobra.Command {
|
||||
}
|
||||
util.DefaultContext.Info(fmt.Sprintf("Pulled: %s %s", info.Target.Digest, info.Name))
|
||||
util.DefaultContext.Info(fmt.Sprintf("Size: %s", units.BytesSize(float64(info.Target.Size))))
|
||||
} else {
|
||||
info, err := docker.ExtractDockerImage(util.DefaultContext, image, destination)
|
||||
if err != nil {
|
||||
util.DefaultContext.Error(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
util.DefaultContext.Info(fmt.Sprintf("Size: %s", units.BytesSize(float64(info.Target.Size))))
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -143,6 +152,7 @@ func NewUnpackCommand() *cobra.Command {
|
||||
c.Flags().String("auth-identity-token", "", "Authentication identity token")
|
||||
c.Flags().String("auth-registry-token", "", "Authentication registry token")
|
||||
c.Flags().Bool("verify", false, "Verify signed images to notary before to pull")
|
||||
c.Flags().Bool("local", false, "Unpack local image")
|
||||
return c
|
||||
}
|
||||
|
||||
|
@@ -33,6 +33,7 @@ import (
|
||||
"github.com/google/go-containerregistry/pkg/authn"
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote"
|
||||
"github.com/google/go-containerregistry/pkg/v1/daemon"
|
||||
"github.com/mudler/luet/pkg/api/core/bus"
|
||||
"github.com/opencontainers/go-digest"
|
||||
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
@@ -192,3 +193,60 @@ func DownloadAndExtractDockerImage(ctx luettypes.Context, image, dest string, au
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ExtractDockerImage(ctx luettypes.Context, local, dest string)(*images.Image, error) {
|
||||
if !fileHelper.Exists(dest) {
|
||||
if err := os.MkdirAll(dest, os.ModePerm); err != nil {
|
||||
return nil, errors.Wrapf(err, "cannot create destination directory")
|
||||
}
|
||||
}
|
||||
|
||||
ref, err := name.ParseReference(local)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
img, err := daemon.Image(ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m, err := img.Manifest()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mt, err := img.MediaType()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
d, err := img.Digest()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var c int64
|
||||
c, _, err = luetimages.ExtractTo(
|
||||
ctx,
|
||||
img,
|
||||
dest,
|
||||
nil,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bus.Manager.Publish(bus.EventImagePostUnPack, UnpackEventData{Image: local, Dest: dest})
|
||||
|
||||
return &images.Image{
|
||||
Name: local,
|
||||
Labels: m.Annotations,
|
||||
Target: specs.Descriptor{
|
||||
MediaType: string(mt),
|
||||
Digest: digest.Digest(d.String()),
|
||||
Size: c,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user