Skip to content

feature: add LocalPath to support use local resource to skip download #3777

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

Closed
wants to merge 1 commit into from
Closed
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
61 changes: 59 additions & 2 deletions pkg/fileutils/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ package fileutils

import (
"context"
"crypto/sha256"
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"

"github.com/sirupsen/logrus"

Expand All @@ -18,12 +22,65 @@ import (
// ErrSkipped is returned when the downloader did not attempt to download the specified file.
var ErrSkipped = errors.New("skipped to download")

func CopyFile(src, dst string) error {
source, err := os.Open(src)
if err != nil {
return err
}
defer source.Close()
if err := os.MkdirAll(filepath.Dir(dst), 0o660); err != nil {
return err
}
destination, err := os.Create(dst)
if err != nil {
return err
}
defer destination.Close()
_, err = io.Copy(destination, source)
return err
}

func GetFileSHA256(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
return "", err
}
return fmt.Sprintf("sha256:%x", hash.Sum(nil)), nil
}

// DownloadFile downloads a file to the cache, optionally copying it to the destination. Returns path in cache.
func DownloadFile(ctx context.Context, dest string, f limayaml.File, decompress bool, description string, expectedArch limayaml.Arch) (string, error) {
func DownloadFile(ctx context.Context, dest string, f limayaml.File, decompress bool, description string, expectedArch limayaml.Arch) (_ string, reterr error) {
if f.Arch != expectedArch {
return "", fmt.Errorf("%w: %q: unsupported arch: %q", ErrSkipped, f.Location, f.Arch)
}
fields := logrus.Fields{"location": f.Location, "arch": f.Arch, "digest": f.Digest}
fields := logrus.Fields{"location": f.Location, "arch": f.Arch, "digest": f.Digest, "LocalPath": f.LocalPath}
if f.LocalPath != "" {
if _, err := os.Stat(f.LocalPath); err != nil {
return "", err
}
logrus.WithFields(fields).Infof("Attempting to copy local file %s", description)
if reterr != nil {
defer os.Remove(dest)
}
if err := CopyFile(f.LocalPath, dest); err != nil {
return "", fmt.Errorf("failed to copy file: %w", err)
}
sha256Sum, err := GetFileSHA256(dest)
if err != nil {
return "", fmt.Errorf("failed to getsha256: %w", err)
}

if sha256Sum != f.Digest.String() {
return "", fmt.Errorf("wrong sha256 for %s", dest)
}
return f.LocalPath, nil
}

logrus.WithFields(fields).Infof("Attempting to download %s", description)
res, err := downloader.Download(ctx, dest, f.Location,
downloader.WithCache(),
Expand Down
7 changes: 4 additions & 3 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ type Rosetta struct {
}

type File struct {
Location string `yaml:"location" json:"location"` // REQUIRED
Arch Arch `yaml:"arch,omitempty" json:"arch,omitempty"`
Digest digest.Digest `yaml:"digest,omitempty" json:"digest,omitempty"`
Location string `yaml:"location" json:"location"` // REQUIRED
Arch Arch `yaml:"arch,omitempty" json:"arch,omitempty"`
Digest digest.Digest `yaml:"digest,omitempty" json:"digest,omitempty"`
LocalPath string `yaml:"localPath,omitempty" json:"localPath,omitempty"`
}

type FileWithVMType struct {
Expand Down
Loading