Skip to content

Commit

Permalink
download nerdctl-full package with HTTP cache
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <[email protected]>
  • Loading branch information
AkihiroSuda committed Jun 9, 2021
1 parent 0305550 commit 184ce42
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
50 changes: 50 additions & 0 deletions pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ package cidata

import (
"bytes"
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strconv"

"github.com/AkihiroSuda/lima/pkg/downloader"
"github.com/AkihiroSuda/lima/pkg/iso9660util"
"github.com/AkihiroSuda/lima/pkg/limayaml"
"github.com/AkihiroSuda/lima/pkg/localpathutil"
"github.com/AkihiroSuda/lima/pkg/sshutil"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

const NerdctlVersion = "0.8.3"

func GenerateISO9660(isoPath, name string, y *limayaml.LimaYAML) error {
if err := limayaml.ValidateRaw(*y); err != nil {
return err
Expand Down Expand Up @@ -81,6 +87,50 @@ func GenerateISO9660(isoPath, name string, y *limayaml.LimaYAML) error {
})
}

if args.Containerd.System || args.Containerd.User {
var nftgzBase string
switch y.Arch {
case limayaml.X8664:
nftgzBase = fmt.Sprintf("nerdctl-full-%s-linux-amd64.tar.gz", NerdctlVersion)
case limayaml.AARCH64:
nftgzBase = fmt.Sprintf("nerdctl-full-%s-linux-arm64.tar.gz", NerdctlVersion)
default:
return errors.Errorf("unexpected arch %q", y.Arch)
}
td, err := ioutil.TempDir("", "lima-download-nerdctl")
if err != nil {
return err
}
defer os.RemoveAll(td)
nftgzLocal := filepath.Join(td, nftgzBase)
nftgzURL := fmt.Sprintf("https://github.com/containerd/nerdctl/releases/download/v%s/%s",
NerdctlVersion, nftgzBase)
logrus.Infof("Downloading %q", nftgzURL)
res, err := downloader.Download(nftgzLocal, nftgzURL, downloader.WithCache())
if err != nil {
return errors.Wrapf(err, "failed to download %q", nftgzURL)
}
switch res.Status {
case downloader.StatusDownloaded:
logrus.Infof("Downloaded %q", nftgzBase)
case downloader.StatusUsedCache:
logrus.Infof("Using cache %q", res.CachePath)
default:
logrus.Warnf("Unexpected result from downloader.Download(): %+v", res)
}
// TODO: verify sha256
nftgzR, err := os.Open(nftgzLocal)
if err != nil {
return err
}
defer nftgzR.Close()
layout = append(layout, iso9660util.Entry{
// ISO9660 requires len(Path) <= 30
Path: "nerdctl-full.tgz",
Reader: nftgzR,
})
}

return iso9660util.Write(isoPath, "cidata", layout)
}

Expand Down
10 changes: 4 additions & 6 deletions pkg/cidata/user-data.TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@ write_files:
#!/bin/bash
set -eux -o pipefail
if [ ! -x /usr/local/bin/nerdctl ]; then
version="0.8.3"
goarch="amd64"
if [ "$(uname -m )" = "aarch64" ]; then
goarch="arm64"
fi
curl -fsSL https://github.com/containerd/nerdctl/releases/download/v${version}/nerdctl-full-${version}-linux-${goarch}.tar.gz | tar Cxz /usr/local
mkdir -p -m 600 /mnt/lima-cidata
mount -t iso9660 -o ro /dev/disk/by-label/cidata /mnt/lima-cidata
tar Cxzf /usr/local /mnt/lima-cidata/nerdctl-full.tgz
umount /mnt/lima-cidata
fi
{{- if .Containerd.System}}
cat >"/etc/containerd/config.toml" <<EOF
Expand Down

0 comments on commit 184ce42

Please sign in to comment.