Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unpack local file bundle #318

Merged
merged 2 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.swp
.idea/
luet
tests/integration/shunit2
tests/integration/bin
Expand Down
7 changes: 6 additions & 1 deletion cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"

"github.com/docker/docker/api/types"
"github.com/docker/go-units"
Expand All @@ -34,6 +35,10 @@ import (
"github.com/spf13/cobra"
)

const (
filePrefix = "file://"
)

func pack(ctx *context.Context, p, dst, imageName, arch, OS string) error {

tempimage, err := ctx.TempFile("tempimage")
Expand Down Expand Up @@ -126,7 +131,7 @@ func NewUnpackCommand() *cobra.Command {
RegistryToken: registryToken,
}

if !local {
if !local && !strings.HasPrefix(image, filePrefix) {
info, err := docker.DownloadAndExtractDockerImage(util.DefaultContext, image, destination, auth, verify)
if err != nil {
util.DefaultContext.Error(err.Error())
Expand Down
27 changes: 22 additions & 5 deletions pkg/helpers/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"encoding/hex"
"net/http"
"os"
"strings"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/tarball"

"github.com/containerd/containerd/images"
luetimages "github.com/mudler/luet/pkg/api/core/image"
Expand All @@ -42,6 +46,11 @@ import (
"github.com/theupdateframework/notary/tuf/data"
)

const (
filePrefix = "file://"
fileImageSeparator = ":/"
)

// See also https://github.com/docker/cli/blob/88c6089300a82d3373892adf6845a4fed1a4ba8d/cli/command/image/trust.go#L171

func verifyImage(image string, authConfig *types.AuthConfig) (string, error) {
Expand Down Expand Up @@ -196,18 +205,26 @@ func DownloadAndExtractDockerImage(ctx luettypes.Context, image, dest string, au
}

func ExtractDockerImage(ctx luettypes.Context, local, dest string) (*images.Image, error) {
var img v1.Image
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
var err error
if strings.HasPrefix(local, filePrefix) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good! I think it's safe to just check the input for the file suffix. No need to specify --local at that point

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

DownloadAndExtractDockerImage will be called only if it's !local && !strings.HasPrefix(image, filePrefix)
rest of the cases - ExtractDockerImage will be called.

parts := strings.Split(local, fileImageSeparator)
if len(parts) == 2 && parts[1] != "" {
img, err = tarball.ImageFromPath(parts[1], nil)
}
} else {
ref, err := name.ParseReference(local)
if err != nil {
return nil, err
}
img, err = daemon.Image(ref)
}

img, err := daemon.Image(ref)
if err != nil {
return nil, err
}
Expand Down