Skip to content

Commit

Permalink
Add config for enabling IPFS
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
  • Loading branch information
afbjorklund committed Oct 9, 2024
1 parent fddec51 commit be4e62f
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 11 deletions.
4 changes: 4 additions & 0 deletions examples/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ rosetta:
# 🟢 Builtin default: use name from /etc/timezone or deduce from symlink target of /etc/localtime
timezone: null

# Allow using IPFS for downloading files with a provided CID.
# 🟢 Builtin default: false
ipfs: null

firmware:
# Use legacy BIOS instead of UEFI. Ignored for aarch64 and vz.
# 🟢 Builtin default: false
Expand Down
10 changes: 9 additions & 1 deletion pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type options struct {
decompress bool // default: false (keep compression)
description string // default: url
expectedDigest digest.Digest
ipfs bool
cid string
}

Expand Down Expand Up @@ -123,6 +124,13 @@ func WithExpectedDigest(expectedDigest digest.Digest) Opt {
}
}

func WithIPFS(ipfs bool) Opt {
return func(o *options) error {
o.ipfs = ipfs
return nil
}
}

func WithContentIdentifier(cid string) Opt {
return func(o *options) error {
o.cid = cid
Expand Down Expand Up @@ -283,7 +291,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
return nil, err
}
status := StatusDownloaded
if o.cid != "" {
if o.ipfs && o.cid != "" {
if err := downloadIPFS(ctx, shadData, fmt.Sprintf("ipfs://%s", o.cid), o.description, o.expectedDigest); err == nil {
status = StatusUsedIPFS
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/fileutils/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var ErrSkipped = errors.New("skipped to download")

// 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, ipfs bool, description string, expectedArch limayaml.Arch) (string, error) {
if f.Arch != expectedArch {
return "", fmt.Errorf("%w: %q: unsupported arch: %q", ErrSkipped, f.Location, f.Arch)
}
Expand All @@ -26,6 +26,7 @@ func DownloadFile(ctx context.Context, dest string, f limayaml.File, decompress
downloader.WithDecompress(decompress),
downloader.WithDescription(fmt.Sprintf("%s (%s)", description, path.Base(f.Location))),
downloader.WithExpectedDigest(f.Digest),
downloader.WithIPFS(ipfs),
downloader.WithContentIdentifier(f.Cid),
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/instance/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func ensureNerdctlArchiveCache(ctx context.Context, y *limayaml.LimaYAML, create
return path, nil
}
}
path, err := fileutils.DownloadFile(ctx, "", f, false, "the nerdctl archive", *y.Arch)
path, err := fileutils.DownloadFile(ctx, "", f, false, *y.IPFS, "the nerdctl archive", *y.Arch)
if err != nil {
errs[i] = err
continue
Expand Down
10 changes: 10 additions & 0 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
y.TimeZone = ptr.Of(hostTimeZone())
}

if y.IPFS == nil {
y.IPFS = d.IPFS
}
if o.IPFS != nil {
y.IPFS = o.IPFS
}
if y.IPFS == nil {
y.IPFS = ptr.Of(false)
}

if y.SSH.LocalPort == nil {
y.SSH.LocalPort = d.SSH.LocalPort
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/limayaml/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestFillDefault(t *testing.T) {
ForwardX11Trusted: ptr.Of(false),
},
TimeZone: ptr.Of(hostTimeZone()),
IPFS: ptr.Of(false),
Firmware: Firmware{
LegacyBIOS: ptr.Of(false),
},
Expand Down Expand Up @@ -175,6 +176,7 @@ func TestFillDefault(t *testing.T) {
},
},
TimeZone: ptr.Of("Antarctica/Troll"),
IPFS: ptr.Of(true),
Firmware: Firmware{
LegacyBIOS: ptr.Of(false),
Images: []FileWithVMType{
Expand Down Expand Up @@ -286,6 +288,7 @@ func TestFillDefault(t *testing.T) {
}

expect.TimeZone = y.TimeZone
expect.IPFS = y.IPFS
expect.Firmware = y.Firmware
expect.Firmware.Images = slices.Clone(y.Firmware.Images)

Expand Down Expand Up @@ -338,6 +341,7 @@ func TestFillDefault(t *testing.T) {
ForwardX11Trusted: ptr.Of(false),
},
TimeZone: ptr.Of("Zulu"),
IPFS: ptr.Of(true),
Firmware: Firmware{
LegacyBIOS: ptr.Of(true),
Images: []FileWithVMType{
Expand Down Expand Up @@ -545,6 +549,7 @@ func TestFillDefault(t *testing.T) {
ForwardX11Trusted: ptr.Of(false),
},
TimeZone: ptr.Of("Universal"),
IPFS: ptr.Of(true),
Firmware: Firmware{
LegacyBIOS: ptr.Of(true),
},
Expand Down
1 change: 1 addition & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type LimaYAML struct {
Rosetta Rosetta `yaml:"rosetta,omitempty" json:"rosetta,omitempty"`
Plain *bool `yaml:"plain,omitempty" json:"plain,omitempty"`
TimeZone *string `yaml:"timezone,omitempty" json:"timezone,omitempty"`
IPFS *bool `yaml:"ipfs,omitempty" json:"ipfs,omitempty"`
NestedVirtualization *bool `yaml:"nestedVirtualization,omitempty" json:"nestedVirtualization,omitempty"`
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ func EnsureDisk(ctx context.Context, cfg Config) error {
var ensuredBaseDisk bool
errs := make([]error, len(cfg.LimaYAML.Images))
for i, f := range cfg.LimaYAML.Images {
if _, err := fileutils.DownloadFile(ctx, baseDisk, f.File, true, "the image", *cfg.LimaYAML.Arch); err != nil {
if _, err := fileutils.DownloadFile(ctx, baseDisk, f.File, true, *cfg.LimaYAML.IPFS, "the image", *cfg.LimaYAML.Arch); err != nil {
errs[i] = err
continue
}
if f.Kernel != nil {
if _, err := fileutils.DownloadFile(ctx, kernel, f.Kernel.File, false, "the kernel", *cfg.LimaYAML.Arch); err != nil {
if _, err := fileutils.DownloadFile(ctx, kernel, f.Kernel.File, false, *cfg.LimaYAML.IPFS, "the kernel", *cfg.LimaYAML.Arch); err != nil {
errs[i] = err
continue
}
Expand All @@ -81,7 +81,7 @@ func EnsureDisk(ctx context.Context, cfg Config) error {
}
}
if f.Initrd != nil {
if _, err := fileutils.DownloadFile(ctx, initrd, *f.Initrd, false, "the initrd", *cfg.LimaYAML.Arch); err != nil {
if _, err := fileutils.DownloadFile(ctx, initrd, *f.Initrd, false, *cfg.LimaYAML.IPFS, "the initrd", *cfg.LimaYAML.Arch); err != nil {
errs[i] = err
continue
}
Expand Down Expand Up @@ -604,7 +604,7 @@ func Cmdline(ctx context.Context, cfg Config) (exe string, args []string, err er
switch f.VMType {
case "", limayaml.QEMU:
if f.Arch == *y.Arch {
if _, err = fileutils.DownloadFile(ctx, downloadedFirmware, f.File, true, "UEFI code "+f.Location, *y.Arch); err != nil {
if _, err = fileutils.DownloadFile(ctx, downloadedFirmware, f.File, true, *y.IPFS, "UEFI code "+f.Location, *y.Arch); err != nil {
logrus.WithError(err).Warnf("failed to download %q", f.Location)
continue loop
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/vz/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ func EnsureDisk(ctx context.Context, driver *driver.BaseDriver) error {
var ensuredBaseDisk bool
errs := make([]error, len(driver.Instance.Config.Images))
for i, f := range driver.Instance.Config.Images {
if _, err := fileutils.DownloadFile(ctx, baseDisk, f.File, true, "the image", *driver.Instance.Config.Arch); err != nil {
if _, err := fileutils.DownloadFile(ctx, baseDisk, f.File, true, *driver.Instance.Config.IPFS, "the image", *driver.Instance.Config.Arch); err != nil {
errs[i] = err
continue
}
if f.Kernel != nil {
// ensure decompress kernel because vz expects it to be decompressed
if _, err := fileutils.DownloadFile(ctx, kernel, f.Kernel.File, true, "the kernel", *driver.Instance.Config.Arch); err != nil {
if _, err := fileutils.DownloadFile(ctx, kernel, f.Kernel.File, true, *driver.Instance.Config.IPFS, "the kernel", *driver.Instance.Config.Arch); err != nil {
errs[i] = err
continue
}
Expand All @@ -48,7 +48,7 @@ func EnsureDisk(ctx context.Context, driver *driver.BaseDriver) error {
}
}
if f.Initrd != nil {
if _, err := fileutils.DownloadFile(ctx, initrd, *f.Initrd, false, "the initrd", *driver.Instance.Config.Arch); err != nil {
if _, err := fileutils.DownloadFile(ctx, initrd, *f.Initrd, false, *driver.Instance.Config.IPFS, "the initrd", *driver.Instance.Config.Arch); err != nil {
errs[i] = err
continue
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/wsl2/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func EnsureFs(ctx context.Context, driver *driver.BaseDriver) error {
var ensuredBaseDisk bool
errs := make([]error, len(driver.Instance.Config.Images))
for i, f := range driver.Instance.Config.Images {
if _, err := fileutils.DownloadFile(ctx, baseDisk, f.File, true, "the image", *driver.Instance.Config.Arch); err != nil {
if _, err := fileutils.DownloadFile(ctx, baseDisk, f.File, true, *driver.Instance.Config.IPFS, "the image", *driver.Instance.Config.Arch); err != nil {
errs[i] = err
continue
}
Expand Down

0 comments on commit be4e62f

Please sign in to comment.