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:
26
cmd/util.go
26
cmd/util.go
@@ -89,7 +89,7 @@ func NewUnpackCommand() *cobra.Command {
|
|||||||
Use: "unpack image path",
|
Use: "unpack image path",
|
||||||
Short: "Unpack a docker image natively",
|
Short: "Unpack a docker image natively",
|
||||||
Long: `unpack doesn't need the docker daemon to run, and unpacks a docker image in the specified directory:
|
Long: `unpack doesn't need the docker daemon to run, and unpacks a docker image in the specified directory:
|
||||||
|
|
||||||
luet util unpack golang:alpine /alpine
|
luet util unpack golang:alpine /alpine
|
||||||
`,
|
`,
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
@@ -107,7 +107,7 @@ func NewUnpackCommand() *cobra.Command {
|
|||||||
util.DefaultContext.Error("Invalid path %s", destination)
|
util.DefaultContext.Error("Invalid path %s", destination)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
local, _ := cmd.Flags().GetBool("local")
|
||||||
verify, _ := cmd.Flags().GetBool("verify")
|
verify, _ := cmd.Flags().GetBool("verify")
|
||||||
user, _ := cmd.Flags().GetString("auth-username")
|
user, _ := cmd.Flags().GetString("auth-username")
|
||||||
pass, _ := cmd.Flags().GetString("auth-password")
|
pass, _ := cmd.Flags().GetString("auth-password")
|
||||||
@@ -126,13 +126,22 @@ func NewUnpackCommand() *cobra.Command {
|
|||||||
RegistryToken: registryToken,
|
RegistryToken: registryToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
info, err := docker.DownloadAndExtractDockerImage(util.DefaultContext, image, destination, auth, verify)
|
if !local {
|
||||||
if err != nil {
|
info, err := docker.DownloadAndExtractDockerImage(util.DefaultContext, image, destination, auth, verify)
|
||||||
util.DefaultContext.Error(err.Error())
|
if err != nil {
|
||||||
os.Exit(1)
|
util.DefaultContext.Error(err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
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))))
|
||||||
}
|
}
|
||||||
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))))
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,6 +152,7 @@ func NewUnpackCommand() *cobra.Command {
|
|||||||
c.Flags().String("auth-identity-token", "", "Authentication identity token")
|
c.Flags().String("auth-identity-token", "", "Authentication identity token")
|
||||||
c.Flags().String("auth-registry-token", "", "Authentication registry 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("verify", false, "Verify signed images to notary before to pull")
|
||||||
|
c.Flags().Bool("local", false, "Unpack local image")
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -33,6 +33,7 @@ import (
|
|||||||
"github.com/google/go-containerregistry/pkg/authn"
|
"github.com/google/go-containerregistry/pkg/authn"
|
||||||
"github.com/google/go-containerregistry/pkg/name"
|
"github.com/google/go-containerregistry/pkg/name"
|
||||||
"github.com/google/go-containerregistry/pkg/v1/remote"
|
"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/mudler/luet/pkg/api/core/bus"
|
||||||
"github.com/opencontainers/go-digest"
|
"github.com/opencontainers/go-digest"
|
||||||
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
@@ -192,3 +193,60 @@ func DownloadAndExtractDockerImage(ctx luettypes.Context, image, dest string, au
|
|||||||
},
|
},
|
||||||
}, nil
|
}, 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